Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.35 KB | None | 0 0
  1. import requests
  2. import glob
  3.  
  4. def download_files():
  5.     #This function creates two .dat files necessary for further calculations
  6.  
  7.     weather_source = requests.get('http://codekata.com/data/04/weather.dat')
  8.     football_source = requests.get('http://codekata.com/data/04/football.dat')
  9.  
  10.     with open('weather.dat', 'wb') as f:
  11.         f.write(weather_source.content)
  12.  
  13.     with open('football.dat', 'wb') as f:
  14.         f.write(football_source.content)
  15.  
  16. def smallest_difference():
  17.     filenames = glob.glob('*.dat')
  18.  
  19.     for file in filenames:
  20.         data = open(file)
  21.         if file.startswith('weather'):
  22.             weather_data_list = []
  23.  
  24.             for line in data:
  25.                 weather_data_list += [line.split()]
  26.  
  27.             separate_days_data = [x for x in weather_data_list[2:]]
  28.             temperatures_list = [x[:3] for x in separate_days_data[:-1]]
  29.             numeric_lists = [[elem.replace('*', '') for elem in lst] for lst in
  30.                              temperatures_list]  # Remove asterisks from string
  31.             values = [[int(x) for x in sublist] for sublist in numeric_lists]
  32.             max_temperatures = [x[1] for x in values]
  33.             min_temperatures = [x[2] for x in values]
  34.             subtraction_results = [x - y for x, y in zip(max_temperatures, min_temperatures)]
  35.             day_with_smallest_temp_spread = subtraction_results.index(min(subtraction_results)) + 1
  36.  
  37.             print(f'Day with smallest temperature spread is {day_with_smallest_temp_spread}th of July 2002')
  38.  
  39.         elif file.startswith('football'):
  40.             football_data = []
  41.  
  42.             for line in data:
  43.                 football_data += [line.split()]
  44.  
  45.             separate_teams = [x for x in football_data[1:]]
  46.             separate_teams.pop(17)
  47.             scored_goals = [int(x[6]) for x in separate_teams]
  48.             lost_goals = [int(x[8]) for x in separate_teams]
  49.             goals_difference = [x - y for x, y in zip(scored_goals, lost_goals)]
  50.             team_names = [x[1] for x in separate_teams]
  51.             teams_goals = {k: v for (k, v) in zip(team_names, goals_difference)}
  52.             lowest_goal_difference_team = min(teams_goals, key=lambda v: abs(teams_goals[v]))
  53.  
  54.             print('Team with smallest difference in "for" and "against" goals is',
  55.                   lowest_goal_difference_team.replace('_', ' '))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement