-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL6_problems.py
More file actions
126 lines (88 loc) · 2.42 KB
/
L6_problems.py
File metadata and controls
126 lines (88 loc) · 2.42 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
__author__ = 'Sam Broderick'
def oddTuples(aTup):
"""
aTup: a tuple
returns: tuple, every other element of aTup.
:param aTup: tuple
"""
# Your Code Here
if aTup == ():
return aTup
return_tuple = (aTup[0], )
for n in range(2, len(aTup), 2):
return_tuple = return_tuple + (aTup[n], )
return return_tuple
print(oddTuples(('I', 'am', 'a', 'test', 'tuple')))
print(oddTuples(()))
listA = [1, 4, 3, 0]
listA.insert(0, 100)
listA.remove(3)
listA.index(1)
def absolute(a):
return abs(a)
def plus_one(a):
return a + 1
def square(a):
return a*a
testList = [1, -4, 8, -9]
def applyToEach(L, f):
for i in range(len(L)):
L[i] = f(L[i])
applyToEach(testList, absolute)
print(testList)
testList = [1, -4, 8, -9]
applyToEach(testList, plus_one)
print(testList)
testList = [1, -4, 8, -9]
applyToEach(testList, square)
print(testList)
print('=============')
def applyEachTo(L, x):
result = []
for i in range(len(L)):
result.append(L[i](x))
return result
def halve(a):
return a/2
def inc(a):
return a+1
print(applyEachTo([inc, square, halve, abs], -3))
print(applyEachTo([inc, square, halve, abs], 3.0))
def howMany(aDict):
'''
aDict: A dictionary, where all the values are lists.
returns: int, how many values are in the dictionary.
'''
# Your Code Here
animal_count = 0
for dict_key in aDict:
animal_count += len(aDict[dict_key])
return animal_count
animals = { 'a': ['aardvark'], 'b': ['baboon'], 'c': ['coati']}
animals['d'] = ['donkey']
animals['d'].append('dog')
animals['d'].append('dingo')
print('howMany:')
print('=============')
print(howMany(animals))
def biggest(aDict):
'''
aDict: A dictionary, where all the values are lists.
returns: The key with the largest number of values associated with it
'''
# Your Code Here
return_str = '' # initialize biggest dict_key for return
if len(aDict) == 0:
return None
for dict_key in aDict:
if aDict.has_key(return_str): # compare entry lengths
if len(aDict[dict_key]) >= len(aDict[return_str]):
return_str = dict_key
elif return_str == '': # set first item for comparison
return_str = dict_key
return return_str
print('biggest:')
print('=============')
print(biggest(animals))
print(biggest({}))
print(biggest({'S': []}))