Home > database >  Roulette game win streak counter
Roulette game win streak counter

Time:12-02

I would like to ask for your help.

The task: There are 50 observations of the 'game of roulette' a subject has played. Given the fact that the data of the wins and losses are already known (I have the time series) and the value of his total capital increases and decreases after every game I want to calculate the number of total win/loss streaks and the length of average win streak. (lets say the starting capital is 100 so the time series goes - series = [102,104,106,105,108,109,120,115,116,118,120,122,..].. in this case the first streak would be 3.)

I am firmly new to this and created this riddle on my own to challenge my logic thinking but it seems to me that I am totaly lost which also frustrates me. I wanted to revise my knowledge of loops and conditions in this exercise so it should not be consist of anything else.

Could you please just give me an advice on how to do that? I tried to think in this way but it really makes no sense to me in the end..

for i in range (0, len(series)-1):
   while series[i] < series[i 1]:        #While the following game brings profit
      counter_win = counter_win   1      #The counter of good games should increase by 1 
   break

Now I can't find a way of how to start over from the beggining of next sequence of numbers.

Thanks everyone because I'm lost now.

CodePudding user response:

I would suggest the following piece of code:

current_streak = 0
streaks = []
for i in range (0, len(series)-1):
   if series[i] < series[i 1]:        
      current_streak  = 1
   elif current_streak > 0:     
      streaks.append(current_streak)
      current_streak = 0

# edge case if the game ends with a streak
if current_streak > 0:
    streaks.append(current_streak)


print("{} Steaks with the average length {}".format(len(streaks), sum(streaks)/len(streaks)))

To count the number of losses, you could use the same approach

CodePudding user response:

The nested loop in your example will count the first win streak of 3. As a win streak of 3, then a win streak of 2 then a win streak of 1. Use this to get a list of the win streaks:

input_ = [100,102,104,106,105,108,109,120,115,116,118,120,122]

win_streak = 0
win_streaks = []
for i in range(1, len(input_)):
    if input_[i] > input_[i-1]:
        win_streak  = 1
    elif win_streak > 0:
        win_streaks.append(win_streak)
        win_streak = 0
if win_streak > 0:
    win_streaks.append(win_streak)
average = sum(win_streaks) / len(win_streaks)
print(average)

CodePudding user response:

Hire is a pice of code that demonstrates a possible way of solving your problem. The code is meant to be instructive so is not optimized too allow easier understanding:

data = [102,104,106,105,108,109,120,115,116,118,120,122]

# menages control between win and lose streak 
boolean = False

# actualy counts the number of consecutive wins and loses
counter = 0

# Store the lenght of streak 
win = []
lose = []

for i in range(1, len(data)):
    if data[i] > data[i - 1]:
        #If we were losing and we won one time add streak to array if needed and reset  
         #counter
        if boolean is False:
            if counter != 1:
                lose.append(counter)

                counter = 1
           # Change boolean to address win streak
            boolean = True
        else:
            counter  = 1
    if data[i] < data[i - 1]:
        # Same as before but with opposite signs 
        if boolean is True:
            if counter != 1:
                win.append(counter)

                counter = 1
            boolean = False
        else:
            counter  = 1
average_win=0
average_lose=0
for i in win:
    average_win =i
for i in lose:
    average_win =i


print(average_win/len(win))
print(average_lose/len(lose))


CodePudding user response:

This is one way to do it. It's not beautiful but it gets the job done. :)

Given more time I would split this in to separate functions etc.

series = [102,104,106,105,108,109,120,115,116,118,120,122]

runs = {}
streak, last = 0, 0 # streak is key variable here as it goes negative for loss-streak
o = 1 # run-counter for our dictionary
for i in series:
    if i > last and last: # first entry isn't a win right?
        # Win
        if streak < 0: # Winds have changed. Reset streak.
            streak = 0
            o  = 1
        if o in runs: runs[o]  = "w"
        else: runs[o] = "w"
        streak  = 1
    elif i < last and last:
        # Loss
        if streak > 0: # Winds have changed. Reset streak.
            streak = 0
            o  = 1
        if o in runs: runs[o]  = "l"
        else: runs[o] = "l"
        streak -= 1

    last = i

high = sorted([ len(x) for x in runs.values() if "w" in x ],reverse=True)[0]
low = sorted([ len(x) for x in runs.values() if "l" in x ],reverse=True)[0]
print(f"Biggest win spree: {high}, loss spree: {low}")

average_wins = [ len(x) for x in runs.values() if "w" in x ]
print("Average winstreak:",round(sum(average_wins)/len(average_wins),1))

Hope this was helpful. Have a good day!

Edit: You don't actually need record_low&high come to think of it. Updated with afew buffs. :)

CodePudding user response:

A functional programming solution:

from itertools import groupby

inp = [100,102,104,106,105,108,109,120,115,116,118,120,122]

streaks = [len(list(y)) for x,y in groupby(map(lambda x,y : x < y, inp, inp[1:])) if x == True]

print(streaks, sum(streaks)/len(streaks))
  • Related