Lecture 10 – More Review

Data 94, Spring 2021

Stock prices

Part 1

Task: Given a list of prices of a stock on a single day, return a string describing whether the stock increased, stayed the same, or decreased from its starting price.

In [1]:
def daily_change(prices):
    first = prices[0]
    last = prices[-1]
    if first > last:
        return 'decrease'
    elif first == last:
        return 'none'
    else:
        return 'increase'
In [2]:
# Returns 'increase', since 1 < 4
daily_change([1, 2, 2.5, 3, 3.5, 4])
Out[2]:
'increase'
In [3]:
# Returns 'none', since 3 = 3
daily_change([3, 9, 3])
Out[3]:
'none'
In [4]:
# Return 'decrease', since 5 > 2
daily_change([5, 4, 3, 4, 5, 4, 3, 2, 2])
Out[4]:
'decrease'

Part 2, Quick Check 1

Task: Given a list of prices of a stock on a single day, return True if the stock was strictly increasing, and False otherwise. A list of numbers is strictly increasing if each one is larger than the previous.

In [5]:
def strictly_increasing(prices):
    i = 0
    while i < len(prices) - 1:
        if ...:
            return False
        i += 1
    return True
In [6]:
# True
# strictly_increasing([1, 2, 5, 8, 10])
In [7]:
# False
# strictly_increasing([2, 3, 9, 7, 11])
In [8]:
# False
# strictly_increasing([2, 3, 4, 4, 5])

Next day of the week

In [9]:
def next_day(day):
    week = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
    curr = week.index(day)
    return week[(curr + 1) % 7]
In [10]:
next_day('Wednesday')
Out[10]:
'Thursday'
In [11]:
next_day('Saturday')
Out[11]:
'Sunday'

Prefixes, Quick Check 2

In [12]:
def full_prefix(name):
    # i and idx are short for "index"
    idx = name.index('.')
    prefix = name[:idx]
    rest = ...
    
    if prefix == 'Dr':
        return 'Doctor ' + rest
    elif prefix == 'Prof':
        return 'Professor ' + rest
    elif prefix == 'Gov':
        return 'Governor ' + rest
    else:
        return name
In [13]:
# 'Governor Newsom'
# full_prefix('Gov. Newsom')
In [14]:
# 'Professor Christ'
# full_prefix('Prof. Christ')
In [15]:
# 'Doctor Biden'
# full_prefix('Dr. Biden')
In [16]:
# 'Hon. Caboto'
# full_prefix('Hon. Caboto')

Kaprekar's constant

In [17]:
# For now, ignore the code in this cell.

def increase_sort(n):
    n_list = list(str(n))
    n_list_sorted = sorted(n_list)
    n_str_sorted = ''.join(n_list_sorted)
    return int(n_str_sorted)

def decrease_sort(n):
    n_list = list(str(n))
    n_list_sorted = sorted(n_list)[::-1]
    n_str_sorted = ''.join(n_list_sorted)
    return int(n_str_sorted)
In [18]:
def find_sequence(n):
    # Need to keep track of the steps, and the "current" number in our sequence
    steps = [n]
    curr = n
    
    # As long as our current number isn't 495
    # make one more step, and store the current step
    while curr != 495:
        curr = decrease_sort(curr) - increase_sort(curr)
        steps.append(curr)
    return steps
In [19]:
find_sequence(813)
Out[19]:
[813, 693, 594, 495]
In [20]:
find_sequence(215)
Out[20]:
[215, 396, 594, 495]