forked from sankalpj100/python-practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercise.py
195 lines (168 loc) · 4.67 KB
/
exercise.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
import math
def digits(x):
return len(list(x))
def count_words(x):
return len(x.split(" "))
def largest(x):
biggest = x[0]
for i in x:
if i > biggest:
biggest = i
return biggest
def smallest(x):
s = x[0]
for i in x:
if i < s:
s = i
return s
def largest_smallest(x):
s = b = x[0] # smallest and biggest are set to the first element
for i in x:
if i > b:
b = i
if i < s:
s = i
return s, b
def mean(x):
acc = 0
for i in x:
acc += i
return acc/len(x)
def median(x):
c = x[:]
c.sort() # Sorting x directly modifies the input. This might not be acceptable
l = len(x) # Calculate this once. Don't repeat the computation
if l % 2 == 0: # Even number of elements. Average of two middle elements
mid = math.floor(l/2) # Prefer using math.floor and math.ceil rather than int (more explicit)
ret = (c[mid]+c[mid-1])/2.0
else:
mid = math.floor(l/2)
ret = c[mid]
return ret
def tables(x, y):
for i in range(1, y+1):
print (f"{x} x {i} = {x*i}")
# i = 1
# while i <= int(y): # Remove superfluous
# k = int(x) * int(i) # calls to "int"
# a = print(f"{x} x {i}= {k}")
# i += 1
def tables2(x):
for c in range(1, x+1):
row = []
for r in range(1, x+1):
row.append("{:5}".format(c*r))
print (" ".join(row) + "\n")
# b = 1
# while b <= x:
# for a in range(1,int(x + 1)):
# print(a * b, end="\t ")
# print('\n')
# if b == 1:
# print("--+-","-" * 8 * (int(x - 1)))
# print('\n')
# b += 1
# return
def panagram(x):
for i in "abcdefghijklmnopqrstuvwxyz": # For each letter
if i not in x: # If it's missing
return False # x is not a panagram
return True # Otherwise all letters are there and it is a panagram
# x = x.replace(" ", "")
# a = list(x)
# b = list(set(a))
# b.sort()
# c = "abcdefghijklmnopqrstuvwxyz"
# d = list(c)
# if b != d:
# return False
# else:
# return True
def freq(s):
counts = {}
for i in s:
if i in counts:
counts[i] += 1
else:
counts[i] = 1
return counts
# a = list(s)
# a.sort()
# inwords = (set(a))
# new = []
# for word in inwords:
# counts = s.count(word)
# new.append(counts)
# freqdict = {}
# for key in inwords:
# for value in new:
# freqdict[key] = value
# new.remove(value)
# break
# return freqdict
def mode(s):
counts = freq(s) # Using the previous function
items = counts.items() # items is of the form [(1, 10), (5, 3),...] where 1,5 etc. are the numbers in s and 10, 3 are the number of times it's there. Each items is of the form (value, no. of times it occurs)
def keyfn(x): # function to return the second element from a tuple like the one mentioned above
return x[1]
ret = max(items, key=keyfn) # Will return the item with the second element (i.e. count) as maximum
return ret[0] # Return the element
# inwords = (set(s))
# new = []
# for word in inwords:
# counts = s.count(word)
# new.append(counts)
# freqdict = {}
# for key in new:
# for value in inwords:
# freqdict[key] = value
# inwords.remove(value)
# break
# return freqdict[max(freqdict.keys())]
def den(x):
a = 0
while x >= 2000: # Use a for loop to loop through denominations. Don't cut/paste like this
x -= 2000
a += 1
print(f"2000 x {a} = {2000 * a} ({x} left)")
a = 0
while x >= 500:
x -= 500
a += 1
print(f"500 x {a} = {500 * a} ({x} left)")
a = 0
while x >= 200:
x -= 200
a += 1
print(f"200 x {a} = {200 * a} ({x} left)")
a = 0
while x >= 100:
x -= 100
a += 1
print(f"100 x {a} = {100 * a} ({x} left)")
a = 0
while x >= 50:
x -= 0
a += 1
print(f"50 x {a} = {50 * a} ({x} left)")
a = 0
while x >= 20:
x -= 20
a += 1
print(f"20 x {a} = {20 * a} ({x} left)")
a = 0
while x >= 10:
x -= 10
a += 1
print(f"10 x {a} = {10 * a} ({x} left)")
a = 0
while x >= 5:
x -= 5
a += 1
print(f"5 x {a} = {5 * a} ({x} left)")
a = 0
while x >= 1:
x -= 1
a += 1
print(f"1 x {a} = {1 * a} ({x} left)")
return