berkeley_temp = 18
miami_temp = 32
def c_to_f(temp):
return (9 / 5) * temp + 32
c_to_f(0)
32.0
c_to_f(berkeley_temp)
64.4
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)
seconds_converter(5400)
Hours: 1 Minutes: 30 Seconds: 0
seconds_converter(8182)
Hours: 2 Minutes: 16 Seconds: 22
Suppose we have a list of temperatures of cities around the world.
# 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'))
c_temps
[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?
def c_to_f(temp):
return (9 / 5) * temp + 32
c_temps[3]
25.8
c_to_f(c_temps[3])
78.44
c_to_f(c_temps[9])
70.52
last_in_f = c_to_f(c_temps[-1])
last_in_f
66.02
Quick recap: while loops allow us to repeat a code block as long as some boolean expression is true.
# 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
# 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?
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
convert_list([32, 19, 23])
[89.6, 66.2, 73.4]
convert_list(c_temps)
[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]
def convert_list_for(c_temps): # c_temps is a list
f_temps = []
for c_temp in c_temps:
f_temp = c_to_f(c_temp)
f_temps.append(f_temp)
return f_temps
convert_list_for([32, 19, 23])
[89.6, 66.2, 73.4]
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.
def daily_change(prices):
first = prices[0]
last = prices[-1]
if first > last:
return 'decrease'
elif first == last:
return 'none'
else:
return 'increase'
# Returns 'increase', since 1 < 4
daily_change([1, 2, 2.5, 3, 3.5, 4])
'increase'
# Returns 'none', since 3 = 3
daily_change([3, 9, 3])
'none'
# Return 'decrease', since 5 > 2
daily_change([5, 4, 3, 4, 5, 4, 3, 2, 2])
'decrease'
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.
def strictly_increasing(prices):
i = 0
while i < len(prices) - 1:
if prices[i] >= prices[i + 1]:
return False
i += 1
return True
lst = [10, 20, 30, 40, 50]
lst[len(lst) - 1 + 1]
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) /tmp/ipykernel_90/3731977247.py in <module> 1 lst = [10, 20, 30, 40, 50] ----> 2 lst[len(lst) - 1 + 1] IndexError: list index out of range
def next_day(day):
week = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
curr = week.index(day)
return week[(curr + 1) % 7]
next_day('Wednesday')
'Thursday'
next_day('Saturday')
'Sunday'
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 differentIf any of the side lengths are less than or equal to 0, return 'invalid'
.
def triangle_type(sides):
a = sides[0]
b = sides[1]
c = sides[2]
if a <= 0 or b <= 0 or c <= 0:
return 'invalid'
if a == b == c:
return 'equilateral'
elif a == b or b == c or a == c:
return 'isoceles'
else:
return 'scalene'
# Uncomment this to check your implementation – the result should be 'equilateral'
triangle_type([6, 6, 6])
'equilateral'
# Uncomment this to check your implementation – the result should be 'isoceles'
triangle_type([5, 6, 5])
'isoceles'
# Uncomment this to check your implementation – the result should be 'scalene'
triangle_type([5, 6, 7])
'scalene'
# Uncomment this to check your implementation – the result should be 'invalid'
triangle_type([5, 6, 0])
'invalid'
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'
def full_prefix(name):
# i and idx are short for "index"
idx = name.index('.')
prefix = name[:idx]
rest = name[idx + 2:]
if prefix == 'Dr':
return 'Doctor ' + rest
elif prefix == 'Prof':
return 'Professor ' + rest
elif prefix == 'Gov':
return 'Governor ' + rest
else:
return name
# 'Governor Newsom'
full_prefix('Gov. Newsom')
'Governor Newsom'
# 'Professor Christ'
full_prefix('Prof. Christ')
'Professor Christ'
# 'Doctor Biden'
full_prefix('Dr. Biden')
'Doctor Biden'
# 'Hon. Caboto'
full_prefix('Hon. Caboto')
'Hon. Caboto'
# 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)
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
find_sequence(813)
[813, 693, 594, 495]
find_sequence(215)
[215, 396, 594, 495]