Dec-02-2017, 10:05 PM
Hey Guys,
I got homework from university but we never learned anything that has to do with it, so I'm really confused with this.
I got a list with games from a tournament. The first 8 games are the first round. In this round the Teams are defined by TXX, XX as their number. Every game got an ID and to define who won there is 0 or 1 (0 = left team won, 1 right team won). After the first round, all teams are redefined every round by WXX or LXX, W being for winner and L for loser and the XX is the gameid from the game which defined their W or L so W27 means it is the winner from game 27.
Now i got the code which is written in the file (see code) and my task is to write recursive function without using for or while and this function needs to find the initial name of the winner from the last game. So it needs to check all their games and names back to the first round where they had the TXX name.
I'd really appreciate any help.
Thanks a lot! - Thomas
I got homework from university but we never learned anything that has to do with it, so I'm really confused with this.
I got a list with games from a tournament. The first 8 games are the first round. In this round the Teams are defined by TXX, XX as their number. Every game got an ID and to define who won there is 0 or 1 (0 = left team won, 1 right team won). After the first round, all teams are redefined every round by WXX or LXX, W being for winner and L for loser and the XX is the gameid from the game which defined their W or L so W27 means it is the winner from game 27.
Now i got the code which is written in the file (see code) and my task is to write recursive function without using for or while and this function needs to find the initial name of the winner from the last game. So it needs to check all their games and names back to the first round where they had the TXX name.
I'd really appreciate any help.
Thanks a lot! - Thomas
# -*- coding: utf-8 -*- """ This dictionary contains the games and result of a tournament. Every game has an ID, the two rivals and the result. The names of the rivals start with 'T', 'W' or 'L', followed by a number. 'TXX': Initial name of the team with the ID XX 'WXX': Winner of the game with the ID XX 'LXX': Loser of the game with the ID XX """ GAMES = { 1: ['T01', 'T02', 0], 2: ['T03', 'T04', 1], 3: ['T05', 'T06', 0], 4: ['T07', 'T08', 0], 5: ['T09', 'T10', 1], 6: ['T11', 'T12', 0], 7: ['T13', 'T14', 0], 8: ['T15', 'T16', 1], 9: ['L01', 'L02', 0], 10: ['L03', 'L04', 1], 11: ['L05', 'L06', 0], 12: ['L07', 'L08', 0], 13: ['W01', 'W02', 0], 14: ['W03', 'W04', 0], 15: ['W05', 'W06', 1], 16: ['W07', 'W08', 0], 17: ['W09', 'L13', 0], 18: ['W10', 'L14', 0], 19: ['W11', 'L15', 1], 20: ['W12', 'L16', 1], 21: ['W13', 'W14', 0], 22: ['W15', 'W16', 0], 23: ['W17', 'W18', 1], 24: ['W19', 'W20', 1], 25: ['W21', 'W22', 1], 26: ['W23', 'L21', 0], 27: ['W24', 'L22', 1], 28: ['W26', 'W27', 1], 29: ['W28', 'L25', 0], 30: ['W25', 'W29', 1], } # Number teams of the tournament TEAM_NUMBER = 16 # Number of games played in the first round # Should be used to stop the recursive calls FIRST_LAP_GAMES_NUMBER = TEAM_NUMBER / 2 # Complete the function which returns the initial name of the team which won or # lost the game. # The function requires the ID of the game and the result of the # team of interest. # The result could be either 'L' for loser or 'W' for winner. # It returns the initial name of this team, which always # starts with an 'T' and a double-digit. # The winner of the tournament should be T13. # # Rules: # * You are not allowed to use for or while loop. Use recursion instead! # Hints: # * Call the function inside the function to create a recursion # * Get a character of a string: var[0] def get_team(gameid, result): """This function returns the initial name of """ game = GAMES[gameid] winner = game[2] team_index = winner if result != 'W': team_index = not winner team = game[team_index] previous_game_id = int(team[-2:]) # write your code here return team # The following code will print the winner of the tournament # if you run this program with "python3 recursive.py" if __name__ == '__main__': print("Winner of the tournament: " + get_team(30, 'W'))