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

Learning on the Homeworks - Delete Key Test

Shorter Code - Paths

> Shorter problems

Shorter 1. shorter()

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

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

Byte

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
Number of bits Different Patterns
1 0 1
2 00 01 10 11
3 000 001 010 011
100 101 110 111

One Byte - 256 Patterns

"HDR" Image