#LAB2
py
keyboard_arrow_up
School
Arizona State University *
*We aren’t endorsed by this school
Course
180
Subject
Mathematics
Date
Feb 20, 2024
Type
py
Pages
8
Uploaded by ConstableSardine4073
#LAB2
import math
# -------- SECTION 1
class Instructor:
'''
>>> t1= Instructor('John Doe')
>>> t1.get_name()
'John Doe'
>>> t1.get_courses()
[]
>>> t1.add_course('MATH140')
>>> t1.get_courses()
['MATH140']
>>> t1.add_course('STAT100')
>>> t1.get_courses()
['MATH140', 'STAT100']
>>> t1.add_course('STAT100')
>>> t1.get_courses()
['MATH140', 'STAT100']
>>> t1.remove_course('MATH141')
>>> t1.get_courses()
['MATH140', 'STAT100']
>>> t1.remove_course('MATH140')
>>> t1.get_courses()
['STAT100']
'''
def __init__(self, name):
self.name = name
self.courses = []
def get_name(self):
return self.name def set_name(self, new_name):
if new_name and isinstance(new_name, str):
self.name = new_name
def get_courses(self):
return self.courses
def remove_course(self, course):
if course in self.courses:
self.courses.remove(course) def add_course(self,course):
if course not in self.courses:
self.courses.append(course)
#Tests to try:
"""
t1 = Instructor('John Doe')
print(t1.get_name()) # Output: 'John Doe'
print(t1.get_courses()) # Output: []
t1.add_course('MATH140')
print(t1.get_courses()) # Output: ['MATH140']
t1.add_course('STAT100')
print(t1.get_courses()) # Output: ['MATH140', 'STAT100']
t1.add_course('STAT100')
print(t1.get_courses()) # Output: ['MATH140', 'STAT100']
t1.remove_course('MATH141')
print(t1.get_courses()) # Output: ['MATH140', 'STAT100']
t1.remove_course('MATH140')
print(t1.get_courses()) # Output: ['STAT100']
"""
# -------- SECTION 2
class Pantry:
""""
>>> sara_pantry = Pantry()
>>> sara_pantry.stock_pantry('Bread', 2)
'Pantry Stock for Bread: 2.0'
>>> sara_pantry.stock_pantry('Cookies', 6)
'Pantry Stock for Cookies: 6.0'
>>> sara_pantry.stock_pantry('Chocolate', 4)
'Pantry Stock for Chocolate: 4.0'
>>> sara_pantry.stock_pantry('Pasta', 3)
'Pantry Stock for Pasta: 3.0'
>>> sara_pantry
I am a Pantry object, my current stock is {'Bread': 2.0, 'Cookies': 6.0,
'Chocolate': 4.0, 'Pasta': 3.0}
>>> sara_pantry.get_item('Pasta', 2)
'You have 1.0 of Pasta left'
>>> sara_pantry.get_item('Pasta', 6)
'Add Pasta to your shopping list!'
>>> sara_pantry
I am a Pantry object, my current stock is {'Bread': 2.0, 'Cookies': 6.0,
'Chocolate': 4.0, 'Pasta': 0.0}
>>> ben_pantry = Pantry()
>>> ben_pantry.stock_pantry('Cereal', 2)
'Pantry Stock for Cereal: 2.0'
>>> ben_pantry.stock_pantry('Noodles', 5)
'Pantry Stock for Noodles: 5.0'
>>> ben_pantry.stock_pantry('Cookies', 9)
'Pantry Stock for Cookies: 9.0'
>>> ben_pantry.stock_pantry('Cookies', 8)
'Pantry Stock for Cookies: 17.0'
>>> ben_pantry.get_item('Pasta', 2)
"You don't have Pasta"
>>> ben_pantry.get_item('Cookies', 2.5)
'You have 14.5 of Cookies left'
>>> sara_pantry.transfer(ben_pantry, 'Cookies')
>>> sara_pantry
I am a Pantry object, my current stock is {'Bread': 2.0, 'Cookies': 20.5,
'Chocolate': 4.0, 'Pasta': 0.0}
>>> ben_pantry.transfer(sara_pantry, 'Rice')
>>> ben_pantry.transfer(sara_pantry, 'Pasta')
>>> ben_pantry
I am a Pantry object, my current stock is {'Cereal': 2.0, 'Noodles': 5.0,
'Cookies': 0.0}
>>> ben_pantry.transfer(sara_pantry, 'Pasta')
>>> ben_pantry
I am a Pantry object, my current stock is {'Cereal': 2.0, 'Noodles': 5.0,
'Cookies': 0.0}
>>> sara_pantry
I am a Pantry object, my current stock is {'Bread': 2.0, 'Cookies': 20.5,
'Chocolate': 4.0, 'Pasta': 0.0}
"""
def __init__(self):
self.items = {}
def __repr__(self):
return f"I am a Pantry object, my current stock is {self.items}"
def stock_pantry(self, item, qty):
if item in self.items:
self.items[item] += qty
else:
self.items[item] = qty
return f"Pantry Stock for {item}: {self.items[item]}"
def get_item(self, item, qty): #check this, what about only use remaining quanitity, check all examples
if item in self.items:
if self.items[item] >= qty:
self.items[item] -= qty
return f'You have {self.items[item]} of {item} left'
else:
return f'Add {item} to your shopping list!'
else:
return f"You don't have {item}" def transfer(self, other_pantry, item): #check
if item in other_pantry.items:
qty = other_pantry.items[item]
if qty > 0:
if item in self.items:
self.items[item] += qty
else:
self.items[item] = qty
other_pantry.items[item] = 0
# -------- SECTION 3
class Player:
"""
>>> p1 = Player('Susy')
>>> print(p1)
No game records for Susy
>>> p1.update_loss()
>>> p1
*Game records for Susy*
Total games: 1
Games won: 0
Games lost: 1
Best game: None
>>> p1.update_win(5)
>>> p1.update_win(2)
>>> p1
*Game records for Susy*
Total games: 3
Games won: 2
Games lost: 1
Best game: 2 attempts
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
"""
def __init__(self, name):
self.name = name
self.games_played = 0
self.games_won = 0
self.games_lost = 0
self.best_game = None
def update_win(self, att):
self.games_played += 1
self.games_won += 1
if self.best_game is None or att < self.best_game: #check this self.best_game = att
def update_loss(self):
self.games_played += 1
self.games_lost += 1
def __str__(self):
if self.games_played == 0:
return f'No game records for {self.name}'
else:
return f'*Game records for {self.name}*\nTotal games: {self.games_played}\nGames won: {self.games_won}\nGames lost: {self.games_lost}\
nBest game: {self.best_game} attempts'
__repr__=__str__
class Wordle:
"""
>>> p1 = Player('Susy')
>>> p2 = Player('Taylor')
>>> w1 = Wordle(p1, 'water')
>>> w2 = Wordle(p2, 'cloud')
>>> w3 = Wordle(p1, 'jewel')
>>> w1.play('camel')
'_A_E_'
>>> w1.play('ranes')
'rA_E_'
>>> w1.play('baner')
'_A_ER'
>>> w1.play('pacer')
'_A_ER'
>>> w1.play('water')
'You won the game'
>>> w1.play('rocks')
'Game over'
>>> w1.play('other')
'Game over'
>>> w3.play('beast')
'_E___'
>>> w3.play('peace')
'_E__e'
>>> w3.play('keeks')
'_Ee__'
>>> w3.play('jewel')
'You won the game'
>>> w2.play('classes')
'Guess must be 5 letters long'
>>> w2.play('cs132')
'Guess must be all letters'
>>> w2.play('audio')
'_ud_o'
>>> w2.play('kudos')
'_udo_'
>>> w2.play('would')
'_oulD'
>>> w2.play('bound')
'The word was cloud'
>>> w2.play('cloud')
'Game over'
>>> p1
*Game records for Susy*
Total games: 2
Games won: 2
Games lost: 0
Best game: 4 attempts
>>> p2
*Game records for Taylor*
Total games: 1
Games won: 0
Games lost: 1
Best game: None
"""
def __init__(self, player, word):
self.player = player
self.word = word
self.attempts_left = 6
self.guesses = []
self.game_over = False
def process_guess(self, guess):
if len(guess) != 5:
return "Guess must be 5 letters long"
if not guess.isalpha():
return "Guess must be all letters"
feedback = ""
for i in range(5):
if guess[i] == self.word[i]:
feedback += guess[i].upper() # Correct letter in the right place
elif guess[i] in self.word:
feedback += guess[i].lower() # Correct letter in the wrong place
else:
feedback += "_" # Letter not in the word
return feedback
def play(self, guess): #switch names and check all examples on terminal
if self.game_over:
return "Game over"
if len(guess) != 5:
return "Guess must be 5 letters long"
if not guess.isalpha():
return "Guess must be all letters"
feedback = self.process_guess(guess)
self.guesses.append(guess)
self.attempts_left -= 1
if feedback == self.word.upper():
self.player.update_win(6 - self.attempts_left)
self.game_over = True
return "You won the game"
elif self.attempts_left == 0:
self.player.update_loss()
self.game_over = True
return f"The word was {self.word}"
return feedback
# -------- SECTION 4
class Point2D:
def __init__(self, x, y):
self.x = x
self.y = y
class Line:
'''
>>> p1 = Point2D(-7, -9)
>>> p2 = Point2D(1, 5.6)
>>> line1 = Line(p1, p2)
>>> line1.getDistance
16.648
>>> line1.getSlope
1.825
>>> line1
y = 1.825x + 3.775
>>> line2 = line1*4
>>> line2.getDistance
66.592
>>> line2.getSlope
1.825
>>> line2
y = 1.825x + 15.1
>>> line1
y = 1.825x + 3.775
>>> line3 = line1*4
>>> line3
y = 1.825x + 15.1
>>> line5=Line(Point2D(6,48),Point2D(9,21))
>>> line5
y = -9.0x + 102.0
>>> Point2D(45,3) in line5
False
>>> Point2D(34,-204) in line5
True
>>> line6=Line(Point2D(2,6), Point2D(2,3))
>>> line6.getDistance
3.0
>>> line6.getSlope
inf
>>> isinstance(line6.getSlope, float)
True
>>> line6
Undefined
>>> line7=Line(Point2D(6,5), Point2D(9,5))
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
>>> line7.getSlope
0.0
>>> line7
y = 5.0
>>> Point2D(9,5) in line7
True
>>> Point2D(89,5) in line7
True
>>> Point2D(12,8) in line7
False
>>> (9,5) in line7
False
'''
def __init__(self, point1, point2):
self.point1 = point1
self.point2 = point2
self.distance = None #check this part
self.slope = None
@property #check this
def getDistance(self):
if self.distance is None: # Calculate the distance using the formula
x1, y1 = self.point1.x, self.point1.y
x2, y2 = self.point2.x, self.point2.y
self.distance = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
# Return the distance rounded to 3 decimals
return round(self.distance, 3)
def getSlope(self):
if self.slope is None:
x1, y1 = self.point1.x, self.point1.y
x2, y2 = self.point2.x, self.point2.y
if x1 == x2:
self.slope = float('inf') # Vertical line
else:
self.slope = (y2 - y1) / (x2 - x1)
return round(self.slope, 3)
def __str__(self):
slope = self.getSlope()
if slope == float('inf'):
return 'Undefined'
else:
intercept = round(self.point1.y - slope * self.point1.x, 3)
return f'y = {slope}x + {intercept}'
def __repr__(self):
return str(self)
def __mul__(self, n):
if not isinstance(n, int):
return None
else:
new_point1 = Point2D(self.point1.x * n, self.point1.y * n)
new_point2 = Point2D(self.point2.x * n, self.point2.y * n)
return Line(new_point1, new_point2)
def __contains__(self, point):
if self.getSlope() == float('inf'):
return False
x, y = point.x, point.y
expected_y = self.getSlope() * x + self.point1.y - self.getSlope() * self.point1.x
return math.isclose(y, expected_y)
#def run_tests():
# import doctest
# Run tests in all docstrings
# doctest.testmod(verbose=True)
# Run tests per function - Uncomment the next line to run doctest by function. Replace Pantry with the name of the class you want to test
#doctest.run_docstring_examples(Pantry, globals(), name='LAB2',verbose=True)
#if __name__ == "__main__":
# run_tests()