-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexercises.py
76 lines (48 loc) · 1.04 KB
/
exercises.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
import random
# 1
def convertC2F(c):
return (9 / 5) * c + 32
print convertC2F(40), 'degrees Fahrenheit'
def convertF2C(f):
return (5 * (f - 32)) / 9
print convertF2C(100), 'degrees convertF2C'
# 2
def isPrime(n):
if n == 1:
return False
elif n == 2:
return True
else:
for i in range(2, n):
if n % i == 0:
return False
return True
for i in range(1, 100):
print i, "->", isPrime(i)
# 3
def breakList(l):
return reduce(lambda x, y: x+y, l)
print breakList([['Geeks', 'For'], ['Geeks']])
# 4
def dic(d):
print "sum:", sum(d.values()), "average:", sum(
d.values())/len(d.values()), "round:", (round, d.values())
dic({'key1': 1, 'key2': 14, 'key3': 47})
# 5
def newWords(w):
for l in w:
print l
newWords("test")
# 6
def hard(d, k, r):
print sorted(d, key=lambda tup: tup[1])
hard([
('a', 1),
('bit', 1),
('of', 1),
('butter', 2),
('but', 1),
('the', 1),
('was', 1),
('bitter', 1)
], 1, True)