Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 61 additions & 10 deletions src/cityreader/cityreader.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import csv


# 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).

Expand All @@ -14,21 +17,40 @@
#
# Note that the first line of the CSV is header that describes the fields--this
# should not be loaded into a City object.

class City:
def __init__(self, name, lat, lon):
self.name = name
self.lat = lat
self.lon = lon

def __str__(self):
return f"{self.name}, {self.lat},{self.lon}"


cities = []


def cityreader(cities=[]):
# TODO Implement the functionality to read from the 'cities.csv' file
# For each city record, create a new City instance and add it to the
# `cities` list

# TODO Implement the functionality to read from the 'cities.csv' file
# For each city record, create a new City instance and add it to the
# `cities` list
with open('cities.csv') as csv_file:
csv_reader = csv.DictReader(csv_file)
line_count = 0
for row in csv_reader:
cities.append(City(row["city"], float(row["lat"]), float(row["lng"])))
return cities


cityreader(cities)


# Print the list of cities (name, lat, lon), 1 record per line.
for c in cities:
print(c)


# STRETCH GOAL!
#
# Allow the user to input two points, each specified by latitude and longitude.
Expand Down Expand Up @@ -60,12 +82,41 @@ def cityreader(cities=[]):

# TODO Get latitude and longitude values from the user

(lat1, lon1) = input('input lat1 and lon1: ').split(' ')
(lat2, lon2) = input('input lat2 and lon2: ').split(' ')


def cityreader_stretch(lat1, lon1, lat2, lon2, cities=[]):
# within will hold the cities that fall within the specified region
within = []
# print(cities)
# within will hold the cities that fall within the specified region
within = []

lat1 = float(lat1)
lat2 = float(lat2)
lon1 = float(lon1)
lon2 = float(lon2)

min_lat = lat2
max_lat = lat1
min_lon = lon2
max_lon = lon1

if lat1 < lat2:
max_lat = lat2
min_lat = lat1

if lon1 < lon2:
max_lon = lon2
min_lon = lon1

# TODO Ensure that the lat and lon valuse are all floats
# Go through each city and check to see if it falls within
# the specified coordinates.

# TODO Ensure that the lat and lon valuse are all floats
# Go through each city and check to see if it falls within
# the specified coordinates.
for city in cities:
if min_lat <= city.lat <= max_lat and min_lon <= city.lon <= max_lon:
within.append(city)
print(city)
return within

return within
cityreader_stretch(lat1, lon1, lat2, lon2, cities)
1 change: 0 additions & 1 deletion src/cityreader/test_stretch.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ def test_cityreader_stretch_correctness(self):
]

inp = cityreader_stretch(45, -100, 32, -120, self.cities)

self.assertEqual(len(inp), len(expected))

for i in range(len(inp)):
Expand Down
44 changes: 26 additions & 18 deletions src/comp/comp.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# The following list comprehension exercises will make use of the
# The following list comprehension exercises will make use of the
# defined Human class.
class Human:
def __init__(self, name, age):
Expand All @@ -8,6 +8,7 @@ def __init__(self, name, age):
def __repr__(self):
return f"<Human: {self.name}, {self.age}>"


humans = [
Human("Alice", 29),
Human("Bob", 32),
Expand All @@ -23,49 +24,56 @@ 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 = [human.name for human in humans if human.name[0] == 'D']
print(a)
print("Starts with D:", a)

# Write a list comprehension that creates a list of names of everyone
# whose name ends in "e".
print("Ends with e:")
b = []

b = [human.name for human in humans if human.name[len(human.name) - 1] == 'e']
print("Ends with e:", b)
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 = [human.name for human in humans if human.name[0] in ['C', 'D', 'E', 'F', 'G']]
print("Starts between C and G, inclusive:", c)
print(c)

# Write a list comprehension that creates a list of all the ages plus 10.
print("Ages plus 10:")
d = []

d = [int(human.age) + 10 for human in humans]
print("Ages plus 10:", d)
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"{human.name}-{human.age}" for human in humans]
print("Name hyphen age:", e)
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 = [(human.name, human.age) for human in humans if 27 <= human.age <= 32 ]
print("Names and ages between 27 and 32:", f)
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 = []
print(g)

g = [Human(human.name.upper(), human.age + 5) for human in humans]
print("All names uppercase:", 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(int(human.age)) for human in humans]
print("Square root of ages:", h)
print(h)
30 changes: 30 additions & 0 deletions src/oop/oop1.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,33 @@
# pass
#
# Put a comment noting which class is the base class


class Vehicle:
# base class
pass


class FlightVehicle(Vehicle):
# base class is Vehicle
pass

class GroundVehicle(Vehicle):
#base class Vehicle
pass

class Starship(FlightVehicle):
# base class is FlightVehicle
pass

class Airplane(FlightVehicle):
# base class is FlightVehicle
pass

class Car(GroundVehicle):
# base class GroundVehicle
pass

class Motorcycle(GroundVehicle):
# base class GroundVehicle
pass
18 changes: 17 additions & 1 deletion src/oop/oop2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
# object is constructed.

class GroundVehicle():
def __init__(self, num_wheels):
def __init__(self, num_wheels=4):
self.num_wheels = num_wheels

def drive(self):
return "vroooom"
# TODO


Expand All @@ -17,6 +19,14 @@ def __init__(self, num_wheels):
#
# Override the drive() method in Motorcycle so that it returns "BRAAAP!!"

class Motorcycle(GroundVehicle):
def __init__(self, num_wheels=2):
super().__init__(num_wheels)
self.num_wheels = 2

def drive(self):
return "BRAAAP!!"

# TODO

vehicles = [
Expand All @@ -30,3 +40,9 @@ def __init__(self, num_wheels):
# Go through the vehicles list and print the result of calling drive() on each.

# TODO

ground_vehicle = GroundVehicle()
motorcycle = Motorcycle()

print(ground_vehicle.drive())
print(motorcycle.drive())