Posts: 1
Threads: 1
Joined: Dec 2017
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
# -*- 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'))
Posts: 12,031
Threads: 485
Joined: Sep 2016
Posts: 232
Threads: 2
Joined: Sep 2017
Dec-06-2017, 11:40 AM
(This post was last modified: Dec-06-2017, 11:40 AM by gruntfutuk.)
Ok, so this looks pretty straightforward.
Have you tried using the interactive shell, entering the key code of the function (copy and paste) and checking what happens?
Inside the get_team() function, game holds the list for the gameid of interest. This is a list of three elements, namely two team strings (each of which are one of: "Wxx" or "Lxx" or "Txx") followed by a number, 0 or 1, telling which team won (either the team in the first list position, position 0, game[0] , or the team in the second list position, position 1, game[1] ).
Subsequently, team holds the name of the team of interest ("Wxx" or "Lxx" or "Txx") which means the last two characters, given by team[-2:] (i.e. counting backwards by 2 from the end of the string, give me the slice of the string from there until the end) which when converted to an integer, gives you the game id you need to check for that team of interest (either as a winner or looser, indicated by the first character, team[0] in team if it is not a T. If it is a T of course, you have found the team you are looking for.
I think you only need to add two lines of code, and the first line is an if statement.
I am trying to help you, really, even if it doesn't always seem that way
|