-1

I haven't found anything even relevant to my question, so i may be asking it wrong.

I am working on an exercise where I am given sequential values starting at 1 and going to n, but not in order. I must find a missing value from the list.

My method is to add the full 1 => n value in a for loop but I can't figure out how to add n - 1 non-sequential values each as its own line of input in order to subtract it from the full value to get the missing one.

I have been searching modifications to for loops or just how to add n inputs of non-sequential numbers. If I am simply asking the wrong question, I am happy to do my own research if someone could point me in the right direction.

total = 0
for i in range (1 , (int(input())) + 1):
    total += i

print(total)

for s in **?????(int(input()))**:
    total -= s

print(total)

sample input: 5 3 2 5 1 expected output: 4

Dave
  • 11
  • 1
  • 5
  • 1
    Please show us the code you've written so far so we can help better – Tom Dalton Apr 12 '18 at 21:21
  • [This](https://stackoverflow.com/questions/20718315/how-to-find-a-missing-number-from-a-list) might help. – Itay4 Apr 12 '18 at 21:24
  • added code. sorry for the oversight. – Dave Apr 12 '18 at 21:41
  • At first you say you're given a list of values. That would make me think you are writing a function which would take a list as an argument, ie. something in the form `[5, 3, 2, 5, 1]`. But you are trying to write some code that takes command-line input using `input()`? I'm not sure how those two fit together. – Igid Apr 12 '18 at 21:53
  • sorry for the confusion. I am new at this. The input is n and then n - 1 values all as individual pieces of input. The sample input I gave is 5 individual line items of input. – Dave Apr 12 '18 at 22:01

2 Answers2

0

I don't know if you are supposed to create the initial data (with the missing item), so I added some lines to generate this sequence:

import random

n = 12 # or n = int(input('Enter n: ')) to get user input

# create a shuffled numeric sequence with one missing value
data = list(range(1,n+1))
data.remove(random.randrange(1,n+1))
random.shuffle(data)
print(data)

# create the corresponding reference sequence (without missing value)  
data2 = list(range(1,n+1))

# find missing data with your algorithm
print("Missing value =", sum(data2)-sum(data))

Here is the output:

[12, 4, 11, 5, 2, 7, 1, 6, 8, 9, 10]
Missing value = 3
sciroccorics
  • 2,357
  • 1
  • 8
  • 21
  • Thanks for the help. I think my real issue is that I do not get to generate the list myself. I have n individual items coming in through input(). I can get the first value and deal with that but can't figure out how to handle the remaining n - 1 values, each as individual inputs. – Dave Apr 12 '18 at 22:06
  • OK, I didn't understand that from your OP. Do you know the value of **n** before starting the loop over `input()` or do you rely on some specific entered value (e.g. empty line or 0) to stop the loop ? Because the solution is very different for both cases... – sciroccorics Apr 12 '18 at 22:43
0

To fill in the approach you're using in your example code:

total = 0
n = int(input("How long is the sequence? "))
for i in range(1, n+1):
    total += i

for i in range(1, n):
    total -= int(input("Enter value {}: ".format(i)))

print("Missing value is: " + str(total))

That first for loop is unnecessary though. First of all, your loop is equivalent to the sum function:

total = sum(range(1,n+1))

But you can do away with any iteration altogether by using the formula:

total = int(n*(n+1)/2) # division causes float output so you have to convert back to an int
Igid
  • 515
  • 4
  • 15
  • Perfect! Thank you. I didn't think to take the value as a variable and use it to end the range of the second loop. – Dave Apr 12 '18 at 23:24