Created
June 13, 2018 01:13
-
-
Save Anteloper/2d5b678f1f4b1b64f760d7ce840053b6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
groupWinners = {} | |
groups = { | |
"a" : ["Russia", "Saudia Arabia", "Egypt", "Uruguay"], | |
"b" : ["Portugal", "Spain", "Morrocco", "Iran"], | |
"c" : ["France", "Austrailia", "Peru", "Denmark"], | |
"d" : ["Argentina", "Iceland", "Croatia", "Nigeria"], | |
"e" : ["Brazil", "Switzerland", "Costa Rica", "Serbia"], | |
"f" : ["Germany", "Mexico", "Sweden", "Korea"], | |
"g" : ["Belgium", "Panama", "Tunisia", "England"], | |
"h" : ["Poland", "Senegal", "Colombia", "Japan"], | |
} | |
def main(): | |
detailedGroups = int(input("\n\nWould you like to \n1. Be asked about individual group stage games \nOR \n2. Just pick group winners?\n\nEnter 1 or 2: ")) | |
if(detailedGroups == 1): | |
for i in range(97, 105): | |
print("\n Group " + chr(i).upper()) | |
groupWinners[chr(i)] = groupStage(groups[chr(i)]) | |
else: | |
for i in range(97, 105): | |
print("\n Group " + chr(i).upper()) | |
groupWinners[chr(i)] = briefGroupStage(groups[chr(i)]) | |
q1 = ["", ""] | |
q2 = ["", ""] | |
q3 = ["", ""] | |
q4 = ["", ""] | |
s1 = ["", ""] | |
s2 = ["", ""] | |
f = ["", ""] | |
print("\n\n\nRound of 16: ") | |
q1[0] = knockout(groupWinners["a"][0], groupWinners["b"][1]) | |
q1[1] = knockout(groupWinners["c"][0], groupWinners["d"][1]) | |
q2[0] = knockout(groupWinners["e"][0], groupWinners["f"][1]) | |
q2[1] = knockout(groupWinners["g"][0], groupWinners["h"][1]) | |
q3[0] = knockout(groupWinners["b"][0], groupWinners["a"][1]) | |
q3[1] = knockout(groupWinners["c"][1], groupWinners["d"][0]) | |
q4[0] = knockout(groupWinners["f"][0], groupWinners["e"][1]) | |
q4[1] = knockout(groupWinners["g"][1], groupWinners["h"][0]) | |
print("\n\n\nQuarter finals: ") | |
s1[0] = knockout(q1[0], q1[1]) | |
s1[1] = knockout(q2[0], q2[1]) | |
s2[0] = knockout(q3[0], q3[1]) | |
s2[1] = knockout(q4[0], q4[1]) | |
print("\n\n\nSemi Finals: ") | |
f[0] = knockout(s1[0], s1[1]) | |
f[1] = knockout(s2[0], s2[1]) | |
print("\n\n\nFinals: ") | |
w = knockout(f[0], f[1]) | |
print("\n\n\n\n") | |
winner = Node(w) | |
f = buildChildren(winner, f) | |
s1 = buildChildren(f[0], s1) | |
s2 = buildChildren(f[1], s2) | |
q1 = buildChildren(s1[0], q1) | |
q2 = buildChildren(s1[1], q2) | |
q3 = buildChildren(s2[0], q3) | |
q4 = buildChildren(s2[1], q4) | |
buildChildren(q1[0], (groupWinners["a"][0], groupWinners["b"][1])) | |
buildChildren(q1[1], (groupWinners["c"][0], groupWinners["d"][1])) | |
buildChildren(q2[0], (groupWinners["e"][0], groupWinners["f"][1])) | |
buildChildren(q2[1], (groupWinners["g"][0], groupWinners["h"][1])) | |
buildChildren(q3[0], (groupWinners["b"][0], groupWinners["a"][1])) | |
buildChildren(q3[1], (groupWinners["c"][1], groupWinners["d"][0])) | |
buildChildren(q4[0], (groupWinners["f"][0], groupWinners["e"][1])) | |
buildChildren(q4[1], (groupWinners["g"][1], groupWinners["h"][0])) | |
print_tree(winner) | |
def buildChildren(node, children): | |
left = Node(children[0].ljust(12, '-'), node) | |
right = Node(children[1].ljust(12, '-'), node) | |
return (left, right) | |
def knockout(team1, team2): | |
res = -1 | |
while(res != "1" and res != "2"): | |
if(res != -1): | |
print("Enter either 1 or 2") | |
res = raw_input("\n\n1." + team1 + "\nvs. \n2." + team2 + "\n\nEnter the number of the winner: ") | |
if(res == "1"): | |
return team1 | |
else: | |
return team2 | |
def groupStage(teams): | |
stats = {} | |
for team in teams: | |
stats[team] = [0,0,0,0] #points, goal difference, goals for, goals against | |
groupGame(0, 1, teams, stats) | |
groupGame(2, 3, teams, stats) | |
groupGame(0, 2, teams, stats) | |
groupGame(3, 1, teams, stats) | |
groupGame(3, 0, teams, stats) | |
groupGame(1, 2, teams, stats) | |
j = 0 | |
res = ["", ""] | |
for i in reversed((sorted(stats, key=stats.get))): | |
if(j < 2): | |
res[j] = i | |
j += 1 | |
print(res[0] + " and " + res[1] + " qualify!") | |
return (res) | |
def groupGame(ind1, ind2, teams, stats): | |
team1 = teams[ind1] | |
team2 = teams[ind2] | |
print("\nEnter each team's score for the game " + team1 + " vs. " + team2) | |
score1 = int(input(team1 + ": ")) | |
score2 = int(input(team2 + ": ")) | |
if(score1 < score2): | |
stats[team2][0] = stats[team2][0] + 3 | |
elif(score1 == score2): | |
stats[team1][0] = stats[team1][0] + 1 | |
stats[team2][0] = stats[team2][0] + 1 | |
else: | |
stats[team1][0] = stats[team1][0] + 3 | |
#goal diff | |
stats[team1][1] = stats[team1][1] + score1 - score2 | |
stats[team2][1] = stats[team2][1] + score2 - score1 | |
#goals for | |
stats[team1][2] = stats[team1][2] + score1 | |
stats[team2][2] = stats[team2][2] + score2 | |
#goals against | |
stats[team1][3] = stats[team1][3] + score2 | |
stats[team2][3] = stats[team2][3] + score1 | |
return stats | |
def briefGroupStage(teams): | |
for i in range(1,5): | |
print(str(i) + ": " + teams[i-1]) | |
winner = int(input("\nEnter the number of the winner of the group: "))-1 | |
runner = int(input("Enter the number of the runner-up of the group: "))-1 | |
while(runner == winner): | |
print("Runner-Up can't be the same as winner") | |
runner = int(input("Enter the number of the runner-up of the group: "))-1 | |
return (teams[winner], teams[runner]) | |
class Node: | |
def __init__(self, name, parent=None): | |
self.name = name | |
self.parent = parent | |
self.children = [] | |
if parent: | |
self.parent.children.append(self) | |
def print_tree(current_node, childattr='children', nameattr='name', indent='', last='updown'): | |
if hasattr(current_node, nameattr): | |
name = lambda node: getattr(node, nameattr) | |
else: | |
name = lambda node: str(node) | |
children = lambda node: getattr(node, childattr) | |
nb_children = lambda node: sum(nb_children(child) for child in children(node)) + 1 | |
size_branch = {child: nb_children(child) for child in children(current_node)} | |
""" Creation of balanced lists for "up" branch and "down" branch. """ | |
up = sorted(children(current_node), key=lambda node: nb_children(node)) | |
down = [] | |
while up and sum(size_branch[node] for node in down) < sum(size_branch[node] for node in up): | |
down.append(up.pop()) | |
""" Printing of "up" branch. """ | |
for child in up: | |
next_last = 'up' if up.index(child) is 0 else '' | |
next_indent = '{0}{1}{2}'.format(indent, ' ' if 'up' in last else '│', ' ' * len(name(current_node))) | |
print_tree(child, childattr, nameattr, next_indent, next_last) | |
""" Printing of current node. """ | |
if last == 'up': start_shape = '┌' | |
elif last == 'down': start_shape = '└' | |
elif last == 'updown': start_shape = ' ' | |
else: start_shape = '├' | |
if up: end_shape = '┤' | |
elif down: end_shape = '┐' | |
else: end_shape = '' | |
print('{0}{1}{2}{3}'.format(indent, start_shape, name(current_node), end_shape)) | |
""" Printing of "down" branch. """ | |
for child in down: | |
next_last = 'down' if down.index(child) is len(down) - 1 else '' | |
next_indent = '{0}{1}{2}'.format(indent, ' ' if 'down' in last else '│', ' ' * len(name(current_node))) | |
print_tree(child, childattr, nameattr, next_indent, next_last) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment