Apply today's Project 1 changes to Project 2
This commit is contained in:
@@ -19,4 +19,13 @@ class Card:
|
|||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return f"{self._value} of {self._suit}"
|
return f"{self._value} of {self._suit}"
|
||||||
|
|
||||||
#Returns a string representation of the card in the format "Value of Suit".
|
#Returns a string representation of the card in the format "Value of Suit".
|
||||||
|
|
||||||
|
def __add__(self, other) -> int:
|
||||||
|
#Adding 2 cards together
|
||||||
|
if isinstance(other, Card):
|
||||||
|
return self.value + other.value
|
||||||
|
elif isinstance(other, int):
|
||||||
|
#allows adding a card to an int
|
||||||
|
return self.value + other
|
||||||
|
#Added for project 2: Created an "__add__" meathod to allow adding Cards together.
|
||||||
|
|||||||
31
Project 2/utilities.py
Normal file
31
Project 2/utilities.py
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
"""
|
||||||
|
Reo
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
from Card import Card
|
||||||
|
|
||||||
|
def sum_cards_iter(cards: list[Card]) -> int:
|
||||||
|
total = 0 #start total at 0
|
||||||
|
for card in cards:
|
||||||
|
total = total + card
|
||||||
|
return total
|
||||||
|
#Added for project 2: Created a function to sum a list of Card objects using iteration.
|
||||||
|
|
||||||
|
def sum_cards_recursive(cards: list[Card]) -> int:
|
||||||
|
if cards == []:
|
||||||
|
#Base case
|
||||||
|
return 0
|
||||||
|
return cards[0] + sum_cards_recursive(cards[1:])
|
||||||
|
#Recursive case
|
||||||
|
#Added for project 2: Created a function to sum a list of Card objects using recursion.
|
||||||
|
|
||||||
|
def test_sum_cards():
|
||||||
|
cards = [
|
||||||
|
Card("diamond", 5),
|
||||||
|
Card("clubs", 3),
|
||||||
|
Card("spade", 12),
|
||||||
|
Card("clubs", 1)
|
||||||
|
]
|
||||||
|
assert sum_cards_iter(cards) == 21
|
||||||
|
assert sum_cards_recursive(cards) == 21
|
||||||
|
#Added for project 2: Example tests given
|
||||||
Reference in New Issue
Block a user