Skip to content

Instantly share code, notes, and snippets.

@z2s8
Last active May 19, 2020 12:53
Show Gist options
  • Save z2s8/9366c1bffabddf47ae7070e9adb85ece to your computer and use it in GitHub Desktop.
Save z2s8/9366c1bffabddf47ae7070e9adb85ece to your computer and use it in GitHub Desktop.
from collections import namedtuple
Measurement = namedtuple('Measurement',
['town', 'time', 'direction', 'speed', 'temperature'])
def format_time(raw):
hour = raw[:2]
minute = raw[2:]
return f'{hour}:{minute}'
# 1.
data = []
with open('tavirathu13.txt') as f:
lines = [line.split(' ') for line in f.read().splitlines()]
for line in lines:
data.append(Measurement(line[0], line[1], line[2][:3], line[2][3:], line[3]))
# 2.
print('2. feladat')
print('Adja meg egy település kódját!')
input_town = input('Telepules: ')
for entry in data[::-1]:
if entry.town == input_town:
print(f'Az utolsó mérési adat a megadott településről {format_time(entry.time)}-kor érkezett.')
break
# 3.
print('3. feladat')
smallest = sorted(data, key=lambda x: x.temperature)[0]
biggest = sorted(data, key=lambda x: x.temperature)[-1]
print(f'A legalacsonyabb hőmérséklet: {smallest.town} {format_time(smallest.time)} {smallest.temperature} fok.')
print(f'A legmagasabb hőmérséklet: {biggest.town} {format_time(biggest.time)} {biggest.temperature} fok.')
# 4.
print('4. feladat')
no_winds = filter(lambda x: x.speed == '00' and x.direction == '000', data)
for entry in no_winds:
print(f'{entry.town}: {format_time(entry.time)}')
if not no_winds:
print('Nem volt szélcsend a mérések idején')
# 5.
print('5. feladat')
daily_mid = dict()
daily_mid_hours = dict()
daily_smallest = dict()
daily_biggest = dict()
for entry in data:
if int(entry.time[:2]) in [1, 7, 13, 19]:
daily_mid[entry.town] = daily_mid.get(entry.town, []) + [entry.temperature]
daily_mid_hours[entry.town] = set.union(
daily_mid_hours.get(entry.town, set()),
set([entry.time[:2]])
)
prev_smallest = daily_smallest.get(entry.town, None)
if (prev_smallest is None or prev_smallest > entry.temperature):
daily_smallest[entry.town] = entry.temperature
prev_biggest = daily_biggest.get(entry.town, None)
if (prev_biggest is None or prev_biggest < entry.temperature):
daily_biggest[entry.town] = entry.temperature
for town in daily_smallest.keys():
town_delta = int(daily_biggest[town]) - int(daily_smallest[town])
town_mid = 'NA'
if len(daily_mid_hours[town]) == 4:
sum = 0
for temperature in daily_mid[town]:
sum += int(temperature)
town_mid = round(sum / len(daily_mid[town]))
print(f'{town} Középhőmérséklet: {town_mid}; Hőmérséklet-ingadozás: {town_delta}')
# 6.
print('6. feladat')
for town in daily_smallest.keys():
with open(f'{town}.txt', 'w') as f:
current_data = filter(lambda x: x.town == town, data)
for idx, entry in enumerate(current_data):
if idx == 0:
print(entry.town, file=f)
bars = '#' * int(entry.speed)
print(f'{format_time(entry.time)} {bars}', file=f)
print('A fájlok elkészültek.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment