From 1621b4eb5bb6bfd133ffbc15016c3f1ec40c4884 Mon Sep 17 00:00:00 2001 From: Thomas Geromichalos Date: Wed, 29 Oct 2025 19:15:30 -0700 Subject: [PATCH] add main2.py --- main2.py | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 main2.py diff --git a/main2.py b/main2.py new file mode 100644 index 0000000..88d9c7f --- /dev/null +++ b/main2.py @@ -0,0 +1,86 @@ +import pymongo +import json +client=pymongo.MongoClient("localhost", 27017) +db = client["timdb"] +collection = db["tim"] + +# first check that the collection exists, if not, load the data from the file +#step 1: import json data and turn into python list of dictionaries insert into tim collection +if "tim" not in db.list_collection_names() or collection.count_documents({}) == 0: + #step 2: Turn it into average team data + #step 3: insert into team collection + with open('example_tim_data.json', 'r') as f: + data = json.load(f) + + # write data into the database + collection.insert_many(data) + +# read the data from the database, not the file +data = list(collection.find()) +teams = [] + +#Helps figure out the number of teams in the data as well as their unique numbers +for item in data: + if item["team_num"] not in teams: + + + teams.append(item["team_num"]) + +# Creates a function where max score is calculated by adding difference of the number and the previous math score if the new number is greater than the old max +def maximumscore(list_of_team_data): + max_score=0 + for item in list_of_team_data: + if item["num_balls"]>=max_score: + max_score+=(item["num_balls"]-max_score) + return(max_score) + + +# finds the minimum score by a similar method to the previous function. +def minimumscore(list_of_team_data): + min_score=1000 + for item in list_of_team_data: + if item["num_balls"]<=min_score: + min_score-=(min_score-item["num_balls"]) + return(min_score) + + +# finds the number of matches played by reading the data and adding one every time the team shows up. +def matches(list_of_team_data): + x=0 + for item in list_of_team_data: + if item["team_num"]==item["team_num"]: + x+=1 + return(x) + +# creates the average by finding the total balls scored divided by the total matches played +def average(list_of_team_data): + total=0 + a=0 + for item in list_of_team_data: + if item["team_num"]==item["team_num"]: + total+=item["num_balls"] + a+=1 + return(total/a) + +# 1 is added to c every time the team has climbed and the percentage is found by c/total matches played +def succsess(list_of_team_data): + c=0 + d=0 + for item in list_of_team_data: + if item["team_num"]==item["team_num"]: + d+=1 + if item["climbed"]==True: + c+=1 + return(c/d) + +for team in teams: + team_list=[] + for item in data: + if item["team_num"]==team: + team_list.append(item) + result=maximumscore(team_list) + result2=minimumscore(team_list) + result3=matches(team_list) + result4=average(team_list) + result5=succsess(team_list) + print(f"team num: {team}. max balls scored: {result}. Min balls scored: {result2}. Matches played: {result3}. Average balls scored: {result4}. Climb succsess rate: {result5}")