Python Forum
possible scores - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: possible scores (/thread-29799.html)



possible scores - 3Pinter - Sep-20-2020

Guys,

Got myself a nerdy thing going on: I want to get all possible scores given a set parameters.

you play 24 games which can either win or lose.

for a win: you earn some points and your level increase with 1
losing: you earn less points and your level decreases with 1.

Basicly these parameters do not change, so it's a given set a values. And I'd like to check those values.


So I created (not a programmer, just an enthousiast!) a script which works BUT it's slow. I mean slow slow slow.

I'm very curious to your thoughts.

import itertools

  
#Possible fight outcomes
win = True
lose = False
outcome = [win, lose]

#Starting level
start_level = 19
min_start_level = 1
max_start_level = 20

if start_level < min_start_level:
    start_level = min_start_level
if start_level > max_start_level:
    start_level = max_start_level
# print start_level  

# final results 
results = []

#win or losing a fight gives a fixed award according to your level
fight_rewards = [100, 120, 150, 190, 240, 300, 370, 450, 540, 640, 750, 870, 1000, 1140, 1290, 1450, 1620, 1800, 1990, 2190, 2400, 2620, 2850, 3090, 3340, 3600, 3870, 4150, 4440, 4740, 5050, 5370, 5700, 6040, 6390, 6750, 7120, 7500, 7890, 8290, 8700, 9120, 9550, 9990]

def new_level(fight_result, current_level):
    if fight_result:
        return current_level + 1
    else:
        return current_level - 1

def fight_reward(fight_result, level):
    if fight_result:
        return fight_rewards[level-1]
    else:
        return fight_rewards[level-2]

# get possible combinations of 24 fights 
possible_combinations = itertools.product(outcome, repeat=24)


for possible_combination in possible_combinations:
    #print possible_combination
    current_level = start_level
    score = 1
    cummulative_score = []
    for item in possible_combination:
        score += fight_reward(item, current_level)
        cummulative_score.append(score)
        current_level = new_level(item, current_level)
        
    #print current_level
    #print score
    results.append([possible_combination, cummulative_score, current_level, score])

print results

# of all the results, which values do you like to check if it exists
score_check = 9221
score_check_results =  [result for result in results if score_check in result[2]]

if score_check_results:
    for score_check_result in score_check_results:
        print "Score {0} exists {1}\n".format(score_check, score_check_result)
else:
    print "Score {0} not found".format(score_check)