145k views
2 votes
For all Problems Filename: tottenham.py Files for input: hotspurs.txt

Problem 1 Use a while loop to read in the data from the file; your code should work no matter how many seasons are in there. Read two lines of the file with each iteration of the while loop — the first becomes a list for the current season’s goals, and the second becomes a list for the current season’s W/L/D record. For each season, answer the following questions and print out the answers: How many total matches did Tottenham play this season (it should be 38)? How many matches did Tottenham win, lose, and draw this season? How many matches did we win despite scoring only one goal? A team starts a season with zero points. They earn 3 points for a win, 1 for a draw, and 0 for a loss (number of points establishes Tottenham’s superiority over other teams; it is not the same thing as goals). How many points did Tottenham have at the end of the season?
Problem 2 For the entire dataset, answer the following questions and print out the answers (and once again, we expect that whatever code you write here would work no matter how many seasons are in the file): In which season did we have the fewest wins? In which season did we score the most goals?
Problem 3 OK, now that we’ve done our data analysis, it’s time for a little visualization. For this part, you are required to create at least one plot, with appropriate titles, x- and y-labels, etc. Although we have suggestions, the type of plot is up to you, as are the colors and other details. A plot will represent just one season, and you can pick which one. Pick one of the following: Plot the win/loss/draw record. You might try a bar chart (plt.bar) for this one, so you can use the height of a bar to represent the number of wins compared to losses compared to draws. Plot the goals record. You might try a scatterplot (plt.scatter). The height of each point should represent the number of goals scored in a match. Try making the colors different for wins, losses, and draws.
hotspurs.txt:
2016-2017 1 1 1 4 1 2 2 1 0 1 1 3 1 5 0 3 2 4 4 2 4 2 0 1 0 4 3 2 2 3 4 4 1 2 0 2 6 7
2016-2017 D W D W W W W D D D D W L W L W W W W W W D D W L W W W W W W W W W L W W W
2017-2018 2 1 1 3 0 3 4 1 4 0 1 0 1 1 1 5 2 1 3 5 2 1 4 1 2 2 1 1 2 4 3 2 1 1 2 0 1 5
2017-2018 W L D W D W W W W L W L D L D W W L W W W D W D W D W W W W W W L D W L W W
2018-2019 3 3 1 1 2 2 1 3 1 3 2 3 2 1 6 5 1 3 0 2 2 1 3 2 1 3 1 0 1 1 1 2 4 0 1 0 0 2
2018-2019 W W L L W W W W W W L W W W W W L W L W W L W W W W L L D L L W W L W L L D
2019-2020 3 2 0 2 4 1 2 0 1 1 1 1 3 3 1 5 2 0 2 2 0 0 0 2 3 2 1 1 2 1 2 1 1 0 2 3 3 1
2019-2020 W D L D W L W L D L D D W W L W W L W D L L D W W W D L L D W L W D W W W D

1 Answer

3 votes

The Python code to solve the problems based on the provided specifications using Python and have the necessary libraries inside is shown below

import matplotlib.pyplot as plt

def process_season(season_goals, season_results):

total_matches = len(season_results)

wins = season_results.count('W')

losses = season_results.count('L')

draws = season_results.count('D')

one_goal_wins = sum(1 for goals in season_goals if goals == 1)

points = 3 * wins + draws

print("Total matches played:", total_matches)

print("Wins:", wins)

print("Losses:", losses)

print("Draws:", draws)

print("Wins with only one goal:", one_goal_wins)

print("Points:", points)

print()

return total_matches, wins, losses, draws, one_goal_wins, points

def analyze_seasons(data):

min_wins_season = min(data, key=lambda x: x[1])

max_goals_season = max(data, key=lambda x: sum(x[0]))

print("Season with the fewest wins:", min_wins_season[0])

print("Season with the most goals:", max_goals_season[0])

print()

def visualize_season(season_goals, season_results):

colors = {'W': 'green', 'L': 'red', 'D': 'gray'}

plt.figure(figsize=(10, 6))

plt.scatter(range(1, len(season_goals) + 1), season_goals, c=[colors[result] for result in season_results])

plt.title('Goals Record for a Season')

plt.xlabel('Match Number')

plt.ylabel('Goals Scored')

plt.legend(['Win', 'Loss', 'Draw'], loc='upper right')

plt.show()

def main():

with open('hotspurs.txt') as file:

data = file.read().splitlines()

season_data = [data[i:i+2] for i in range(0, len(data), 2)]

for season in season_data:

season_goals = list(map(int, season[0].split()[1:]))

season_results = season[1].split()[1:]

total, wins, losses, draws, one_goal_wins, points = process_season(season_goals, season_results)

visualize_season(season_goals, season_results)

analyze_seasons([(list(map(int, season[0].split()[1:])), season[1].split()[1:]) for season in season_data])

if __name__ == "__main__":

main()

User Berezh
by
8.9k points