From bfceebd17123227c6a2f97801a01d80b19541915 Mon Sep 17 00:00:00 2001 From: Seaswimmer Date: Thu, 6 Jun 2024 22:19:00 -0400 Subject: [PATCH] finished Cars Co-authored-by: raymondlopez279@gmail.com --- .gitignore | 1 + .vscode/settings.json | 5 +++-- cars/cars.json | 28 ++++++++++++++-------------- cars/main.py | 25 +++++++++++++++++++++++-- 4 files changed, 41 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index 49cd206..d24ba3a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ __pycache__ files/test.txt +cars/sorted_cars.json diff --git a/.vscode/settings.json b/.vscode/settings.json index 28f3330..3838ebe 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,7 @@ { "python.analysis.inlayHints.functionReturnTypes": true, "python.analysis.inlayHints.pytestParameters": true, - "python.analysis.inlayHints.variableTypes": true, - "python.analysis.typeCheckingMode": "strict" + "python.analysis.inlayHints.variableTypes": false, + "python.analysis.typeCheckingMode": "strict", + "python.analysis.inlayHints.callArgumentNames": "partial" } diff --git a/cars/cars.json b/cars/cars.json index 2d98026..123df1c 100644 --- a/cars/cars.json +++ b/cars/cars.json @@ -1,18 +1,18 @@ { - "Tahoe" : "Chevrolet", - "Mustang" : "Ford", - "Blazer" : "Chevrolet", - "NSX" : "Nissan", - "Fusion" : "Ford", - "Model S" : "Tesla", - "Civic" : "Honda", - "F150" : "Ford", - "Huracan": "Lamborghini", - "Volt": "Chevrolet", - "Silverado": "Chevrolet", - "CX-5" : "Mazda", "Accord": "Honda", + "Aventador": "Lamborghini", + "Blazer": "Chevrolet", "Camaro": "Chevrolet", - "Focus" : "Ford", - "Aventador" : "Lamborghini" + "Civic": "Honda", + "CX-5": "Mazda", + "F150": "Ford", + "Focus": "Ford", + "Fusion": "Ford", + "Huracan": "Lamborghini", + "Model S": "Tesla", + "Mustang": "Ford", + "NSX": "Nissan", + "Silverado": "Chevrolet", + "Tahoe": "Chevrolet", + "Volt": "Chevrolet" } diff --git a/cars/main.py b/cars/main.py index 306e638..722830b 100644 --- a/cars/main.py +++ b/cars/main.py @@ -1,5 +1,26 @@ import json +from typing import Any, Dict, List + + +def get_cars() -> Any: # Defining the function get_cars() + with open('cars.json', 'rt') as f: # Opens the cars.json file + return json.load(f) # Serializes the JSON data into a Python object + +def sort(data: Dict[str, str]) -> Dict[str, List[str]]: # Defining the function sort() + dictionary: Dict[str, List[str]] = {} # Creating an empty dictionary to store the sorted data + for car, manufacturer in data.items(): # Iterating through the input data + if manufacturer not in dictionary: # If the manufacturer does not already exist as a key in the new dictionary, + dictionary[manufacturer] = [car] # create a new key with the manufacturer as the key and the car as the value, in a list + else: # If the manufacturer does already exist as a key in the new dictionary, + dictionary[manufacturer].append(car) # append the car to the list of cars for that manufacturer + return dictionary # Return the dictionary object + +def save(data: Dict[str, Any]) -> None: + with open('sorted_cars.json', 'w') as f: # Opening the file sorted_cars.json for the purpose of writing to it + json.dump(data, f, indent=2) # Dumps the JSON-serialized python object into the sorted_cars.json file, with an indentation of 2 if __name__ == '__main__': - with open('cars.json') as f: - cars = json.load(f) + cars = get_cars() + sorted_cars = sort(cars) + save(sorted_cars) + print("Successfully sorted cars by manufacturer!")