finished Cars
Co-authored-by: raymondlopez279@gmail.com <raymondlopez279@gmail.com>
This commit is contained in:
parent
df27844a36
commit
bfceebd171
4 changed files with 41 additions and 18 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,2 +1,3 @@
|
||||||
__pycache__
|
__pycache__
|
||||||
files/test.txt
|
files/test.txt
|
||||||
|
cars/sorted_cars.json
|
||||||
|
|
5
.vscode/settings.json
vendored
5
.vscode/settings.json
vendored
|
@ -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"
|
||||||
}
|
}
|
||||||
|
|
|
@ -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"
|
||||||
}
|
}
|
||||||
|
|
25
cars/main.py
25
cars/main.py
|
@ -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!")
|
||||||
|
|
Loading…
Reference in a new issue