Lab 5

# Lab5_Python4
# Students” Names: Steven Somwaru

# 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):
  if len(s) <=2:
    return (“”)
  else:
    newstring=s[0] + s[1] + s[len(s)-2] + s[len(s)-1]
  return(newstring)

# 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):
  count = 1

  while len(s)>count:
    if s[count] ==s[0]:
      letter = s[0]
      s=s.replace(s[count], “*”)
      s=letter+s[1:len(s)]

    count +=1
  return (s)

# 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):
  name=b[0:2] + a[2:] + ” ” + a[0:2] + b[2:]
  return (name)

# 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):
  count=0
  num=0
  while len(words) > count:
      if len(words[count])>=2:
        if words[count][0]==words[count][len(words[count])-1]:
          num+=1
      count+=1
  return (num)

# 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):
  xWord=[]
  otherWords=[]
  num=0
  while len(words) > num:
      if words[num][0]==”x”:
        xWord.append(words[num])
      else:
        otherWords.append(words[num])
      num +=1
  xWord.sort()
  otherWords.sort()
  return (xWord + otherWords)

# 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()