Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Count of sums
#8
use a list:
import itertools

max_digit = 9
# initialize counts list to 0
counts = [0] * (max_digit +1)

thelist = [0, 0, 0, 1, 1, 2, 2, 1]
for L in range(5,6):
    for subset in itertools.combinations(thelist, L):
        sum_subset = sum(subset)
        print(f"{subset} => {sum_subset}")
        counts[sum_subset] += 1

print('\nCount of sums:')
for n in range(max_digit + 1):
   print(f"sum of {n}'s' is {counts[n]}")
result:
Output:
(0, 0, 0, 1, 1) => 2 (0, 0, 0, 1, 2) => 3 (0, 0, 0, 1, 2) => 3 (0, 0, 0, 1, 1) => 2 (0, 0, 0, 1, 2) => 3 (0, 0, 0, 1, 2) => 3 (0, 0, 0, 1, 1) => 2 (0, 0, 0, 2, 2) => 4 (0, 0, 0, 2, 1) => 3 (0, 0, 0, 2, 1) => 3 (0, 0, 1, 1, 2) => 4 (0, 0, 1, 1, 2) => 4 (0, 0, 1, 1, 1) => 3 (0, 0, 1, 2, 2) => 5 (0, 0, 1, 2, 1) => 4 (0, 0, 1, 2, 1) => 4 (0, 0, 1, 2, 2) => 5 (0, 0, 1, 2, 1) => 4 (0, 0, 1, 2, 1) => 4 (0, 0, 2, 2, 1) => 5 (0, 0, 1, 1, 2) => 4 (0, 0, 1, 1, 2) => 4 (0, 0, 1, 1, 1) => 3 (0, 0, 1, 2, 2) => 5 (0, 0, 1, 2, 1) => 4 (0, 0, 1, 2, 1) => 4 (0, 0, 1, 2, 2) => 5 (0, 0, 1, 2, 1) => 4 (0, 0, 1, 2, 1) => 4 (0, 0, 2, 2, 1) => 5 (0, 1, 1, 2, 2) => 6 (0, 1, 1, 2, 1) => 5 (0, 1, 1, 2, 1) => 5 (0, 1, 2, 2, 1) => 6 (0, 1, 2, 2, 1) => 6 (0, 0, 1, 1, 2) => 4 (0, 0, 1, 1, 2) => 4 (0, 0, 1, 1, 1) => 3 (0, 0, 1, 2, 2) => 5 (0, 0, 1, 2, 1) => 4 (0, 0, 1, 2, 1) => 4 (0, 0, 1, 2, 2) => 5 (0, 0, 1, 2, 1) => 4 (0, 0, 1, 2, 1) => 4 (0, 0, 2, 2, 1) => 5 (0, 1, 1, 2, 2) => 6 (0, 1, 1, 2, 1) => 5 (0, 1, 1, 2, 1) => 5 (0, 1, 2, 2, 1) => 6 (0, 1, 2, 2, 1) => 6 (0, 1, 1, 2, 2) => 6 (0, 1, 1, 2, 1) => 5 (0, 1, 1, 2, 1) => 5 (0, 1, 2, 2, 1) => 6 (0, 1, 2, 2, 1) => 6 (1, 1, 2, 2, 1) => 7 Count of sums: sum of 0's' is 0 sum of 1's' is 0 sum of 2's' is 3 sum of 3's' is 9 sum of 4's' is 19 sum of 5's' is 15 sum of 6's' is 9 sum of 7's' is 1 sum of 8's' is 0 sum of 9's' is 0
Reply


Messages In This Thread
Count of sums - by mattis - Oct-22-2018, 07:55 PM
RE: Count of sums - by Larz60+ - Oct-22-2018, 08:14 PM
RE: Count of sums - by mattis - Oct-22-2018, 08:21 PM
RE: Count of sums - by nilamo - Oct-22-2018, 08:26 PM
RE: Count of sums - by mattis - Oct-22-2018, 08:36 PM
RE: Count of sums - by nilamo - Oct-22-2018, 08:40 PM
RE: Count of sums - by mattis - Oct-22-2018, 08:44 PM
RE: Count of sums - by Larz60+ - Oct-22-2018, 08:49 PM
RE: Count of sums - by Larz60+ - Oct-22-2018, 08:50 PM
RE: Count of sums - by mattis - Oct-22-2018, 08:58 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Row Count and coloumn count Yegor123 4 1,381 Oct-18-2022, 03:52 AM
Last Post: Yegor123

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020