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
Binary file not shown.
44 changes: 41 additions & 3 deletions src/cityreader/cityreader.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import csv
# Create a class to hold a city location. Call the class "City". It should have
# fields for name, latitude, and longitude.

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

def __repr__(self):
return f'Name: {self.name} Latitude: {self.lat} Longitude: {self.lon}'

def __str__(self):
return f'Name: {self.name} Latitude: {self.lat} Longitude: {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".)
Expand All @@ -20,8 +32,12 @@ 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

return cities
with open('cities.csv') as f:
reader = csv.DictReader(f)
for row in reader:
cities.append(City(row['city'], row['lat'], row['lng']))

return cities

cityreader(cities)

Expand Down Expand Up @@ -59,13 +75,35 @@ def cityreader(cities=[]):
# Salt Lake City: (40.7774,-111.9301)

# TODO Get latitude and longitude values from the user
input1 = input('Enter lat1, long1: ')
latNlong1 = input1.split(',')
lnlfloats1 = [float(n) for n in latNlong1]

input2 = input('Enter lat2, long2: ')
latNlong2 = input2.split(',')
lnlfloats2 = [float(n) for n in latNlong2]

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

newLat1 = lat1
newLat2 = lat2
newLon1 = lon1
newLon2 = lon2
# 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.
if lat1 > lat2:
newLat1 = lat2
newLon1 = lon2
newLat2 = lat1
newLon2 = lon1

for c in cities:
if float(c.lat) >= newLat1 and float(c.lat) <= newLat2 and float(c.lon) >= newLon1 and float(c.lon) <= newLon2:
within.append(c)

return within

print(cityreader_stretch(lnlfloats1[0], lnlfloats1[1], lnlfloats2[0], lnlfloats2[1], cities))

Binary file added src/comp/__pycache__/comp.cpython-37.pyc
Binary file not shown.
17 changes: 9 additions & 8 deletions src/comp/comp.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
# The following list comprehension exercises will make use of the
# defined Human class.
class Human:
Expand All @@ -24,48 +25,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 = [n.name for n in humans if n.name[0][0] == '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 = [n.name for n in humans if n.name[-1][-1] == '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 = [n.name for n in humans if re.search('[c-gC-G]', n.name[0][0])]
print(c)

# Write a list comprehension that creates a list of all the ages plus 10.
print("Ages plus 10:")
d = []
d = [n.age+10 for n 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'{n.name}-{n.age}' for n 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 = [(n.name, n.age) for n in humans if n.age <= 32 and n.age >= 27]
print(f)

# Write a list comprehension that creates a list of new Humans like the old
# list, except with all the names capitalized and the ages with 5 added to them.
# The "humans" list should be unmodified.
print("All names capitalized:")
g = []
g = [Human(n.name.upper(), n.age+5) for n 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(n.age) for n in humans]
print(h)
Binary file added src/oop/__pycache__/oop1.cpython-37.pyc
Binary file not shown.
Binary file added src/oop/__pycache__/oop2.cpython-37.pyc
Binary file not shown.
26 changes: 26 additions & 0 deletions src/oop/oop1.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,29 @@
# pass
#
# Put a comment noting which class is the base class

# Base class
class Vehicle:
pass

# Class is subclass of Vehicle
class GroundVehicle(Vehicle):
pass

# Class is subclass of Vehicle
class FlightVehicle(Vehicle):
pass

# Classes are subclasses of GroundVehicle
class Car(GroundVehicle):
pass

class Motorcycle(GroundVehicle):
pass

# Classes are subclasses of FlightVehicle
class Airplane(FlightVehicle):
pass

class Starship(FlightVehicle):
pass
13 changes: 12 additions & 1 deletion src/oop/oop2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
# 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.
#
Expand All @@ -18,6 +20,12 @@ def __init__(self, num_wheels):
# Override the drive() method in Motorcycle so that it returns "BRAAAP!!"

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

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

vehicles = [
GroundVehicle(),
Expand All @@ -30,3 +38,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:
print(v.drive())