-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCh4_exercises.py
More file actions
209 lines (157 loc) · 6.33 KB
/
Ch4_exercises.py
File metadata and controls
209 lines (157 loc) · 6.33 KB
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
# Exercise 1
print(f'\nExercise 1\n')
a = ['a','b','c']
b = [1,2,3]
# Write a loop that creates the dic {'a': 1, 'b': 2, 'c': 3}
dict1 = {}
count = 0
while len(dict1) != len(a): # The while loop will iterate until there are no more keys in a.
dict1[a[count]] = b[count] # Assig key a to value b
count +=1 # Increase counter after each iteration
print(dict1)
# Exercise 2
print(f'\nExercise 2\n')
# Write code that takes all pairs (x,y) of numbers between 2 and 20 such that x != y and prints out their greatest common divisor (GCD).
print('x', 'y', 'GCD', sep='\t') # header
for x in range(2,21): # Use a nested loop to create the pairs
print(f'=================')
for y in range(2,21):
if x == y: # if x and y are the same ignore the iteration
continue
else:
a = max(x,y) # else do the GCD function
b = min(x,y)
r = a % b
while r != 0:
a = b
b = r
r = a % b
print(x, y, b, sep='\t') # print the row with the values
# Exercise 3
print(f'\nExercise 3\n')
print('x', 'y', 'GCD', sep='\t') # header
for x in range(2,21): # Use a nested loop to create the pairs
print(f'=================')
for y in range(x+1,21): # make y start at x+1, since we don't want to repeat lines, we eliminate the row where x == y, and make x always be smaller than y
a = max(x,y) # apply the GCD function
b = min(x,y)
r = a % b
while r != 0:
a = b
b = r
r = a % b
print(x, y, b, sep='\t') # print the row with the values
# Exercise 4
print(f'\nExercise 4\n')
# Create the dictionary vowels where each vowel has a value associated to it
vslist = list("AEIOUYaeiouy")
count = 0
vs = {}
for v in vslist:
vs[v] = count
count +=1
print(vs)
# Rewrite the vowels function, so that instead it replaces the vowels with the corresponding value
def vowels(s):
out = ""
for c in s:
if c in vs.keys():
out += str(vs[c])
else:
out += c
return out
vowels("Hey Mr. Handers!")
# Exercise 5
print(f'\nExercise 5\n')
# Use the structure of the split function (used in previous exercise of chapter 3) to create a new function that:
# 1. Takes a string
# 2. Associates each word with a number representing where the word appears the last time in the string.
# 3. Use a dictionary!
def last_seen(s,sep = " "):
"""Function takes a string and associates each word with a number representing where the word appears the last time in the string."""
out = {}
term = ""
count = 0 # to associate a number to the word
# form the words and append them to the dictionary with the associated number
for c in s:
if c in sep and term != "":
# if the c is a sep and term is not empty, the term should be added to the dictionary and the count should be associated to it
out[term] = count
count += 1
term = ""
else:
# if the previous condition was not met, and if c is not a sep, than we start to create a new term by combining the c
if c != sep :
term += c
# add the final word unless it is empty
if term != "":
out[term] = count
count += 1
return out
print(last_seen("hej du hej du hej du"))
# Exercise 6
print(f'\nExercise 6\n')
# change the last_seen() function so that it outputs dictionary with numbers as keys and words as values
def last_seen1(s,sep = " "):
"""Function takes a string and associates each word with a number representing where the word appears the last time in the string."""
out = {}
term = ""
count = 0 # to associate a number to the word
# form the words and append them to the dictionary with the associated number
for c in s:
if c in sep and term != "":
# if the c is a sep and term is not empty, the term should be added to the dictionary and the count should be associated to it
out[count] = term
count += 1
term = ""
else:
# if the previous condition was not met, and if c is not a sep, than we start to create a new term by combining the c
if c != sep :
term += c
# add the final word unless it is empty
if term != "":
out[count] = term
count += 1
return out
print(last_seen1("hej du hej du hej du"))
# Exercise 7
print(f'\nExercise 7\n')
# change the last_seen() function so that it outputs dictionary with all the positions for each word
def last_seen2(s,sep = " "):
"""Function takes a string and associates each word with a number representing where the word appears the last time in the string."""
out = {}
term = ""
count = 0 # to associate a number to the word
# form the words and append them to the dictionary with the associated number
for c in s:
if c in sep and term != "":
# if the c is a sep and term is not empty, the term should be added to the dictionary and the count should be associated to it
# if the term is already present in the dictionary just append the new value
if term not in out.keys():
out[term] = str(count)
count += 1
term = ""
else:
out[term] += " " + str(count)
count += 1
term = ""
else:
# if the previous condition was not met, and if c is not a sep, than we start to create a new term by combining the c
if c != sep:
term += c
# add the final word unless it is empty
if term != "":
if term not in out.keys():
out[term] = str(count)
count += 1
term = ""
else:
out[term] += " " + str(count)
count += 1
term = ""
return out
print(last_seen2("hej du hej du hej du"))
# apply the function to a text to determine position of words
with open('example.txt', 'r') as text:
print(last_seen2(text.read()))
print(f'Finito')