Snippets

Ella Sharakanski Practice Session 2018 - Google Code Jam 2018 - Senate Evacuation

Created by Ella Sharakanski
from collections import Counter


def party_index_to_name(party_index):
    return chr(ord("A") + party_index)

def evacuate(name_to_count):
    largest, second = name_to_count.most_common(2)
    largest_name, largest_count = largest
    second_name, second_count = second
    evacuate_name(largest_name, largest_count - second_count)
    for name, count in name_to_count.items():
        if name != largest_name and name != second_name:
            evacuate_name(name, count)
    for i in range(second_count):
        print(" ", end="")
        print("{}{}".format(largest_name, second_name), end="")

def evacuate_name(name, count_to_evacuate):
    for i in range(count_to_evacuate // 2):
        print(" {}{}".format(name, name), end="")
    if count_to_evacuate % 2 == 1:
        print(" {}".format(name), end="")

def run_tests(tests_count):
    for i in range(1, tests_count + 1):
        parties_count = int(input())
        people_per_party = input().split()
        name_to_count = Counter({party_index_to_name(i): int(count)
            for i, count in enumerate(people_per_party)})
        if i != 1:
            print("")
        print("Case #{}:".format(i), end="")
        evacuate(name_to_count)

def main():
    test_cases_count = int(input())
    run_tests(test_cases_count)
    

if __name__ == "__main__":
    main()

Comments (0)