First, simply use Ctrl
+Enter
to install the package this notebook needs.
!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
.
ipython_doctester.student_name = ''
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!
Given a string name, return a string of the form "Happy Birthday
If you need them, we have <span style="text-decoration:underline" title="
concatenation
) using '+'.">Hints (hover over with your mouse to see them)
@test
def happyBirthday(name):
'''
>>> happyBirthday('Jessica')
'Happy Birthday Jessica!'
>>> happyBirthday('Adam')
'Happy Birthday Adam!'
>>> happyBirthday('Liz')
'Happy Birthday Liz!'
'''
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 '*'.
@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
@test
def tooLongForTwitter(myString):
'''
>>> 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
'''
Given a string myString and a number numRepetitions, return myString multiplied numRepetitions times.
Hints
@test
def multiplyString(myString, numRepetitions):
'''
>>> multiplyString('a', 4)
'aaaa'
>>> multiplyString('Hello', 2)
'HelloHello'
>>> multiplyString('Python', 3)
'PythonPythonPython'
'''
Try these if you have time!
Given a lowercase string nickname and a lowercase string firstname, return True if nickname is completely a part of firstname.
Hints
@test
def checkNickname(nickname, firstname):
'''
>>> checkNickname('jess', 'jessica')
True
>>> checkNickname('bill', 'william')
False
>>> checkNickname('liz', 'elizabeth')
True
'''
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
@test
def greatest(x, y, z):
'''
>>> greatest(3, 9, 6)
9
>>> greatest(0.5, 0.25, 0)
0.5
>>> greatest(4, 5, 5)
5
'''
Congratulations! Once you've gotten this far, please head back to the workshop wiki to finish the rest of the Friday material.
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.
@test
def menuSpecials(vegetable, entree):
'''
>>> menuSpecials('asparagus', 'pasta primavera')
'Today our specials are: asparagus and pasta primavera'
>>> menuSpecials('artichoke', 'steak frites')
'Today our specials are: artichoke and steak frites'
>>> menuSpecials('kale', 'fondue')
'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
@test
def enoughLadybugs(ladybugs, trees):
'''
>>> enoughLadybugs(300, 3)
True
>>> enoughLadybugs(300, 4)
False
>>> enoughLadybugs(250, 11)
False
'''
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')
@test
def makeHTML(tag, word):
'''
>>> makeHTML('i', 'Test')
'<i>Test</i>'
>>> makeHTML('b', 'Important!')
'<b>Important!</b>'
>>> makeHTML('code', 'variable')
'<code>variable</code>'
'''
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!
Given a list, myList, return the very first (what programmers might call "zero-th"), item in that list
Hints
@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
@test
def addExclamation(myList):
'''
>>> addExclamation(['alpha', 'beta'])
['alpha', 'beta', '!']
>>> addExclamation(['Jessica', 'likes', 'ice cream'])
['Jessica', 'likes', 'ice cream', '!']
>>> addExclamation(['?', '#', '$'])
['?', '#', '$', '!']
'''
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?
@test
def findStudent(name, studentList):
'''
>>> findStudent('Wanda', ['Henry', 'Beatrice', 'Wanda', 'Evan'])
True
>>> findStudent('Adam', ['Tanya', 'Alice', 'Sean'])
False
>>> findStudent('Jen', ['Jessica', 'Jessie', 'Jerry', 'Jen'])
True
'''
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!
@test
def getFirstQWord(wordList):
'''
>>> getFirstQWord(['quail', 'quetzal', 'quizzical'])
'quail'
>>> getFirstQWord(['croissant', 'quiche', 'toast'])
'quiche'
>>> getFirstQWord(['zip', 'blip', 'quip'])
'quip'
'''
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.
@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
'''
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?
@test
def startsOrEndsWithZ(myString):
'''
>>> startsOrEndsWithZ('pizzazz')
True
>>> startsOrEndsWithZ('python')
False
>>> startsOrEndsWithZ('zap')
True
'''
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.
@test
def sumOfAllStrings(stringList):
'''
>>> sumOfAllStrings(['Eleanor', 'Rigby'])
12
>>> sumOfAllStrings(['Eight', 'Days', 'a', 'Week'])
14
>>> sumOfAllStrings(['Strawberry', 'Fields', 'Forever'])
23
'''
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.
@test
def stretchString(myString, stretch_number):
'''
>>> stretchString('banana', 2)
'bbaannaannaa'
>>> stretchString('apple', 3)
'aaappppppllleee'
>>> stretchString('fig', 4)
'ffffiiiigggg'
'''
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
@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]
'''
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.
@test
def removeVowels(myString):
'''
>>> removeVowels('bookkeeper')
'bkkpr'
>>> removeVowels('almanac')
'lmnc'
>>> removeVowels('syzygy')
'syzygy'
'''
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
append()
function to add elements to the end of a list.">Hints@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']
'''
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.
@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!