diff --git a/src/cityreader/cityreader.py b/src/cityreader/cityreader.py index 2bd8007ce7..930687109e 100644 --- a/src/cityreader/cityreader.py +++ b/src/cityreader/cityreader.py @@ -1,11 +1,26 @@ + + # Create a class to hold a city location. Call the class "City". It should have # fields for name, lat and lon (representing latitude and longitude). +import csv + + +class City: + def __init__(self, name, lat, lon): + self.name = name + self.lat = lat + self.lon = lon + + def __repr__(self): + return f'{self.name}, {self.lat}, {self.lon}' + + # We have a collection of US cities with population over 750,000 stored in the # file "cities.csv". (CSV stands for "comma-separated values".) # -# In the body of the `cityreader` function, use Python's built-in "csv" module +# In the body of the `cityreader` function, use Python's built-in "csv" module # to read this file so that each record is imported into a City instance. Then # return the list with all the City instances from the function. # Google "python 3 csv" for references and use your Google-fu for other examples. @@ -16,14 +31,24 @@ # should not be loaded into a City object. cities = [] +# TODO Implement the functionality to read from the 'cities.csv' file +# Ensure that the lat and lon valuse are all floats +# For each city record, create a new City instance and add it to the +# `cities` list + +# open csv file for reading +# read the file and get the values in a comma-separated state +# append the list using our new class. (note the csv uses lng for longitude, and we have it set up here as lon) + + def cityreader(cities=[]): - # TODO Implement the functionality to read from the 'cities.csv' file - # Ensure that the lat and lon valuse are all floats - # For each city record, create a new City instance and add it to the - # `cities` list - + with open('cities.csv', 'r') as csvfile: + city_doc = csv.DictReader(csvfile, delimiter=',') + for e in city_doc: + cities.append(City(e['city'], float(e['lat']), float(e['lng']))) return cities + cityreader(cities) # Print the list of cities (name, lat, lon), 1 record per line. diff --git a/src/comp/comp.py b/src/comp/comp.py index 82f8821d63..904645d064 100644 --- a/src/comp/comp.py +++ b/src/comp/comp.py @@ -1,5 +1,8 @@ -# The following list comprehension exercises will make use of the -# defined Human class. +# The following list comprehension exercises will make use of the +# defined Human class. +import math + + class Human: def __init__(self, name, age): self.name = name @@ -8,6 +11,7 @@ def __init__(self, name, age): def __repr__(self): return f"" + humans = [ Human("Alice", 29), Human("Bob", 32), @@ -24,48 +28,48 @@ def __repr__(self): # Write a list comprehension that creates a list of names of everyone # whose name starts with 'D': print("Starts with D:") -a = [] +a = [name.name for name in humans if name.name.startswith('D')] print(a) # Write a list comprehension that creates a list of names of everyone # whose name ends in "e". print("Ends with e:") -b = [] +b = [name.name for name in humans if name.name.endswith('e')] print(b) # Write a list comprehension that creates a list of names of everyone # whose name starts with any letter between 'C' and 'G' inclusive. print("Starts between C and G, inclusive:") -c = [] +c = [name.name for name in humans if name.name[0] <= 'G' and name.name[0] >= 'C'] print(c) # Write a list comprehension that creates a list of all the ages plus 10. print("Ages plus 10:") -d = [] +d = [age.age + 10 for age in humans] print(d) # Write a list comprehension that creates a list of strings which are the name # joined to the age with a hyphen, for example "David-31", for all humans. print("Name hyphen age:") -e = [] +e = [f"{name.name}-{name.age}" for name in humans] print(e) # Write a list comprehension that creates a list of tuples containing name and # age, for example ("David", 31), for everyone between the ages of 27 and 32, # inclusive. print("Names and ages between 27 and 32:") -f = [] +f = [(ages.name, ages.age) + for ages in humans if ages.age >= 27 and ages.age <= 32] print(f) # Write a list comprehension that creates a list of new Humans like the old # list, except with all the names uppercase and the ages with 5 added to them. # The "humans" list should be unmodified. print("All names uppercase:") -g = [] +g = [Human(name.name.upper(), name.age + 5) for name in humans] print(g) # Write a list comprehension that contains the square root of all the ages. print("Square root of ages:") -import math -h = [] +h = [math.sqrt(age.age) for age in humans] print(h) diff --git a/src/oop/oop1.py b/src/oop/oop1.py index b7268c5263..aee882b62c 100644 --- a/src/oop/oop1.py +++ b/src/oop/oop1.py @@ -17,3 +17,30 @@ # pass # # Put a comment noting which class is the base class + +class Vehicle(): # Root Class + pass + + +class GroundVehicle(Vehicle): + pass + + +class FlightVehicle(Vehicle): + pass + + +class Starship(FlightVehicle): + pass + + +class Car(GroundVehicle): + pass + + +class Motorcycle(GroundVehicle): + pass + + +class Airplane(FlightVehicle): + pass diff --git a/src/oop/oop2.py b/src/oop/oop2.py index 29d3e481fe..4b4d7aab7b 100644 --- a/src/oop/oop2.py +++ b/src/oop/oop2.py @@ -4,10 +4,12 @@ # object is constructed. class GroundVehicle(): - def __init__(self, num_wheels): + def __init__(self, num_wheels=4): self.num_wheels = num_wheels # TODO + def drive(self): + return 'vroooom' # Subclass Motorcycle from GroundVehicle. @@ -19,6 +21,14 @@ def __init__(self, num_wheels): # TODO +class Motorcycle(GroundVehicle): + def __init__(self, num_wheels=2): + self.num_wheels = num_wheels + + def drive(self): + return 'BRAAAP!!' + + vehicles = [ GroundVehicle(), GroundVehicle(), @@ -30,3 +40,6 @@ def __init__(self, num_wheels): # Go through the vehicles list and print the result of calling drive() on each. # TODO +for v in vehicles: + engine = v.drive() + print(engine)