09-14
Ongoing topics
- Print vs return
- Mutation functions do not return values. Also: Why does append return None? Examples: append, extend. The Python boss post on the topic is short and informative.
- Copy vs in-place:
sorted(data)vsdata.sort().
Upcoming
- Changing the collecting while you iterate through it (copy instead)
- Using
unittestto write “check-expects”.
Questions
-
Make a list of ten foods (
allFoods). Make your own functionsomeRand(data, N)to randomly select N items from a given list with replacement. -
Randomly select 20 items from the list (with replacement) and use that as
foodListfor the next problems. -
Make a list of ordered pairs (food, number of that food in
foodList) so that every possible food appears once. -
Create a dictionary
foodCountwith the same information as the previous problem. That isfoodCount[food]is the number of that food infoodList. -
Create a function
randomCapthat takes in a string and puts out a string with a random capitalization, one of three cases:lower(),upper()andtitle(). -
Use your function to make a new variable
foodListCapsby applyingrandomCaptofoodList. -
Sort
foodListCapsand print it out. -
Sort it again, but in a way that ignores the case of the letters. Print that out too.
-
Write a function
del37that deletes the items in indices 3 through 7 from a given input list. Verify that it works:squares20 = [ x*x for x in range(15) ] del37(squares20) print(squares20) -
Make a set containing every food in
foodList. -
Write a function that takes in a string and returns a set of all of the non-vowels in the string. Test it on your name.
Problems
Everyone does the first one. If you want to skip the questions above, do the second one.
-
USACO 2019 January Bronze 1: shell game.
-
USACO 2018 December Bronze 3: back and forth.