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
'''
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:
Get Answers For Free
Most questions answered within 1 hours.