1
1
Fork 0

finished Cars
Some checks failed
Lint Code / Ruff (push) Successful in 9s
Lint Code / MyPy (push) Failing after 13s
Lint Code / Pylint (push) Failing after 13s

Co-authored-by: raymondlopez279@gmail.com <raymondlopez279@gmail.com>
This commit is contained in:
Seaswimmer 2024-06-06 22:19:00 -04:00
parent df27844a36
commit bfceebd171
Signed by: cswimr
GPG key ID: 5D671B5D03D65A7F
4 changed files with 41 additions and 18 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
__pycache__ __pycache__
files/test.txt files/test.txt
cars/sorted_cars.json

View file

@ -1,6 +1,7 @@
{ {
"python.analysis.inlayHints.functionReturnTypes": true, "python.analysis.inlayHints.functionReturnTypes": true,
"python.analysis.inlayHints.pytestParameters": true, "python.analysis.inlayHints.pytestParameters": true,
"python.analysis.inlayHints.variableTypes": true, "python.analysis.inlayHints.variableTypes": false,
"python.analysis.typeCheckingMode": "strict" "python.analysis.typeCheckingMode": "strict",
"python.analysis.inlayHints.callArgumentNames": "partial"
} }

View file

@ -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", "Accord": "Honda",
"Aventador": "Lamborghini",
"Blazer": "Chevrolet",
"Camaro": "Chevrolet", "Camaro": "Chevrolet",
"Focus" : "Ford", "Civic": "Honda",
"Aventador" : "Lamborghini" "CX-5": "Mazda",
"F150": "Ford",
"Focus": "Ford",
"Fusion": "Ford",
"Huracan": "Lamborghini",
"Model S": "Tesla",
"Mustang": "Ford",
"NSX": "Nissan",
"Silverado": "Chevrolet",
"Tahoe": "Chevrolet",
"Volt": "Chevrolet"
} }

View file

@ -1,5 +1,26 @@
import json 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__': if __name__ == '__main__':
with open('cars.json') as f: cars = get_cars()
cars = json.load(f) sorted_cars = sort(cars)
save(sorted_cars)
print("Successfully sorted cars by manufacturer!")