Task1_of_summer_practice/individual_1.py
2026-06-28 14:24:23 +03:00

79 lines
3.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
import sys
import matplotlib.pyplot as plt
import pandas as pd
import main as mn
def calculate_team_points(filename):
df = pd.read_csv(filename)
# Считаем очки команд за периоды, овертаймы и булиты
h1, a1 = mn.parse_score(df, 'Период_1')
h2, a2 = mn.parse_score(df, 'Период_2')
h3, a3 = mn.parse_score(df, 'Период_3')
oth, ota = mn.parse_score(df, 'Овертайм')
soh, soa = mn.parse_score(df, 'Буллиты') # Счет по буллитам
# Складываем очки, получая итоговый счет
home_goals = h1 + h2 + h3
away_goals = a1 + a2 + a3
tie = home_goals == away_goals
# Проверка кто проиграл/выйграл по итогу матча или овертайма, или буллита
home_win = (home_goals > away_goals) | (tie & ((oth > ota) | ((oth == ota) & (soh > soa))))
away_win = (away_goals > home_goals) | (tie & ((ota > oth) | ((oth == ota) & (soa > soh))))
home_overtime_loss = tie & ((ota > oth) | ((oth == ota) & (soa > soh)))
away_overtime_loss = tie & ((oth > ota) | ((oth == ota) & (soh > soa)))
# Подсчитываем итоговое кол-во очков
home_points = 2 * home_win.astype(int) + home_overtime_loss.astype(int)
away_points = 2 * away_win.astype(int) + away_overtime_loss.astype(int)
# Соединяем 2 таблицы
points = pd.concat([
pd.DataFrame({'Команда': df['Команда_1'], 'О': home_points}),
pd.DataFrame({'Команда': df['Команда_2'], 'О': away_points}),
])
# Собираем таблицу с помощью groupby,
return points.groupby('Команда')['О'].sum().sort_values(ascending=False)
def show_points_histogram(filename, output=None):
points = calculate_team_points(filename) # Получаем очки
plt.figure(figsize=(12, 7)) # Создаем таблицу 12 на 7 дюймов
plt.bar(points.index, points.values, color='steelblue') # Строим столбчатую диаграмму, по очкам по y
plt.title('Гистограмма набранных командами очков')
plt.xlabel('Команда')
plt.ylabel('Очки')
plt.xticks(rotation=75, ha='right') # Чтобы не накладывались подписи, мы выравниваем и вращаем их
plt.grid(axis='y', alpha=0.3) # Отображение координатной сетки
plt.tight_layout() # Автоматическая настройка отступов
# Сохранение если указан нужный аргумент
if output:
plt.savefig(output, dpi=200)
print(f'Гистограмма сохранена в файл: {output}')
else:
plt.show()
def main():
if len(sys.argv) not in (2, 3):
print('Использование: python3 individual_1.py <csv-файл> [файл_для_сохранения]')
sys.exit(1)
filename = sys.argv[1]
output = sys.argv[2] if len(sys.argv) == 3 else None
if not os.path.isfile(filename):
raise FileNotFoundError(f'Файл не найден: {filename}')
show_points_histogram(filename, output)
if __name__ == '__main__':
main()