Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cspt1 alexis sc intro python #2

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
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
11 changes: 11 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]

[dev-packages]

[requires]
python_version = "3.7"
21 changes: 21 additions & 0 deletions src/cityreader.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Create a class to hold a city location. Call the class "City". It should have
# fields for name, latitude, and longitude.
import csv

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

# TODO

Expand All @@ -18,10 +25,24 @@

cities = []

with open('cities.csv') as csvfile:
csvCityData = csv.reader(csvfile, delimiter=',')
next(csvCityData)
for data in csvCityData:
# print(data[0], "city")
# print(data[3], "lat")
# print(data[4], "lon")
city = City(data[0], data[3], data[4])
cities.append(city)

# TODO

# Print the list of cities (name, lat, lon), 1 record per line.

# loop through list/ array usinf a for in

for i in cities:
print(f'City {i.name}, Lattitude {i.lat}, Longitude {i.lon}')
# TODO

# *** STRETCH GOAL! ***
Expand Down
24 changes: 16 additions & 8 deletions src/comp.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import math

class Human:
def __init__(self, name, age):
self.name = name
Expand All @@ -23,49 +25,55 @@ def __repr__(self):
# whose name starts with 'D':

print("Starts with D:")
r = [] # TODO
r = [ i.name for i in humans if i.name[0] == 'D' ] # TODO
print(r)

# Write a list comprehension that creates a list of names of everyone
# whose name ends in "e".

print("Ends with e:")
r = [] # TODO
# Need to split the string. If i.name[:-2] is last two character, then i.name[-1 is last character of string?]
r = [ i.name for i in humans if i.name[-1] == 'e' ] # TODO
print(r)

# 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:")
r = [] # TODO
letters = ['C', 'D', 'E', 'F', 'G']
r = [ i.name for i in humans if i.name[0] in letters ] # TODO
print(r)

# Write a list comprehension that creates a list of all the ages plus 10.
print("Ages plus 10:")
r = [] # TODO
r = [ i.age + 10 for i in humans ] # TODO
print(r)

# 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:")
r = [] # TODO
r = [ '{0}-{1}'.format(i.name, i.age) for i in humans ] # TODO
print(r)

# 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:")
r = [] # TODO
ages = [27, 28, 29, 30, 31, 32]
r = [ ( i.name, i.age ) for i in humans if i.age in ages ] # TODO
print(r)

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

# Create new Humans class then?

print("All names capitalized:")
r = [] # TODO
r = [ Human( i.name.upper(), i.age + 5 ) for i in humans ] # TODO
print(r)

# Write a list comprehension that contains the square root of all the ages.
print("Square root of ages:")
r = [] # TODO
r = [ math.sqrt(i.age) for i in humans ] # TODO
print(r)
27 changes: 27 additions & 0 deletions src/oop1.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,30 @@
# pass
#
# Put a comment noting which class is the base class

class Vehicle:
pass

class GroundVehicle(Vehicle):
pass

class Car(GroundVehicle):
pass

class Motorcycle(GroundVehicle):
pass



# ---------------------------------------------------------



class FlightVehicle:
pass

class Airplane(FlightVehicle):
pass

class Starship(FlightVehicle):
pass
16 changes: 15 additions & 1 deletion src/oop2.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# To the GroundVehicle class, add method drive() that prints "vroooom".

#
# Also change it so the num_wheels defaults to 4 if not specified when the
# object is constructed.

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

def drive(self):
print("vroooooooooooooooooooooooooom")

# TODO


Expand All @@ -19,6 +23,13 @@ def __init__(self, num_wheels):

# TODO

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

def drive(self):
print("BRAAAAAP!!")

vehicles = [
GroundVehicle(),
GroundVehicle(),
Expand All @@ -29,4 +40,7 @@ def __init__(self, num_wheels):

# Go through the vehicles list and call drive() on each.

for v in vehicles:
v.drive()

# TODO