Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Casino - Homework
#5
Totally agree with your advice.

Like you so rightly said "Next time you have a programming assignment don't think about how to solve the problem using Python, think about how to solve the problem efficiently. Try to write all the steps in a manner that would make it easy for someone else to follow. When you have a clearly defined solution you can then start programming."

Flowcharting then executing...is what I'm working on.

Not trying to make any excuses but I have a teacher who's a deep diver.

Teaches how to swim...then takes you deep sea diving and tells you to swim back up Big Grin.

Cheers,


(Dec-21-2021, 05:33 PM)deanhystad Wrote: Your error was that you didn't create the proper abstraction. You could solve this problem by changing the value of aces from 11 to 1 and checking the sum.
def handValue(*cards):
    # Initially force all aces to be 11 before we start.
    cards = [11 if card == 1 else card for card in cards]
    # While sum > 21, convert aces from 11 to 1
    while sum(cards) > 21 and len(cards.count(11)) > 0:
        cards[cards.index(11)] = 1
This is a very literal interpretation of your algorithm. It does exactly what is asked. Starting programmers write a lot of code like this because they focus a lot on finding the right commands and using correct syntax and spend very little time thinking about the problem they are trying to solve.

The problem becomes simpler if you stop thinking about an hand of cards and think about the value of the hand and the number of aces.
def handValue(*cards):
    # Initially force all aces to be 11 before we start.
    cards = [11 if card == 1 else card for card in cards]
    value = sum(cards)
    aces = cards.count(11)
    # While value> 21, convert aces from 11 to 1
    while value > 21 and aces > 0:
        value -= 10
        aces -= 1
In this example the abstraction is minor, but I think it still offers a significant improvement. Instead of searching for 11's in the list, converting them to 1's, and summing the hand value, the abstraction has two integer subtractions. This would be a big performance boost if you were computing millions of hands.

Next time you have a programming assignment don't think about how to solve the problem using Python, think about how to solve the problem efficiently. Try to write all the steps in a manner that would make it easy for someone else to follow. When you have a clearly defined solution you can then start programming.
Reply


Messages In This Thread
Casino - Homework - by Men - Dec-21-2021, 01:57 PM
RE: Casino - Homework - by BashBedlam - Dec-21-2021, 04:13 PM
RE: Casino - Homework - by Men - Dec-21-2021, 04:50 PM
RE: Casino - Homework - by deanhystad - Dec-21-2021, 05:33 PM
RE: Casino - Homework - by Men - Dec-22-2021, 04:24 PM
RE: Casino - Homework - by LoriBrown - Dec-15-2022, 05:28 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020