Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
b9948d8
Setting up GitHub Classroom Feedback
github-classroom[bot] Nov 16, 2024
e9d069c
add deadline
github-classroom[bot] Nov 16, 2024
7cece9d
add file
vmikhailishyna Nov 18, 2024
cb461e5
Delete .idea directory
vmikhailishyna Nov 18, 2024
12615be
Add main file
vmikhailishyna Nov 19, 2024
286b666
user input with data
dpavlishchuk Nov 19, 2024
a3c8cd4
Add output function
vmikhailishyna Nov 19, 2024
e80bfe1
Merge branch 'output-data'
vmikhailishyna Nov 19, 2024
46f3e6f
update output function
vmikhailishyna Nov 21, 2024
7b8beb1
uptaded func and took put of function
dpavlishchuk Nov 21, 2024
38ec131
Merge branch 'update-output-files'
vmikhailishyna Nov 21, 2024
2866ac3
Add output_from_file function and rewrite output_into_scr function
vmikhailishyna Nov 22, 2024
6a3fd67
Add output_from_file function and rewrite output_into_scr function.
vmikhailishyna Nov 26, 2024
c37f4a1
The third task
vmikhailishyna Nov 27, 2024
e5e568e
argparse func
dpavlishchuk Nov 27, 2024
efb4a8a
Improve overall task
vmikhailishyna Nov 27, 2024
a4e9fda
rename file
vmikhailishyna Nov 27, 2024
e1f8d9b
Merge remote-tracking branch 'origin/newUptades'
vmikhailishyna Nov 27, 2024
a2d56df
2 task
dpavlishchuk Nov 28, 2024
c3e8cd8
add a check for call every function
vmikhailishyna Nov 28, 2024
a34b291
Merge remote-tracking branch 'origin/newUptades'
vmikhailishyna Nov 28, 2024
274869a
2 task in main.py
dpavlishchuk Nov 28, 2024
8d21e31
Merge branch 'newUptades'
vmikhailishyna Nov 28, 2024
f2f3a18
Delete trying.py
vmikhailishyna Nov 28, 2024
c384143
add a check for call every function and add optimization to all code
vmikhailishyna Nov 28, 2024
56e149a
Merge remote-tracking branch 'origin/main'
vmikhailishyna Nov 28, 2024
7dde661
added validation
dpavlishchuk Nov 28, 2024
753cbd8
add function for task four
vmikhailishyna Nov 28, 2024
35ac655
Merge remote-tracking branch 'origin/validation'
vmikhailishyna Nov 28, 2024
825d4c4
first participation
dpavlishchuk Nov 29, 2024
f4ceebc
update functions add function: average_medals, write_result_into_file.
vmikhailishyna Nov 29, 2024
c1e6d4d
update functions add function: average_medals, write_result_into_file.
vmikhailishyna Nov 29, 2024
d4757a7
Merge branch 'forth_task'
vmikhailishyna Nov 29, 2024
227c33f
first participation
dpavlishchuk Nov 29, 2024
653045c
uptades
dpavlishchuk Nov 29, 2024
6dfa2f9
min medals
dpavlishchuk Nov 29, 2024
9795a3a
rewrite max function
vmikhailishyna Nov 29, 2024
d46f1c2
correct max_min function
vmikhailishyna Nov 29, 2024
0aed882
drop into files
vmikhailishyna Nov 29, 2024
6a88809
Add some validation
vmikhailishyna Nov 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
271,117 changes: 271,117 additions & 0 deletions OlympicAthletes-athlete_events.tsv

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[![Review Assignment Due Date](https://classroom.github.com/assets/deadline-readme-button-22041afd0340ce965d47ae6ef1cefeee28c7c493a6346c4f15d667ab976d596c.svg)](https://classroom.github.com/a/eCpvRij9)
###### Практична робота №3
## Робота з текстовими даними

Expand Down
37 changes: 37 additions & 0 deletions auxiliary_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
def max_min_values(dict_country_medals, country, max_or_min):
min_max_year = None
max_medals = 0
min_medals = float('inf')
gold_medals = silver_medals = bronze_medals = 0
for year, medals in dict_country_medals[country].items():
total_medals = medals["Gold"] + medals["Silver"] + medals["Bronze"]
if max_or_min == "min":
if total_medals < min_medals:
min_medals = total_medals
min_max_year = year
gold_medals = medals["Gold"]
silver_medals = medals["Silver"]
bronze_medals = medals["Bronze"]
elif max_or_min == "max":
if total_medals > max_medals:
max_medals = total_medals
min_max_year = year
gold_medals = medals["Gold"]
silver_medals = medals["Silver"]
bronze_medals = medals["Bronze"]
if max_or_min == "min":
result = f"{country} - Year with lowest numbers of medals: {min_max_year} ({min_medals} medals)\nGold: {gold_medals}, Silver: {silver_medals}, Bronze: {bronze_medals}"
elif max_or_min == "max":
result = f"{country} - Year with highest numbers of medals: {min_max_year} ({max_medals} medals)\nGold: {gold_medals}, Silver: {silver_medals}, Bronze: {bronze_medals}"
return result

def average_medals(dict_country_medals, country, count_of_sports, list_of_years):
results = []
for year in list_of_years:
if year in dict_country_medals[country]:
average_gold_medals = dict_country_medals[country][year]["Gold"] / count_of_sports
average_silver_medals = dict_country_medals[country][year]["Silver"] / count_of_sports
average_bronze_medals = dict_country_medals[country][year]["Bronze"] / count_of_sports
result = f"Average in {year}:\n Gold medals: {average_gold_medals}\n Silver medals: {average_silver_medals}\n Bronze medals: {average_bronze_medals}\n"
results.append(result)
return results
18 changes: 18 additions & 0 deletions first_participation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
def first_participation(country, file_address):
list_of_year = []
dict_country_years = {country: {}}
with open(file_address, 'r') as olympics_data:
next(olympics_data)
for line in olympics_data:
list_of_information = line.split("\t")
year = list_of_information[9]
city = list_of_information[11]
if country in line:
if year not in dict_country_years[country]:
dict_country_years[country][year] = city
for year in dict_country_years[country]:
year_int = int(year)
list_of_year.append(year_int)
min_year = min(list_of_year)
result = f"The first participation of {country} is {min_year} in {dict_country_years[country][year]}"
return result
35 changes: 35 additions & 0 deletions first_task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import work_with_file
def output_into_scr(file_address, country, year_of_olympics, text_file):
dict_medals = {}
results = []
counter = 0
with open(file_address, 'r') as olympics_data:
for line in olympics_data:
if (country in line) and (year_of_olympics in line):
list_of_information = line.split("\t")
name = list_of_information[1]
discipline = list_of_information[12]
medal_type = list_of_information[14].strip()
result = f"{name} - {discipline} - {medal_type}"
if medal_type in dict_medals:
dict_medals[medal_type] += 1
else:
dict_medals[medal_type] = 1
if (medal_type == "Bronze") or (medal_type == "Silver") or (medal_type == "Gold"):
counter += 1
if text_file is not None and counter <= 10:
results.append(result)
elif text_file is None and counter <= 10:
print(result)
if counter == 0:
if text_file is None:
print("There were no winners this year.")
results.append("There were no winners this year.")
for medal, count in dict_medals.items():
number_of_medals = f"{medal} - {count}\n"
results.append(number_of_medals)
if text_file is None:
print(number_of_medals)
if text_file is not None:
work_with_file.write_result_into_file(text_file, results)
work_with_file.output_from_file(text_file)
47 changes: 47 additions & 0 deletions interactive.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import auxiliary_function
import first_participation
import work_with_file
def interactive(file_address, country, text_file):
varification = 0
count_of_sports = 0
list_of_years = []
results = []
dict_country_medals = {country: {}}
with open(file_address, 'r') as olympics_data:
for line in olympics_data:
list_of_information = line.split("\t")
year = list_of_information[9]
medal_type = list_of_information[14].strip()
if year not in list_of_years:
list_of_years.append(year)
for country in dict_country_medals:
if country in line:
varification += 1
if year not in dict_country_medals[country]:
count_of_sports += 1
dict_country_medals[country][year] = {"Gold": 0, "Silver": 0, "Bronze": 0, "NA": 0}
dict_country_medals[country][year][medal_type] += 1
if varification != 0 and count_of_sports != 0:
result_max = auxiliary_function.max_min_values(dict_country_medals, country, "max")
result_min = auxiliary_function.max_min_values(dict_country_medals, country,"min")
results_average = auxiliary_function.average_medals(dict_country_medals, country, count_of_sports, list_of_years)
results.append(result_max)
results.append(result_min)
for res in results_average:
results.append(res)
if text_file is None:
for res in results_average:
print(res)
print(result_max)
print(result_min)
first_country = first_participation.first_participation(country, file_address)
print(first_country)
results.append(first_country)
else:
if text_file is None:
print("No such country")
else:
results.append("No such country")
if text_file is not None:
work_with_file.write_result_into_file(text_file, results)
work_with_file.output_from_file(text_file)
36 changes: 36 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import argparse
import sys
import overall
import first_task
import interactive
import total_medals



parser = argparse.ArgumentParser("Information from user about olympic data")
parser.add_argument("address_file", type=str, help="File with data")
parser.add_argument("command", choices=["medals", "total", "overall", "interactive"], help="optional")
parser.add_argument("arg", nargs="*", help="")
parser.add_argument("-output", type=str, help="Output file", default=None)

args = parser.parse_args()
file_address = args.address_file
if file_address != "OlympicAthletes-athlete_events.tsv":
sys.exit("Not a valid file")
command = args.command
args_for_function = args.arg
output_file = args.output


if command == "medals":
country = args_for_function[0]
year_of_olympics = args_for_function[1]
first_task.output_into_scr(file_address, country, year_of_olympics, output_file)
elif command == "total":
year_of_olympics = args_for_function[0]
total_medals.total_medals(file_address, year_of_olympics, output_file)
elif command == "overall":
overall.overall(file_address, " ".join(args_for_function), output_file)
elif command == "interactive":
country = input("Please enter a country: ")
interactive.interactive(file_address, country, output_file)
34 changes: 34 additions & 0 deletions overall.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import auxiliary_function
import work_with_file
def overall(file_address, countries, text_file):
verification = 0
results = []
list_of_countries = countries.split(" ")
dict_country_medals = {country: {} for country in list_of_countries}
with open(file_address, 'r') as olympics_data:
for line in olympics_data:
list_of_information = line.split("\t")
year = list_of_information[9]
medal_type = list_of_information[14].strip()
for country in list_of_countries:
if country in line and medal_type in {"Gold", "Silver", "Bronze"}:
verification += 1
if year not in dict_country_medals[country]:
dict_country_medals[country][year] = {"Gold": 0, "Silver": 0, "Bronze": 0}
dict_country_medals[country][year][medal_type] += 1
for country in list_of_countries:
if country in dict_country_medals and dict_country_medals[country]:
try:
results_max = auxiliary_function.max_min_values(dict_country_medals, country, "max")
except ValueError:
continue
results.append(results_max)
if text_file is None:
print(results_max)
if verification == 0:
if text_file is None:
print("Country not found! Try again.")
results.append("Country not found! Try again.")
if text_file is not None:
work_with_file.write_result_into_file(text_file, results)
work_with_file.output_from_file(text_file)
33 changes: 33 additions & 0 deletions total_medals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import work_with_file

def total_medals(file_address, year_input, result_file):
results = []
dict_for_medals = {}
verification = 0
with open(file_address, 'r') as olympics_data:
for line in olympics_data:
data_list = line.split('\t')
country = data_list[7].strip()
year_of_olympic = data_list[9]
medal_type = data_list[14].strip()
if year_input == year_of_olympic:
if country not in dict_for_medals:
dict_for_medals[country] = {"Gold": 0, "Silver": 0, "Bronze": 0}
if medal_type in dict_for_medals[country]:
dict_for_medals[country][medal_type] += 1
for country in dict_for_medals:
if (dict_for_medals[country]['Gold'] > 1) or (dict_for_medals[country]['Silver'] > 1) or (
dict_for_medals[country]['Bronze'] > 1):
verification += 1
result = f"{country} - Gold medals:{dict_for_medals[country]['Gold']} - Silver medals:{dict_for_medals[country]['Silver']} - Bronze medals:{dict_for_medals[country]['Bronze']}"
if result_file is None:
print(result)
else:
results.append(result)
if verification == 0:
if result_file is None:
print("There was no Olympics this year")
results.append("There was no Olympics this year")
if result_file is not None:
work_with_file.write_result_into_file(result_file, results)
work_with_file.output_from_file(result_file)
10 changes: 10 additions & 0 deletions work_with_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def write_result_into_file(result_file, results):
with open(result_file, "w") as file:
for i, result in enumerate(results):
file.write(result + "\n")


def output_from_file(text_file):
with open(text_file, 'r') as result:
for line in result:
print(line)