Skip to content

Instantly share code, notes, and snippets.

View pamelafox's full-sized avatar

Pamela Fox pamelafox

View GitHub Profile
@pamelafox
pamelafox / can_be_president.py
Created August 30, 2021 05:46
Can Be President
""""
Implement a function that determines if a candidate can be president.
According to the US constitution, a presidential candidate must be at least
35 years old and have been a US resident for at least 14 years.
The age parameter represents a candidate's age and
the residency parameter represents their years of residency.
"""
def can_be_president(age, residency):
"""
@pamelafox
pamelafox / has_free_lobe.py
Created August 30, 2021 05:43
Has Free Lobe
"""
This function should only return True
if both the father allele and the mother allele are "G".
"""
def has_free_lobe(father_allele, mother_allele):
"""
>>> has_free_lobe("G", "g")
False
>>> has_free_lobe("g", "G")
False
@pamelafox
pamelafox / has_curly_hair.py
Created August 30, 2021 05:38
Has Curly Hair
"""
This function should only return True
if both the father allele and the mother allele are "C".
"""
def has_curly_hair(father_allele, mother_allele):
"""
>>> has_curly_hair("C", "c")
False
>>> has_curly_hair("c", "C")
False
@pamelafox
pamelafox / temperature_converter.py
Created August 26, 2021 17:17
Temperature converter
"""
How can we communicate the highs and lows of climate change with people who use a different temperature unit?
Let's make a temperature converter based on the steps here:
https://www.mathsisfun.com/temperature-conversion.html
Create a function called celsius_to_fahrenheit that:
* takes a single argument, the temperature in celsius
* calculates and returns the fahrenheit equivalent
Similarly, create another function called fahrenheiht_to_celsius that:
@pamelafox
pamelafox / lifetime_supply.py
Last active August 26, 2021 17:10
Lifetime supply
"""
Ever wonder how much a "lifetime supply" of your favorite snack is? Wonder no more!
Write a function named calculate_supply that:
* takes 2 arguments: age, amount per day.
* calculates the amount consumed for rest of the life (based on a constant max age of 81).
* returns the result
"""
# This def statement may be incomplete...
@pamelafox
pamelafox / dog_age.py
Created August 26, 2021 17:05
Dog Age Calculator
"""
You know how old a dog is in human years, but what about dog years? Calculate it!
Write a function named calculate_dog_age that:
* takes 1 argument: a dog's age in human years.
* calculates the dog's age based on the conversion rate of 1 human year to 7 dog years.
* returns the age in dog years.
"""
def calculate_dog_age(years):
"""
@pamelafox
pamelafox / fortune_teller.py
Created August 26, 2021 17:00
Fortune Teller
"""
Why pay a fortune teller when you can just program your fortune yourself?
Write a function named tell_fortune that:
* takes 3 arguments: job title, partner's name, geographic location.
* returns a fortune of the form: 'You will be a {job_title} in {location} living with {partner}.
"""
def tell_fortune(job_title, location, partner):
"""
@pamelafox
pamelafox / operator_expressions.py
Created August 26, 2021 16:30
Operator Expressions Exercise
from operator import add, sub, mul, truediv, floordiv, mod
"""
In the console, type expressions that use function calls
and correspond to each arithmetic expression.
Use the console to check your work.
Useful documentation:
https://docs.python.org/3/library/operator.html#mapping-operators-to-functions
"""
@pamelafox
pamelafox / advice_helpers.py
Created August 25, 2021 17:10
Advice helpers for Lecture 1 Demo
# There are helper functions,
# just so that we can avoid calling object methods for demo 1
def read_file(file):
return file.read()
def count(str, text):
return str.count(text)
def lower(str):
return str.lower()
@pamelafox
pamelafox / larkoutput
Created April 15, 2021 17:21
Lark output
>>> grammar = """
?start: calc_op
?calc_op: OPEN OP operand* CLOSE
operand: calc_op | NUMBER
OPEN: "("
CLOSE: ")"