from item import item
import random
def compare(item1, item2, attribute):
# complete this function
def quick_sort_goods(goods_list, attribute):
quick_sort_helper(goods_list, 0, len(goods_list)-1, attribute)
# a recursive quick sort method
def quick_sort_helper(goods_list, first, last, attribute):
if first < last:
splitpoint = partition(goods_list, first, last, attribute)
quick_sort_helper(goods_list, first,splitpoint-1, attribute)
quick_sort_helper(goods_list, splitpoint+1, last, attribute)
# to partition the given list by
# (1) picking the first int as the pivot value
# (2) re-arrange all integers accoding to the pivot value
def partition(goods_list, first, last, attribute):
pivotvalue = goods_list[first].get_price()
leftmark = first+1
rightmark = last
done = False
while not done:
while leftmark <= rightmark and \
compare(goods_list[leftmark], goods_list[first], attribute) <= 0:
leftmark = leftmark + 1
while compare(goods_list[rightmark], goods_list[first], attribute) >= 0 and \
rightmark >= leftmark:
rightmark = rightmark -1
if rightmark < leftmark:
done = True
else:
# exchange items pointed by the
# leftmark and rightmarks
temp = goods_list[leftmark]
goods_list[leftmark] = goods_list[rightmark]
goods_list[rightmark] = temp
temp = goods_list[first]
goods_list[first] = goods_list[rightmark]
goods_list[rightmark] = temp
return rightmark
def quick_sort_goods_test():
goods_list = []
for i in range(0, 10):
goods_list.append(item("goods"+str(i), random.randint(1,100), random.randint(1,100)))
for goods in goods_list:
print(goods)
print("----------------------")
quick_sort_goods(goods_list, "price")
for goods in goods_list:
print(goods)
print("----------------------")
quick_sort_goods(goods_list, "quantity")
for goods in goods_list:
print(goods)
quick_sort_goods_test()
class item:
def __init__(self, name, price, quantity):
self.__name = name
self.__price = price
self.__quantity = quantity
def set_name(self, name):
self.__name = name
def set_price(self, price):
self.__price = price
def set_quantity(self, quantity):
self.__quantity = quantity
def get_name(self):
return self.__name
def get_price(self):
return self.__price
def get_quantity(self):
return self.__quantity
def __str__(self):
return '[' + self.__name + ', ' + str(self.__price) \
+ ', ' + str(self.__quantity) + ']'
Im new to python and Im having alot of difficulty. How would I compare the two items and what would I compare? item1.get_quantity==item2.get_quantiy
Im having a difficult time understand this, any help would be great.

New Topic/Question
Reply


MultiQuote



|