forked from likair/python-programming-course-assignments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
323 lines (298 loc) · 7.54 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
'''
Created on 12.5.2015
@author: e1201757
'''
'''
foo=0
print([x[:] for x in [[foo]*10]*10])
a = [[1, 2, 3], [4, 5 , 6], [7, 8, 9]]
c=[[0]*len(a)]*len(a[0])
print(c)
'''
'''
matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
#0
print(list(zip(*matrix)))
#1
print([[row[i] for row in matrix] for i in range(4)])
#2
transposed = []
for i in range(4):
transposed.append([row[i] for row in matrix])
print(transposed)
#3
transposed = []
for i in range(4):
# the following 3 lines implement the nested listcomp
transposed_row = []
for row in matrix:
transposed_row.append(row[i])
transposed.append(transposed_row)
print(transposed)
'''
'''
a = [1, 2, 3, 4, 5]
b = [1, 2, 3, 4, 5]
for i, j in zip(a, b):
print(str(i) + ' ' + str(j))
print([(i, j) for i, j in zip(a, b)])
'''
'''
matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
print(*matrix)
print(list(zip(*matrix)))
'''
'''
a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
b = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
c = []
for i in range(0, len(a)):
c.append([])
for j in range(0, len(a[0])):
c[i].append(a[i][j] + b[i][j])
print(c)
'''
'''
a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
b = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
#c = list([0] * len(a[0])) * len(a)
#c = [0] * len(a[0])
#c = [[0] * len(a[0])] * len(a)
#d = c * 3
#d = list(c) * 3
c = [[0]*3]*3
#print(c)
for i in range(len(c)):
for j in range(len(c[0])):print(c[i][j], end=' ')
print()
'''
'''
a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
b = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
c = [[0] * len(a)] * len(a[0]) # the mistake is here, because ... the copy lists will be changed as the first one
#c = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] # This is the right way
#c = [i[:] for i in [[0] * len(a)] * len(a[0])] # This is the right way too
print('the initial state:')
print(c)
print()
time = 0
for i in range(len(a)):
for j in range(len(a[0])):
time +=1
c[i][j] = a[i][j] + b[i][j]
print('the ' + str(time) + ' time')
print('c[' + str(i) + '][' + str(j) +'] = ' + str(c[i][j]))
print(c)
print()
print('the final result:')
print(c)
'''
'''
a = [[0] * 3] * 3
print(a)
a[0][0] = 1
print(a)
'''
'''
print([i[:] for i in [[0]*3]*3])
'''
'''
li = [1, '']
new_list = [ x for x in li if x != '' ]
print(new_list)
'''
'''
li = ['', '', '', 'a']
for s in li[:]:
print(s)
if s == '':
li.remove(s)
print(li)
'''
'''
print("hello".maketrans('he', 'ab'))
'''
'''
a = ['x', 'x', 'x', '0']
b = []
for s in a:
print(s)
if s != 'x':
#a.remove(s)
b.append(s)
a = b
#print(a)
print(a)
'''
'''
name = 'lebs'
age = 10
print(name, age)
print(name, age, sep=';')
'''
'''
day = 10
month = 3
year = 2015
print('{}/{}/{}'.format(day, month, year))
'''
'''
print('{}')
print('{}'.format('hello'))
'''
'''
# the default parameter sequence
print('{:5}{:8}'.format(456, 8973))
# 1 means the second parameter, 0 means the first parameter
print('{1:5}{0:8}'.format(456, 8973))
# It seems it will show all the length of parameter even we limit the length less than its length
print('{1:3}{0:5}'.format(456, 8973))
print('{:5.2}'.format(10/3))
'''
'''
import random
month = random.randrange(1, 13)
'''
'''
year = 2008
if (not year % 4 and year % 100) or not year % 400:
print('leap')
'''
'''
a = (1, 2, 3, 4)
b = {}
for i in a: b[i] = 0
print(b)
'''
'''
print(int(1000/23))
num = 32
print(str(num)[1])
#print(len(num)) #wrong
print(len(str(num)))
'''
'''
# a method to replace the switch in other language
def f(x):
return {
'a': 1,
'b': 2,
}.get(x, 9)
'''
'''
import random
import time
def strTimeProp(start, end, format, prop):
"""Get a time at a proportion of a range of two formatted times.
start and end should be strings specifying times formated in the
given format (strftime-style), giving an interval [start, end].
prop specifies how a proportion of the interval to be taken after
start. The returned time will be in the specified format.
"""
stime = time.mktime(time.strptime(start, format))
etime = time.mktime(time.strptime(end, format))
ptime = stime + prop * (etime - stime)
return time.strftime(format, time.localtime(ptime))
def randomDate(start, end, prop):
return strTimeProp(start, end, '%m/%d/%Y %I:%M %p', prop)
print(randomDate("1/1/2008 1:30 PM", "1/1/2009 4:50 AM", random.random()))
'''
'''
import time
import datetime
#今天星期几
today=int(time.strftime("%w"))
print(today)
#某个日期星期几
anyday=datetime.datetime(2012, 4, 21).strftime("%w")
print(anyday)
'''
'''
sum = 0
for year in range(10):
sum = (1000 + sum) * (1 + 0.047)
print(sum)
'''
'''
# get the current time
import time
print(time.strftime('%Y-%m-%d', time.localtime(time.time())))
'''
'''
# solve the equation
a = 10
b = 40
c = 15
delta = ** 2 - 4 * a * c
if delta < 0:
print(' No solution!')
elif delta == 0:
print('x = ' + str(-b / (2 * a)))
else:
print('x1 = ' + str((-b + delta ** 0.5) / (2 * a)))
print('x2 = ' + str((-b - delta ** 0.5) / (2 * a)))
'''
'''
year = 2008
day = 28 if (year % 4) or ((year % 400) and not(year % 100)) else 29
print(day)
'''
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Created on May 17, 2015
# A program which generates randomly a number of date and time values and displays them in Finnish language.
# @author: Likai
NUMBERS = ['nolla', 'yksi', 'kaksi', 'kolme', 'neljä', 'viisi', 'kuusi', 'seitsemän', 'kahdeksan', 'yhdeksän']
SUFFIX1 = ['', 'kymmentä', 'sata', 'tuhat']
SUFFIX2 = ['', 'kymmentä', 'sataa', 'tuhatta']
def numberInFinnish(num):
if num < 10000:
literality = ''
i = 0
while num > 0:
digit = int(num % 10)
num /= 10
if digit == 1:
if i == 1:
if literality == '':
literality = 'kymmenen'
else:
literality += 'toista'
else: literality = NUMBERS[digit] + SUFFIX1[i] + literality
elif digit > 1 or num == digit:
literality = NUMBERS[digit] + SUFFIX2[i] + literality
i += 1
else:
literality = 'This number is not supported!'
return literality
SUFFIX1 = ['', 'kymmentä ', 'sata ', 'tuhat ']
SUFFIX2 = ['', 'kymmentä ', 'sataa ', 'tuhatta ']
def numberInFinnish2(num):
length = len(str(num))
if length < 5:
literality = ''
for i in range(length):
digit = length - i
if digit == 4:
if str(num)[i] == '0': pass
elif str(num)[i] == '1':
literality += NUMBERS[int(str(num)[i])] + 'tuhat '
else: literality += NUMBERS[int(str(num)[i])] + 'tuhatta '
elif digit == 3:
if str(num)[i] == '0': pass
elif str(num)[i] == '1':
literality += NUMBERS[int(str(num)[i])] + 'yksisata '
else: literality += NUMBERS[int(str(num)[i])] + 'sattaa '
elif digit == 2:
if str(num)[i] == '0': pass
elif str(num)[i] == '1':
if str(num)[i + 1] == '0':
literality += 'kymmenen'
else: literality += NUMBERS[int(str(num)[i + 1])] + 'toista'
else: literality += NUMBERS[int(str(num)[i])] + 'kymmentä'
elif digit == 1:
if num == int(str(num)[i]) or (length > 1 and str(num)[i - 1] != '1' and str(num)[i] != '0'):
literality += NUMBERS[int(str(num)[i])]
return literality
else: return 'This number is not supported!'
print(numberInFinnish(1200))