Question

Python A text file (WorldSeriesWinners.txt ) contains a chronological list of the World Series winning teams...

Python

A text file (WorldSeriesWinners.txt ) contains a chronological list of the World Series winning teams from 1903 through 2009. The first line in the file is the name of the team that won in 1903, and the last line is the name of the team that won in 2009. Note the World Series was not played in 1904 and 1994.

Write a program to open the file WorldSeriesWinners.txt, read it line by line and store the team names in a dictionary. Specifically, create a dictionary in which keys are the team names (e.g., Chicago White Sox’) and values are a list of the wining years (e.g., [1906, 1917, 2005]). The year information is not available in the text file but you should be able to create it based on the year information given above. Don’t hardcode the values, but compute them on the fly.

For instance, an entry for Chicago White Sox in the dictionary would look like this:

‘Chicago White Sox’ : [1906, 1917, 2005]

Display the teams in the alphabetical order with years in brackets. For instance,

Team-A: [1903, 1920, 1990]

Team-B: [1910, 1923, 1982, 1991]

….

Note that the above display this is just a template and numbers in the brackets don’t correspond to the actual information.

Create another dictionary in which keys are the team names (e.g., Chicago White Sox’) and values are the occurrence of win (e.g., 3 for Chicago White Sox’). Display the wins per team . For instance,

Teams : Total Wins

Team-A: 3

Team-B: 4

Note that this is just a template and numbers don’t correspond to the actual information.

Sort the data by the number of wins and create a bar graph of the data as shown below . Each star represents one win.

New York Yankee (27): **************************

Team-XYZ (20):                ********************

Team-ABC (10):               **********

Chicago White Sox (3)    ***

File:

Boston Americans
New York Giants
Chicago White Sox
Chicago Cubs
Chicago Cubs
Pittsburgh Pirates
Philadelphia Athletics
Philadelphia Athletics
Boston Red Sox
Philadelphia Athletics
Boston Braves
Boston Red Sox
Boston Red Sox
Chicago White Sox
Boston Red Sox
Cincinnati Reds
Cleveland Indians
New York Giants
New York Giants
New York Yankees
Washington Senators
Pittsburgh Pirates
St. Louis Cardinals
New York Yankees
New York Yankees
Philadelphia Athletics
Philadelphia Athletics
St. Louis Cardinals
New York Yankees
New York Giants
St. Louis Cardinals
Detroit Tigers
New York Yankees
New York Yankees
New York Yankees
New York Yankees
Cincinnati Reds
New York Yankees
St. Louis Cardinals
New York Yankees
St. Louis Cardinals
Detroit Tigers
St. Louis Cardinals
New York Yankees
Cleveland Indians
New York Yankees
New York Yankees
New York Yankees
New York Yankees
New York Yankees
New York Giants
Brooklyn Dodgers
New York Yankees
Milwaukee Braves
New York Yankees
Los Angeles Dodgers
Pittsburgh Pirates
New York Yankees
New York Yankees
Los Angeles Dodgers
St. Louis Cardinals
Los Angeles Dodgers
Baltimore Orioles
St. Louis Cardinals
Detroit Tigers
New York Mets
Baltimore Orioles
Pittsburgh Pirates
Oakland Athletics
Oakland Athletics
Oakland Athletics
Cincinnati Reds
Cincinnati Reds
New York Yankees
New York Yankees
Pittsburgh Pirates
Philadelphia Phillies
Los Angeles Dodgers
St. Louis Cardinals
Baltimore Orioles
Detroit Tigers
Kansas City Royals
New York Mets
Minnesota Twins
Los Angeles Dodgers
Oakland Athletics
Cincinnati Reds
Minnesota Twins
Toronto Blue Jays
Toronto Blue Jays
Atlanta Braves
New York Yankees
Florida Marlins
New York Yankees
New York Yankees
New York Yankees
Arizona Diamondbacks
Anaheim Angels
Florida Marlins
Boston Red Sox
Chicago White Sox
St. Louis Cardinals
Boston Red Sox
Philadelphia Phillies
New York Yankees

Homework Answers

Answer #1

'''

Python version : 2.7

Python program to read file and store in dictionary and perform operations on this dictionary

'''

# variable to store the start year

start_year = 1903

team_year = {} # dictionary with keys of team name and values consists of years of winnings

file = open("WorldSeriesWinners.txt") # open the file in read mode

line = file.readline() # read a line from the file

# loop to read the file till the end of file

while(line):

               # ignore the years 1904 and 1994

               if start_year in [1904, 1994]:

                              start_year += 1

               # check if team name is present in             team_year dictionary

               if line.strip() in team_year:

                              team_year[line.strip()].append(start_year) # append the year to the list of years

               else: # if team is not present in the dictionary

                              team_year[line.strip()] = [start_year] # create a new entry with team name as key and value consisting of list with start_year

               start_year += 1 # increment the start_year

               line = file.readline() # read the next line

# close the file

file.close()

# get the list of teams from the dictionary             

teams = team_year.keys()

# sort the teams in alphabetical order

teams.sort()

# display the team and the list of years

for team in teams:

               print(team +" : "+str(team_year[team]))

# create another dictionary with team names as keys and number of wins as values             

team_wins = {}

# populate the team name and num of wins          

for team in team_year.keys():

               team_wins[team] = len(team_year[team])

# display the contents of team_wins        

print('\n%30s : %s' %('Team','Wins'))        

for team in team_wins:

               print('%30s : %d' %(team,team_wins[team]))

teams = team_wins.keys() # get the list of teams

wins = team_wins.values() # get the list of values

# loop to sort the teams and wins according to the wins in descending order

i=0

while(i<len(wins)-1):

               j = i+1

               max = i

               while(j < len(wins)):

                              if wins[j] > wins[max]:

                                             max = j

                              j += 1

               if(max != i):

                              tempWin = wins[i]

                              wins[i] = wins[max]

                              wins[max] = tempWin

                             

                              tempTeam = teams[i]

                              teams[i] = teams[max]

                              teams[max] = tempTeam

              

               i += 1

# print the teams and number of wins order by number of wins      

print('')

for team in teams:

               print('%30s : %s' %(team,'*'*team_wins[team]))  

#end of program             

Code Screenshot:

Output:

Input file:

Output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Do it in python please Boston Americans 1903 No Winner 1904 New York Giants 1905 Chicago...
Do it in python please Boston Americans 1903 No Winner 1904 New York Giants 1905 Chicago White Sox 1906 Chicago Cubs 1907 Chicago Cubs 1908 Pittsburgh Pirates 1909 Philadelphia Athletics 1910 Philadelphia Athletics 1911 Boston Red Sox 1912 Philadelphia Athletics 1913 Boston Braves 1914 Boston Red Sox 1915 Boston Red Sox 1916 Chicago White Sox 1917 Boston Red Sox 1918 Cincinnati Reds 1919 Cleveland Indians 1920 New York Giants 1921 New York Giants 1922 New York Yankees 1923 Washington Senators...
Do highly performing athletic teams have a larger fan base and generate greater attendance at games...
Do highly performing athletic teams have a larger fan base and generate greater attendance at games or matches? Use the data (in the data document) for the number of wins and attendance in 2006 for the 14 American League baseball teams to answer the follow questions: a) Examine a scatterplot for the 2 variables and test the conditions for regression. b) Do you think there is a linear association between Home Attendance and Wins? Team Wins Runs Home Attendance Minnesota...
team   league    price    wins    cost/win Arizona Diamondbacks   NL   19.68   90   35.40 Atlanta Braves  ...
team   league    price    wins    cost/win Arizona Diamondbacks   NL   19.68   90   35.40 Atlanta Braves   NL   17.07   84   32.89 Chicago Cubs   NL   34.30   85   65.33 Cincinnati Reds   NL   17.90   72   40.32 Colorado Rockies   NL   14.72   90   26.67 Florida Marlins   NL   16.70   71   38.13 Houston Astros   NL   26.66   73   59.11 Los Angeles Dodgers   NL   20.09   82   34.64 Milwaukee Brewers   NL   18.11   83   35.37 N.Y. Mets   NL   25.28   88   46.56 Philadelphia Phillies     NL   26.73   89   48.69 Pittsburgh Pirates   NL   17.08  ...
Here is the data Stat7_prob2.txt : "Team","WINS","HR","BA","ERA" "Anaheim Angels",99,152,.282,3.69 "Baltimore Orioles",67,165,.246,4.46 "Boston Red Sox",93,177,.277,3.75 "Chicago White...
Here is the data Stat7_prob2.txt : "Team","WINS","HR","BA","ERA" "Anaheim Angels",99,152,.282,3.69 "Baltimore Orioles",67,165,.246,4.46 "Boston Red Sox",93,177,.277,3.75 "Chicago White Sox",81,217,.268,4.53 "Cleveland Indians",74,192,.249,4.91 "Detroit Tigers",55,124,.248,4.93 "Kansas City Royals",62,140,.256,5.21 "Minnesota Twins",94,167,.272,4.12 "New York Yankees",103,223,.275,3.87 "Oakland Athletics",103,205,.261,3.68 "Seattle Mariners",93,152,.275,4.07 "Tampa Bay Devil Rays",55,133,.253,5.29 "Texas Rangers",72,230,.269,5.15 "Toronto Blue Jays",78,187,.261,4.8 "Arizona Diamondbacks",98,165,.267,3.92 "Atlanta Braves",101,164,.26,3.13 "Chicago Cubs",67,200,.246,4.29 "Cincinnati Reds",78,169,.253,4.27 "Colorado Rockies",73,152,.274,5.2 "Florida Marlins",79,146,.261,4.36 "Houston Astros",84,167,.262,4 "Los Angeles Dodgers",92,155,.264,3.69 "Milwaukee Brewers",56,139,.253,4.73 "Montreal Expos",83,162,.261,3.97 "New York Mets",75,160,.256,3.89 "Philadelphia Phillies",80,165,.259,4.17 "Pittsburgh Pirates",72,142,.244,4.23 "St. Louis Cardinales",97,175,.268,3.7 "San Diego Padres",66,136,.253,4.62 "San Francisco Giants",95,198,.267,3.54 Here...
11v On December 17, 2007 baseball writer John Hickey wrote an article for the Seattle P-I...
11v On December 17, 2007 baseball writer John Hickey wrote an article for the Seattle P-I about increases to ticket prices for Seattle Mariners games during the 2008 season. The article included a data set that listed the average ticket price for each MLB team, the league in which the team plays (AL or NL), the number of wins during the 2007 season and the cost per win (in dollars). The data for the 16 National League teams are shown...
. If a sports team is maximizing their wins, is that the same as maximizing their...
. If a sports team is maximizing their wins, is that the same as maximizing their profits? Explain at least three ways that teams seek to maximize their profits. b. Using at least 2 teams from the following list, explain how teams can become wealthy even if they do not win all the time Rank, Team, Value, 1-Year change (Sport) 1. Dallas Cowboys, $4.8 billion, 14% (NFL) 2. Manchester United, $4.123 billion, 12% (Soccer) 3. Real Madrid, $4.09 billion, 14%...
Notes: Please type your work, or 10% credits will be deducted Show your work for full...
Notes: Please type your work, or 10% credits will be deducted Show your work for full credits Follow the six steps for hypothesis testing If Excel is used, please copy and paste Excel Output as part of your answer How does cellphone service compare between different cities? The data stored in CellServicee represent the rating of Verizon and AT&T in 22 different cities. (Data extracted from “Best Phones and Service,” Consumer Reports, January 2012, p. 28, 37.) At the 0.05...
For each of the following two-samples t-tests (problems 1-6): (a) Determine if a F test for...
For each of the following two-samples t-tests (problems 1-6): (a) Determine if a F test for the ratio of two variances is appropriate to calculate for the context. If it is appropriate, conduct the analysis and report the result. Include what statistical conclusion you should draw from the analysis (i.e., whether you should conduct a pooled-variance t-test or an unequal-variances t-test). (b) Identify the most appropriate t-test to conduct for the situation/data given. Don’t forget to consider if the context...
This file contains data on the crime rates (total and violent) and population. Using Excel or...
This file contains data on the crime rates (total and violent) and population. Using Excel or other software of your choice, for each of the following pairs make a ScatterPlot then determine the Correlation Coefficient: (3pts) Crime Rate and Population (3pts) Violent Crime Rate and Population (3pts) Crime Rate and Population Density (that’s the Number of people Per square mile – Hint: You need to compute this last one on your own using data in the table.) Copy and paste...
Suppose you are a rabid football fan and you get into a discussion about the importance...
Suppose you are a rabid football fan and you get into a discussion about the importance of offense (yards made) versus defense (yards allowed) in terms of winning a game. You decide to look at football statistics to provide evidence of which variable is a stronger predictor of wins. Can use the minitab. Part a) Develop a simple linear regression that compares wins to yards made (please show me the scatter plot) . Perform the following diagnostics on this regression:...