-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunit4_assignment_03.py
87 lines (65 loc) · 2.42 KB
/
unit4_assignment_03.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
__author__ = 'Kalyan'
notes = '''
You have written similar code in earlier units. These exercise are for practicing
writing code using comprehensions.
'''
import string
def get_reverse_mapping():
"""
Maps a -> z , b -> y, c -> x etc. Only smalls. Use dict comprehension
"""
result = {x: chr((ord('z') - ord(x)) + ord('a')) for x in string.ascii_lowercase}
return result
def test_get_reverse_mapping():
result = get_reverse_mapping()
for index, letter in enumerate(string.ascii_lowercase):
value = result[letter]
assert 1 == len(value)
index2 = string.ascii_lowercase.find(value)
assert index + index2 == 25
def get_lower_to_upper_dict():
"""
returns a dict which contains a mapping from lower case letters to upper case letters. Use dict comprehension
"""
result = {x: chr((ord(x) - ord('a')) + ord('A')) for x in string.ascii_lowercase}
return result
def test_lower_to_upper_dict():
lower_to_upper = get_lower_to_upper_dict()
assert 26 == len(lower_to_upper)
for x in lower_to_upper:
y = lower_to_upper[x]
assert 1 == len(x)
assert x.islower()
assert 1 == len(y)
assert y.isupper()
assert x.upper() == y
def get_encoding_dict():
"""
returns a dict which maps a -> 1, b -> 2 ... z->26 and A -> 1, B ->2 ... Z->26
"""
result={x: ord(x.lower()) - ord('a') + 1 for x in string.ascii_letters}
return result
def test_get_encoding_dict():
d = get_encoding_dict()
assert len(d) == 52
for key in d:
assert ord(key.lower()) - ord('a') + 1 == d[key]
import calendar
# use the calendar.month_abbr (read the source code in calendar.py and understand how it works. yes, really!)
#
# right click on calendar import above and go to declaration
# and use it in your comprehension below)
def get_months():
"""
returns a map of month index (1 to 12) to the month abbreviation ("Jan", "Feb"....).
Use calendar.month_abbr as indicated above. Use dict comprehensions
"""
result = {x: y for y, x in enumerate(calendar.month_abbr) if y>0}
return result
def test_get_months():
month_map = get_months()
assert type(month_map) == dict
assert len(month_map) == 12
months = list(calendar.month_abbr)
for index in range(1,13):
assert months[index] == months[index]