Mini Cactpot Algorithm Part 2

This is the second part of the mini cactpot algorithm. This post is about calculating potential values of each line on the ticket and recommend the highest line of value to the user. If you want to see how to set up the mini cactpot ticket, use this link: Part 1.

Class Calculate

Similar to class Game, class Calculate will also take one argument, the dictionary of ticket positions. I’m going to initialize the class.

Since I want to calculate each three number lines, I use dictionary to map each line to its three positions on the ticket.

My next method, combinations, is actually a Python built-in. The reason why I wrote out the code was because my free Trinket.io did not support itertools.

The method takes an iterable and return tuples of combination for those elements at specific length. You can read more about it here: itertools.combinations. Suppose if you have a list of 4 items and you want to get all 2 item combinations, you can use itertools.combinations(list, length) for that.

With this method, I can calculate possible combinations for the numbers the user has not picked. So if the user picked 1, 2, 3, 4 then 5, 6, 7, 8, 9 are still available. Consequently, I just need to find out which positions on the ticket that is still hidden and replace those with the available numbers. I received help from Francisco Couzo on StackOverflow on the method to replace hidden values with combinations. Here’s the link for the question I posted: question.

His function finds the indices of all the string in the first list and replace them with combinations from second list. I modified the function to fit with the class. In my case, list_1  is the value of each line for the dictionary I created earlier. While list_2 is the available numbers.

Calculating Payout

The actual calculation happens in the method lines_combinations. I used another dictionary to map each line to its possible combinations. It uses the method earlier to do that. num_lines and num_list are simply the same arguments for list_combinations.

With the combinations for each line calculated, I can go on crunching out the potential payout of each line. The payout varies depend on the sum for each line. By summing up each combinations returned from lines_combination and multiply them with the payout, I can get all potential payouts. Finally, I can find the average potential payout by using floor division of the total sum with the length of the list.

However, the user does not need to know what happens in the code. Therefore I used another method to print out a message notifying the user which line has the best possible payout. There are cases where multiple lines can have the best payout. recommendation takes the dictionary with each line average payout and find the maximum value. It returns a string containing the line(s) recommended and the potential payout value(s).

All that’s left to do now is to write a function to set up the game and calculate the potential payout. That should be a simple matter so I’m going to end the post here. If you see how the code can be improved, please let me know in the comment below.

Leave a Reply

Your email address will not be published. Required fields are marked *