More lists and loops solution

Overview 
Points to remember:
• Name your submissions as hw4_part1.py and hw4_part2.py, all lower case. Otherwise, we will not be able to locate and execute your submissions properly.
• As in Homework#3, expect different input cases and input files in the submission server. Your program must work for all possible inputs, not just the ones we test for.
• Remember to print any input you read immediately as we have done in previous homewroks.
• We will grade you on correct program as well as good program structure. We will also continue to check for “excessive collaboration”.


Part 1: World cup strikes again
Your solution for this part must be called hw4_part1.py. Only submit this file.
You will use the input module called hw4_util.py to read a file all_games.txt containing all matches in the World Cup. Read this file into a list of lists in which each item in the list is a different game.
import hw4_util results = hw4_util.read_games('all_games.txt') print results[-1]
For example, given ['Brazil', 3, 'Croatia', 1, 'Brazil'], we know that the score of the Brazil-Croatia game was 3-1, and Brazil won.
Similarly, ['Netherlands', 0, 'Costa Rica', 0, 'Netherlands'] means that the score of the Netherlands-Costa Rica was 0-0, but Netherlands won (presumably by penalty kicks).
Finally, ['Ecuador', 0, 'France', 0, 'Draw'] means that this game was a draw. No one won.
Write a program to do the following:
• Read the name of a country (upper/lower case should not matter). • Find and print all the games this country played. Print the total number of games played, wins, lose, draws, goals for and against.
• Print whether the country played in the round of 16, quarter finals, semi finals or the final. The game number will tell the story. First 48 games are group matches, games 49-56 are round of 16, games 57-60 are quarter finals, 61-62 are semi-finals, 63rd game is the third place playoff and 64th game is the final. Print the highest achievement for the team (only the group they played in).
• Each game is printed as team 1 name (rjust(24)), team 1 score (rjust(4)), team 2 score (ljust(4)) and team 2 name (ljust(24)). All team stats are printed with ljust(6). • (Bonus +5 points) Print the final standing of this team if it is one of first, second, third or fourth place.
Here is a possible run of the program:
Please enter a country == Algeria Algeria All games: Belgium 2-1 Algeria Winner:Belgium Korea Republic 2-4 Algeria Winner:Algeria Algeria 1-1 Russia Winner:Draw Germany 2-1 Algeria Winner:Germany Scores: Games Win Lose Draw GF GA 4 1 2 1 7 7 The highest achievement in 2014 World cup was round of 16
Please enter a country == England England All games: England 1-2 Italy Winner:Italy Uruguay 2-1 England Winner:Uruguay Costa Rica 0-0 England Winner:Draw Scores: Games Win Lose Draw GF GA 3 0 2 1 2 4 The highest achievement in 2014 World cup was group games
Please enter a country == Germany Germany All games: Germany 4-0 Portugal Winner:Germany Germany 2-2 Ghana Winner:Draw USA 0-1 Germany Winner:Germany Germany 2-1 Algeria Winner:Germany France 0-1 Germany Winner:Germany Brazil 1-7 Germany Winner:Germany Germany 1-0 Argentina Winner:Germany Scores: Games Win Lose Draw GF GA 7 6 0 1 18 4 The highest achievement in 2014 World cup was finals This country placed in the first place
Please enter a country == Turkey Turkey
 
All games: Scores: Games Win Lose Draw GF GA 0 0 0 0 0 0 The highest achievement in 2014 World cup was not making it to the tournament
Here are some hints to get you started. Clearly, you will need a loop to go through all the games one by one, and check whether the current team is actually the team we are searching for. One you find it, print it out. Test your code!
Now, add some variables to count the total number of wins, draws and losses. Remember the team name can appear as the first or the second team.
When you are done with all this, the final step is looking at the team standing. Keep track of the highest index in the list in which you find a game for this team. Using this index, you can figure out where this team has played.


Part 2: Olympic Medals


Your solution for this part must be called hw4_part2.py. Only submit this file.
You will use the input module called hw4_util.py to read a file containing medal standings of different countries. We have two files, one for 2010 and one for the current standings in 2014. For each country, you have medal counts for: gold, silver, bronze, total medals
If the country has won no medals, it is not in this list. Here is an example use:
import hw4_util medals2010 = hw4_util.read_medals('medals2010.txt') print medals2010
Write a program that:
• Reads the medal counts for 2010 and 2014 using this module.
• Asks the user for the name of a country
• Prints the percentage of all gold, silver, bronze and total medals the input country and United States got in 2010 and in 2014.
• If the input country has no medals, you print nothing.
• Format your output so that the country column is 14 characters long and all the other columns are 8 characters long. The first two columns are left justified as before, but the last 4 columns are right justified (use rjust).
Example output for different runs of this program shown below.
 
Enter the name of a country == Russia Russia Year Country Gold Silver Bronze Total ====================================================== 2010 Russia 3% 5% 8% 5% 2010 United States 10% 17% 15% 14%
2014 Russia 10% 11% 7% 9% 2014 United States 10% 8% 12% 10%
Enter the name of a country == Ukraine Ukraine Year Country Gold Silver Bronze Total ====================================================== 2010 United States 10% 17% 15% 14%
2014 Ukraine 0% 0% 1% 0% 2014 United States 10% 8% 12% 10%
As Ukraine won no medals in 2010, so the programs prints nothing for this country.
Here is a bit of help on how to organize your code. You do not have to use these suggestions, but if you do, your code will be considerably shorter and easier to debug.
• First find the medal totals in each of the four categories (gold, silver, bronze, total) for 2010 and 2014. Store them in a list e.g. totals2010= [0,0,0,0]. You need to go through each list of medals one by one using a loop.
• Write a function such as: print_percentage(year, medals, totals, country) which goes through a list of medals for an input country using a loop and if it finds the given country in the list, it prints the percentage information using the totals from the second list.
• Once you have all this in place, all you need to do is to call this function for the searched country and US for years 2010 and 2014. For example:
print_percentage('2010', medals2010, totals2010, country) print_percentage('2010', medals2010, totals2010, 'United States')
Part 3: A bit of repetition!
Your solution for this part must be called hw4_part3.py. You cannot use any modules for this part.
Ok, this final part is simple and challenging at the same time. It is only worth 10 points and builds on monday’s lecture on string slicing (October 6). My solution is 15 lines of code. We will replicate every horror movie cliche here.
 
Write a program that:
• Reads an input string
• Reads a line length
• Prints the input string on 10 lines up to the given line length, wrapping the remainder of the string from one line in the next line (spaces are not treated as special).
Here is an example run of the program.
Please enter a line == I know what you did last summer I know what you did last summer Please enter a line length == 60 60 I know what you did last summer I know what you did last sum mer I know what you did last summer I know what you did last summer I know what you did last summer I know what you did last summer I know what you did last summer I know what you did last summer I know what you did last summer I know what you did last summer I know what you did last summer I know w hat you did last summer I know what you did last summer I kn ow what you did last summer I know what you did last summer I know what you did last summer I know what you did last sum mer I know what you did last summer I know what you did last
 
Powered by