6 Replies - 996 Views - Last Post: 20 March 2013 - 11:09 PM Rate Topic: -----

#1 leogrr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 12-March 13

Comparing items

Posted 20 March 2013 - 11:08 AM

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.

Is This A Good Question/Topic? 0
  • +

Replies To: Comparing items

#2 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Comparing items

Posted 20 March 2013 - 04:22 PM

If you're comparing the items quantities, then yeah, item1.get_quantity == item2.get_quantity will test to see if they're equal. If you want to know which one is bigger, you'll likely want to use a ">" or "<" comparison.
Was This Post Helpful? 0
  • +
  • -

#3 leogrr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 12-March 13

Re: Comparing items

Posted 20 March 2013 - 04:59 PM

well my problem is I dont know what am suppose to compare quantity price, where does it say?
Was This Post Helpful? 0
  • +
  • -

#4 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Comparing items

Posted 20 March 2013 - 05:00 PM

I have no idea where it would say that lol. Is this a homework assignment?
Was This Post Helpful? 0
  • +
  • -

#5 leogrr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 12-March 13

Re: Comparing items

Posted 20 March 2013 - 06:14 PM

I messaged my TA he said either price or quantity.

So can I just do this:

def compare(item1, item2, attribute):
    if item1.get_quantity==item2.get_quantity:
        print("The items have the same quantity")
    elif item1.get_quantity<item2.get_quantity:
        print("Item2 has a gerter quantity")
    elif item1.get_quantity>item2.get_quantity:

Was This Post Helpful? 0
  • +
  • -

#6 leogrr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 12-March 13

Re: Comparing items

Posted 20 March 2013 - 07:11 PM

It seems right but you need to add the brackets after item*.get_quantity()

I* need to add brackets.

Is that correct?
Was This Post Helpful? 0
  • +
  • -

#7 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Comparing items

Posted 20 March 2013 - 11:09 PM

They're parenthesis. For example item2.get_quantity should be item2.get_quantity()
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1