Lab 4.

Lists, Strings and Loops

To get started, download a copy of the lab4_programs folder from the cs server. Rename the folder to contain your name, and set your working directory in Canopy to be this folder. Now you're ready to begin.

Getting warmed up

  • string manipulations
  • 2 minute for loop review
  • graphics and loops
  • working with bigger lists
  • filtering and mapping over lists of words

Handling strings

It's useful to know how to extract parts of strings.
Note: does it matter if you use single quotes (') or double quotes (") in python around strings? Short answer: no. Click here for a long explanation.

Here are some special python string operators:

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

Part I: String manipulation

Task 1. Writing simple string functions

Open the lab4.py file in your lab4_programs folder. You may, if you like, use the built-in Python function len(s), which returns the length of a string s. Complete the function definitions in your file. You'll need to fill in the missing code to make sure that your code works correctly.
Note that each function should be just one line of code.
def first(s):
def last(s):
def sameLength(s1,s2):
def average(a,b):

Task 2. Writing more string functions

  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. Note: you may assume that the word contains distinct letters, e.g, "noon" or "pump" will not work correctly with your betterStarify(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. *OPTIONAL* 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:
    1. Password must contain a mix of upper and lower case letters
    2. Password must contain at least 1 number
    3. Password must contain at least 1 special character from this set !#$%&*
    Hint: Python has some very useful built-in functions such as islower(),isupper() and isdigit(), for example 'a'.islower() returns True.

    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
    

  5. *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).

    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
    

Part II: For loops and graphics

Here is some incomplete code that does not quite work correctly. Let's figure out why.
paper1 = Canvas(600,600,'beige','CS111 Lab 4')

def loopySquares1(number,size,color):
    for ______________:
        s = Square(size,Point(200,300))
        s.setFillColor(color)
        paper1.add(s)

Task A: loopySquares1(5,100,'darkseagreen1')

Complete the definition of the loopySquares1() function that takes the number of squares, the size of the square, and the color of the square and create the above picture using a loop. Details:
  1. You can add your squares directly to your canvas from within loopySquares1()
  2. Each successive square's center point shifts size to the right

Task B: loopySquares2(5,200,'darkseagreen1')

Write the definition of the loopySquares2() function that takes the number of squares, the size of the square, and the color of the square and create the above picture using a loop. Details:

  1. Create a new canvas, separate from the one above
  2. Add your squares directly to your canvas from within loopySquares2()
  3. Each successive square's center point shifts size/4 to the right
Task C: loopySquares3(5,200,72,'darkseagreen1')
Write the definition of the loopySquares3() function that takes the number of squares, the size of the square, the angle of rotation and the color of the square and create the above picture using a loop. Details:
  1. Create a new canvas, separate from the ones above
  2. Add your squares directly to your canvas from within loopySquares3()
  3. Each successive square rotates angle degrees more than its predecessor
Task D: loopySquares3(5,200,72,['darkred','ivory','orange','pink','navy'])

Write the definition of the loopySquares4() function that takes the number of squares, the size of the square, the angle of rotation and the color of the square and create the above picture using a loop. Details:
  1. Create a new canvas, separate from the ones above
  2. Add your squares directly to your canvas from within loopySquares4()
  3. Each successive square rotates angle degrees more than its predecessor
  4. The colors are supplied in a list. Conveniently, the number of colors in the list is the same as the number of squares drawn. CHALLENGE: what if the number of colors and number of squares are different? How to handle this? Hint: See lecture 6 notes, slide 32.
  5. You can use str() on a list to get a string representation of that list, e.g. str(['darkred','ivory','orange']) returns
    "['darkred','ivory','orange']"
    .


Part III: For loops and lists

Now we'll graduate to working with longer lists of words. It's faster to work with lists of words that are already defined, so we have set up files that create word lists for you.

file name variable(s) created
sampleLists.py greetings, colors, fruit,...
wordLists.py tinyWordList,smallWordList
songLyrics.py beautifulLyricList, beforeHeCheatsLyricList, iGetByWithALittleHelpLyricList, imSoFancyLyricList, loveStoryLyricList, thriftShopLyricList
vocabulary.py englishwords

Here is a screenshot of what the contents of songLyrics.py looks like:
To access these lists in your code, add the name of the relevant file at the top of your code, e.g.

  from songLyrics import * # can use beautifulLyricList and other song lyrics

Some useful python reminders

  • len() returns the number of items in a list (or a string), e.g. len(["happy","birthday"]) ==> 2 and len("mississippi") ==> 11
  • count() returns the number of occurrences of a letter in a string (or words in a list), e.g. "mississippi".count("s") ==> 4 and "mississippi".count("x") ==> 0
  • float() converts integers to floating point numbers, e.g. float(21) ==> 21.0. This is useful when you need to divide an integer by an integer and get a decimal result, e.g. 5/4 ==> 1 but float(5)/4 ==> 1.25
  • Your new friend append (add new items to an existing list)

Write a python function to answer each question

  1. How many words in the wordlist begin with the letter "a"? Hint: Use a variable to keep track of how many words start with "a", looking at each word one at a time as you loop through the list of words.
    Examples:
    print str(wordsStartWithA(smallWordList)) # 65
    print str(wordsStartWithA(loveStoryLyricList)) # 27
    print str(wordsStartWithA(englishwords)) # 3685
    print str(wordsStartWithA(beautifulLyricList)) # 17
    print str(wordsStartWithA(imSoFancyLyricList)) # 17
  2. What is the average word length of the word list or song? Examples:
    print str(averageLength(imSoFancyLyricList)) #4.055
    print str(averageLength(beautifulLyricList)) # 4.029
    print str(averageLength(beforeHeCheatsLyricList)) # 4.269
    print str(averageLength(loveStoryLyricList)) # 3.87
    print str(averageLength(thriftShopLyricList)) # 4.43
    print str(averageLength(iGetByWithALittleHelpLyricList)) # 3.66
    print str(averageLength(tinyWordList)) # 5.7
    print str(averageLength(smallWordList)) # 6.08
    print str(averageLength(englishwords)) # 8.37
  3. Which song uses the word "you" the most? Examples:
    print str(countYou(imSoFancyLyricList)) #13
    print str(countYou(beautifulLyricList)) #28 # the winner!
    print str(countYou(beforeHeCheatsLyricList)) # 1
    print str(countYou(loveStoryLyricList)) # 16
    print str(countYou(thriftShopLyricList)) # 5
    print str(countYou(iGetByWithALittleHelpLyricList)) # 10
  4. Which words in the wordlist contain at least 5 occurrences of the letter "s"? Hint: append is useful here to build a list of words that have at least 5 s's in them. Start with an empty list and append each word that meets the criteria. Then return the list. Examples:
    print str(wordsWith5s(smallWordList)) # []
    print str(wordsWith5s(englishwords)) #['ambassadresses', 'assassinates', 'assassinations', 'assassins', 'assesses', 'assessments', 'assessors', 'classlessness', 'crisscrosses', 'dispassionateness', 'dispossess', 'dispossessed', 'dispossesses', 'dispossessing', 'dispossession', 'housemistresses', 'listlessness', 'noiselessness', 'possesses', 'possessions', 'possessiveness', 'possessives', 'possessors', 'postmistresses', 'prepossesses', 'prepossessions', 'purposelessness', 'remorselessness', 'repossesses', 'restlessness', 'schoolmistresses', 'seamstresses', 'secessionists', 'selflessness', 'sempstresses', 'senselessness', 'sensuousness', 'shamelessness', 'shapelessness', 'shiftlessness', 'sinlessness', 'sleeplessness', 'songstresses', 'soullessness', 'speechlessness', 'spinelessness', 'spiritlessness', 'spotlessness', 'statelessness', 'subconsciousness', 'submissiveness', 'suppressiveness', 'taskmistresses', 'tastelessness', 'uselessness']
    print str(wordsWith5s(beforeHeCheatsLyricList)) # []
  5. Which words in the wordlist contain all the characters in a given string (e.g. "love")?
    Hint: first, figure out how to tell if one word contains all the letters of another string. Examples:
    print (wordsContainAllChars(smallWordList,"pam")) ==> ['amorphous', 'atmosphere', 'campanile', 'imperial', 'pam', 'pamela', 'polynomial', 'program', 'scamper', 'temptation']


    print (wordsContainAllChars(loveStoryLyricList,"love")) ==> ['love', 'love', 'love', 'love', 'love', 'love']

    print (wordsContainAllChars(thriftShopLyricList,"ough")) ==> ['bought', 'bought', 'bought', 'through', 'dough', 'through']

    print (wordsContainAllChars(englishwords,"evrngast")) ==> ['advertising', 'asseverating', 'circumnavigates', 'everlasting', 'everlastingly', 'eviscerating', 'extravagances', 'extravaganzas', 'gravestone', 'gravestones', 'harvesting', 'interrogatives', 'investigator', 'investigators', 'invigorates', 'overcompensating', 'overestimating', 'overmastering', 'overstating', 'overstaying', 'starveling', 'starvelings', 'tergiversating', 'tergiversation', 'traversing', 'travestying', 'variegations', 'vegetarianism', 'vegetarians']

  6. What are the longest words in a song or word list (without repeating words)?
    Examples:
    print str(longestWords(imSoFancyLyricList))==>['department', 'Chandelier', 'expensive,']
    print str(longestWords(beautifulLyricList)) ==> ['overwhelmed', 'desperately']
    print str(longestWords(beforeHeCheatsLyricList))==>['bleached-blond']
    print str(longestWords(loveStoryLyricList)) ==>['http://www.elyrics.net']
    print str(longestWords(thriftShopLyricList))==>['hand-me-downs?']
    print str(longestWords(iGetByWithALittleHelpLyricList))==>['Ah-ah-ah-ah-ah-ah-ah-ah-ah']
    print str(longestWords(englishwords)) ==>['electroencephalographs']

    Part IV: List manipulation (time permitting)

    Create a new file called lists.py in your lab4_programs folder. Note that there is a file called sampleLists.py that you can use for testing. You have two options for using these sample lists:
    1. Add from sampleLists import * to the top of your file, then you can access any list, e.g. mapPluralize(despicableMe), OR
    2. Copy and paste the code from sampleLists.py into your other files

    Task 3a: filterDrop(value, mylist) Write a function called filterDrop(value,mylist) that takes a value and a list and returns a new list with all the occurrences of value dropped.
    Examples:

    skyfall = ['M','Q','Moneypenny','James','JamesBond','Bond','Silva','Patrice']
    filterDrop ('James',skyfall) ==> ['M','Q','Moneypenny','JamesBond','Bond','Silva','Patrice']
    filterDrop ('Q',skyfall) ==> ['M','Moneypenny','James','JamesBond','Bond','Silva','Patrice']

    Task 3b: filterLength(maxLength, mylist) Write a function called filterLength(maxlength,mylist) that takes a list of pairs of strings and returns a new list containing the pairs that do not exceed the specified maximum combined string length.
    Examples:

    roster = [('Yoyo','Ma'),('Sohie','Lee'),('Brian','Tjaden'),('Jean','Herbst'),('Santa','Claus'),('Happy','Camper'),('Harry','Styles'),('Taylor','Swift'),('Alexander','TheTerrible')]
    filterLength(8,roster) ==> [('Yoyo','Ma'),('Sohie','Lee')] # combined length of 'SohieLee' is less than or equal to 8
    filterLength(10,roster) ==> [('Yoyo','Ma'),('Sohie','Lee'),('Jean','Herbst'),('Santa','Claus')]
    filterLength(12,roster) ==> [('Yoyo','Ma'),('Sohie','Lee'),('Jean','Herbst'),('Brian','Tjaden'),('Santa','Claus'),('Happy','Camper'),('Harry','Styles'),('Taylor','Swift')]
    Task 3c: mapPluralize(mylist) Write a function called mapPluralize(mylist) that takes a list of words and returns a new list with the plurals of each of the words. The nouns are pluralized by adding the suffix "s". For instance, the plural of "bagel" is "bagels". Note that this is a simple pluralizer and that it does not handle special cases like "kiss" -> 'kisses'. There are opportunities for improving your mapPluralize near the bottom of this page.
    Examples:
    mapPluralize(['assignment']) ==> ['assignments']
    mapPluralize(['donut','muffin','bagel']) ==> ['donuts','muffins','bagels']
    mapPluralize(['tree','witch','kiss','moose', 'alpaca']) ==> ['trees','witchs','kisss', 'mooses','alpacas']