this is lab number 5 . for this lab i was provided with starter . I basically followed the instructions and executed them. bellow are the source codes and picture.
Source code:
# Lab5_Python4
# Students’ Names: <Al Sufian>
# INSTRUCTIONS
# Fill in the code for the functions below. The main() fucntion is already set up
# to call the functions with a few different inputs,
# printing ‘OK’ when each function is correct and ‘X’ when wrong.
# The starter code for each function includes a ‘return’
# which is just a placeholder for your code.
#
# SUGGESTIONS
# 1.Run this starter program before you start coding anything, to get familiar with the tests.
# 2.Save this program as Lab5<your last names>.py (replace <your last names> with your lastnames.
# 4.Code one function and test it.
# 5.When the code of your function passes the test, work on the next function.
# 5.Repeat this until you complete all the functions and all of them pass the corresponding test.
# STRING FUNCTIONS
# 1.- cupcakes
# Given an int count of a number of cupcakes, return a string
# of the form ‘Number of cupcakes: <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 cupcakes(5) returns ‘Number of cupcakes: 5′
# and cupcakes(23) returns ‘Number of cupcakes: many’
def cupcakes(count):
if count<10:
return(‘Number of cupcakes: ‘ + str(count))
else:
return(‘Number of cupcakes: many’)
# 2.- 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):
num=len(s)
last=len(s)-2
if len(s)<2:
line=”
else:
line=s[0:2]+s[last:num]
return line
# 3.- 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):
first=s[0]
s=s.replace(s[0],’*’)
new=first+s[1:]
return new
# 4.- 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):
first=a[0:2]
second=b[0:2]
mix= b[0:2]+a[2:]+’ ‘+a[0:2]+b[2:]
return mix
# LIST FUNCTIONS
# 5.- match_ends
# Given a list of strings, return the count of the number of
# strings where the string length is 2 or more and the first
# and last chars of the string are the same.
# Note: python does not have a ++ operator, but += works.
# For example, the result of n = n + 1 can be also achieved by n += 1.
def match_ends(words):
num=0
count=0
while num < len(words):
string=words[num]
if len(string)>=2:
if string[0]== string[len(string)-1]:
count +=1
num +=1
return count
# 6.- front_x
# Given a list of strings, return a list with the strings
# in sorted order, except group all the strings that begin with ‘x’ first.
# e.g. [‘mix’, ‘xyz’, ‘apple’, ‘xanadu’, ‘aardvark’] yields
# [‘xanadu’, ‘xyz’, ‘aardvark’, ‘apple’, ‘mix’]
# Hint: this can be done by making 2 lists (i.e., xWords =[] and otherWords =[])
# and sorting each of them before combining them (i.e., xWords.sort()).
def front_x(words):
xWords=[]
otherWords=[]
num=0
while num < len(words):
if (words[num][0])==’x’:
xWords.append(words[num])
else:
otherWords.append(words[num])
num +=1
xWords.sort()
otherWords.sort()
new=xWords+otherWords
return new
# 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 (‘cupcakes’)
# Each line calls cupcakes, compares its result to the expected for that call.
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’])
# Standard boilerplate to call the main() function.
if __name__ == ‘__main__’:
main()