For Lab 5 i was given a code which was wrong because it was showing not x and was suppose to see Ok. When starting you should try to get Ok  where x said before . If Ok  showed in all then i successfully fixed the messed up code  here is the code that i used

 

def cupcakes(count):
if count>=10:
return ‘Number of cupcakes: many’
else:
return ‘Number of cupcakes: %s’ % (count)
def both_ends(s):
n1 = len(s)
if 2>n1:
s22 = ”
else:
n2=n1-2
n3=n1-1
s22 = ‘%s%s%s%s’ %(s[0],s[1],s[n2],s[n3])
return s22

m = s[0]

m1 = s.replace(m, ‘*’)

m2 = s[0] + m1[1:]

return m2

def mix_up(a, b):
d = b[:2] + a[2:]
b = a[:2] + b[2:]

return d + ” ” + b

def match_ends(words):
f = 0
for word in words:
if len(word) >= 2 and word[0] == word[-1]:
f = f + 1
return f

def front_x(words):
v = []
z = []
for w in words:
if w.startswith(‘x’):
v.append(w)
else:
z.append(w)
return sorted(v) + sorted(z)

def test(got, expected):
if got == expected:
prefix = ‘ OK ‘
else:
prefix = ‘ X ‘
print ‘%s got: %s expected: %s’ % (prefix, repr(got), repr(expected))

 

def main():
print ‘cupcakes’

test(cupcakes(4), ‘Number of cupcakes: 4′)
test(cupcakes(9), ‘Number of cupcakes: 9′)
test(cupcakes(10), ‘Number of cupcakes: many’)
test(cupcakes(99), ‘Number of cupcakes: 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 ‘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 ‘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 ‘match_ends’
test(match_ends([‘aba’, ‘xyz’, ‘aa’, ‘x’, ‘bbb’]), 3)
test(match_ends([”, ‘x’, ‘xy’, ‘xyx’, ‘xx’]), 2)
test(match_ends([‘aaa’, ‘be’, ‘abc’, ‘hello’]), 1)

print
print ‘front_x’
test(front_x([‘bbb’, ‘ccc’, ‘axx’, ‘xzz’, ‘xaa’]),
[‘xaa’, ‘xzz’, ‘axx’, ‘bbb’, ‘ccc’])
test(front_x([‘ccc’, ‘bbb’, ‘aaa’, ‘xcc’, ‘xaa’]),
[‘xaa’, ‘xcc’, ‘aaa’, ‘bbb’, ‘ccc’])
test(front_x([‘mix’, ‘xyz’, ‘apple’, ‘xanadu’, ‘aardvark’]),
[‘xanadu’, ‘xyz’, ‘aardvark’, ‘apple’, ‘mix’])

if __name__ == ‘__main__’:
main()