Courses‎ > ‎Intro to CS - Full Year‎ > ‎Spring‎ > ‎Dyrland-Weaver‎ > ‎Work‎ > ‎

Work 18: 3/21

posted Mar 21, 2019, 6:39 AM by JonAlf Dyrland-Weaver

IMPORTANT NOTE: TEST Next Thursday 3/28

Write the following python functions but DO NOT USE ANY LOOPS (or recursion):

  • To do the following functions, it will help to have a function that finds the index of a character in a string. We did this in class already, but there is a built in one called find. It works in the following way:
    • s = 'hello'
    • s.find('l') ==> 2
    • s.find('q') ==> -1
    • Note that to use find you put a . after the variable of the string you want to look through!
    • Test this out a few times so that you are comfortable with it.
  • splitName
    • Takes a string representing a name as a parameter. Assume the string has a first and last name, separated by spaces, like "Peter Frampton"
    • Should return a string with each part of the name on its own line
    • Example: print splitName("John Shaft") ==> John
    •                                         Shaft
  • bondify
    • Names sound cooler the way James Bond says them ("Bond... James Bond"), so we'll write a python program to do that!
    • Takes a single parameter representing a name, the same format as splitName.
    • Should return a new string with the input name written Bond style.
    • Example: bondify("Mr DW") ==> "DW... Mr DW"
  • findLast
    • Takes 2 parameters, one representing a string and the second representing a character.
    • Returns the index of the last occurrence of the character in the string, or -1 if the character is not in the string.
    • Examples
      • findLast( 'hello', 'l') ==> 3
      • findLast('hello', 'h') ==> 0
      • findLast('hello', 'z') ==> -1
  • replace
    • This function will take a string and replace part of it with something new
    • Takes 3 string parameters, The first represents the original string, the second represents the part you want to take out (key), and the last represents the replacement string.
    • Returns a new string that is a copy of the original except that the replacement string is located where the key use to be.
    • If the key is not in the original string, you should return the original, unmodified.
    • Example:
      • replace("I hate cs!", "hate", "love") ==> "I love cs!"
submit this as slicing