Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Roulette - Add Outcome object to a frozenset (Bin object)

18 views
Skip to first unread message

Quoc Khanh Le

unread,
Sep 29, 2024, 6:09:50 AM9/29/24
to Building Skills Books
Hi there,

I'm working on the Wheel class of Roulette game and I'm struggling to to figure out how to implement the addOutcome method of the Wheel class. The requirement here is to add a given Outcome object to the Bin instance with the given number. 

Since each Bin object is a frozenset (immutable), does it mean we need to convert it into a set, then add a new Outcome object to it and then reconvert it back into a new Bin object inside the self.bins tuple of the Wheel object? 

Here, I'm using the .union() method to generate a new frozenset with the added Outcome. However, since the self.bins itself is a tuple (immutable) so changing the Bin object inside it is also not possible.

I'm confused here and would really appreciate any help on how to implement this.

Below is my current code.

class Bin(frozenset):
    pass


class Wheel:
    def __init__(self) -> None:
        self.bins = tuple(Bin() for _ in range(38))

    def addOutcome(self, number: int, outcome: Outcome) -> None:
        """
        Add the given Outcome object to the Bin instance with the given number
        """
       
# Does not work since self.bins is an immutable tuple
        self.bins[number] = self.bins[number].union(outcome)

Steven F. Lott

unread,
Sep 29, 2024, 5:27:02 PM9/29/24
to building-s...@googlegroups.com
Thanks for the question.  It may take a few days to reply. Hurricane Helene has caused us some problems. 


Sent from my iPhone

On Sep 29, 2024, at 06:09, Quoc Khanh Le <quockh...@gmail.com> wrote:


--
You received this message because you are subscribed to the Google Groups "Building Skills Books" group.
To unsubscribe from this group and stop receiving emails from it, send an email to building-skills-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/building-skills-books/371a4b67-dd34-44c2-a8a8-b55ac00bc03bn%40googlegroups.com.

Steven F. Lott

unread,
Oct 3, 2024, 10:47:58 AM10/3/24
to building-s...@googlegroups.com
I think the idea of building the bins one outcome at a time might be (as you’ve observed) relatively complicated. 

Another approach is to accumulate a sequence of outcome instances in a temporary collection like a list or set. You can you list (or set) comprehensions to build a temporary collection, and build the frozen Bin object from this collection. 

Sent from my iPhone

On Sep 29, 2024, at 06:09, Quoc Khanh Le <quockh...@gmail.com> wrote:


--

Le Quoc Khanh

unread,
Oct 4, 2024, 3:49:25 PM10/4/24
to building-s...@googlegroups.com
I'm so sorry to hear about the damage caused by Hurricane Helene. I can't imagine how difficult this must be. 

Hope you and your loved ones are safe.

Quoc Khanh Le

unread,
Oct 5, 2024, 5:42:39 AM10/5/24
to Building Skills Books
That makes sense. Thanks Steven!

I moved the add_outcome method to the BinBuilder class and used set and list as temporary collections to accumulate Outcome instances. Then converted them to frozenset and tuple to create Bin and Wheel instances. Here is my current code (docstrings and some parts have been omitted for brevity).

I'm still wondering about benefits of using frozenset instead of set for the Bin class, and tuple instead of list as a collection of bins for the Wheel class. The fact that frozenset and tuple are immutable makes them a bit more complicated to build compared to the other two.
Reply all
Reply to author
Forward
0 new messages