Python Forum

Full Version: Recursive Function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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.