-
Notifications
You must be signed in to change notification settings - Fork 0
/
string1.py
executable file
·186 lines (155 loc) · 5.51 KB
/
string1.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
#!/usr/bin/python -tt
# Copyright 2010 Google Inc.
# Licensed under the Apache License, Version 2.0
# http://www.apache.org/licenses/LICENSE-2.0
# Google's Python Class
# http://code.google.com/edu/languages/google-python-class/
# Basic string exercises
# Fill in the code for the functions below. main() is already set up
# to call the functions with a few different inputs,
# printing 'OK' when each function is correct.
# The starter code for each function includes a 'return'
# which is just a placeholder for your code.
# It's ok if you do not complete all the functions, and there
# are some additional functions to try in string2.py.
# A. donuts
# 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.
# So donuts(5) returns 'Number of donuts: 5'
# and donuts(23) returns 'Number of donuts: many'
def donuts(count):
if count < 10:
return 'Number of donuts: ' + str(count)
return 'Number of donuts: many'
# or
def donuts_2(count):
res = 'Number of donuts: '
if count < 10:
res += str(count)
else:
res += 'many'
return res
# or
def donuts_3(count):
return 'Number of donuts: {0}' .format(count if count <= 9 else 'many')
# B. both_ends
# 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.
def both_ends(s):
if len(s) < 2:
return ''
return s[:2] + s[-2:]
# or
def both_ends2(s):
return '' if len(s) <= 1 else s[:2] + s[-2:]
# or bether
def both_ends3(s):
return s[:2] + s[-2:] if len(s) >= 2 else ''
# C. fix_start
# 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.
# Hint: s.replace(stra, strb) returns a version of string s
# where all instances of stra have been replaced by strb.
def fix_start(s):
head, tail = s[0],s[1:]
return head + tail.replace(head, '*')
# or
def fix_start2(s):
return s[0] + s[1:].replace(s[0], '*')
# D. MixUp
# 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.
# e.g.
# 'mix', pod' -> 'pox mid'
# 'dog', 'dinner' -> 'dig donner'
# Assume a and b are length 2 or more.
def mix_up(a, b):
return b[:2] + a[2:] + ' ' + a[:2] + b[2:]
# or bether
def mix_up2(a, b):
parts = [b[:2], a[2:], ' ', a[:2], b[2:]]
return ''.join(parts)
# Provided simple test() function used in main() to print
# what each function returns vs. what it's supposed to return.
def test(got, expected):
if got == expected:
prefix = ' OK '
else:
prefix = ' X '
print('%s got: %s expected: %s' % (prefix, repr(got), repr(expected)))
# Provided main() calls the above functions with interesting inputs,
# using test() to check if each result is correct or not.
def main():
print('donuts')
# Each line calls donuts, compares its result to the expected for that call.
test(donuts(4), 'Number of donuts: 4')
test(donuts(9), 'Number of donuts: 9')
test(donuts(10), 'Number of donuts: many')
test(donuts(99), 'Number of donuts: many')
print()
print('donuts 2')
# Each line calls donuts, compares its result to the expected for that call.
test(donuts_2(4), 'Number of donuts: 4')
test(donuts_2(9), 'Number of donuts: 9')
test(donuts_2(10), 'Number of donuts: many')
test(donuts_2(99), 'Number of donuts: many')
print()
print('donuts 3')
# Each line calls donuts, compares its result to the expected for that call.
test(donuts_3(4), 'Number of donuts: 4')
test(donuts_3(9), 'Number of donuts: 9')
test(donuts_3(10), 'Number of donuts: many')
test(donuts_3(99), 'Number of donuts: many')
print()
print('both_ends')
test(both_ends('spring'), 'spng')
test(both_ends('Hello'), 'Helo')
test(both_ends('a'), '')
test(both_ends('xyz'), 'xyyz')
print()
print('both_ends 2')
test(both_ends2('spring'), 'spng')
test(both_ends2('Hello'), 'Helo')
test(both_ends2('a'), '')
test(both_ends2('xyz'), 'xyyz')
print()
print('both_ends 3')
test(both_ends3('spring'), 'spng')
test(both_ends3('Hello'), 'Helo')
test(both_ends3('a'), '')
test(both_ends3('xyz'), 'xyyz')
print()
print('fix_start')
test(fix_start('babble'), 'ba**le')
test(fix_start('aardvark'), 'a*rdv*rk')
test(fix_start('google'), 'goo*le')
test(fix_start('donut'), 'donut')
print()
print('fix_start 2')
test(fix_start2('babble'), 'ba**le')
test(fix_start2('aardvark'), 'a*rdv*rk')
test(fix_start2('google'), 'goo*le')
test(fix_start2('donut'), 'donut')
print()
print('mix_up')
test(mix_up('mix', 'pod'), 'pox mid')
test(mix_up('dog', 'dinner'), 'dig donner')
test(mix_up('gnash', 'sport'), 'spash gnort')
test(mix_up('pezzy', 'firm'), 'fizzy perm')
print()
print('mix_up 2')
test(mix_up2('mix', 'pod'), 'pox mid')
test(mix_up2('dog', 'dinner'), 'dig donner')
test(mix_up2('gnash', 'sport'), 'spash gnort')
test(mix_up2('pezzy', 'firm'), 'fizzy perm')
# Standard boilerplate to call the main() function.
if __name__ == '__main__':
main()