# Given an array of pairs representing the teams that have competed 
# against each other and an array containing the results of each 
# competition, write a function that returns the winner of the 
# tournament. The input arrays are named competitions and results, 
# respectively. The competitions array has elements in the form of 
# [homeTeam, awayTeam], where each team is a string of at most 30 
# characters representing the name of the team. The results array 
# contains information about the winner of each corresponding 
# competition in the competitions array. Specifically, results[i] 
# denotes the winner of competitions[i], where a 1 in the results array 
# means that the home team in the corresponding competition won and a 0 
# means that the away team won.

def tournamentWinner(competitions, results):
    wins = {}
    winner_so_far = None
    for i in range(len(competitions)):
        pair = competitions[i]
        result = results[i]
        if result == 0:
            winner = pair[1]
        else:
            winner = pair[0]
        wins[winner] = wins.get(winner, 0) + 1
        if winner_so_far is None or wins[winner_so_far] < wins[winner]:
            winner_so_far = winner

    return winner_so_far

competitions = [
  ["HTML", "C#"],
  ["C#", "Python"],
  ["Python", "HTML"],
]
results = [0, 0, 1]

print(tournamentWinner(competitions, results))

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: