Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import glob
- import requests
- import re
- 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 read_file(data):
- filenames = glob.glob('*.dat')
- for file in filenames:
- data = open(file)
- data_list = []
- for line in data:
- data_list += [line.split()]
- return data_list
- def smallest_difference(column1, column2):
- data_list = read_file(data)
- cleaned_up_data = [[re.sub(r'[^\w]', '', elem) for elem in lst if elem] for lst in
- data_list[1:]]
- separate_rows_data = [x for x in cleaned_up_data[1:] if x!=['']]
- values = [[int(x) for x in sublist if x.isdigit()] for sublist in separate_rows_data]
- column1_list = [x[column1] for x in values]
- column2_list = [x[column2] for x in values]
- subtraction_results = [x - y for x, y in zip(column1_list, column2_list)]
- #smallest_spread = subtraction_results.index(abs(min(subtraction_results)))
- print(subtraction_results)
- if data.startswith('weather'):
- day_with_smallest_temp_spread = subtraction_results.index(min(subtraction_results)) + 1
- print('Day with smallest temperature spread is day number', day_with_smallest_temp_spread)
- elif data.startswith('football'):
- team_names = [x[1] for x in separate_rows_data]
- teams_goals = {k:v for (k, v) in zip(team_names, subtraction_results)}
- 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)
- print(team_names)
- data='weather.dat'
- read_file(data)
- smallest_difference(1, 2)
Advertisement
Add Comment
Please, Sign In to add comment