forked from edooley7/dsp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq6_strings.py
193 lines (149 loc) · 6.07 KB
/
q6_strings.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
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
# Based on materials copyright 2010 Google Inc.
# Licensed under the Apache License, Version 2.0
def donuts(count):
"""
Given an int count of a number of donuts, return a string of the
form 'Number of donuts: <count>', where <count> is the number
passed in. However, if the count is 10 or more, then use the word
'many' instead of the actual count.
>>> donuts(4)
'Number of donuts: 4'
>>> donuts(9)
'Number of donuts: 9'
>>> donuts(10)
'Number of donuts: many'
>>> donuts(99)
'Number of donuts: many'
"""
number_list = ["zero","one","two","three","four","five","six","seven","eight","nine"]
if count < 0: return "ERROR: you owe me some donuts"
elif count < len(number_list) : return number_list[count]
else: return "many"
raise NotImplementedError
def both_ends(s):
"""
Given a string s, return a string made of the first 2 and the last
2 chars of the original string, so 'spring' yields 'spng'.
However, if the string length is less than 2, return instead the
empty string.
>>> both_ends('spring')
'spng'
>>> both_ends('Hello')
'Helo'
>>> both_ends('a')
''
>>> both_ends('xyz')
'xyyz'
"""
if len(s) < 2: return ""
else: return s[:2] + s[-2:]
raise NotImplementedError
def fix_start(s):
"""
Given a string s, return a string where all occurences of its
first char have been changed to '*', except do not change the
first char itself. e.g. 'babble' yields 'ba**le' Assume that the
string is length 1 or more.
>>> fix_start('babble')
'ba**le'
>>> fix_start('aardvark')
'a*rdv*rk'
>>> fix_start('google')
'goo*le'
>>> fix_start('donut')
'donut'
"""
Start_letter = s[0] #store first letter for final output
#delete the next line if you want the function to be case sensitive, I assumed it should not.
s = s.lower() #convert word to all lower case, so make the function case insensive
censored = "" #set up empty string to put processed letters into
for l in s[1:]: #check every letter, except the first
if l == s[0]: censored += "*" #if the letter is the same as the first letter, add "*" to the output instead of the letter
else: censored += l #otherwise add the letter
return Start_letter + censored #return the original first letter, plus the processes string
raise NotImplementedError
def mix_up(a, b):
"""
Given strings a and b, return a single string with a and b
separated by a space '<a> <b>', except swap the first 2 chars of
each string. Assume a and b are length 2 or more.
>>> mix_up('mix', 'pod')
'pox mid'
>>> mix_up('dog', 'dinner')
'dig donner'
>>> mix_up('gnash', 'sport')
'spash gnort'
>>> mix_up('pezzy', 'firm')
'fizzy perm'
"""
return b[:2] + a[2:] + " " + a[:2] + b[2:] #just selected the parts of each string to print out
raise NotImplementedError
def verbing(s):
"""
Given a string, if its length is at least 3, add 'ing' to its end.
Unless it already ends in 'ing', in which case add 'ly' instead.
If the string length is less than 3, leave it unchanged. Return
the resulting string.
>>> verbing('hail')
'hailing'
>>> verbing('swiming')
'swimingly'
>>> verbing('do')
'do'
"""
if len(s) < 3: return s
elif s[-3:] == "ing": return s + "ly"
else: return s + "ing"
raise NotImplementedError
def not_bad(s):
"""
Given a string, find the first appearance of the substring 'not'
and 'bad'. If the 'bad' follows the 'not', replace the whole
'not'...'bad' substring with 'good'. Return the resulting string.
So 'This dinner is not that bad!' yields: 'This dinner is
good!'
>>> not_bad('This movie is not so bad')
'This movie is good'
>>> not_bad('This dinner is not that bad!')
'This dinner is good!'
>>> not_bad('This tea is not hot')
'This tea is not hot'
>>> not_bad("It's bad yet not")
"It's bad yet not"
"""
inot = s.find("not") #locate the first 'not'
ibad = s.find("bad") #locate the firt 'bad'
if inot == -1: return s #return original if 'not' is not found
if ibad == -1: return s #return original if 'bad' is not found
if ibad < inot: return s #return original if 'bad' comes before 'not'
front = s[:inot] #everything before 'not'
end = s[ibad+len("bad"):] #everything after 'bad'
return front + "good" + end #put "good" in between the two ends
raise NotImplementedError
def front_back(a, b):
"""
Consider dividing a string into two halves. If the length is even,
the front and back halves are the same length. If the length is
odd, we'll say that the extra char goes in the front half. e.g.
'abcde', the front half is 'abc', the back half 'de'. Given 2
strings, a and b, return a string of the form a-front + b-front +
a-back + b-back
>>> front_back('abcd', 'xy')
'abxcdy'
>>> front_back('abcde', 'xyz')
'abcxydez'
>>> front_back('Kitten', 'Donut')
'KitDontenut'
"""
a_front = "" #initialize 1st half of a
a_back = "" #initialize 2nd half of a
for i in range(len(a)): #for every index in input a
if i < len(a)/2: a_front += a[i] #if the index is less than half the length put the letter in the first half
else: a_back += a[i] #otherwise put it in the second half
b_front = "" #repeat the same steps for string b
b_back = ""
for i in range(len(b)):
if i < len(b)/2: b_front += b[i]
else: b_back += b[i]
return a_front + b_front + a_back + b_back #put together the halves in the mixed order described above
raise NotImplementedError