Keysorting using Ordereddict?

Asked

Viewed 59 times

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
  • 3

    Both the function reversed as to sorted 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.

  • 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

1 answer

0

So, it doesn’t make much sense this order, but if that’s the case, follow the code:

class Produto:
    def __init__(self):
        self.text = self.show_weird_order(self.prod())

    def prod(self):
        estoque = []
        t = int(input(": "))
        for i in range(t):
            nome, valor = input().split()
            estoque.append((nome, float(valor)))
        print('\n')
        return estoque

    def show_weird_order(self, estoque):
        for i in range(1, len(estoque)+1):
            if i % 2 == 0:
                index = i*-1
                print(f"{len(estoque)+(index+1)} = {estoque[index][0]} - R${round(estoque[index][1], 2)}")
        for j in range(len(estoque)):
            if (j+1) % 2 == 0:
                print(f"{j+1} = {estoque[j][0]} - R${round(estoque[j][1], 2)}")

When to call the class:

exemplo = Produto()

I will pass:

6
celular 999.99
computador 2499.90
tv 1299.50
drive 149.50
usb 120.75
teclado 450.31

Will return:

5 = usb - R$120.75
3 = tv - R$1299.5
1 = celular - R$999.99
2 = computador - R$2499.9
4 = drive - R$149.5
6 = teclado - R$450.31

Anything, just customize according to your needs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.