-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassignment_03.py
executable file
·71 lines (58 loc) · 2.16 KB
/
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
__author__ = 'Chaithra'
notes = '''
1. Read instructions for each function carefully.
2. Try to use builtins and data structures instead of writing your own.
3. If something about the function spec is not clear, use the corresponding test
for clarification
'''
from placeholders import *
# Convert a sentence which has either or to only the first choice.
# e.g we could either go to a movie or a hotel -> we could go to a movie.
# note: do not use intermediate lists, only use string functions
# assume words are separated by spaces. you can use control flow statements
def prune_either_or(sentence):
if(sentence.rfind("or") == -1):
return sentence
else:
list1 = (sentence.split(" or"))[0]
list2 = list1.split(" either")
result= "".join(list2)
if(result.rfind("either")==-1):
return result
else:
return ""
# Create a palindrome of twice the length of the word passed in.
# e.g. app -> appppa, bat -> battab etc.
# hint: look up extended slice notation.
def create_palindrome(word):
if word == None:
return None
reverse_word = word[::-1]
pallin = word + reverse_word
return pallin
import string
# returns a dict which maps a -> 1, b -> 2 ... z->26 and A -> 1, B ->2 ... Z->26
# no control flow allowed.
def get_encoding_dict():
return dict(zip(string.letters, range(1, 27)*2))
def test_either_or_pruning():
assert "We can go to a movie" == prune_either_or("We can either go to a movie or a hotel")
assert "We can go either way" == prune_either_or("We can go either way")
assert "" == prune_either_or("either or")
assert "either way is fine" == prune_either_or("either way is fine")
def test_create_palindrome():
assert "battab" == create_palindrome("bat")
assert "abba" == create_palindrome("ab")
assert "" ==create_palindrome("")
assert None == create_palindrome(None)
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]
three_things_i_learnt = """
-
-
-
"""
time_taken_minutes = ___