Identify missing value in numerical sequence

Asked

Viewed 208 times

0

There is a set of cards numbered from 1 to N. Among 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,.

Input example

5
3
5
2
1

Example of an exit

4

Well this is the exercise, with the help of the comments I arrived at 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
  • 1

    Remo, among this mentioned problem, you could share with us what you have tried and where you have doubt?

  • If you have trouble translating from English have a website Português SE where they can help you. Believe these machine translations are very ruims

  • So dude, I hadn’t met with a problem like this before. I honestly don’t know where to start. I believe what I need is for him to map from 1 to N, and then print the number that was not entered in this range. but how to do this?

4 answers

1

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

The full deck can be defined as:

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

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

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

At this point it was not clear whether it really should be two letters 5 or whether it was a typo in the question, but this will not interfere with the solution. To determine the lost card, just look for the card that is on deck who is not in hand, calculating the difference between the two sets, with the operator ^:

lost_card = deck ^ hand

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

  • Genial Anderson

  • but how do I receive these input values and already keep them sequentially in a set?

  • @Remo what information needs to read from user?

  • c have any email preu pass you the exercise print? do not know how to paste it Aki. (is a loop exercise, for) .

  • @Remo understood, but do the following: try to solve with the information passed by the answers and, if you still can’t, edit the question and add the code you did. With this we will be able to help better exactly where you are struggling.

1

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

Then use the function max(...) instead of a fixed number (or instead of len()), so I’d have the most valuable card, I’d be 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 supposing you come to have something as possible:

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

So I guess if it really is a set of cards a repeat situation like that could occur.

Example in repl: https://repl.it/@brcontainer/Missing-cards-in-handpy

Then in a repeat would end up that the 4 would return as missing, so: {2, 4}

0

I don’t quite understand the question, but this might 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

  • Liked it bro, thanks!

0

Follow an option, I haven’t done many tests, but at first it’s working

I request the size of the deck on the entry and add it to 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)

Browser other questions tagged

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