Guest User

Untitled

a guest
Apr 27th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.03 KB | None | 0 0
  1. import glob
  2. import requests
  3. import re
  4.  
  5.  
  6. def download_files():
  7.     #This function creates two .dat files necessary for further calculations
  8.  
  9.     weather_source = requests.get('http://codekata.com/data/04/weather.dat')
  10.     football_source = requests.get('http://codekata.com/data/04/football.dat')
  11.  
  12.     with open('weather.dat', 'wb') as f:
  13.         f.write(weather_source.content)
  14.  
  15.     with open('football.dat', 'wb') as f:
  16.         f.write(football_source.content)
  17.  
  18. def read_file(data):
  19.     filenames = glob.glob('*.dat')
  20.  
  21.     for file in filenames:
  22.         data = open(file)
  23.  
  24.         data_list = []
  25.         for line in data:
  26.             data_list += [line.split()]
  27.     return data_list
  28.  
  29.  
  30.  
  31. def smallest_difference(column1, column2):
  32.     data_list = read_file(data)
  33.  
  34.     cleaned_up_data = [[re.sub(r'[^\w]', '', elem) for elem in lst if elem] for lst in
  35.                    data_list[1:]]
  36.     separate_rows_data = [x for x in cleaned_up_data[1:] if x!=['']]
  37.     values = [[int(x) for x in sublist if x.isdigit()] for sublist in separate_rows_data]
  38.     column1_list = [x[column1] for x in values]
  39.     column2_list = [x[column2] for x in values]
  40.     subtraction_results = [x - y for x, y in zip(column1_list, column2_list)]
  41.     #smallest_spread = subtraction_results.index(abs(min(subtraction_results)))
  42.     print(subtraction_results)
  43.  
  44.     if data.startswith('weather'):
  45.         day_with_smallest_temp_spread = subtraction_results.index(min(subtraction_results)) + 1
  46.         print('Day with smallest temperature spread is day number', day_with_smallest_temp_spread)
  47.  
  48.     elif data.startswith('football'):
  49.  
  50.         team_names = [x[1] for x in separate_rows_data]
  51.         teams_goals = {k:v for (k, v) in zip(team_names, subtraction_results)}
  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', lowest_goal_difference_team)
  55.         print(team_names)
  56. data='weather.dat'
  57. read_file(data)
  58. smallest_difference(1, 2)
Advertisement
Add Comment
Please, Sign In to add comment