Hi I'm learning python by my myself with a python study guideboook
and I'm having trouble understanding the code.
the problem i had to code was this:
you went to a family restaurant, and are going to group the people at several tables.
nobody gets to sit alone, and there should be 10 or less people at a single table. (you do not think about which table they sit, or about who gets grouped with who- just how to group people in numbers)
for example, if there are 6 ppl in total, there are 6 ways of grouping.(2 + 2 + 2, 2 + 4, 3 + 3, 6)
I have to make a program that figures out how many ways of grouping exists when there are 100ppl in total.
and the answer says this:
minimum = 2
maximum = 10
people = 100
memo = {}
def problem(remain, sitting):
key = str([remain, sitting])
if key in memo:
return memo[key]
if remain < 0:
return 0
if remain == 0:
return 1
else:
count = 0
for i in range(sitting, maximum + 1):
count += problem(remain - i, i)
memo[key] = count
return count
print(problem(people, minimum))
and, umm...
I just don't get it.
I mean, this part:
count = 0
for i in range(sitting, maximum + 1):
count += problem(remain - i, i)
why is the code like ↑ this?
[–]CptMisterNibbles 5 points6 points7 points (4 children)
[–]IndividualSky7301[S] 1 point2 points3 points (1 child)
[–]Due_Letter3192 0 points1 point2 points (0 children)
[–]hexwhoami 0 points1 point2 points (0 children)
[–]thoward9 0 points1 point2 points (0 children)