thebaddie

python lottery code

Sep 5th, 2023
593
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | Source Code | 0 0
  1. import itertools
  2.  
  3. # Read in the data from the text file
  4. with open('lottery_data.txt', 'r') as f:
  5. data = f.read().splitlines()
  6.  
  7. # Convert the data to a list of winning combinations
  8. winning_combinations = []
  9. for line in data:
  10. winning_combinations.append(list(map(int, line.split())))
  11.  
  12. # Create a list of all possible combinations
  13. all_combinations = list(itertools.product(range(1, 51), repeat=5))
  14.  
  15. # Calculate the frequency of each number appearing in the winning combinations
  16. #number_frequencies = {}
  17. number_frequencies = {n: 0 for n in range(1, 51)}
  18. for combination in winning_combinations:
  19. for number in combination:
  20. if number not in number_frequencies:
  21. number_frequencies[number] = 1
  22. else:
  23. number_frequencies[number] += 1
  24.  
  25. # Calculate the score for each combination based on the frequency of its numbers
  26. combination_scores = {}
  27. for combination in all_combinations:
  28. score = sum([number_frequencies[number] for number in combination])
  29. combination_scores[combination] = score
  30.  
  31. # Sort the combinations by score and output the top 5
  32. top_combinations = sorted(combination_scores, key=combination_scores.get, reverse=True)[:5]
  33. print("The best 5 combinations are:")
  34. for combination in top_combinations:
  35. print(combination)
Tags: python Lottery
Advertisement
Add Comment
Please, Sign In to add comment