-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractical5.py
More file actions
62 lines (56 loc) · 2.02 KB
/
Copy pathpractical5.py
File metadata and controls
62 lines (56 loc) · 2.02 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
#List
def applyToEach(L, f):
"""Assumes L is a list, f a function
Mutates L by replacing each element, e, of L by f(e)"""
for i in range(len(L)):
L[i] = f(L[i])
L = [1, -2, 3.33]
print ('L =', L )
print ('Apply abs to each element of L.' )
applyToEach(L, abs)
print ('L =', L)
print ('Apply int to each element of', L )
applyToEach(L, int)
print ('L =', L)
print ('Apply factorial to each element of', L )
#Tuple
def findDivisors (n1, n2):
"""Assumes that n1 and n2 are positive ints
Returns a tuple containing all common divisors of n1 & n2"""
divisors = () #the empty tuple
for i in range(1, min (n1, n2) + 1):
if n1%i == 0 and n2%i == 0:
divisors = divisors + (i,)
return divisors
divisors = findDivisors(20, 100)
print (divisors)
total = 0
for d in divisors:
total += d
print (total)
#Dictionary
EtoF = {'bread':'pain', 'wine':'vin', 'with':'avec', 'I':'Je', 'eat':'mange', 'drink':'bois', 'John':'Jean', 'friends':'amis', 'and': 'et', 'of':'du','red':'rouge'}
FtoE = {'pain':'bread', 'vin':'wine', 'avec':'with', 'Je':'I', 'mange':'eat', 'bois':'drink', 'Jean':'John', 'amis':'friends', 'et':'and', 'du':'of', 'rouge':'red'}
dicts = {'English to French':EtoF, 'French to English':FtoE}
def translateWord(word, dictionary):
if word in dictionary.keys():
return dictionary[word]
elif word != '':
return '"' + word + '"'
return word
def translate(phrase, dicts, direction):
UCLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
LCLetters = 'abcdefghijklmnopqrstuvwxyz'
letters = UCLetters + LCLetters
dictionary = dicts[direction]
translation = ''
word = ''
for c in phrase:
if c in letters:
word = word + c
else:
translation = translation + translateWord(word, dictionary) + c
word = ''
return translation + ' ' + translateWord(word, dictionary)
print (translate('I drink good red wine, and eat bread.', dicts,'English to French'))
print (translate('Je bois du vin rouge.', dicts, 'French to English'))