Skip to content

updating type hint for solutions #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ lecture_slides
pictures
old_lessons
Getting Started.docx

.DS_Store
# settings and other directoreis
__pycache__/
.ipynb_checkpoints/
Expand Down
4 changes: 2 additions & 2 deletions 02-IDE_exercises/solutions/02b-project/temp_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

print("Please enter the temperature in Celsius.")

temp_c = float(input())
temp_f = (9/5) * temp_c + 32
temp_c: float = float(input())
temp_f: float = (9/5) * temp_c + 32

print("The corresponding temperature in Fahrenheit is:")
print(temp_f)
Expand Down
12 changes: 7 additions & 5 deletions 02-IDE_exercises/solutions/02c-project/coin_change.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@
# Date: 09/24/2020
# Description: Asks the user for int of cents less than $1 and returns
# breakout of coin denominations with fewest number of coins
# Hint: the floor division // could be useful where it rounds the result down to the nearest whole number
# or the modulo operator can also be used.

print("Please enter an amount between 0 and 99 cents.")

coins = int(input())
coins: int = int(input())

print("Your change will be:")

quarters = coins // 25
quarters: int = coins // 25
coins -= quarters * 25
dimes = coins // 10
dimes: int = coins // 10
coins -= dimes * 10
nickels = coins // 5
nickels: int = coins // 5
coins -= nickels * 5
pennies = coins
pennies: int = coins

print("Q:", quarters)
print("D:", dimes)
Expand Down
12 changes: 6 additions & 6 deletions 02-IDE_exercises/solutions/03a-project/min_max_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@
# integers from user

print("How many numbers would you like to enter?")
num_int = int(input())
num_int: int = int(input())
# The first while loop ensures that the user enters at least one number
# if, the user enters anything less than 1, it will prompt the user to enter something greater than one

while num_int < 1:
print("Please enter at least one number")
num_int = int(input())
num_int: int = int(input())
else:
print("Please enter", num_int, "numbers.")

first_int = int(input())
first_int: int = int(input())
# the first number is the largest as well as the smallest number
min = first_int
max = first_int
min: int = first_int
max: int = first_int

# if the number of ints requested by user is greater than one, we'll initialize
# a while loop

while num_int > 1:
n = int(input())
n: int = int(input())
if n < min:
min = n
if n > max:
Expand Down
5 changes: 3 additions & 2 deletions 02-IDE_exercises/solutions/03b-project/find_factors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
# Date: 10/05/2020
# Description: Take in positive integer and print list of factors

integer = int(input("Please enter a positive integer:"))
integer: int = int(input("Please enter a positive integer:"))
print("The factors of", integer, "are:")

for i in range(1, integer+1):
i: int
for i in range(1, integer + 1):
if integer % i == 0:
print(i)
12 changes: 5 additions & 7 deletions 02-IDE_exercises/solutions/03c-project/guess_num.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Author: Brandon Hoffman
# Date: 10/05/2020
# Description: Guessing game that takes a number to guess and has user contiously
# Description: Guessing game that takes a number to guess and has user continuously
# make guesses till they get the answer correct

print("Enter the number for the player to guess.")
solution = int(input())
solution: int = int(input())

print("Enter your guess.")
guess = int(input())
guess: int = int(input())

number_of_guesses = 1
number_of_guesses: int = 1

while guess != solution:
if guess > solution:
Expand All @@ -19,8 +19,6 @@
print("Too low - try again:")
guess = int(input())

number_of_guesses += 1

number_of_guesses +=1
number_of_guesses += 1

print("You guessed it in", number_of_guesses, "tries.")
12 changes: 8 additions & 4 deletions 02-IDE_exercises/solutions/04a-project/distance_fallen.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
# Description: created a function called "fall_distance() that takes an input of seconds and returns
# the distance an object has fallen."

time: int = int(input("Please enter an amount of time in second(s): "))

def fall_distance(t):
'''takes input in seconds and returns distance object has fallen'''
g = 9.8 # velocity

return 0.5 * g * t*t
def fall_distance(t:int) -> float:
"""takes input in seconds and returns distance object has fallen"""
g: float = 9.8 # velocity

return 0.5 * g * t * t


print("The distance the object has fallen is : ", fall_distance(time), "meter:")
26 changes: 15 additions & 11 deletions 02-IDE_exercises/solutions/04b-project/fibonacci.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
# Author: Brandon Hoffman
# Date: 10/17/2020
# Description: created a function called fib() that calculate a fibonnaci number for a given 'n' without recursion or the golden ratio.
# Author: Brandon Hoffman Date: 10/17/2020 Description: created a function called fib() that calculate a Fibonacci
# number for a given 'n' without recursion or the golden ratio.

def fib(n):
'''calculate a fibonnaci number given the input 'n' without recursion using the golden ratio'''

prev_num = 0
curr_num = 1
sol = 1
for i in range(n-1):
def fib(n: int) -> int:
"""calculate a fibonnaci number given the input 'n' without recursion using the golden ratio"""

prev_num: int = 0
curr_num: int = 1
sol: int = 1
print("The numbers in fibonacci series are : ")
i: int
for i in range(n - 1):
sol = curr_num + prev_num
prev_num = curr_num
curr_num = sol


print(sol)
return sol


number: int = int(input("Please enter a number to calculate its Fibonacci number: "))
print("The Fibonacci number is: ", fib(number))
16 changes: 10 additions & 6 deletions 02-IDE_exercises/solutions/04c-project/hailstone_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@
# same process until the result terminates at a value of 1.


def hailstone(n):
'''takes an integer, 'n' and if even divides by 2 and if odd multiples by 3 and adds 1
the result continues until the value reaches 1 and the function terminates and returns the count the number of steps'''
count = 0
def hailstone(n: int) -> int:
"""takes an integer, 'n' and if even divides by 2 and if odd multiples by 3 and adds 1 the result continues until
the value reaches 1 and the function terminates and returns the count the number of steps """
count: int = 0
n: int
while n != 1:
print(count, n)
if n % 2 == 0:
n //= 2
else:
n = (n * 3) + 1

count += 1
return count


return count
number: int = int(input("Please enter a number: "))
print("The count is: ", hailstone(number))
9 changes: 7 additions & 2 deletions 02-IDE_exercises/solutions/05a-project/multiply_recur.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
# Description: write a function that uses recursive iteration to multiply two positive integers
# with addition

def multiply(multiplier, multiplicand):
def multiply(multiplier: int, multiplicand: int) -> int:
"""
Takes in two positive integers and multiplies them by recursively adding the int multiplicand int multiplier times.
"""
if multiplier == 1:
return multiplicand
else:
return multiply(multiplier-1, multiplicand) + multiplicand
return multiply(multiplier - 1, multiplicand) + multiplicand


num1: int = int(input("Please enter a multiplier: "))
num2: int = int(input("Please enter a multiplicand: "))
print("The result is: ", multiply(num1, num2))
12 changes: 6 additions & 6 deletions 02-IDE_exercises/solutions/05b-project/Taxicab.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Taxicab:
"""
Represents a taxicab that tracks the distance traveled when given x and y coordinates
"""
def __init__(self, x, y):
def __init__(self, x: float, y: float) -> None:
"""
Creates a taxicab object with x and y coordiantes and intializes an odometer to 0
"""
Expand All @@ -16,32 +16,32 @@ def __init__(self, x, y):

self._odometer = 0

def get_x_coord(self):
def get_x_coord(self) -> float:
"""
returns the current x coordinate
"""
return self._x

def get_y_coord(self):
def get_y_coord(self) -> float:
"""
returns the current y coordinate
"""
return self._y

def get_odometer(self):
def get_odometer(self) -> float:
"""
sums the abs value of the change in x and y coordinates
"""
return self._odometer

def move_x(self, x_vector):
def move_x(self, x_vector: float) -> None:
"""
takes in a one-dimensional vector and adds it to the x coordiante as its original value and the odometer as an absolute value
"""
self._x += x_vector
self._odometer += abs(x_vector)

def move_y(self, y_vector):
def move_y(self, y_vector: float) -> None:
"""
takes in a one-dimensional vector and adds it to the x coordiante as its original value and the odometer as an absolute value
"""
Expand Down
7 changes: 7 additions & 0 deletions 02-IDE_exercises/solutions/05b-project/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from Taxicab import Taxicab

cab = Taxicab(3, -8) # creates a Taxicab object at coordinates (3, -8)
cab.move_x(4) # moves cab 4 units "right"
cab.move_y(-5) # moves cab 5 units "down"
cab.move_x(-2) # moves cab 2 unit "left"
print(cab.get_odometer()) # prints the current odometer reading
13 changes: 8 additions & 5 deletions 02-IDE_exercises/solutions/06a-project/find_median.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@
# Date: 10/25/2020
# Description: Sorts a list of numbers and returns the median number in the list

def find_median(some_nums):
def find_median(some_nums: list[int]) -> float:
"""
Sorts a list of numbers and returns the median value
"""
some_nums.sort()
array_length = len(some_nums)
array_length: int = len(some_nums)

mid_index = int(array_length/2)
mid_index: int = int(array_length / 2)

if array_length % 2 == 0:
mid_index_alt = int((array_length/2)-1)
mid_index_alt = int((array_length / 2) - 1)
return (some_nums[mid_index] + some_nums[mid_index_alt]) / 2
else:
return some_nums[mid_index]



list_of_nums: list[int] = [13, 7, -3, 82, 4]
result: float = find_median(list_of_nums)
25 changes: 19 additions & 6 deletions 02-IDE_exercises/solutions/06b-project/add_surname.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
# Author: Brandon Hoffman
# Date: 10/25/2020
# Description: create a function that takes a list of names and stores any value that starts with the letter 'K' in a new list concatenated also with the string ' Hoffman'
# Author: Brandon Hoffman Date: 10/25/2020 Description: create a function that takes a list of names and stores any
# value that starts with the letter 'K' in a new list concatenated also with the string ' Hoffman'

def add_surname(name_list):
def add_surname(name_list: list[str]) -> list[str]:
"""
takes a list of names and stores any value that starts with the letter 'K' in a new list concatenated also with the string ' Hoffman'
takes a list of names and stores any value that starts with the letter 'K' in a new list concatenated also with
the string ' Hoffman'
"""
return [name + " Hoffman" for name in name_list if name[0] == "K"]
new_name_list: list[str] = []
for name_in_list in name_list:
if name_in_list[0] == 'K':
new_name_list += [name_in_list + ' Hoffman']

return new_name_list


# Testing the function
names: list[str] = ["Kiki", "Krystal", "Pavel", "MaryKay", "Annie", "Koala", "Killer"]
changed_name_lists: list[str] = add_surname(names)
name: str
for name in changed_name_lists:
print(name)
Loading