Today: midterm prep, Doctests, exponentiation, modulus, num-histogram example

No new material shown today is on the midterm - the midterm is about topics on HW5, but we will do an additional HW5 dict example today.

## Midterm Prep Page

- Look at the Midterm Prep page - summary...
- Also have last Spring exam
- Topics not on: "dict" "bluescreen"
- Include on exam: list of functions, file reading code
- Closed Note
- Code Writing - a lot like lecture/homework functions
- You have tons of practice problems with solutions
- We do not grade off for syntax
- Partial credit for partially correct answer
- The trap of just looking at solution code to study
- How to study for this
- Bluebook software on your laptop - set this up Wed

## Learning on the Homeworks - Delete Key Test

- 1. Watch lecture example - get basic idea a little
- 2. Later coding in Lair or at home...
- -Look at lecture example again .. fine!
- No: copy/mutate lecture example into HW until it works without full understanding
- Yes: experiment and understand lecture example, write your hw code
- You can use lecture example as much as you like, so long as you are also building understanding
- Delete key test: could you select all the working code of a HW problem and delete it?
- You should be confident that you could re-build it
- Delete key test .. is what the exam looks like!

## Shorter Code - Paths

- We prefer fewer lines to solve a problem
- Show some techniques to reduce number of lines / number of cases
- Unlikely to mark off for these, but SL might point these techniques out

>
Shorter problems

## Shorter 1. shorter()

- Issue here: factor the if-logic, don't like have 4 cases here, 2 of which are the same.
- Copy-paste of the code is a clue that this can shortened.
- Pull out one case, handle as "pick-off" if/return
- Nice when a case is handled, can think about remaining cases only

Given name string and score.
If score is less than 10 return
a string like 'Alice:5!'.
If score is larger, return a string
like 'Alice:12 yay!'.
If name is empty string, return
just the plain score.
Challenge: change this code to be shorter,
not have so many distinct paths.

Before/after code:

# before: this works, but is long
def shorter(name, score):
if score < 10:
if name != '':
return name + ':' + str(score) + '!'
else:
return str(score)
else:
if name != '':
return name + ':' + str(score) + ' yay!'
else:
return str(score)
# 1. Handle 1 case first, (minor) trim off "else"
def shorter(name, score):
if name == '':
return str(score)
if score < 10:
return name + ':' + str(score) + '!'
return name + ':' + str(score) + ' yay!'

## Shorter 2. Speeding Ticket - Variable Invariant

Say compute speeding ticket as a function of speed and if it's your birthday. This code works.

def speeding(speed, birthday):
if birthday:
if speed < 55:
return 100
else:
return 200
else:
if speed < 50:
return 100
else:
return 200

- These cases look really similar
- They differ by the value in the if-test
- Solution: "limit" variable holds the limit, use if-logic to set it
- Invariant: limit holds the value to use, code below just uses it, so no if/logic there
- i.e. set state in a var for later lines vs. having if/else

Solution

def speeding(speed, birthday):
# Setup limit var
limit = 50
if birthday:
limit = 55
# Invariant: limit holds value to use
if speed > limit:
return 200
return 100

## Shorter 3. ncopies

Given name string, n, suffix, return
n copies of string + suffix.
If suffix is empty, use '!' for it.
Challenge: change this code to be shorter,
not have so many distinct paths.

def copies(word, n, suffix):
result = ''
if suffix == '':
for i in range(n):
result += word + '!'
else:
for i in range(n):
result += word + suffix
return result

Solution: use logic to set "suffix" as invariant, code uses its value vs. if/logic

def copies(word, n, suffix):
result = ''
# Set suffix to be value to use regardless
if suffix == '':
suffix = '!'
# invariant: suffix is value to use
for i in range(n):
result += word + suffix
return result

## Bits and Bytes

At the smallest scale in the computer, information is stored as bits and bytes. In this section, we'll look at how that works.

## Bit

- a "bit", like an atom, the smallest unit of storage
- A bit stores just a 0 or 1
- "In the computer it's all 0's and 1's" ... bits
- Anything with two separate states can store 1 bit

-Nick's tennis racket example
- In a chip: electric charge = 0/1
- In a hard drive: spots of North/South magnetism = 0/1
- A bit is too small to be much use
- Group 8 bits together to make 1 byte

## Byte

- One byte = grouping of 8 bits
- e.g. 0 1 0 1 1 0 1 0
- One byte can store one roman character, e.g. 'A' or 'x' or '$'

## How Many Patterns With N Bits? (demo)

How many different patterns can be made with 1, 2, or 3 bits?

Number of bits | Different Patterns |

1 | 0 1 |

2 | 00 01 10 11 |

3 | 000 001 010 011 100 101 110 111 |

- Combare 3 bits vs. 2 bits
- Consider just the leftmost bit
- It can only be 0 or 1
- Leftmost bit is 0, then append 2-bit patterns
- Leftmost bit is 1, then append 2-bit patterns again
- Result ... 3-bits has twice as many patterns as 2-bits
- Every row - double the number of patterns of previous row

Number of bits | Different Patterns |

1 | 0 1 |

2 | 00 01 10 11 |

3 | 000 001 010 011 100 101 110 111 |

- In general: add 1 bit, double the number of patterns
- 1 bit - 2 patterns
- 2 bits - 4
- 3 bits - 8
- 4 bits - 16
- 5 bits - 32
- 6 bits - 64
- 7 bits - 128
- 8 bits - 256 - one byte
- Mathematically: n bits yields 2
^{n} patterns (2 to the nth power)

## One Byte - 256 Patterns

- 1 byte is group of 8 bits
- 8 bits can make 256 different patterns
- Storing an int in a byte is very common
- Start with 0, go up, one pattern per number, until run out of patterns
- 0, 1, 2, 3, 4, 5, ... 254, 255
- One byte can hold a number between 0 and 255
- i.e. with 256 different patterns, we can store a number in the range 0..255
- pixel.red takes in a number 0..255. Why?
- The red/green/blue numbers of a pixel are each stored in
**one byte**

## "HDR" Image

- HDR uses 10 bits per color
- 8 bits = 256 patterns
- 9 bits = 512 patterns
- 10 bits = 1024 patterns - HDR
- Uses a little more space, looks better
- Some software complexity, as 1/byte color is so simple