Home > database >  TypeError "list cannot be interpreted as integer" in bubblesort
TypeError "list cannot be interpreted as integer" in bubblesort

Time:09-15

I have to design an algorithm to sort a list of orders by selection time (t selection, finding the good in the warehouse and bringing it to the surface) plus shipping time (t shipping, constant). The customer orders can be retrieved (in the same order as placed) from a server database. You should expect between 100-10K elements.

The program takes as input a data-set of orders where the id, t selection, and t shipping are of type unsigned int, n is the number of orders and a space character.

id1, t selection1, t shipping1; ...; idn, t selectionn, t shippingn \n

The expected output is a space-separated list of the ids, sorted by t selection t shipping and terminated by a new line \n.

Input: 1, 500, 100; 2, 700, 100; 3, 100, 100\n

Output: 3 1 2\n

I am trying to do it with bubble sort, I do understand the algorithm, however, I am still a rookie when it comes to working with classes and objects. Could somebody help me out, see my code below. I am getting the error

Traceback (most recent call last):

File "C:\Users\31680\PycharmProjects\Assignment1\PY3-A1-boilerplates\Task-1\attachments\task-1.py", line 32, in <module>
sort(order_list)
File "C:\Users\31680\PycharmProjects\Assignment1\PY3-A1-boilerplates\Task-1\attachments\task-1.py", line 14, in sort
    for i in range(list_order):
TypeError: 'list' object cannot be interpreted as an integer

i do not know how to make this work.

    #!/usr/bin/env python3
    import sys


    class Order:
        def __init__(self, id: int, selection_time: int, shipping_time: int) -> object:
        self.id: int = id
        self.selection_time: int = selection_time
        self.shipping_time: int = shipping_time


    def sort(list_order):
       inp = len(list_order)
       for i in range(list_order):
          for j in range(0, list_order - i - 1):
              if shipping_t[j]   selection_t[j] < shipping_t[j   1]   selection_t[j   1]:
                 list_order[j], list_order[j   1] = list_order[j   1], list_order[j]
      return list_order


if __name__ == '__main__':
'''
Retrieves and splits the input
'''
data = input()
data = data.split('; ')
order_list = []
for d in data:
    id, selection_t, shipping_t = [int(s) for s in d.split(", ")]
    order: Order = Order(id, selection_t, shipping_t)
    order_list.append(order)
    sort(order_list)
    for order in order_list:
        print(order.id)

# sys.stdout.write(str(order.id))
# sys.stdout.write(" ")

CodePudding user response:

Two issues:

  • range(list_order) and list_order - i - 1 are not valid expressions as list_order is not a number, but a list. Should be inp.

  • shipping_t and selection_t are not defined variables. What you want is to evaluate attributes of list_order items, such as list_order[j].shipping_time and so on.

def sort(list_order):
    inp = len(list_order)
    for i in range(inp):
        for j in range(0, inp - i - 1):
            if list_order[j].shipping_time   list_order[j].selection_time < list_order[j   1].shipping_time   list_order[j   1].selection_time:
                list_order[j], list_order[j   1] = list_order[j   1], list_order[j]
    return list_order

A good editor / IDE would highlight such errors, making it easy to spot and correct them.

  • Related