## Class 39 — Monday November 23

#### I know that you know that

Programming basics — You achieved this semester — Bask in the success

### Agenda

• Be chrestohistorical

### Thanks to the TAs for making the course successful

 Katherine Brickley Ann Hoang Rachel McNamara MaryJeanine Seaman Ryan Green Hallie Khoung Jimmy Njuguna Jacob St. Martin Nadia Hassan Kevin Luk

### Test problem solving process

• Read entire problem before coding
• Think about solution stratgey before coding
• Is a choice being made?
• Likely need an `if-elif-else` statement
• Is something be built? If so,
• Likely need an accumulator and a loop structure
• Initialize accumulator
• `total = 0` for summation
• `product = 1` for product
• `values = []` for list
• `mappings = {}` for dictionary
• Loop to update accumulator
• Is a sequence just being examined?
• A loop is probably necessary unless there are built-in functions to assist (e.g., `max()`, `min()`, and `len()`).

for value in sequence :

...

• Are the elements of a simple sequence being modified?
• A `for i in range` loop is probably necessary.

n = len( sequence )

for i in range( 0, n ) :

value = sequence[ i ]

...

sequence[ i ] = ...

• When is a nested loop necessary? The major examples are images and datasets. Otherwise, look for a solution without nested loops first.
• Examining an image

for x in range( 0, width ) :

for y in range( 0, height ) :

spot = (x, y )

...

• Examining a dataset / table

for row in dataset :

for value in row :

...

• How to check whether all elements in a sequence have a property.
• Guess `True`, check if any element does not.

result = True

for v in values :

if v does not have the property

result = False

break

• How to check whether any elements in a sequence has a property.
• Guess `False`, check if any element does not.

result = False

for v in values :

if v has the property

result = True

break

• How to check for the best element in a sequence
• Use built-in function `max()` or `min()`, if appropriate
• Otherwise, guess first element and check other elements to see if their better.

result = values[ 0 ]

for v in values :

if v is better than the result

result = v

### Other patterns

• Examine and create dictionaries
• Convert a list of tuples into a dictionary
• Convert a list of two-element lists into a dictionary
• Examine a list with respect to a dictionary

• Examine a table
• Perform an accumulation
• Process a table on a row by row basis
• Convert cells from strings to numerics
• Check to see if all data cells in the table are of the same form
• Check to see if any data cells in the table are of the desired form
• Check to see if all rows in the table are of the same form
• Check to see if any rows in the table are of the desired form

• Photo manipulation

### Realizations

• A variable has an identity and a type
• The identity of a variable indicates where the object which it represents can be found

• Strings are immutable

• Converting string data to numeric data.
• To get a single integer value `nbr` from a single numeric representation `v`

nbr = int( v )

• To get a list of integer values `nbrs` from a list of numeric representations

nbrs = []

for v in s :

nbr = int( v )

nbrs.append( nbr )

• Python function usage
• A function invocation cannot change the identity of an argument
• All functions return a value either explicitly or implicitly. If there is no explicit return, None is returned.

### Truisms

• The test will cummulative.
• Questions can require in part familiarity with
• Using `input()` function
• Implementing programs and modules
• Analyzing code
• Fundamental types; e.g., `int`, `float`, and `str`
• Arithmetic operators; e.g., `+`, `-`, `*`, `**`, `/`, `//`, and `%`
• Identifiers, keywords, variables, objects, values, and references
• Logical and relational operators; `and`, `or`, `not`, `==`, `!=`, `<`, `>`, `<=`, and `>=`
• Control constructs; e.g., `if`, `for`, and `while`
• Nested `for` constructs
• Random value manipulation
• `random.seed()`
• `random.randrange()`
• `random.choice()`
• Builtin-in functions; e.g., `print()`, `input()`, `int()`, `float()`, `str()`, `max()`, `min()`, `len()`, `type()`, and `id()`
• URLs and web files; e.g., numeric, text and CSV contents
• Ranges
• Data structures and their use: `list`, `dict`, tuples, methods and slicing
• Functions, parameters, definitions, and invocations
• Colors and PIL Image

### Final thought

• I say thanks to you — Your presence is a great gift — Brings me happiness

 © 2020 Jim Cohoon Resources from previous semesters are available.