2011-07-02
Red Sox
7
Astros
5
Minute Maid Park
2011-07-03
Red Sox
2
Astros
1
Minute Maid Park
2011-07-04
Red Sox
7
Blue Jays
9
Fenway Park
2011-07-05
Red Sox
3
Blue Jays
2
Fenway Park
2011-07-06
Red Sox
6
Blue Jays
4
Fenway Park
2011-07-07
Red Sox
10
Orioles
4
Fenway Park
2011-07-08
Red Sox
10
Orioles
3
Fenway Park
2011-07-09
Red Sox
4
Orioles
0
Fenway Park
2011-07-10
Red Sox
8
Orioles
6
Fenway Park
2011-07-15
Red Sox
6
Rays
9
Tropicana Field
2011-07-16
Red Sox
9
Rays
5
Tropicana Field
2011-07-17
Red Sox
1
Rays
0
Tropicana Field
2011-07-18
Red Sox
15
Orioles
10
Camden Yards
2011-07-19
Red Sox
2
Orioles
6
Camden Yards
2011-07-20
Red Sox
4
Orioles
0
Camden Yards
2011-07-22
Red Sox
7
Mariners
4
Fenway Park
2011-07-23
Red Sox
3
Mariners
1
Fenway Park
2011-07-24
Red Sox
12
Mariners
8
Fenway Park
2011-07-25
Red Sox
1
Royals
3
Fenway Park
2011-07-26
Red Sox
13
Royals
9
Fenway Park
2011-07-27
Red Sox
12
Royals
5
Fenway Park
2011-07-28
Red Sox
3
Royals
4
Fenway Park
2011-07-29
Red Sox
1
White Sox
3
U.S. Cellular Field
2011-07-30
Red Sox
10
White Sox
2
U.S. Cellular Field
2011-07-31
Red Sox
5
White Sox
3
U.S. Cellular Field
The code should be written in python.
Task 1: Create a file using the data above called RedSox.txt where the record is in one line.
Task 2: Read the created file (RedSox.txt)
Task 3: Create two dimensional list where each element is a list of score of each team (total 8 teams) and name.
Example: [["RedSox",5,10,4,7,4] , ["Astros",5,1] , ["Orioles",4,3,0,6] ]
Task 4: Find Average, Min, Max for each team and print.
Example:
TeamName Avg Min Max
RedSox 6 4 10
Astros 3 1 5
Orioles 3.25 0 6
Task 5: Divide your task into functions.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
#code
#method to open RedSox.txt file, read and fill 2D list and
return it
def open_file():
#opening file, make sure file exists in the
same directory
#also make sure that the file does not contain any
blank lines
file=open('RedSox.txt')
#reading all lines to a list
lines=file.readlines()
#closing file
file.close()
#creatig an empty list
data=[]
#index for controlling loop
i=0
#loops until the end of file
while
i<len(lines):
#reading second line
from i as team1 name
team1=lines[i+1].strip()
# reading next line
from i as team1 score (integer)
score1=int(lines[i+2].strip())
# reading next line
from i as team2 name
team2 = lines[i +
3].strip()
# reading next line
from i as team2 score (integer)
score2 =
int(lines[i + 4].strip())
#boolean flags to
denote if the data is added to 2d list
team1_added=False
team2_added=False
#looping
through current elements in data array
for k in range(len(data)):
#checking if current team's name is team1
if data[k][0]==team1:
#adding current score to current team's scores
data[k].append(score1)
#setting corresponding flag to True
team1_added=True
#doing the same for team2
if data[k][0]==team2:
data[k].append(score2)
team2_added = True
#after the
loop if any team's data is not added, adding as new entry
#with name being first
value and score being second inside a list
if
not team1_added:
data.append([team1,score1])
if not
team2_added:
data.append([team2,score2])
i+=6 #moving to
fetch next record
return data #returning
data
#method to find the average of a team, specified by
index
def find_avg(data,index):
sum=0
#looping and summing scores of team at index
'index'
for i in
range(1,len(data[index])):
sum+=data[index][i]
#finding average, (subtracting 1 from data
length because 1 value on the
# list is name)
avg=sum/len(data[index])-1
return avg
#method to find the minimum score of a team, specified by
index
def find_min(data,index):
min=None #setting min to
None
#looping and finding min score
for i in
range(1,len(data[index])):
#checking if min is
not initialized or if current value is smaller than min
if
min==None or data[index][i]<min:
#updating min
min=data[index][i]
return min
#method to find the maximum score of a team, specified by
index
def find_max(data,index):
max=None
for i in
range(1,len(data[index])):
if
max==None or data[index][i]>max:
max=data[index][i]
return max
if __name__ == '__main__':
#opening file, creating 2d list
data=open_file()
#printing heading
print('{:<15s} {:<10s}
{:<10s}
{:<10s}'.format('TeamName','Avg','Min','Max'))
#looping through each record
for i in
range(len(data)):
#finding name,
average, min and max
name=data[i][0]
avg=find_avg(data,i)
min=find_min(data,i)
max=find_max(data,i)
#displaying
details
print('{:<15s} {:<10.2f} {:<10d}
{:<10d}'.format(name,avg,min,max))
#output
2011-07-02 Red Sox 7 Astros 5 Minute Maid Park 2011-07-03 Red Sox 2 Astros 1...
Here is the red_sox.txt file 2011-07-02 Red Sox @ Astros Win 7-5 2011-07-03 Red Sox @ Astros Win 2-1 2011-07-04 Red Sox vs Blue Jays Loss 7-9 2011-07-05 Red Sox vs Blue Jays Win 3-2 2011-07-06 Red Sox vs Blue Jays Win 6-4 2011-07-07 Red Sox vs Orioles Win 10-4 2011-07-08 Red Sox vs Orioles Win 10-3 2011-07-09 Red Sox vs Orioles Win 4-0 2011-07-10 Red Sox vs Orioles Win 8-6 2011-07-15 Red Sox @ Rays Loss 6-9 2011-07-16 Red...
Tampa Texas Toront 4-30 In 2012, the total payroll for the New York Yankees was almost $200 million, while the total payroll for the Oakland Athletics (a team known for using baseball analytics or sabermetrics) was about $55 million, less than one-third of the Yankees' payroll. In the following table, you will see the payrolls (in millions) and the total number of victories for the baseball teams in the American League in the 2012 season. Develop a regression model to...
3) American League baseball teams play their games with the designated hitter rule, meaning that pitchers do not bat. The league believes that replacing the pitcher, typically a weak hitter, with another player in the batting order produces more runs. Using a significance level of a = 0.05, determine if the average number of runs is higher for the American League Following are the average number of runs scored by each team in the 2016 season: American League National League...
Refer to the Lincolnville School District bus data. First, add a variable to change the type of engine (diesel or gasoline) to a qualitative variable. If the engine type is diesel, then set the qualitative variable to 0. If the engine type is gasoline, then set the qualitative variable to 1. Develop a regression equation using statistical software with maintenance cost as the dependent variable and age, odometer miles, miles since last maintenance, and engine type as the independent variables....