Identify missing value in numerical sequence

0

There is a set of cards numbered from 1 to N. Within this set a card was lost, determine the number of the lost card based on the remaining cards.

Given an N number, followed by N-1 integer values representing the numbers of the remaining cards, Find and print the number of the lost card.

Example input

5
3
5
2
1

Example output

4

Well this is the exercise, with the help of the comments I got to the following code

num1 = int(input())
conj1 = set(range(1, num1+1))

for i in range(1,num1):
  valores1 = int(input())
  conj2 =
#          "aqui está o problema, quero que o conj2 seja um conjunto 
#           com todos os numeros digitados em valores1 como fazer isso?"

lost = conj1 ^ conj2
    
asked by anonymous 16.05.2018 / 22:15

4 answers

1
Assuming that the numbered cards from 1 to N are sequential; that is, a 5-letter deck would have faces 1, 2, 3, 4 and 5, so the solution is trivial using the set class of Python. Just define the two sets and calculate the difference between them.

The complete deck can be defined as:

deck = set(range(1, N+1))

While the deck described by the entry can be defined by:

hand = {1, 2, 3, 5, 5}

At this point it was not clear if it really should be two cards 5 or if it was a typo in the question, but this will not interfere with the solution. To determine the lost card, simply search for the card that is in deck which is not in hand , calculating the difference between the two sets, with the ^ :

lost_card = deck ^ hand

For this example, the return would be {4} , indicating that letter 4 was lost.

    
18.05.2018 / 02:36
1

Based on the excellent Anderson hint, however, instead of thinking about the number of fixed cards to idea is to see the highest value card, assuming that this is an exercise based on something that will evolve, like more cards or even "suits"

Then you would use the max(...) function instead of a fixed number (or instead of len() ), thus would have the letter of greater value, would look like this:

hand = {1, 2, 3, 5, 5}
higher = max(hand)
deck = set(range(1, higher+1))
lost_cards = deck ^ hand

print(lost_cards)

So assuming you'll have something possible like:

hand = {1, 2, 3, 4, 4}

So I imagine if it really is a set of cards a situation of repeating so could occur.

Example on repl: link

Then in one repetition it would end up that 4 would return as missing, like this: {2, 4}

    
18.05.2018 / 03:01
0

I do not quite understand the question, but this can help:

entrada = [] #lista de valores recuperados
N = -(float('inf')) # diz que N é muito pequeno(Eu não sabia o que usar)

print('informe a entrada:')

        #A quantidade de valores como entrada é definida? usei 5
for i in range(5):
    value = int(input('>'))
    entrada.append(value)
    #Use isso para saber o intervalo máximo
    if value > N:
        N = value

for i in range(1,N):
    #Apos ter capturado o maior valor, percorro todos os valores de 1 a N
    #Se algum não for encontrado, esse é o valor da carta
    if i not in entrada:
        print(i)

This will give you a light (Possibly). good studies

    
17.05.2018 / 23:02
0

There is an option, I did not do many tests, but it is working at first

I ask the size of the deck in the entry and add it in a list, then "lose" a card and finally check the sequence if it is ok

from random import randint
cartas = []
n = int(input("Quantas cartas há: "))

for i in range(n):
    cartas.append(i + 1)

perdida = randint(0, n-1)

cartas[perdida] =  "x"

for i in range(n + 1):
    if cartas[i -1] != i:
        carta_perdida =  i
print(cartas)
print("A carta perdida é: ", carta_perdida)
    
17.05.2018 / 23:37