# Setup¶

First, simply use Ctrl+Enter to install the package this notebook needs.

In []:
!pip install ipython_doctester
from ipython_doctester import test
import ipython_doctester
ipython_doctester.workshop_name = ''
ipython_doctester.verbose = True


Next, fill in your own (first) name and hit Ctrl+Enter.

In []:
ipython_doctester.student_name = ''


# Friday evening exercises¶

These exercises cover material from the Midwest Python Workshop Friday Python tutorial.

If you have any questions, don't hesitate to ask your neighbor or a staff member for help. We're all here to cooperate!

# happyBirthday(name)

Given a string name, return a string of the form "Happy Birthday !".

If you need them, we have <span style="text-decoration:underline" title="

• You can join strings together (called concatenation) using '+'.
• Use the return keyword to return your new string.
• Don't forget the exclamation point! :) ">Hints (hover over with your mouse to see them)
In []:
@test
def happyBirthday(name):
'''
>>> happyBirthday('Jessica')
'Happy Birthday Jessica!'
>>> happyBirthday('Liz')
'Happy Birthday Liz!'
'''


## cube(i)

given a number, return the cube of that number.

<span style="text-decoration:underline" title="- You can use '' for multiplication. You can also use the exponent operator '*'.

In []:
@test
def cube(i):
'''
>>> cube(1)
1
>>> cube(2)
8
>>> cube(3)
27
'''


Twitter messages can't be more than 140 characters! Given a string myString, return True if the string is longer that 140 characters, and return False if the string is less than or equal to 140 characters.

<span style="text-decoration:underline" title="- Check your notes on the len() function

• Check your notes on <, >, <=, >=">Hints
In []:
@test
'''
>>> tooLongForTwitter('The Analytical Engine weaves algebraic patterns, just as the Jacquard loom weaves flowers and leaves.' +
... '-- Ada Lovelace, the first programmer')
False
>>> tooLongForTwitter('Four score and seven years ago our fathers brought forth on this continent a new nation, ' +
... 'conceived in liberty, and dedicated to the proposition that all men are created equal.')
True
>>> tooLongForTwitter('They told me computers could only do arithmetic. -- Computer pioneer Grace Hopper')
False
'''


## multiplyString(myString, numRepetitions))

Given a string myString and a number numRepetitions, return myString multiplied numRepetitions times.

Hints

In []:
@test
def multiplyString(myString, numRepetitions):
'''
>>> multiplyString('a', 4)
'aaaa'
>>> multiplyString('Hello', 2)
'HelloHello'
>>> multiplyString('Python', 3)
'PythonPythonPython'
'''


# Bonus exercises (Friday night)

Try these if you have time!

## checkNickname(nickname, firstname)

Given a lowercase string nickname and a lowercase string firstname, return True if nickname is completely a part of firstname.

Hints

In []:
@test
def checkNickname(nickname, firstname):
'''
>>> checkNickname('jess', 'jessica')
True
>>> checkNickname('bill', 'william')
False
>>> checkNickname('liz', 'elizabeth')
True
'''


## greatest(x, y, z)

Given numbers x, y, and z, return the greatest of these three numbers.

<span style="text-decoration:underline" title="- You can use the 'and' keyword to check multiple conditions in an 'if' statement

• You might end up using multiple 'if' statements">Hints
In []:
@test
def greatest(x, y, z):
'''
>>> greatest(3, 9, 6)
9
>>> greatest(0.5, 0.25, 0)
0.5
>>> greatest(4, 5, 5)
5
'''


# End Friday exercises

Congratulations! Once you've gotten this far, please head back to the workshop wiki to finish the rest of the Friday material.

# Saturday morning exercises

Wake up your brain with these exercises that review Friday's material.

If you have any questions, don't hesitate to ask your neighbor or a staff member for help!

Now is also a great time to review the Friday tutorial material.

Given a string vegetable and a string entree, return the string "Today our specials are: <vegetable> and <entree>".

<span style="text-decoration:underline" title="- Review the Friday tutorial for how to use single and double quote marks together in strings.

• Remember to return instead of printing. What's the difference between returning and printing?">Hints
In []:
@test
'''
'Today our specials are: asparagus and pasta primavera'
'Today our specials are: artichoke and steak frites'
'Today our specials are: kale and fondue'
'''



It takes 100 ladybugs to protect 1 tree from aphids. Given an integer ladybugs and an integer trees, return True if you have enough ladybugs to protect the trees, or False if you need to buy more ladybugs.

<span style="text-decoration:underline" title="- Review using >, <, >=, and <= in 'if' statements

• Be sure to return the boolean True and not the string 'True'">Hints
In []:
@test
'''
True
False
False
'''



## makeHTML(tag, word)

Web pages use "HTML markup" to style their text. For example, "<i>Test</i>" causes the string "Test" to be displayed in italics. In this example, the "i" tag surrounds the string "Test" starting with <i> and ending with </i>. Given a string tag and a string word, return the word with HTML tags around it, for example "<i>Test</i>".

<span style="text-decoration:underline" title="- Use '+' to join strings together (called 'concatenation')

• Try writing this function at a Python prompt and printing the string you are forming to make sure it looks right.">Hints
In []:
@test
def makeHTML(tag, word):
'''
>>> makeHTML('i', 'Test')
'<i>Test</i>'
>>> makeHTML('b', 'Important!')
'<b>Important!</b>'
>>> makeHTML('code', 'variable')
'<code>variable</code>'
'''


# Saturday afternoon exercises

These exercises will help you review Saturday's material before the afternoon projects.

If you have any questions, don't hesitate to ask your neighbor or a staff member for help!

## returnFirstItem(myList)

Given a list, myList, return the very first (what programmers might call "zero-th"), item in that list

Hints

In []:
@test
def returnFirstItem(myList):
'''
>>>
>>> returnFirstItem(['a', 'b', 'c'])
'a'
>>> returnFirstItem([4.5, 72, -1, 6])
4.5
>>> returnFirstItem(['Hermione', 'Ron', 'Harry'])
'Hermione'
'''


Given a list myList, add a string "!" to the end of the list and return the list

Hints

In []:
@test
'''
['alpha', 'beta', '!']
['Jessica', 'likes', 'ice cream', '!']
>>> addExclamation(['?', '#', '$']) ['?', '#', '$', '!']
'''


## findStudent(name, studentList)

Given a student's name, name, and a list of student names, studentList, return the boolean True if the student name is in the list, and False otherwise.

<span style="text-decoration:underline" title="- You don't need a 'for' loop. Check the data types handout: how can you check if something is in a list?

• Think about the 'in' keyword.">Hints
In []:
@test
def findStudent(name, studentList):
'''
>>> findStudent('Wanda', ['Henry', 'Beatrice', 'Wanda', 'Evan'])
True
False
>>> findStudent('Jen', ['Jessica', 'Jessie', 'Jerry', 'Jen'])
True
'''


## getFirstQWord(wordList)

Given a list of lowercase words, return the first word that starts with a "q". There will always be at least one "q" word.

<span style="text-decoration:underline" title="- We need to go through every item in the word list checking for a condition. This sounds like a good job for a for loop!

• How do you get the first letter of a word?
• How do you compare things?">Hints
In []:
@test
def getFirstQWord(wordList):
'''
>>> getFirstQWord(['quail', 'quetzal', 'quizzical'])
'quail'
>>> getFirstQWord(['croissant', 'quiche', 'toast'])
'quiche'
>>> getFirstQWord(['zip', 'blip', 'quip'])
'quip'
'''



## countFs(phrase)

Given a string phrase, return the total number of "f"s and "F"s in the phrase.

<span style="text-decoration:underline" title="- You can loop over the letters in a string with a for loop just like you can with lists.

• Review your notes on how to use the 'or' and 'and' keywords to test more than one condition in an 'if' statement.
• Use a counter to keep track of how many 'f's you've found. Set it to 0 before you start your for loop.">Hints
In []:
@test
def countFs(phrase):
'''
>>> countFs('Finished files are the result of years of scientific study combined with the experience of years.')
6
>>> countFs('Four score and seven years ago')
1
>>> countFs('A man, a plan, a canal - Panama!')
0
'''


## startsOrEndsWithZ(myString)

Given a string myString, return the boolean True if myString starts or ends with 'z' or 'Z', and False if it doesn't.

<span style="text-decoration:underline" title="- How do you get the first element of a string or list? How do you get the last element?

• You can use keywords 'and' and 'or' in an 'if' statement to test multiple conditions.
• Make sure you are returning a boolean (True or False) and not a string ('True' or 'False') ">Hints
In []:
@test
def startsOrEndsWithZ(myString):
'''
>>> startsOrEndsWithZ('pizzazz')
True
>>> startsOrEndsWithZ('python')
False
>>> startsOrEndsWithZ('zap')
True
'''


## sumOfAllStrings(stringList)

Given a list stringList that contains several strings, return the total of the lengths of all of the strings.

<span style="text-decoration:underline" title="- Use a variable to track the total length. Set it equal to 0 and add to it every time you loop through stringList.

• Use the 'len()' function to get the length of a string.">Hints
In []:
@test
def sumOfAllStrings(stringList):
'''
>>> sumOfAllStrings(['Eleanor', 'Rigby'])
12
>>> sumOfAllStrings(['Eight', 'Days', 'a', 'Week'])
14
>>> sumOfAllStrings(['Strawberry', 'Fields', 'Forever'])
23
'''


## stretchString(myString, stretch_number)

Given a string myString and an integer stretch_number, return a string in which each character of the original string is repeated stretch_number times.

<span style="text-decoration:underline" title="- You can loop over the letters in a string with a for loop just like you can with lists.

• Start with an empty string. Add to the end of the string with '+' as you loop through the letters in myString.
• Try this at a Python prompt: print 'p'*4">Hints
In []:
@test
def stretchString(myString, stretch_number):
'''
>>> stretchString('banana', 2)
'bbaannaannaa'
>>> stretchString('apple', 3)
'aaappppppllleee'
>>> stretchString('fig', 4)
'ffffiiiigggg'
'''


## squaresList(numberList)

Given a list of numbers numberList, return a list containing the squares of each of the numbers in numberList in the order in which they appeared in numberList.

Hints

In []:
@test
def squaresList(numberList):
'''
>>> squaresList([1, 2, 3])
[1, 4, 9]
>>> squaresList([4, 0])
[16, 0]
>>> squaresList([12, 9, 6, 3])
[144, 81, 36, 9]
'''


## removeVowels(myString)

Given a lower-case string myString, return that string with all of the vowels removed.

<span style="text-decoration:underline" title="- Start with an empty string. Add the letters from myString to this new string only if they aren't vowels.

• You can loop over a string just like you can loop over a list.">Hints
In []:
@test
def removeVowels(myString):
'''
>>> removeVowels('bookkeeper')
'bkkpr'
>>> removeVowels('almanac')
'lmnc'
>>> removeVowels('syzygy')
'syzygy'
'''


## removeWordsWithVowels(stringList)

Given a list of lower-case strings wordList, return that list with all the words that contain vowels removed.

<span style="text-decoration:underline" title="- Start with an empty list. Add the words from wordList to this new list only if they don't contain vowels

• Try writing a second function called containsVowels whose job is to check if a word has any vowels. Use this function in a for loop in removeWordsWithVowels. Here's the start of containsVowels... def containsVowels(word):
  for letter in word:

• Use the append() function to add elements to the end of a list.">Hints
In []:
@test
def removeWordsWithVowels(stringList):
'''
>>> removeWordsWithVowels(['hymn','myth','myrrh','nymph'])
['hymn', 'myth', 'myrrh', 'nymph']
>>> removeWordsWithVowels(['who','what','when','where','why'])
['why']
>>> removeWordsWithVowels(['coy','sly','shy','bashful'])
['sly', 'shy']
'''


## spellingBee(correct, guess)

Given the correct spelling of a word, correct, and the contestant's guessed spelling, guess, return the string "correct" if the guess is spelled exactly correctly, "almost" if there are at most two mistakes in the spelling, and "wrong" if three or more letters are different. You can assume that all guesses are the correct length.

<span style="text-decoration:underline" title="Use a counter to keep track of the number of spelling errors.

• We need to compare correct and guess letter by letter. Use the '[0]' syntax to index into a string - for example: if correct[0] != guess[0]
• Review your notes on the len() function, which returns the length of a string.
• Review your notes on the range() function, which returns a list of indices that you can loop over. Experiment with what happens when you try this at a Python prompt: for letter in range(len('hello')):
  print letter">Hints</span>

In []:
@test
def spellingBee(correct, guess):
'''
>>> spellingBee('hello', 'hello')
'correct'
>>> spellingBee('python', 'pithon')
'almost'
>>> spellingBee('echolocation', 'eckolocashun')
'wrong'
'''


That's all! If you've made it this far, you're amazing! (Even if you finished up after the workshop.) Hope you get involved with your local Python user group to find out more of the great stuff you can do!