-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdict.py
77 lines (63 loc) · 1.91 KB
/
dict.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
animals = { 'a': ['aardvark'], 'b': ['baboon'], 'c': ['coati']}
animals['d'] = ['donkey']
animals['d'].append('dog')
animals['d'].append('dingo')
def how_many(aDict):
'''
aDict: A dictionary, where all the values are lists.
returns: int, how many values are in the dictionary.
'''
# Your Code Here
return sum(map(len, aDict.values()))
def how_many(aDict):
'''
aDict: A dictionary, where all the values are lists.
returns: int, how many individual values are in the dictionary.
'''
result = 0
for value in aDict.values():
# Since all the values of aDict are lists, aDict.values() will
# be a list of lists
result += len(value)
return result
def how_many(aDict):
'''
Another way to solve the problem.
aDict: A dictionary, where all the values are lists.
returns: int, how many individual values are in the dictionary.
'''
result = 0
for key in aDict.keys():
result += len(aDict[key])
return result
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
result = {}
for key,value in aDict.items():
# Since all the values of aDict are lists, aDict.values() will
# be a list of lists
result[key]=len(value)
maxCount = 0
maxKey = ''
for key,value in result.items():
if value > maxCount:
maxCount = value
maxKey = key
return maxKey
#alternative
def biggest(aDict):
'''
aDict: A dictionary, where all the values are lists.
returns: The key with the largest number of values associated with it
'''
result = None
biggestValue = 0
for key in aDict.keys():
if len(aDict[key]) >= biggestValue:
result = key
biggestValue = len(aDict[key])
return result