homework for cs python, Project ObjectivesIntroduce you to the implementation of simulation codes following a Top-Down design.Reinforce your knowledge in importing different libraries (pre-installed or from a provided python file) and use functions defined in them.Practice decision and loop structures, functions, lists, and the use of the library matplotlib.Learn how to use Unit Tests to verify the correctness of your project subparts.What is a Unit Test?Unit Testing is a practice of independently testing parts of your programs such as functions. It gives programmers the ability to verify that functions work as expected. That is to say, for any given function and a given set of inputs, a Unit Test validates whether or not the function is returning the proper values.In this project, you will be given Unit Tests to help you validate the functionality of your program as you progress. The test functions code are provided to you in the file testCases.py. This file contains the definitions of all the function that test each of the functions you need to implement. Details about how to use this file are provided in a later section.Be aware that you can test your functions using the method followed in Project 2. Namely, you can import your code (project3.py) to IDLE and use the provided test case codes to test from it.Please notice that the provided skeleton include the definition of the functions with a dumb print in each. This print must be removed before you start coding. The reason of providing you a dumb print statement in each function is to avoid errors when you import your code (project3.py) to either IDLE ortestCases.py. Python throw an error when a function that is defined without any instruction in it is imported.Setting up the environmentGo to your working directory in data.cs.purdue.edu and create the directory cs177/project3. Refer to the lab01 if you need to remember the steps to do so. Make sure that you import the following libraries:matplotlib, sorter, tabulate. The last two are provided to you in the package below.Your project package is here. Download this package, and start working on the file called project3.pyThis package contain the following files:groupInfo.txt: File with information about the groups of teams of the world cup.round0Results.txt: File containing the results of round 0 stage matches. This is required to determine the winners in round 0.breaker.txt: File required to determine the winners in round 1, 2 and 3.sorter.py: Library containing the function that sorts tables.tabulate.py: Library used in main to achieve a better look of the printed tables.project3.py: Skeleton of the project. Start working here!!!testCases.py: File that allows you to test each of the 9 functions you must implement in this project. Also the file includes test cases to test the function sortTable in sorter.py. You are not required to implement the function sortTable in this project, you just need to use it. The cases for this function are intended to illustrate the functionality of the function so that you can become familiar with it.PROJECT DESCRIPTION This cup will be played by 16 countries grouped into four groups: A, B C and D as it is depicted above.Tournament Rules:There will be 4 rounds of elimination starting from round 0. Round 4 is the final round of the tournament. In each round a number of games (hereafter, the terms game and match are used interchangeably) are played and the results of those games determine the matches to be played in the next round. The selection process for the next rounds is explained as follows. Please refer to the image below to have a better understanding of the tournament rules.Round 0: In round 0 every team will play one game with each of the other members of its group. A team accumulate points depending on its number of wins, draws and looses. Only the best two teams of the group qualify for the round 1. Between the two best teams of a group there will always be a winner (the best team of the group) and a runner-up (the second best team of the group). In round 0 the best two teams of a group are crossed with the best two teams of another group. Specifically, the winner of group A is matched with the runner-up of group B, and the winner of group B is matched with the runner-up of group A to play in round 1. Similarly, the winner of group C is matched with the runner-up of group D, and the winner of group D is matched with the runner-up of group C to play in round 1.Round 1: In round 1 there will only be 4 games: (1) Winner of group A vs Runner-up of group B, (2) Winner of group B vs Runner-up of group A, (3) Winner of group C vs Runner-up of group D, and (4) Winner of group D vs Runner-up of group C. Winners of the games qualify for round 2 and thus forwarded. Losers are eliminated. In this occasion groups are not crossed as happened in round 0. The winner of game (1) is matched with the winner of game (2) to play in round 2, and thus the winner of the groups A and B is determined. Likewise, the winner of game (3) is matched with the winner of game (4) to play in next round and the winner of the groups C and D is determined.Round 2: This round is the semifinal of this world cup tournament. In this round, there will be only 2 games. One is to determine the winner of the groups A and B, and another is to determine the winner of the groups C and D. The winner of groups A and B qualifies for (i.e., passes to) the final. In a similar manner, the winner of groups C and D make its way to the final. The losers of the games in round 2 fight for the third place deciding game in the next round.Round 3: This is the final of the tournament. In this stage, there will be 2 games. One for the championship and another for the third place. Since it is the last round no more matching need to be done for the next round. Statistics: Obtaining the final results of the tournament, you are required to plot some statistics of the results of round 0 about the champion, sub-champion (i.e., the runner-up) and third-place winning teams to see whether the performance of these teams in round 0 reflects the results of the final.Point Rules:As it was mentioned, in round 0 every team plays one game with each of the other members of its group. In every game each team accumulate points according to the following rules:Wins: 3 ptsDraws: 1 ptsLosses: 0 ptsExpected output at the completion of the project:In this section we provide you with a sample output of the project after completion. The idea is to give you a clear understanding of your goals here. We will walk through the tasks you need to complete in each round and present the results you are expected to have at the end of each of them. Also, we go over the tasks you need to complete after finishing all the rounds. The results presented here and throughout this document were obtained with the files groupInfo.txt, round0Results.txt, and breaker.txt provided to you.Round 0: The results of the matches of round 0 will be provided to you in a randomly generated file called round0Results.txt. The information about the groups A, B, C, and D will be also provided to you in the file groupInfo.txt. In round 0 you are required to parse these files and create a rank table for each of the groups. The rank tables will allow you to pick the best two teams of each group and then cross them to get the two crossed tables to be used in round 1. Let us see an example of the expected output after concluding round 0:>>> ================================ RESTART ================================
>>>
***************************************************************
* Rank Tables after ROUND 0 *
***************************************************************
Rank table for groupA
Team Point Rank
-
France 7 1
Japan 4 2
Colombia 3 3
Mexico 3 4
Rank table for groupB
Team Point Rank
-
Argentina 9 1
India 6 2
Saudi Arabia 3 3
USA 0 4
Rank table for groupC
Team Point Rank
-
Germany 7 1
Netherlands 6 2
Panama 3 3
Bangladesh 1 4
Rank table for groupD
Team Point Rank
- -
England 9 1
Italy 6 2
Brazil 1 3
Spain 1 4
Matches for next round resulting of crossing group A and B
Team1 Team2
-
France India
Argentina Japan
Matches for next round resulting of crossing group C and D
Team1 Team2
-
Germany Italy
England NetherlandsIn round 0 your task is to complete the functions that create the rank tables and the functions that create the crossed tables. The rank table has to be created in such a way that all the teams in a group are assigned an unique ranking. Mechanisms to break the ties are also required to be implemented and they are part of your tasks in this project. You will see more details about this in the section YOUR TASKSpresented later in this document.Round 1: For this round you need to determine the winner of the crossed tables. You are expected to write a function able to determine the winner between two teams. This function will use the filebreaker.txt. This file contains a match between each two of the 16 teams. The results of these matches are guaranteed to determine a winner and a loser. Note that the breaker.txt file does not contain a matchs result with draw. Once the winners of round 1 are determined, the match tables for round 2 are created. Let us see an example of the expected output after concluding round 1:***************************************************************
* ROUND 1 *
***************************************************************
Results of the matches of the crossed groups A and B
Team1 Team2 Winner
-
France India India
Argentina Japan Argentina
Results of the matches of the crossed groups C and D
Team1 Team2 Winner
-
Germany Italy Germany
England Netherlands England
Match tables of groups A and B for semi-finals
Team1 Team2
-
India Argentina
Match tables of groups C and D for semi-finals
Team1 Team2
- -
Germany EnglandRound 2: Here, we need to determine the winner of the match tables created in the previous round. You will reuse the same function written in the previous round. Again, this function works with the filebreaker.txt to determine which teams win the matches. An example of the expected output after round 2 is as follows:***************************************************************
* SEMI FINALS *
***************************************************************
Results of the matches of groups A and B in semi-finals
Team1 Team2 Winner
-
India Argentina India
Results of the matches of groups C and D in semi-finals
Team1 Team2 Winner
- -
Germany England England
Match table for championship
Team1 Team2
- -
India England
Match table for third place
Team1 Team2
-
Argentina GermanyRound 3: Here we determine the winner of the match tables created in the previous round to determine the champion, sub-champion (i.e., the runner-up) and third-place winning teams of the world cup. How? Reusing previously created functions and the breaker.txt file. An example of the output after the final round:***************************************************************
* FINALS *
***************************************************************
Results of the championship match
Team1 Team2 Winner
- -
India England England
esults of the third place match
Team1 Team2 Winner
-
Argentina Germany Argentina
Finals results:
Champion Sub-Champion Third Place
- -
England India ArgentinaStatistics: You are required to plot a bar chart of the number of goals for and against in round 0 for the champion, sub-champion and third-place winning teams. For this your function must use the fileround0Results.txt. Using the provided files you are expected to plot a bar chart that looks like below:***************************************************************
* Statistics *
***************************************************************
Do the results of round 0 coincide with the forecast?
Lets compare the scores of the champion, sub-champion and
third-place IMPLEMENTATION DETAILSTop-Down Design:In this section we will provide you with some hints and details about how to implement your solution. We will do so walking through the tasks you need to complete in each round and the required tasks after the completion of all the rounds. Before continuing we urge you to carefully study the Top-Down Design presented below. The functions main and sortTable will be provided to you. YOU DO NOT HAVE TO CHANGE THEM. You will be in charge of writing the remaining 9 functions. We recommend you to visit this link to learn know to use sortTable. The link includes the output of the test cases provided in testCases.py for this function.Be aware that some functions depend on others. We call these functions dependent functions. Also, there are functions that do not depend on any other function. This functions are independent functions. We highly recommend you to write independent functions first since dependent functions cannot be tested without the completion of the functions they depend on.As an example, the function buildRankTable cannot be tested without completing first the functionsreadGroupInfo, readResultsRound0, buildPointsTable, and addRankToPointsTable. In this casebuildRankTable is a dependent function and readGroupInfo, for example, is an independent function. Notice that addRankToPointsTable is a dependent function that depends on the implementation of breakTies.List of functions (tasks) to be completed:Below we list the functions you need to complete in the project divided by rounds. Because of the dependency of some functions on others we recommend you to implement your functions in the order provided below. Notice that this order corresponds to the order of the 9 tasks listed in the next section and the order of functions in the file project3.py.readGroupInfo (round0)readResultsRound0 (round0)buildPointsTable (round0)bre
akTies (round0)addRankToPointsTable (round0)buildRankTable (round0)buildCrossedTable (round0)determineWinner (round 1, 2, and 3)plotGoalsBarChar (After completion of rounds)As you can see round 0 requires the highest load of works since 7 functions must been competed in it. Rounds 1, 2 and 3 requires only one function. After the winners of the world cup (Champion, sub-Champion, and Third-Place) are determined, a bar chart is plotted.How to test your functions:In order to test your code you will use the file testClasses.py. You only need to uncomment the task you want to test one at a time in the function main. The content of this file is as follows:##########################################################
# Required Imports #
##########################################################
from project3 import buildRankTable
from project3 import readGroupInfo
from project3 import readResultsRound0
from project3 import buildPointsTable
from project3 import addRankToPointsTable
from project3 import breakTies
from project3 import buildCrossedTable
from project3 import determineWinner
from project3 import plotGoalsBarChar
from sorter import sortTable
##########################################################
# Test cases for Task 1 Task 9 #
##########################################################
def testTask1():
teams = readGroupInfo(groupA)
for t in teams:
print (t)
def testTask2():
teams = [Colombia, Japan, Mexico, France]
results = readResultsRound0(teams)
for r in results:
print (r)
def testTask3():
teams = [Colombia, Japan, Mexico, France]
results = [Mexico, France, 4, 6,
Japan, Mexico, 8, 2,
Colombia, Mexico, 3, 9,
Colombia, Japan, 9, 7,
Colombia, France, 2, 8,
Japan, France, 4, 4]
pointsTable = buildPointsTable(teams, results)
for p in pointsTable:
print (p)
def testTask4():
rankTable = [[France, 7, 1],
[Japan, 4, 2],
[Colombia, 3, 3],
[Mexico, 3, 4]]
rankTable = breakTies(rankTable)
for r in rankTable:
print (r)
def testTask5():
pointsTable = [[Colombia, 3],
[Japan, 4],
[Mexico, 3],
[France, 7]]
rankTable = addRankToPointsTable(pointsTable)
for r in rankTable:
print (r)
def testTask6():
group = groupA
rankTableA = buildRankTable(group)
print(rankTableA)
def testTask7():
rankTableA = [[Japan, 6, 1], [Colombia, 4, 2],
[Mexico, 4, 3], [France, 3, 4]]
rankTableB = [[USA, 9, 1], [Saudi Arabia, 4, 2],
[India, 3, 3], [Argentina, 1, 4]]
crossedTableAB = buildCrossedTable(rankTableA, rankTableB)
print(crossedTableAB)
def testTask8():
teams = [Panama, India]
winner = determineWinner(teams)
print(winner)
def testTask9():
finalResults = [Brazil, Germany, Italy]
plotGoalsBarChar(finalResults)
##########################################################
# Test cases for Sort Table (Optional) #
##########################################################
def test1SortTable():
rankTable = [[Brazil, 6, 1],
[Bangladesh, 6, 1],
[Panama, 6, 1],
[Argentina, 0, 4]]
rankTable = sortTable(rankTable, 0, 0, 2, ascending)
for r in rankTable:
print (r)
def test2SortTable():
pointsTable = [[Bangladesh, 1],
[Brazil, 4],
[Panama, 6],
[Argentina, 4]]
pointsTable = sortTable(pointsTable, 1, 0, len(pointsTable)-1, descending)
for p in pointsTable:
print (p)
##########################################################
# The main function contains the call to all the tests #
# Uncomment the desired test one at a time #
##########################################################
def main():
# Test for tasks
#testTask1()
#testTask2()
#testTask3()
#testTask4()
#testTask5()
#testTask6()
#testTask7()
#testTask8()
#testTask9()
# Test for sort table. Just for you to get familiar with the function
#test1SortTable()
#test2SortTable()
main()As you can see the function main of this file contains a call to the test case of each function. You just need to uncomment in main for the desired test one at a time. In the next section, YOUR TASKS, we will present per task the piece of code of testCases.py that test each function and its corresponding result. Feel free to test your code from IDLE using this information.Be aware that the definition of functions provided to you in project3.py includes a print statement you need to remove before start coding your solution. The purpose of that print statement is preventing errors when you test the functions independently. Since testCases.py imports each of the functions in project3.py, functions inproject3.py need to be complete with at least one instuction to avoid errors.YOUR TASKS:Task 0: Set your environment using the content of the project 3 packageThis task gives you instructions about how to set up your environment using some of the files provided to you in project 3 package, and how to submit your final file.Before you start coding your solution in project3.py you need to procceed as follows:Import matplotlibImport function tabulate provided to you in tabulate.pyImport function sortTable provided to you in sorter.pyMake sure you DO NOT call the function main a the end of the file project3.py while you are implementing your solution (that is, the instruction main() must not be present in your file). You must implement your functions without having that instruction at the end of the file to avoid errors when you use testCases.py to test your functions.After you finish coding your functions:Call the function main (that is, write instruction main()) at the end of the file project3.py ONLY when you have completed all the functions. Run your solution. The expected output must be the one showed in the section Project Description above.After testing your solution REMOVE the call to main at the end of the file project3.py and submit your solution without that instruction.Task 1: Write the function that reads information of the groups from the file groupInfo.txtIn this task you will implement the function readGroupInfo(groupName).This function reads groupInfo.txt file and finds the teams that belong to groupName given as parameter to the function. groupName is of string datatype. The four possible values of groupName are:groupA, groupB, groupC, groupD. You must not use other values for goupName.readGroupInfo function returns a list of team names that belong to the given groupName. E.g., teams = [ Colombia, Japan, Mexico, France] if the value of groupName is groupA.Unit Test from file testCases.py: testTask1() is a unit test for function readGroupInfo.def testTask1():
teams = readGroupInfo(groupA)
for t in teams:
print (t)Sample Output: Expected output of testTask1() is following:>>> ================================ RESTART ================================
>>>
Colombia
Japan
Mexico
France
>>> Task 2: Write the function that reads the results of round 0 from the file round0Results.txtIn this task you will implement the function readResultsRound0(teams).The function readResultsRound0 receives teams, a list of strings as parameters. E.g., teams = [Brazil, Netherlands, Panama, Italy]. This function reads round0results.txt file and finds the matches of the teams given in the list teams.It returns a list of strings containing the results of each team that belongs to list teams. The returned list should not contain any duplicate items. For example, the returned results is like following:results = [Brazil, Netherlands, 4, 1,
Panama, Netherlanders, 0, 4,
Italy, Brazil, 7, 6,
Italy, Panama, 2, 5,
Brazil, Panama, 4, 3,
Italy, Netherlands, 8, 7]In general, The format of the return list is as follows: [,,,,
,,,,
,,,,
,,,,
,,,,
,,,]The format of each string is: ,,,, where:: Team i of the group: Team j of the group: Score of Team i in the match: Score of Team j in the matchUnit Test from file testCases.py: testTask2() is a unit test for function readResultRound0.def testTask2():
teams = [Colombia, Japan, Mexico, France]
results = readResultsRound0(teams)
for r in results:
print (r)Sample Output: Expected output of testTask2() is following:>>> ================================ RESTART ================================
>>>
Mexico, France, 4, 6
Japan, Mexico, 8, 2
Colombia, Mexico, 3, 9
Colombia, Japan, 9, 7
Colombia, France, 2, 8
Japan, France, 4, 4
>>> Task 3: Write the function that builds the Points TableIn this task you will implement the function buildPointsTable(teams, results).The function buildPointsTable(teams, results) creates a table of points for each team in a specific group. It receives two lists:teams: list of strings. E.g., [England, Spain, Germany, Bangladesh]results: list of strings, where each string represents a match result between two teams of the same group. The format of the list is as follows: [,,,,
,,,,
,,,,
,,,,
,,,,
,,,]The format of each string is: ,,,, where:: Team i of the group: Team j of the group: Score of Team i in the match: Score of Team j in the matchNotice that each groups contains 4 teams. The function produces a table of the following format:This table is represented as a list of lists. Every inner list contains the pair listed above, where is a string data type, and an integer data type. For example:pointsTable:[ [England, 5], [Spain, 2], [Germany, 5], [Bangladesh, 4] ]Datatypes of the pointsTable is following: [ [str, int], [str, int]
[str, int] ].Please make sure the order of the teams in the pointsTable will be the same as the order of the team name in the teams list.The points of each group is calculated by adding the points obtained in each following the rules :Win (3 pts)Draw (1 pts)Loss (0 pts)Finally, buildPointsTable function returns the pointsTable.Unit Test from file testCases.py: testTask3() is a unit test for function buildPointsTabledef testTask3():
teams = [Colombia, Japan, Mexico, France]
results = [ Mexico, France, 4, 6,
Japan, Mexico, 8, 2,
Colombia, Mexico, 3, 9,
Colombia, Japan, 9, 7,
Colombia, France, 2, 8,
Japan, France, 4, 4 ]
pointsTable = buildPointsTable(teams, results)
for p in pointsTable:
print(p) Sample Output: Expected output of testTask3() is following:>>> ================================ RESTART ================================
>>>
[Colombia, 3]
[Japan, 4]
[Mexico, 3]
[France, 7]
>>> Task 4: Write the function that breaks ties in ranking values of the Rank TableIn this task, you need to write the function breakTies(rankTable). We recommend you to understandaddRankToPointsTable in task 5 since breakTies(rankTable) is called from that function.The function breakTies(rankTable) breaks the ties among teams with equal ranks within a group. It receives a rankTable that may include teams with equal rank, i.e., a tie.The format of the received rankTable is as follows (see Task 5 for better understanding):This table is represented as a list of lists. Every inner list contains the tuple listed above, where is a string data type, an integer data type, and is an integer datatype. For example:received rankTable: with ties, odered by points[ [Brazil, 5, 1], [Argentina, 5, 1], [Italy, 4, 3], [Spain, 2, 4] ]This table, as you can appreciate, is sorted by points (again see Task 5 for better understanding). Now we need to break the ties among teams of equal rank. The resulting rankTable without ties, which is returned, should be:rankTable to return: without ties, ordered by points and names[[Argentina, 5, 1], [Brazil, 5, 2], [Italy, 4, 3], [Spain, 2, 4] ]In order to complete this task, you may follow the algoithm specified below (this is optional, you can come up with your own algorithm). For a better understanding we will start our explanation with the rankTable received as parameter specified above.1. Teams with equal ranks must be sorted by names (i.e., alphabetically ascending order). For this, use the function sortTable to sort the rankTable with respect to column 0 (i.e., team names) indicating the first (startRow) and last (endRow) rows with equal ranks. Specifically you need to iterate each row of the rankTable:Find the startRow and endRow (both inclusive) among which ranks are equal. That is, the first and last row with equal rank.Use sortTable to sort with respect to column starting from startRow to endRow in the ascending order.In our example the startRow is 0 and the endRow row would be 1. The resulting sorted rankTabe would be:[[Argentina, 5, 1], [Brazil, 5, 1], [Italy, 4, 3], [Spain, 2, 4] ]2. You need make sure that all teams involved in a tie are ordered by names. At that point you will have rankTable sorted with respect to both points (received as parameter already sorted by points) and names. Then, you may update the rank of each team with the corresponding row-index of the rankTable. How? Just assigning/updating the rank values from 1 to 4 starting in row 0.In our example after following this procedure, the resulting rankTable would be:[[Argentina, 5, 1], [Brazil, 5, 2], [Italy, 4, 3], [Spain, 2, 4] ]3. Return the rankTable (witout ties and sorted by points and names) obtained in point 2.Unit Test from file testCases.py: testTask4() is a unit test for function breakTies.def testTask4():
rankTable = [[Bangladesh, 6, 1],
[Brazil, 6, 1],
[Panama, 6, 1],
[Argentina, 0, 4]]
rankTable = breakTies(rankTable)
for r in rankTable:
print (r)Sample Output: Expected output of testTask4() is following:[Bangladesh, 6, 1]
[Brazil, 6, 2]
[Panama, 6, 3]
[Argentina, 0, 4]Task 5: Write the function that adds ranks to the Points TableIn this task, you need to write the fun