-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunit1_lesson_05_understanding_dicts.py
156 lines (121 loc) · 4.81 KB
/
unit1_lesson_05_understanding_dicts.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
__author__ = 'Kalyan'
from placeholders import *
notes = '''
dicts are unordered sets of key value pairs which facilitate fast lookups by key.
'''
def test_dictionary_type():
test_dict = {1 : "one"} # note the new syntax
assert "dict" == type(test_dict).__name__
def test_dictionary_empty():
empty_dict_1 = {}
assert True == isinstance(empty_dict_1, dict)
empty_dict_2 = dict() # another way of creating empty dict
assert 0 == len(empty_dict_2)
assert empty_dict_1 == empty_dict_2
def test_dictionary_create():
dict_1 = { 1 : "one", 2 : "two" }
assert True == isinstance(dict_1, dict)
#init from a sequence of tuple pairs, useful in many cases.
dict_2 = dict([(1, "one"), (2, "two")])
assert "one" == dict_2[1]
assert "two" == dict_2[2]
def test_dictionary_length():
word_to_digit = { "one" : 1, "two" : 2}
assert 2 == len(word_to_digit) #note that a key value pair is treated as one item
def test_dictionary_is_indexed_by_key():
word_to_digit = { "one" : 1, "two" : 2}
assert 1 == word_to_digit["one"]
assert 2 == word_to_digit["two"]
try:
word_to_digit[1]
except Exception as ex:
#Note that numeric indicies don't mean much like in case of lists and tuples
print("See me in pytest output?", ex, sep="\n")
assert 1 == word_to_digit["one"]
def test_dictionary_is_mutable():
word_to_digit = { "one" : 1, "two" : 2}
word_to_digit["three"] = 3
assert {'one': 1, 'two': 2, 'three': 3} == word_to_digit
del word_to_digit["one"]
assert {'two': 2, 'three': 3} == word_to_digit
word_to_digit["one"] = 10
assert {'two': 2, 'three': 3, 'one': 10} == word_to_digit
def test_dictionary_is_unordered():
dict1 = { 'one': 1, 'two': 2 }
dict2 = { 'two': 2, 'one': 1}
equal = (dict1 == dict2)
assert True == equal # True or False?
def test_dictionary_keys_and_values():
word_to_digit = { "one" : 1, "two" : 2}
assert 2 == len(word_to_digit.keys())
assert 2 == len(word_to_digit.values())
keys = word_to_digit.keys()
#sort to get a deterministic order
keys = sorted(keys)
assert ['one', 'two'] == keys
values = word_to_digit.values()
values = sorted(values)
assert [1,2] == values
def test_dictionary_contains():
word_to_digit = { "one" : 1, "two" : 2}
assert True == ("one" in word_to_digit)
assert True == ("two" in word_to_digit)
assert True == ("one" in word_to_digit.keys())
assert True == ("two" in word_to_digit.keys())
assert False == (1 in word_to_digit)
assert False == (2 in word_to_digit)
assert True == (1 in word_to_digit.values())
assert True == (2 in word_to_digit.values())
def test_valid_dictionary_keys():
test_dict = {}
test_dict[1] = 1
test_dict["one"] = "string"
try:
key = []
test_dict[key] = "list"
except TypeError as te:
print("See me in pytest output?", te, sep="\n") # observe the error message
assert True
try:
key = (1,2)
test_dict[key] = "tuple with immutable elements"
except TypeError as te:
print("See me in pytest output?", te, sep="\n") # observe the error message
assert True # do we reach here?
try:
key = (1, [])
test_dict[key] = "tuple with mutable element"
except TypeError as te:
print("See me in pytest output?", te, sep="\n") # observe the error message
assert True #do we reach here?
assert {1: 1, 'one': 'string', (1, 2): 'tuple with immutable elements'} == test_dict
# Apply what you have learnt in this lesson.
# Hint: See help(zip) in console or in online documentation
def make_dict(keys, values):
"""
Returns a dictionary that maps keys to values correspondingly.
Assume inputs are of same length.
"""
return dict(zip(keys,values)) # write a single line of code to create the dict.
import string
def test_make_dict():
#map digits to their successors
result = make_dict(range(10), range(1,11))
for key, value in result.items():
assert key + 1 == value
# map numerical digits to their string values
result = make_dict(range(10), string.digits)
for key, value in result.items():
assert value == str(key)
assert key == int(value)
notes2= '''
It is good to understand how dictionaries are generally implemented under the hood.
Go through the the entire thread at
http://stackoverflow.com/questions/730620/how-does-a-hash-table-work
and discuss in the group if required.
'''
three_things_i_learnt = """
-dictionary is mutable and is reperesented by {}
-dictinaries have keys and values
-Returns a dictionary that maps keys to values correspondingly using zip
"""