How to print the number that appears odd times (Python 3)

Asked

Viewed 73 times

-5

num = input()
numerosString = num.split(" ")
numeros = [int(numero) for numero in numerosString] 
numeros.sort()
print(" ".join(map(str,numeros)))

The question asks as output in the first line the ascending order of the numbers given (this I was able to complete), but it asks that in the bottom line (2nd line), be printed the number that appears odd times. How do I return this number? NOTE: do not confuse with the AMOUNT of numbers that repeat odd times, this is not what I want, to clarify following examples:

On receipt: 1 2 3 1 2 3 4

What should return: "4" because it is the only number that appeared odd times (1 time is the amount)

Quando receber: 1 2 2 1 3 3 4 4 5 5 6 7 8 6 8 7 6 9 9

What should return: "6" because it is the only number that appeared odd times (3 times is the amount)

2 answers

1

From what I understand you want to implement a script that is able to display the value(s) whose(s) count of your occurrences is an odd number.

Well, for that we must make the following logic:

  1. Take the values UNREPEATED from the past list;
  2. Sort these values. Optional. Displays the output results in ascending order;
  3. Assemble an eternal object - either a list or a dictionary - containing each value together with the count of its occurrence;
  4. Display value(s) whose occurrence count is odd.

Following this logic, we can implement a script with list of lists or with a dictionary.

Implementation with list list:

def impares_vezes(li):
    return [j[0] for j in [[i, li.count(i)] for i in sorted(set(li))] if j[1] % 2]


if __name__ == '__main__':
    lista = list(map(int, input().split()))
    resp = impares_vezes(lista)

    print(*resp)

Dictionary implementation:

def impares_vezes(li):
    return [k for k, v in {i: li.count(i) for i in sorted(set(li))}.items() if v % 2]


if __name__ == '__main__':
    lista = list(map(int, input().split()))
    resposta = impares_vezes(lista)

    print(*resposta)

Note that when we execute the two codes, we receive the blinking cursor in the upper left corner of the screen. At this time we must type all the values of that list, in same line, separated for a single space and press Enter.

After this, the listed values will be passed as parameters to the function odd-fold(). Once there, the repeated values will be removed and ordered in an increasing order.

Subsequently, according to the script chosen, a list of lists or a dictionary containing, respectively, the said value together with the count of its occurrence.

Then another list will be mounted containing only the value(s) whose occurrence count results in an odd number.

OBS:

The last line of the code...

print(*resposta)

...is just unpacking the possible values stored in the list.

1


Always look for the simplest alternative.

Use an accountant collection.Counter to know how many times an element has repeated itself in a list and use the rest of the division of a number by 2 to know their parity:

from collections import Counter

num = input("Digite a lista de inteiros:\n")
numeros = list(map(int, num.split(" ")))      #Quebra a entrada numa lista de números.
numeros.sort()                                #Oredena a lista de números.

print(*numeros)                               #Imprime a a lista de ordenada números.

#Itera pelos itens do contador contador onde k é a chave(elemento da lista numeros) e v é o seu respectivo valor(quantidade de aparições do elemento na lista numeros)...
for k,v in Counter(numeros).items():
  #...se o número de aparições do número for impar...
  if v % 2 != 0:
    print(k, end=" ")                         #...o imprime na segunda linha.

Upshot:

Digite a lista de inteiros:
1 2 2 1 3 3 4 4 5 5 6 7 8 6 8 7 6 9 9
1 1 2 2 3 3 4 4 5 5 6 6 6 7 7 8 8 9 9
6

Test the code on Repl.it

Browser other questions tagged

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