Python - List of Files: Functions, Modules and Files - return statement
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(19/108)の解答を求めてみる。
コード
swimclub.py
import statistics
FOLDER = 'swimdata/'
def read_swim_data(filename):
swimmer, age, distance, stroke = filename.removesuffix('.txt').split('-')
with open(FOLDER + filename) as file:
lines = file.readlines()
times = lines[0].rstrip('\n').split(',')
converts = []
for t in times:
minutes, rest = t.split(':')
seconds, hundredths = rest.split('.')
converted_time = (
int(minutes) * 60 * 100 + int(seconds) * 100 + int(hundredths)
)
converts.append(converted_time)
average = statistics.mean(converts)
mins_secs, hundredths = str(round(average / 100, 2)).split('.')
mins_secs = int(mins_secs)
minutes = mins_secs // 60
seconds = mins_secs - minutes * 60
average = f'{minutes}:{seconds}.{hundredths}'
return (
swimmer,
age,
distance,
stroke,
average,
)