0
The code below shows that the elements are ordered with odd keys in descending order and those with even keys in ascending order, but are not being ordered according to the rule.
from collections import OrderedDict
class Produto ():
def __init__(self, nome, preco):
self.__nome = nome
self.__preco = preco
def __str__(self):
return "{!s} - R${:.2f}".format(self.__nome, self.__preco)
if __name__ == "__main__":
t = int(input())
# Informe seu código aqui
estoque = OrderedDict()
for i in range(1, t+1):
chave, nome, valor = input().split()
prod = Produto(nome, float(valor))
estoque[int(chave)] = prod
for k, v in estoque.items():
if k % 2 != 0:
reversed(estoque.keys())
else:
sorted(estoque.keys())
[print("{:d}={!s}".format(k,v)) for k, v in estoque.items()]
Entree:
6
1 celular 999.99
2 computador 2499.90
3 tv 1299.50
4 drive 149.50
5 usb 120.75
6 teclado 450.31
My Way out:
1=celular - R$999.99
2=computador - R$2499.90
3=tv - R$1299.50
4=drive - R$149.50
5=usb - R$120.75
6=teclado - R$450.31
Expected Departure:
5=usb - R$120.75
3=tv - R$1299.50
1=celular - R$999.99
2=computador - R$2499.90
4=drive - R$149.50
6=teclado - R$450.31
Both the function
reversed
as tosorted
return the sorted sequence; you ignored the return of the two in your code, so effectively you did not order your sequence. For ease, I recommend separating two distinct sequences, of which is even of what is odd.– Woss
Flavio, if any answer solved your problem, you can mark one of them as accepted. Understand the importance of this link: https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-reply
– Lucas