Lecture 9 – Review

Data 94, Spring 2021

In [1]:
berkeley_temp = 18
miami_temp = 32
In [2]:
def c_to_f(temp):
    return (9 / 5) * temp + 32
In [3]:
c_to_f(0)
Out[3]:
32.0
In [4]:
c_to_f(berkeley_temp)
Out[4]:
64.4

Hours, minutes, and seconds

In [5]:
def seconds_converter(seconds_in):
    seconds_in_minute = 60
    seconds_in_hour = 60 * 60
    
    # First, determine the number of whole hours
    # e.g. if s = 5152, this is 1
    hours = seconds_in // seconds_in_hour
    
    # Compute the number of "seconds left over"
    seconds_left = seconds_in % seconds_in_hour
    
    # Now, determine the number of whole minutes
    # e.g. if seconds_left = 1850, this is 30
    minutes = seconds_left // seconds_in_minute
    
    # The number of seconds left over now is the number of seconds we should print
    seconds = seconds_left % seconds_in_minute
    
    print('Hours:   ', hours)
    print('Minutes: ', minutes)
    print('Seconds: ', seconds)
In [6]:
seconds_converter(5400)
Hours:    1
Minutes:  30
Seconds:  0
In [7]:
seconds_converter(8182)
Hours:    2
Minutes:  16
Seconds:  22

Temperature conversion

Suppose we have a list of temperatures of cities around the world.

In [8]:
# Run this cell, don't worry about the code.
from datascience import *
table = Table.read_table('data/africa-temperatures.csv')
c_temps = list(table.apply(lambda s: float(s[:s.index('(')]), 'Year'))
In [9]:
c_temps
Out[9]:
[17.4,
 21.7,
 28.3,
 25.8,
 27.2,
 26.8,
 27.7,
 22.4,
 21.0,
 21.4,
 28.3,
 28.6,
 23.8,
 28.3,
 22.0,
 26.7,
 23.8,
 24.4,
 26.0,
 26.5,
 28.3,
 29.4,
 25.3,
 20.8,
 29.9,
 21.4,
 20.0,
 25.1,
 26.3,
 15.6,
 30.5,
 22.7,
 16.0,
 25.9,
 26.0,
 26.0,
 26.4,
 27.9,
 26.0,
 26.4,
 26.5,
 27.0,
 26.0,
 26.0,
 26.0,
 26.0,
 26.3,
 17.8,
 29.3,
 29.3,
 20.0,
 19.9,
 17.9,
 23.4,
 17.9,
 25.2,
 26.3,
 24.1,
 24.4,
 22.2,
 17.7,
 28.0,
 28.0,
 27.8,
 21.3,
 25.7,
 17.2,
 19.6,
 18.9,
 22.8,
 29.3,
 26.8,
 26.0,
 27.4,
 26.4,
 28.0,
 25.0,
 26.1,
 24.0,
 24.0,
 24.0,
 30.0,
 21.7,
 27.1,
 18.2,
 16.2,
 15.5,
 15.6,
 20.4,
 20.6,
 27.8,
 27.8,
 28.4,
 29.9,
 25.8,
 26.0,
 23.0,
 22.7,
 26.9,
 28.1,
 18.4,
 19.5,
 20.0,
 21.0,
 20.3,
 19.9,
 21.8,
 18.4,
 18.9]

Remember, we already defined c_to_f, which takes a temperature in Celsius to a temperature in Fahrenheit.

How do we convert the 3rd temperature to Fahrenheit? The 9th temperature? The very last temperature in the list?

In [10]:
def c_to_f(temp):
    return (9 / 5) * temp + 32
In [11]:
c_temps[3]
Out[11]:
25.8
In [12]:
c_to_f(c_temps[3])
Out[12]:
78.44
In [13]:
c_to_f(c_temps[9])
Out[13]:
70.52

Quick Check 1

In [ ]:
last_in_f = ...
In [ ]:
 

Quick recap: while loops allow us to repeat a code block as long as some boolean expression is true.

In [14]:
# Don't do this
print('1 is not even')
print('2 is even')
print('3 is not even')
print('4 is even')
print('5 is not even')
print('6 is even')
print('7 is not even')
print('8 is even')
print('9 is not even')
print('10 is even')
1 is not even
2 is even
3 is not even
4 is even
5 is not even
6 is even
7 is not even
8 is even
9 is not even
10 is even
In [15]:
# Instead, do this
n = 1
while n <= 10:
    if n % 2 == 0:
        print(str(n) + ' is even')
    else:
        print(str(n) + ' is not even')
    n = n + 1
1 is not even
2 is even
3 is not even
4 is even
5 is not even
6 is even
7 is not even
8 is even
9 is not even
10 is even

Back to the question at hand: how do we make a new list with all temperatures converted to Fahrenheit?

In [16]:
def convert_list(c_temps):
    f_temps = []
    i = 0
    while i < len(c_temps):
        # Get the ith C temp
        c_temp = c_temps[i]
        
        # Convert it to F
        f_temp = c_to_f(c_temp)
        
        # Add it to the output list
        f_temps.append(f_temp)
        
        # Increment i
        i += 1
    return f_temps
In [17]:
convert_list([32, 19, 23])
Out[17]:
[89.6, 66.2, 73.4]
In [18]:
convert_list(c_temps)
Out[18]:
[63.31999999999999,
 71.06,
 82.94,
 78.44,
 80.96000000000001,
 80.24000000000001,
 81.86,
 72.32,
 69.80000000000001,
 70.52,
 82.94,
 83.48,
 74.84,
 82.94,
 71.6,
 80.06,
 74.84,
 75.92,
 78.80000000000001,
 79.7,
 82.94,
 84.92,
 77.53999999999999,
 69.44,
 85.82,
 70.52,
 68.0,
 77.18,
 79.34,
 60.08,
 86.9,
 72.86,
 60.8,
 78.62,
 78.80000000000001,
 78.80000000000001,
 79.52,
 82.22,
 78.80000000000001,
 79.52,
 79.7,
 80.6,
 78.80000000000001,
 78.80000000000001,
 78.80000000000001,
 78.80000000000001,
 79.34,
 64.03999999999999,
 84.74000000000001,
 84.74000000000001,
 68.0,
 67.82,
 64.22,
 74.12,
 64.22,
 77.36,
 79.34,
 75.38,
 75.92,
 71.96000000000001,
 63.86,
 82.4,
 82.4,
 82.03999999999999,
 70.34,
 78.25999999999999,
 62.96,
 67.28,
 66.02,
 73.03999999999999,
 84.74000000000001,
 80.24000000000001,
 78.80000000000001,
 81.32,
 79.52,
 82.4,
 77.0,
 78.98,
 75.2,
 75.2,
 75.2,
 86.0,
 71.06,
 80.78,
 64.75999999999999,
 61.16,
 59.900000000000006,
 60.08,
 68.72,
 69.08000000000001,
 82.03999999999999,
 82.03999999999999,
 83.12,
 85.82,
 78.44,
 78.80000000000001,
 73.4,
 72.86,
 80.42,
 82.58000000000001,
 65.12,
 67.1,
 68.0,
 69.80000000000001,
 68.53999999999999,
 67.82,
 71.24000000000001,
 65.12,
 66.02]

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

Part 2, Quick Check 2

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 [ ]:
def strictly_increasing(prices):
    i = 0
    while i < len(prices) - 1:
        if ...:
            return False
        i += 1
    return True
In [ ]:
 
In [ ]:
 
In [ ]:
 

Next day of the week

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

More Practice

Given the side lengths of a triangle as a list (sides), return:

  • 'equilateral' if all three side lengths are equal
  • 'isoceles' if exactly two of the three side lengths are equal
  • 'scalene' if all three side lengths are different

If any of the side lengths are less than or equal to 0, return 'invalid'.

In [ ]:
def triangle_type(sides):
    ...
In [ ]:
# Uncomment this to check your implementation – the result should be 'equilateral'
# triangle_type([6, 6, 6])
In [ ]:
# Uncomment this to check your implementation – the result should be 'isoceles'
# triangle_type([5, 6, 5])
In [ ]:
# Uncomment this to check your implementation – the result should be 'scalene'
# triangle_type([5, 6, 7])
In [ ]:
# Uncomment this to check your implementation – the result should be 'invalid'
# triangle_type([5, 6, 0])
Answer def triangle_type(sides): if sides[0] <= 0 or sides[1] <= 0 or sides[2] <= 0: return 'invalid' if sides[0] == sides[1] == sides[2]: return 'equilateral' elif (sides[0] == sides[1]) or (sides[1] == sides[2]) or (sides[0] == sides[2]): return 'isoceles' else: return 'scalene'