-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab05.py
137 lines (115 loc) · 2.82 KB
/
lab05.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
def tree(root, branches=[]):
return lambda dispatch: root if dispatch == 'root' else list(branches)
def root(tree):
return tree('root')
def branches(tree):
return tree('branches')
t = tree(1,
[tree(2),
tree(3,
[tree(4),
tree(5)]),
tree(6,
[tree(7)])])
def print_tree(t, indent=0):
print(' ' * indent + str(root(t)))
for branch in branches(t):
print_tree(branch, indent + 1)
""" Q1
Define the function countdown_tree so that it returns
the specific tree below.Make sure to use the tree constructor
from the Data Abstraction!"""
def countdown_tree():
"""Return a tree that has the following structure.
>>> print_tree(countdown_tree())
10
9
8
7
6
5
"""
"*** YOUR CODE HERE ***"
return tree(10,[tree(9,[tree(8)]),tree(7,[tree(6,[tree(5)])])])
print_tree(countdown_tree())
""" Q2
Define the function size_of_tree, which takes in a tree
as an argument and returns the number of entries in the tree."""
def size_of_tree(t):
"""Return the number of entries in the tree.
>>> print_tree(numbers)
1
2
3
4
5
6
7
>>> size_of_tree(numbers)
7
"""
"*** YOUR CODE HERE ***"
return 1 + sum([size_of_tree(t) for t in branches(t)])
# alt solution
"""
return len(t)
if len(branches(t))==0:
return 1
else:
size=1
for branch in branches(t):
size += size_of_tree(branch)
return size """
def counter(message):
""" Returns a dictionary of each word in message mapped
to the number of times it appears in the input string.
>>> x = counter('to be or not to be')
>>> x['to']
2
>>> x['be']
2
>>> x['not']
1
>>> y = counter('run forrest run')
>>> y['run']
2
>>> y['forrest']
1
"""
word_list = message.split()
"*** YOUR CODE HERE ***"
words = {}
for i in word_list:
if not i in words:
words[i] = 1
else:
words[i] += 1
return words
# Q3
def counter(message):
""" Returns a dictionary of each word in message mapped
to the number of times it appears in the input string.
>>> x = counter('to be or not to be')
>>> x['to']
2
>>> x['be']
2
>>> x['not']
1
>>> y = counter('run forrest run')
>>> y['run']
2
>>> y['forrest']
1
"""
word_list = message.split()
"*** YOUR CODE HERE ***"
words = {}
for i in word_list:
if i not in words:
words[i] = 1 # add all words as keys in a dict and count values
else:
words[i] += 1 # keep count if key appears in list
return words
x = counter('to be or not to be')
resp = x['be']