Python Assignment

profilertcfbt007
assignment_2.zip

Assignment 2.pdf

Assignment 2 – CSCI/Python

Overview

In the ZIP folder associated with this homework, you will find hw2_util.py, team_scores.txt, and legos.txt. The first is a

module that is written to help you read information from files. You should be able to write this module yourself soon,

but for now, you can simply use it.

The other files are text files that you will use in different parts of this homework.

The goal of this assignment is to work with lists and use if statements. As your programs get longer, you will need to

develop some strategies for testing your program. Start early, test small parts of your program by writing a little bit and

testing. We will walk you through program construction in the homework description and provide some ideas for

testing.

As always, make sure you follow the program structure guidelines. You will be graded on program correctness as well as

good program structure.

Part 1:

FIFA world cup, and its crazy rules In this homework, we will be making use of a module called wrldcup_util.py that

provides a function for this part of the homework. To get started with this part, unzip the folder containing the input

files and this util file. You will start by reading the file containing information about teams in the FIFA world cup, the

group round. Try the following: import hw2_util teams = hw2_util.read_fifa() print teams[0] The function called

read_fifa() returns 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 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

point.

• If the points are the same, then the team with the higher goal difference is superior. The goal difference is computed

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 rules for breaking ties to this point, but we will stop here.

Your program must ask the index of any two teams from the list. Assume a valid index is given. Next, it will 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 for

grading! 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 hw2_part1.py.

Part 2:

Legos (Everything is Awesome!)

In celebration of everything being awesome, we have a Lego problem in this homework. We will solve a simple problem

in this part. But we will revisit the problem and solve a more difficult 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 determine if you have

enough of a specific piece. If you don't have a specific piece, you can put together different Lego pieces to make up

bigger pieces, too.

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. Next, return whether you can make that many Lego pieces

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, 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 piece 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 2x1 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 piece you

currently have. The file will be formatted as follows: 1x1, 6 2x1, 2 2x2, 2 2x4, 1 You can use the function provided in

hw2_util as follows: import hw2_util legos = hw2_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 will 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

substitutions 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. Create other test files to make sure that the program works for all possible

cases. Feel free to share your test files with each other!

We will test your code with various input files, so be ready for your code to be tested thoroughly! Once you are done,

turn in only your code hw2_part2.py.

legos.txt

1x1, 6 2x1, 2 2x2, 2 2x4, 1

team_scores.txt

1 Brazil 3 2 1 0 7 2
1 Cameroon 3 0 0 3 1 9
1 Croatia 3 1 0 2 6 6
1 Mexico 3 2 1 0 4 1
2 Australia 3 0 0 3 3 9
2 Chile 3 2 0 1 5 3
2 Netherlands 3 3 0 0 10 3
2 Spain 3 1 0 2 4 7
3 Colombia 3 3 0 0 9 2
3 Greece 3 1 1 1 2 4
3 Ivory Coast 3 1 0 2 4 5
3 Japan 3 0 1 2 2 6
4 Costa Rica 3 2 1 0 4 1
4 England 3 0 1 2 2 4
4 Italy 3 1 0 2 2 3
4 Uruguay 3 2 0 1 4 4
5 Ecuador 3 1 1 1 3 3
5 France 3 2 1 0 8 2
5 Honduras 3 0 0 3 1 8
5 Switzerland 3 2 0 1 7 6
6 Argentina 3 3 0 0 6 3
6 Bosnia and Herzegovina 3 1 0 2 4 4
6 Iran 3 0 1 2 1 4
6 Nigeria 3 1 1 1 3 3
7 Germany 3 2 1 0 7 2
7 Ghana 3 0 1 2 4 6
7 Portugal 3 1 1 1 4 7
7 USA 3 1 1 1 4 4
8 Algeria 3 1 1 1 6 5
8 Belgium 3 3 0 0 4 1
8 Korea Republic 3 0 1 2 3 6
8 Russia 3 0 2 1 2 3

wrldcup_util.py

""" Utility functions for Homework #2 To use this module, first import it import hw2_util """ def read_fifa(): """ Reads the file containing team scores, and returns a list. Each item in the list is a list containing: [group id, team country, games_played, win, draw, loose, goals for, goals against] """ teams = [] for line in open("team_scores.txt"): m = line.strip().split(",") for i in range(len(m)): if i != 1: m[i] = int(m[i]) teams.append(m) return teams def read_legos(filename): """This function is to be used for part2 of the homework. Read a file containing one lego type per line, and each line containing the type of lego and the number, separated by a comma. It returns a list for all the legos read from the file. Call this function as: mylegos = hw2_util.read_legos(filename) where mylegos is a list of all your legos. For example, if you are given the following file contents: 1x1, 6 2x1, 2 2x2, 2 2x4, 1 The above call will return the following list: ['1x1', '1x1', '1x1', '1x1', '1x1', '1x1', '2x1', '2x1', '2x2', '2x2', '2x4'] """ all_legos = [] for line in open(filename): line = line.strip("\n") lego_info = line.split(",") lego_type = lego_info[0].strip() lego_count = int(lego_info[1]) for i in range(lego_count): all_legos.append(lego_type) return all_legos if __name__ == "__main__": teams = read_fifa() for team in teams: print team legos = read_legos() print legos