Lists and If statements solution

Overview
Part 1: FIFA world cup, and its crazy rules

In this homework, we will be making use of a module called hw3_util.py that provides a function for this part of the homework. To get started with this part, unzip the folder given to you containing the input files and this util file. You will start with reading the file containing information about teams in FIFA world cup, the group round. Try the following: import hw3_util teams = hw3_util.read_fifa() print teams[0] The function called read_fifa() will return you a list of teams. Each item in the list is information for a team given in a list containing the following information: [group id, country, games, win, draw, lose, goals scored, goals against] Write a program to compare any two teams from this list and to print which team is superior. Here are the rules:
• The team with the highest points is superior. Points are calculated as follows: each win is 3 points and each draw is 1 points.

• If the points are the same, then the team with the higher goal difference is superior. The goal difference is compuated as follows: subtract the number of goals against from the number of goals scored.

• If the two teams have the same goal difference, then the team with the higher number of goals scored is superior.

• If the two teams are the same with respect to all of the above rules, we will assume that they are the same. In this case, print that the two teams are the same. FIFA has even rules for breaking the ties for that. But, we will stop here. Your program must ask the index of any two teams from the list. Assume a valid index is given. Then, it will first print the full information corresponding to these teams. The format is given below (the country column is 20 characters long, all other columns are 6 characters long). You will then compare the two teams corresponding to the input indices, and print which team is greater. Here are some possible runs of the program: First team index (0-31) == 2 2 Second team index (0-31) == 3 3 Group Team Win Draw Lose GF GA Gdiff Pts 1 Croatia 1 0 2 6 6 0 3 1 Mexico 2 1 0 4 1 3 7 Mexico is better than Croatia First team index (0-31) == 0 0 Second team index (0-31) == 3 3 Group Team Win Draw Lose GF GA Gdiff Pts 1 Brazil 2 1 0 7 2 5 7 1 Mexico 2 1 0 4 1 3 7 Brazil is better than Mexico First team index (0-31) == 10 10 Second team index (0-31) == 14 14 Group Team Win Draw Lose GF GA Gdiff Pts 3 Ivory Coast 1 0 2 4 5 -1 3 4 Italy 1 0 2 2 3 -1 3 Ivory Coast is better than Italy Remember to print any value we read to make the output appear correctly at the submission server. To match the formatting, you can use the padding function ljust() that will pad the input string with spaces on the right up to the given length. x = "abc" x.ljust(6) 'abc ' Once you have tested your program, turn in only your code in a file named hw3_part1.py. Part 2: Legos (Everything is awesome!)

In celebration of everyone’s childhood, we have a lego problem in this homework. We will solve a simple problem in this part. But, then we will revisit the problem and solve a harder version in a future homework. Suppose you are given a list of lego pieces that you own, but you have a new project. You want to see if you have enough of a specific piece. But, you can put together different lego pieces to make up bigger pieces too. 1x1 2x1 2x2 2x4 2x2 2x2 + = 2x4 Figure 1: All the possible lego pieces for this homework are shown on the top row. The name explains the dimensions of the lego. The bottom row shows how you can combine two 2x2 pieces to make up a 2x4 piece. Write a program to read from a file the list of all lego pieces you currently have. Then, ask the user for the type of lego piece and the number of such pieces that she is searching for. Then, return whether you can make that many pieces of legos by using the legos in your collection. You will only consider methods in which one type of lego is repeated. For example, you can make up a 2x4 lego using: two 2x2 legos, or four 2x1 legos or eight 1x1 legos. Here are some sample outputs of your program: What type of lego do you need? == 2x4 2x4 How many pieces of this lego do you need? == 1 1 I have 1 pieces of 2x4 for this What type of lego do you need? == 2x1 2x1 How many pieces of this lego do you need? == 2 2 I have 2 pieces of 2x1 for this What type of lego do you need? == 2x1 twobyone How many pieces of this lego do you need? == 3 3 I have 6 pieces of 1x1 for this What type of lego do you need? == 2x1 2x1 How many pieces of this lego do you need? == 4 4 I don't have enough pieces of this lego To solve this problem, you will first read from a file how many of each type of lego pieces you currently have, such as the one below: 1x1, 6 2x1, 2 2x2, 2 2x4, 1 using the function provided in hw3_util as follows: import hw3_util legos = hw3_util.read_legos('legos.txt') print legos If you execute this program with the above file, you will get the list below. ['1x1', '1x1', '1x1', '1x1', '1x1', '1x1', '2x1', '2x1', '2x2', '2x2', '2x4'] A very easy way to solve this problem is to use the count() function of lists. For example, given the above list, legos.count('1x1') returns 4. You need to write the if statements to check for each lego, whether a substitute exists. Always try to using the biggest possible lego first. It should be obvious how you can put smaller pieces together to make up bigger pieces, but we provide possible subtitutions here for completeness. We only use one type of lego for any request, no mix and match. Piece Possible replacement 2x1 2 1x1 2x2 2 2x1 4 1x1 2x4 2 2x2 4 2x1 8 1x1 Note that we only gave you one test file. But, you must create other test files to make sure that the program works for all possible cases. Feel free to share your test files on Piazza and test cases. Discussing test cases on Piazza are a good way to understand the problem. We will test your code with different input files than the one we gave you in the submission server. So, be ready to be tested thoroughly! Once you are done, turn in only your code hw3_part2.py.
Powered by