OverviewPart 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 les and this util le. You will start with reading the le 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 dierence is superior. The goal dierence is compuated as follows: subtract the number of goals against from the number of goals scored.
If the two teams have the same goal dierence, 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 rst 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 le named hw3_part1.py. Part 2: Legos (Everything is awesome!)
In celebration of everyones 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 specic piece. But, you can put together dierent lego pieces to make up bigger pieces too. 11 21 22 24 22 22 + = 24 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 22 pieces to make up a 24 piece. Write a program to read from a le 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 24 lego using: two 22 legos, or four 21 legos or eight 11 legos. Here are some sample outputs of your program: What type of lego do you need? == 24 24 How many pieces of this lego do you need? == 1 1 I have 1 pieces of 24 for this What type of lego do you need? == 21 21 How many pieces of this lego do you need? == 2 2 I have 2 pieces of 21 for this What type of lego do you need? == 21 twobyone How many pieces of this lego do you need? == 3 3 I have 6 pieces of 11 for this What type of lego do you need? == 21 21 How many pieces of this lego do you need? == 4 4 I dont have enough pieces of this lego To solve this problem, you will rst read from a le how many of each type of lego pieces you currently have, such as the one below: 11, 6 21, 2 22, 2 24, 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 le, you will get the list below. [11, 11, 11, 11, 11, 11, 21, 21, 22, 22, 24] A very easy way to solve this problem is to use the count() function of lists. For example, given the above list, legos.count(11) 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 rst. 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 21 2 11 22 2 21 4 11 24 2 22 4 21 8 11 Note that we only gave you one test le. But, you must create other test les to make sure that the program works for all possible cases. Feel free to share your test les on Piazza and test cases. Discussing test cases on Piazza are a good way to understand the problem. We will test your code with dierent input les 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.
Reviews
There are no reviews yet.