Unit 5 Session 1 (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
0
as the sum, assuming all cards are valid or there are no cards.Plan the solution with appropriate visualizations and pseudocode.
General Idea: Calculate the total value of all cards in a Hand
instance, checking each card's validity before summing their values.
1) Initialize a variable `total` to `0` to store the cumulative value of the cards.
2) Iterate over each card in the hand.
3) Check if the card is valid using the `is_valid()` method.
a) If any card is invalid, immediately return `None`.
4) If the card is valid, add its value (obtained from `get_value()`) to `total`.
5) After all cards have been processed, return the value of `total`.
⚠️ Common Mistakes
def sum_hand(hand):
total = 0
for card in hand.cards:
if not card.is_valid():
return None
total += card.get_value()
return total