Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import glob
- def download_files():
- #This function creates two .dat files necessary for further calculations
- weather_source = requests.get('http://codekata.com/data/04/weather.dat')
- football_source = requests.get('http://codekata.com/data/04/football.dat')
- with open('weather.dat', 'wb') as f:
- f.write(weather_source.content)
- with open('football.dat', 'wb') as f:
- f.write(football_source.content)
- def smallest_difference():
- filenames = glob.glob('*.dat')
- for file in filenames:
- data = open(file)
- if file.startswith('weather'):
- weather_data_list = []
- for line in data:
- weather_data_list += [line.split()]
- separate_days_data = [x for x in weather_data_list[2:]]
- temperatures_list = [x[:3] for x in separate_days_data[:-1]]
- numeric_lists = [[elem.replace('*', '') for elem in lst] for lst in
- temperatures_list] # Remove asterisks from string
- values = [[int(x) for x in sublist] for sublist in numeric_lists]
- max_temperatures = [x[1] for x in values]
- min_temperatures = [x[2] for x in values]
- subtraction_results = [x - y for x, y in zip(max_temperatures, min_temperatures)]
- day_with_smallest_temp_spread = subtraction_results.index(min(subtraction_results)) + 1
- print(f'Day with smallest temperature spread is {day_with_smallest_temp_spread}th of July 2002')
- elif file.startswith('football'):
- football_data = []
- for line in data:
- football_data += [line.split()]
- separate_teams = [x for x in football_data[1:]]
- separate_teams.pop(17)
- scored_goals = [int(x[6]) for x in separate_teams]
- lost_goals = [int(x[8]) for x in separate_teams]
- goals_difference = [x - y for x, y in zip(scored_goals, lost_goals)]
- team_names = [x[1] for x in separate_teams]
- teams_goals = {k: v for (k, v) in zip(team_names, goals_difference)}
- lowest_goal_difference_team = min(teams_goals, key=lambda v: abs(teams_goals[v]))
- print('Team with smallest difference in "for" and "against" goals is',
- lowest_goal_difference_team.replace('_', ' '))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement