Housekeeping
- There will be a lab practical next week. You should try the practice lab practical I placed on the specs page. You have access to your notes, the book, and the Pyhon docs. YOu can google, but you can't post on a forum (no intelligent agents rule).
- Medium Functions are starting to come in, so I will grade a big chunk of them on Fri.
Comprehensions
Quoth the Textbook Comprehensions provide a succinct and convenient way to filter and transform collections and other iterables. These operations produce a transformed copy of the original iterable; they do not modify the original iterable. The transformed collection can be a list, set or dictionary. If you wish it to be a tuple, make it a list, then convert the result into a tuple since mutability is needed for comprehensions to work.
They can eliminate the need for a lot of error-prone looping and conditional logic. What is nice is that you just say what you want and it happens. You have to get used to their quirky “backwardness,” but this is a miniscule price to pay in exchage for this new and succinct form of expression.
Gimme what I want!! Suppose you are the registrar and you need to create a list of all students with failing grades (score <=60). How can you do this?
Watch us make mincemeat of this.
$$\sum_{k=1}^n f(k)$$
list = [("smith24a", 58), ("jones24b", 100), ("schmo24j", 85), ("roberts24t", 45), ("martin24q", 61)]
Here is what you normally do
Scrabble Dictionary Search We can get the Scrabble dictionary
- Make a list of all Scrabble words with three or more Es.
- Find all palindromes in the Scrabble dictionary.
- How long is the longest word in the Scrabble dictionary?
- Is there just one longest word or it is a tie? If so, how many tying entries are there?
Let's play some Crossword Games! Cheat and find all candidates in the Scrabble dictionary for these crossword problems.
Clue: Butter Pattern: G_A_ Clue: Quacker on NCIS Pattern: _ALL__D Clue: Base for a big ego Pattern: __DE__AL Clue: Source of an expensive "row" Pattern: __UR_E_N Clue: Bullies are the bane of this place Pattern: __A_GR___D
Searching
Warmup Program
Make a program search.py
. Create a function
def contains(x, quarry):
"""precondition: x is a list, quarry is an object
postcondition: returns True if quarry is found in the
list"""
pass