Lab 5, Part 2: Strings with For Loops

Handling strings

A necklace strung with 5 beads. Each bead has a letter on it, and the letters spell out c-s-1-1-1.

Table of some string operations:

symbol operation example
+ concatenation str1 = 'I am '
str2 = 'your father '
darth = str1 + str2
yoda = str2 + str1
* repetition 'hee'*3 ==> 'heeheehee'
[] index str2[5]==> 'f'
[:] slice str2[2:5]==> 'ur '
in in 'dad' in str2 ==> False
'at' in str2 ==> True
not in not in str1 not in str2 ==> True
str1 not in darth ==> False

Task 1. Writing string functions with loops

Continue writing your functions in your lab05.py file.

  1. Write a function called getWordValue(word) that takes in a word, and returns the value of the word using the following (made-up) rule: Each vowel (a,e,i,o,u) counts as 5 and each consonant counts as 1. You may ignore numbers and special characters for this function.
    print(getWordValue('hello')) ==> 13
    print(getWordValue('I')) ==> 5
    print(getWordValue('ouch')) ==> 12
    print(getWordValue('WELLESLEY')) ==> 21
    print(getWordValue('Go 123 Wellesley!!!!')) ==> 27
  2. Write a function called starify(word) that takes in a word, and returns a new string with a * after each letter in the original word. Hint: create a new string variable, initially empty, that will store the new string to be returned.
    print(starify('OMG')) ==> O*M*G*
    print(starify('wicked')) ==> w*i*c*k*e*d*
    print(starify('Starry')) ==> S*t*a*r*r*y*
  3. Write a function called betterStarify(word) that takes in a word, and returns a new string with a * after each letter in the original word, but not after the last letter in the word.
    print(betterStarify('OMG')) ==> O*M*G
    print(betterStarify('wicked')) ==> w*i*c*k*e*d
    print(betterStarify('Starry')) ==> S*t*a*r*r*y
  4. Write a predicate called lottaVowels(word) that returns True if the word contains more than 2 vowels. You may use the provided isVowel in your lab05.py file.
    print(lottaVowels('OMG')) ==> False
    print(lottaVowels('hello')) ==> False
    print(lottaVowels('ABBA')) ==> False
    print(lottaVowels('GottaLOVEABBA')) ==> True
    print(lottaVowels('piano')) ==> True
    print(lottaVowels('eee')) ==> True
    print(lottaVowels('gummybears')) ==> True
    print(lottaVowels('laugh')) ==> False
    print(lottaVowels('laughingxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')) ==> True

    Note: look at the last example, with this string 'laughingxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'. It would be efficient if your lottaVowels returned True as soon as the third vowel was encountered, rather than waiting until the end of the string. Can you amend your lottaVowels so it does that? (Sometimes we call this 'early exit')

  5. Write a function called checkPassword(word) that takes in a word, and returns True if the word satisfies the following password criteria and False otherwise. Hint: Python has some very useful built-in functions such as islower,isupper and isdigit, for example 'a'.islower() returns True and 'a'.isupper() returns False.

    • Password must contain a mix of upper and lower case letters
    • Password must contain at least 1 number
    • Password must contain at least 1 special character from this set !#$%&*
      print(checkPassword('hello123')) ==> False
      print(checkPassword('PASSWORD')) ==> False
      print(checkPassword('myPASSWORD?')) ==> False
      print(checkPassword('loveMyDogCharlie!')) ==> False
      print(checkPassword('CS111#rocksmyworld')) ==> True
      print(checkPassword('running99*FAST')) ==> True
      print(checkPassword('oK8!')) ==> True # Minimum length would be good idea too
  6. [OPTIONAL] Write a function called ransomNote(sentence) that takes in a sentence, and returns a new sentence, based on the original sentence, with each letter randomly capitalized (with a 50% chance of being capitalized. Might need to use the random.randint function). Need a randint() refresher? Here is an example from the previous lab with the coin flip, and here are the general python documentation pages for random numbers.
    print(ransom("Must provide chocolate to get your teddy bear back")) ==>
    muST ProvIDE CHocOlATe To get YOur TEddY bEAr bACK
    print(ransom("Meet me at grand central station with harry styles and a million dollars tonight")) ==>
    mEET mE at graND CentRAL sTatIOn WITh HarRY StYlEs AND a mIlLIOn DOLLaRs TOnigHT

Table of Contents