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.
57 changes: 54 additions & 3 deletions src/cityreader/cityreader.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
# 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).
class City:
def __init__(self, name, lat, lon):
self.name = name
self.lat = lat
self.lon = lon

def __str__(self):
output = f'City Class: City:{self.name}, latitude: {self.lat}, longitude: {self.lon}.\n'
print(output)
return output

# 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 @@ -14,20 +23,27 @@
#
# Note that the first line of the CSV is header that describes the fields--this
# should not be loaded into a City object.

import csv

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

with open('cities.csv', newline='') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
if row[3] != 'lat':
print(row[0], row[3], row[4])
return cities

cityreader(cities)

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

# STRETCH GOAL!
#
Expand Down Expand Up @@ -60,12 +76,47 @@ def cityreader(cities=[]):

# TODO Get latitude and longitude values from the user

selection_one = input('Select lat1 and lon1: ').strip().split(',')
selection_two = input('Select lat2 and lon2: ').strip().split(',')
# selection_one = int(selection_one)
# selection_two = int(selection_two)

try:
lat1, lon1 = [selection_one[0], selection_one[1]]
lat2, lon2 = [selection_two[0], selection_two[1]]
except:
print('Error occured...')

def cityreader_stretch(lat1, lon1, lat2, lon2, cities=[]):


# within will hold the cities that fall within the specified region
within = []

try:
lat1 = float(lat1)
lon1 = float(lon1)
lat2 = float(lat2)
lon2 = float(lon2)
except:
'did you float?'

check_latlon = [
City('Boston', 70, 20),
City('New York City', 60, 10),
City('Washington, D.C.', 45, -20)
]

for c in check_latlon:
if float(c.lon) in range(lon1, lon2, ''):
within.append(f'{c.name}: {c.lat}, {c.lon}')



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

print(within)
return within

cityreader_stretch(lat1, lon1, lat2, lon2, cities)
16 changes: 8 additions & 8 deletions src/comp/comp.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,48 +24,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 = [idx.name for idx in humans if idx.name[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 = [end.name for end in humans if end.name[-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 = [letter.name for letter in humans if letter.name[0] >= 'C' and letter.name[0] <= 'G']
print(c)

# Write a list comprehension that creates a list of all the ages plus 10.
print("Ages plus 10:")
d = []
d = [ a.age + 10 for a 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 = [(s.name + '-' + str(s.age)) for s 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 = [(t.name, t.age) for t in humans if t.age > 26 and t.age < 33]
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(u.name.upper(), u.age + 5) for u 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(sq.age) for sq 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.
88 changes: 88 additions & 0 deletions src/oop/oop1.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,91 @@
# pass
#
# Put a comment noting which class is the base class

# base class - Vehicle

class Vehicle:
# base class
pass

class GroundVehicle(Vehicle):
# secondary class (parent is Vehicle)
pass

class Car(GroundVehicle):
# third level class Grandparent Vehicle, parent is GroundVehicle
pass

class Motorcycle(GroundVehicle):
# third level class Grandparent Vehicle, parent is GroundVehicle
pass

class FlightVehicle(Vehicle):
# secondary class (parent is Vehicle)
pass

class Starship(FlightVehicle):
# third level class Grandparent Vehicle, parent is FlightVehicle
pass

class Airplane(FlightVehicle):
# third level class Grandparent Vehicle, parent is FlightVehicle
pass


# class Vehicle:
# def __init__(self, vehicle):
# self.vehicle = vehicle

# def __str__(self):
# return f'the Vehicle name is {self.vehicle}.'


# class FlightVehicle(Vehicle):
# def __init__(self, vehicle, flight_vehicle):
# super().__init__(vehicle)
# self.flight_vehicle = flight_vehicle

# def __str__(self):
# return f'Class FlightVehicle vehicle: {self.vehicle}; flight_vehicle: {self.flight_vehicle}.'

# class Starship(FlightVehicle):
# def __init__(self, vehicle, flight_vehicle, starship):
# super().__init__(vehicle, flight_vehicle)
# self.starship = starship

# def __str__(self):
# return f'Class Starship vehicle: {self.vehicle}; \n flight_vehicle: {self.flight_vehicle}, \n starship: {self.starship}.'

# class Airplane(FlightVehicle):
# def __init__(self, vehicle, flight_vehicle, airplane):
# super().__init__(vehicle, flight_vehicle)
# self.airplane = airplane

# def __str__(self):
# return f'Class Airplane vehicle: {self.vehicle}; \n flight_vehicle: {self.flight_vehicle}, \n airplane: {self.airplane}.'


# class GroundVehicle(Vehicle):
# def __init__(self, vehicle, ground_vehicle):
# super().__init__(vehicle)
# self.ground_vehicle = ground_vehicle

# def __str__(self):
# return f'Class GroundVehicle vehicle: {self.vehicle}, \n ground_vehicle: {self.vehicle}'

# class Car(GroundVehicle):
# def __init__(self, vehicle, ground_vehicle, car):
# super().__init__(vehicle, ground_vehicle)
# self.car = car

# def __str__(self):
# return f'Class Car vehicle: {self.vehicle}, \n ground_vehicle: {self.vehicle} \n car: {self.car}'

# class Motorcycle(GroundVehicle):
# def __init__(self, vehicle, ground_vehicle, motorcycle):
# super().__init__(vehicle, ground_vehicle)
# self.motorcycle = motorcycle

# def __str__(self):
# return f'Class Motorcycle vehicle: {self.vehicle}, \n ground_vehicle: {self.vehicle} \n motorcycle: {self.motorcycle}'
26 changes: 24 additions & 2 deletions src/oop/oop2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,21 @@
# object is constructed.

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

# TODO
def __str__(self):
output = f'Groundvehicle Class - number of wheels: {self.num_wheels}.'
return output

def drive(self):
drive = "vroooom"
print(drive)
return f'{drive}'





# Subclass Motorcycle from GroundVehicle.
Expand All @@ -16,8 +27,18 @@ def __init__(self, num_wheels):
# of wheels to 2 by passing that to the constructor of its superclass.
#
# Override the drive() method in Motorcycle so that it returns "BRAAAP!!"
class Motorcycle(GroundVehicle):
def __init__(self, num_wheels=2):
super().__init__(num_wheels)

def __str__(self):
output = f'Motorcycle Class - number of wheels: {self.num_wheels}.'
return output
# TODO
def drive(self):
drive = "BRAAAP!!"
print(drive)
return f'{drive}'

vehicles = [
GroundVehicle(),
Expand All @@ -28,5 +49,6 @@ def __init__(self, num_wheels):
]

# Go through the vehicles list and print the result of calling drive() on each.

for d in vehicles:
print( f'Vehicle drive sound: ', d.drive())
# TODO