-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathPDA_note_week1.py
More file actions
213 lines (150 loc) · 4.05 KB
/
PDA_note_week1.py
File metadata and controls
213 lines (150 loc) · 4.05 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
210
211
212
"""
Python Data Analysis
WEEK ONE
Course Note and Quiz
"""
"""
Dictionary creation.
"""
print("Dictionary Literals")
print("===================")
# Dictionary literals
empty = {}
print(empty)
simple = {1: 2}
print(simple)
squares = {1: 1, 2: 4, 3: 9, 4: 16}
print(squares)
cipher = {'p': 'o', 'y': 'h', 't': 'n',
'h': 't', 'o': 'y', 'n': 'p'}
print(cipher)
goodinstructors = {'Rixner': True, 'Warren': False}
print(goodinstructors)
cities = {'China': ['Shanghai', 'Beijing'],
'USA': ['New York', 'Los Angeles'],
'Spain': ['Madrid', 'Barcelona'],
'Australia': ['Sydney', 'Melbourne'],
'Texas': ['Houston', 'San Antonio']}
print(cities)
print("")
print("Creating Dictionaries")
print("=====================")
empty2 = dict()
print(empty2)
data = [(1, 'one'), (2, 'two'), (3, 'three')]
names = dict(data)
print(names)
cipher2 = dict(cipher)
print(cipher2)
"""
Dictionary lookup and update.
"""
print("Dictionary Lookup")
print("=================")
cipher = {'p': 'o', 'y': 'h', 't': 'n',
'h': 't', 'o': 'y', 'n': 'p'}
print(cipher)
# Use indexing with keys to access values
print(cipher['t'])
print(cipher['n'])
def encrypt(cipher, word):
"""encrypt word using cipher"""
encrypted = ""
for char in word:
encrypted += cipher[char]
return encrypted
python = "python"
enc = encrypt(cipher, python)
print(python, enc)
# It is an error to use a non-existent key
# print(cipher[1])
# Use .get when you are unsure if the key exists
print(cipher.get('t'))
print(cipher.get(1))
print(cipher.get(1, 'z'))
print("")
print("Dictionary Update")
print("=================")
print(cipher)
# Modify an existing key->value mapping
cipher['p'] = 'q'
print(cipher)
# Create a new key->value mapping
cipher['r'] = 'z'
print(cipher)
enc2 = encrypt(cipher, python)
print(python, enc, enc2)
"""
Checking for keys in a dictionary.
"""
print("Using 'in'")
print("==========")
mapping = {1: 5, 8: -3, 7: 22, 4: 13, 22: 17}
# Keys
print(1 in mapping)
print(8 in mapping)
# Values
print(5 in mapping)
print(-3 in mapping)
# Both
print(22 in mapping)
# Neither
print(82 in mapping)
print("")
print("Protecting from Errors")
print("======================")
keys = [8, 14, 22, 25]
#for key in keys:
# print(key, mapping[key])
for key in keys:
if key in mapping:
print(key, mapping[key])
else:
print("{} not in mapping".format(key))
print("Issues with Keys")
print("================")
# Be careful with what you use as keys!
# If all keys are of the same type, you won't run
# into these issues
mapping = {4.0: 2, 'a': 3, True: 'true', False: 9}
print(mapping)
mapping[1] = 7
print(mapping)
mapping[0] = 'false'
print(mapping)
mapping[4] = 7
print(mapping)
mapping['A'] = 'abc'
print(mapping)
print("Week 1 Quiz")
print("================")
q3_dict = {1: 2, 3: 4}
print(1 in q3_dict)
def count_letters(word_list):
"""
Input: a list of words that are composed entirely of lower case letters
Return: the lower case letter that appears most frequently
(total number of occurrences) in the words in 𝚠𝚘𝚛𝚍_𝚕𝚒𝚜𝚝.
In the case of ties, return the earliest letter in alphabetical order
"""
ALPHABET = "abcdefghijklmnopqrstuvwxyz"
letter_count = {}
for letter in ALPHABET:
letter_count[letter] = 0
# enter code here
for word in word_list:
letters = list(word)
for letter in letters:
if letter in ALPHABET:
letter_count[letter] += 1
max_count = 0
max_letter = ''
for key, value in letter_count.items():
if letter_count[key] > max_count:
max_count = value
max_letter = key
return max_letter
print(count_letters(["hello", "world"]))
monty_quote = "listen strange women lying in ponds distributing swords is no basis for a system of government supreme executive power derives from a mandate from the masses not from some farcical aquatic ceremony"
monty_words = monty_quote.split(" ")
print(count_letters(monty_words))