The program takes as input a data-set of orders where 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 newline \n.
Input: 1, 500, 100; 2, 700, 100; 3, 100, 100; 4, 50, 50\n
Output: 4 3 1 2\n
My output however shows this
output: 4 1 2 3
Could somebody help me fix this? thanks in advance. below you can see my code. in the code there are some annotations from my teacher btw, don't mind them.
#!/usr/bin/env python3
import sys
class Order:
def __init__(self, id: int, selection_time: int, shipping_time: int):
self.id: int = id
self.selection_time: int = selection_time
self.shipping_time: int = shipping_time
'''
Remove me if you don't need me.
Add a method to assign to me.
'''
self.next: Order = None
'''
Make your life easier and your code prettier, use `Operator Overloading`.
'''
def sort(data):
sorted_order = selection_t shipping_t
for i in range(len(data)):
for j in range(i 1, len(data)):
if sorted_order[i] > sorted_order[j]:
data[i], data[j] = data[j], data[i]
return data
if __name__ == '__main__':
'''
Retrieves and splits the input
'''
data = input()
data = data.split('; ')
for d in data:
id, selection_t, shipping_t = d.split(', ', 2)
order: Order = Order(int(id), int(selection_t), int(shipping_t))
sort(data)
for order.id in data:
sys.stdout.write(order.id[0])
sys.stdout.write(" ")
CodePudding user response:
as pointed out by Matt, you're not actually using the Order class. Funny thing about classes is that they have a number of so-called magic methods (that naming is very correct)
The magic method that is useful to you in this case is __lt__
. This is an abbreviation for Lesser Than. Google search
If you set up this magic method for the class, you can simply call sort on a list containing only instances of that class.
If you do not want to use this magic method, the other option is to use a lambda to tell the sort method how to sort the list. This is very well explained here.
(also, I removed the sys.stdout
and replaced it with the standard print
)
#!/usr/bin/env python3
class Order:
def __init__(self, id: int, selection_time: int, shipping_time: int):
self.id: int = id
self.selection_time: int = selection_time
self.shipping_time: int = shipping_time
self.sort_value: int = shipping_time selection_time
def __lt__(self, other) -> bool:
return self.sort_value < other.sort_value
if __name__ == "__main__":
data = "1, 500, 100; 2, 700, 100; 3, 100, 100; 4, 50, 50"
data = data.split("; ")
order_list = []
order_list_lambda = []
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)
order_list_lambda.append(order)
print("using __lt__ class magic method")
order_list.sort()
for order in order_list:
print(order.id)
print("-----")
print("using lamda")
order_list_lambda.sort(key=lambda x: x.sort_value)
for order in order_list_lambda:
print(order.id)
output
using __lt__ class magic method
4
3
1
2
-----
using lamda
4
3
1
2