Download and install Python 2.7.x. Fill in the 4 functions in hw1_template.py
. The functions are:
sortwithloops
. Take the input list and return a sorted list, do not use list functions. Do this just looping through the values. Use any kind of sort you wish.def sortwithloops(input):
output = input
for n in range(len(output) - 1, 0, -1):
for i in range(n):
if output[i] > output[i + 1]:
temp = output[i]
output[i] = output[i + 1]
output[i + 1] = temp
return output
if __name__ == "__main__":
L = [5,3,6,3,13,5,6]
Q = [1,5,8,9,2]
print sortwithloops(L) # [3, 3, 5, 5, 6, 6, 13]
## [3, 3, 5, 5, 6, 6, 13]
sortwithoutloops
: Take the input list and return a sorted list, use list functions.def sortwithoutloops(input):
output = input
output.sort()
return output
if __name__ == "__main__":
L = [5,3,6,3,13,5,6]
Q = [1,5,8,9,2]
print sortwithoutloops(L) # [3, 3, 5, 5, 6, 6, 13]
## [3, 3, 5, 5, 6, 6, 13]
searchwithloops
: Takes two inputs, the list and a value to search for. Returns true if the value is in the list, otherwise false. Do not use list functions. Do this just looping through, with any kind of search.def searchwithloops(input, value):
for i in range(1, len(input)):
if value == input [i]:
output = True
break
else:
output = False
return output #return a value
if __name__ == "__main__":
L = [5,3,6,3,13,5,6]
Q = [1,5,8,9,2]
print searchwithloops(L, 5) #true
print searchwithloops(L, 11) #false
## True
## False
searchwithoutloops
: Takes two inputs, the list and a value to search for. Returns true if the value is in the list, otherwise false. Use list functions.def searchwithoutloops(input, value):
output = value in input
return output #return a value
if __name__ == "__main__":
L = [5,3,6,3,13,5,6]
Q = [1,5,8,9,2]
print searchwithoutloops(L, 5) #true
print searchwithoutloops(L, 11) #false
## True
## False
Fill in the CarEvaluation
class. We won’t be doing a lot of OOP in this course, but it is good to know how to represent some data sets as objects. The class needs to provide what is needed to get through the main at the end of the file. This mean you’ll need at least a constructor that takes values as (Brand, Price, Safety Rating), a function called showEvaluation
and an attribute carCount
. I’ll leave the details up to you.
class CarEvaluation():
'A simple class that represents a car evaluation'
carCount = 0
def __init__(self, Brand, Price, Rating):
self.Brand = Brand
self.Price = Price
self.Rating = Rating
CarEvaluation.carCount += 1
def showEvaluation(self):
print "The", self.Brand, "has a", self.Price, "price and it's safety is rated a", self.Rating
if __name__ == "__main__":
eval1 = CarEvaluation("Ford", "High", 2)
eval2 = CarEvaluation("GMC", "Med", 4)
eval3 = CarEvaluation("Toyota", "Low", 3)
print "Car Count = %d" % CarEvaluation.carCount # Car Count = 3
eval1.showEvaluation() #The Ford has a High price and it's safety is rated a 2
eval2.showEvaluation() #The GMC has a Med price and it's safety is rated a 4
eval3.showEvaluation() #The Toyota has a Low price and it's safety is rated a 3
## Car Count = 3
## The Ford has a High price and it's safety is rated a 2
## The GMC has a Med price and it's safety is rated a 4
## The Toyota has a Low price and it's safety is rated a 3
Fill in the function sortbyprice
. This takes a list of CarEvaluation
objects as input, and sorts it by price in the specified order.
class CarEvaluation():
'A simple class that represents a car evaluation'
carCount = 0
def __init__(self, Brand, Price, Rating):
self.Brand = Brand
self.Price = Price
self.Rating = Rating
CarEvaluation.carCount += 1
def showEvaluation(self):
print "The", self.Brand, "has a", self.Price, "price and it's safety is rated a", self.Rating
def sortbyprice(input, value):
for i in range(len(input)):
if input[i].Price == "Low":
input[i].Price = 1
elif input[i].Price == "Med":
input[i].Price = 2
else:
input[i].Price = 3
order = value == "des"
input = sorted(input, key=lambda input: input.Price, reverse = order)
output = []
for i in range(len(input)):
output.append(input[i].Brand)
return output
if __name__ == "__main__":
eval1 = CarEvaluation("Ford", "High", 2)
eval2 = CarEvaluation("GMC", "Med", 4)
eval3 = CarEvaluation("Toyota", "Low", 3)
L = [eval1, eval2, eval3]
print sortbyprice(L, "asc"); #[Toyota, GMC, Ford]
print sortbyprice(L, "des"); #[Ford, GMC, Toyota]
## ['Toyota', 'GMC', 'Ford']
## ['Ford', 'GMC', 'Toyota']
Fill in the function searchforsafety
. This takes the list of CarEvaluation
objects as input, searches for a value in the safety ratings, and returns true or false.
class CarEvaluation():
'A simple class that represents a car evaluation'
carCount = 0
def __init__(self, Brand, Price, Rating):
self.Brand = Brand
self.Price = Price
self.Rating = Rating
CarEvaluation.carCount += 1
def showEvaluation(self):
print "The", self.Brand, "has a", self.Price, "price and it's safety is rated a", self.Rating
def searchforsafety(input, value):
for i in range(len(input)):
if value == input[i].Rating:
output = True
break
else:
output = False
return output #return a value
if __name__ == "__main__":
eval1 = CarEvaluation("Ford", "High", 2)
eval2 = CarEvaluation("GMC", "Med", 4)
eval3 = CarEvaluation("Toyota", "Low", 3)
L = [eval1, eval2, eval3]
print searchforsafety(L, 2); #true
print searchforsafety(L, 1); #false
## True
## False
Install Python 2.7.x: https://www.python.org/downloads/
Install Anaconda: https://www.continuum.io/downloads
Install PyCharm Edu: https://www.jetbrains.com/pycharm-edu/
https://developers.google.com/edu/python/
https://interactivepython.org/runestone/static/pythonds/SortSearch/TheBubbleSort.html