Python - List of Files: Functions, Modules and Files - return statement 2
Head First Python: A Learner’s Guide to the Fundamentals of Python Programming, A Brain-Friendly Guide、 Paul Barry(著)、 O’Reilly Mediaの Chapter 4.(List of Files: Functions, Modules and Files)、SHARPEN YOUR PENCIL(206/682)の解答を求めてみる。
コード
swimclub.py
import os
import statistics
FOLDER = 'swimdata'
def read_swim_data(filename: str):
swimmer, age, distance, stroke = filename.removesuffix('.txt')
with open(os.path.join(FOLDER, filename)) as f:
lines = f.readlines()
times = lines[0].strip().split(',')
converts = []
for t in times:
minutes, rest = t.split(':')
seconds, hundredths = rest.split('.')
converts.append(
int(minutes) * 60 * 100 + int(seconds) * 100 + int(hundredths)
)
average = statistics.mean(converts)
mins_secs, hundredths = str(round(average / 100, 2)).split('.')
mins_secs = int(mins_secs)
minutes, seconds = divmod(mins_secs, 60)
average = f'{minutes}:{seconds}.{hundredths}'
return swimmer, age, distance, stroke, average