Skip to content

Instantly share code, notes, and snippets.

@alfa-carinae
Created March 22, 2019 14:25
Show Gist options
  • Save alfa-carinae/c570fa19ea908dcd8d1bfee81c19c8ee to your computer and use it in GitHub Desktop.
Save alfa-carinae/c570fa19ea908dcd8d1bfee81c19c8ee to your computer and use it in GitHub Desktop.
u = int(input("Enter the number of round tables\n: "))
v = int(input("Enter the number of people\n: "))
if (u > 0 and v > 0) and (u < 100 and v < 100):
from itertools import *
if v % u != 0:
distr0 = list(combinations([i for i in range(v)], int(v / u)))
tmp = list(product(distr0, [i for i in range(v % u)]))
else:
tmp = list(combinations([i for i in range(v)], int(v / u)))
"""for i in tmp:
print(i)"""
print(len(tmp))
else:
print("The number of people and tables must be between 1 and 99.")
@jonatonavich
Copy link

jonatonavich commented Mar 23, 2019

from itertools import combinations
from itertools import product

table_count = 3 #int(input("Enter the number of round tables\n: "))
people_count = 4 #int(input("Enter the number of people\n: "))

if 100 > table_count > 0 and 100 > people_count > 0:
    people = list(range(1, people_count + 1))
    straggler_count = int(people_count % table_count)

    print('table count: {}'.format(table_count))
    print('people: {}'.format(people))
    print()

    table_combinations = list()
    seating_combinations = set(combinations(people, int(people_count / table_count)))
    for table_combo in set(combinations(seating_combinations, table_count)):
        print(table_combo)
        last = None
        common_people = set()
        for person_combo in table_combo:
            if last is not None:
                common_people.union(last.intersection(person_combo))
            
            last = set(person_combo)
            # if there are any common people in the groups of 
            # people sitting at different tables, skipt it since
            # the same person can't sit at more than one table at once
            if common_people:
                break

        if not common_people:
            table_combinations.append(table_combo)

    
    print("table combinations:")
    for combo in table_combinations:
        print(combo)
    
    print()

    new_table_combos = list()
    if straggler_count > 0:
        updated_table_combos = list()
        straggler_combinations = set(combinations(people, straggler_count))

        for table_seating in table_combinations:
            all_people = set(*zip(*table_seating))
            new_table_seating = None
            for straggler in straggler_combinations:
                all_s = set([s for s in straggler])
                inter =  all_s.intersection(all_people)
                if not inter:
                    new_table_seating = (table_seating[0] + straggler,) + table_seating[1:]
                    break
            if new_table_seating:
                new_table_combos.append(new_table_seating)


        print("updated table combinations (including stragglers):")
        for combo in new_table_combos:
            print(combo)

else:
    print("The number of people and tables must be between 1 and 99.")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment