Reputation: 79
Numbers which are in sequence in ascending order are given as a input as string.The program should print the missing number ?
EX:
INPUT
567568600601602
OUTPUT
569
EXPLANATION
567,568,600,601,602 are the numbers in sequence and 569 is the missing number
INPUT
1112131516
OUTPUT
14
EXPLANATION
11,12,13,15,16 are the numbers in sequence and 569 is the missing
MY CODE
a=input()
a=a.rstrip()
b=list(set(a))
l=[]
p=[]
c=0
for i in range(len(b)):
if a.count(b[i])>1:
c+=1
j=0
while j<=len(a):
l.append(a[j:j+c+1])
j=j+c+1
del l[-1]
k=int(l[0])
while k<=int(l[-1]):
p.append(k)
k=k+1
for u in range(len(p)):
if str(p[u]) not in l:
print(p[u])
break
My query:
My program is unable to find the missing number in case of 2 or 4 digit numbers given as input in form of string .
input 6768707172
output of my program 677
expected output 69
How to check the whether the string has 2 or 3 or 4 digit numbers and break them accordingly ?
Upvotes: 2
Views: 485
Reputation: 1633
I just made this approach for fun and it seems to work (I'm not controling bad input exceptions and it can surely be improved). I basically start from 1 char lenght and try if I'm right.
I hope it helps:
MAX_LEN = 6
def findMissingNumber(s):
print 'Input received: ' + s
for digitsNumber in range(1,MAX_LEN+1):
print
# Initializing stuff
index = digitsNumber
currentNumber = int(s[:digitsNumber])
exit = False
missing = None
while not exit and index < len(s):
# Store expected next number
nextNumber = currentNumber + 1
# Store expected next number length
nextNumberLen = len(str(nextNumber))
# Store next number in the provided string
# based the lenght of the expected number
nextStringNumber = int(s[index : index + nextNumberLen])
print 'Have ' + str(currentNumber) + ', expecting ' + str([nextNumber,nextNumber+1]) + ' and got ' + str(nextStringNumber)
# Check if number gotten is the next or the following
if nextStringNumber in [nextNumber, nextNumber+1]:
# Check if number is not the next
# (means there is a number missing)
if nextStringNumber != nextNumber:
# Check if there was a previous missing number
if not missing:
print 'Found missing ' + str(nextNumber)
missing = nextNumber
# If there was, exit and forget missing number
# (only 1 missing number allowed)
else:
print 'More than 1 missing, exit'
missing = None
exit = True
# Set stuff for next iteration
currentNumber = nextStringNumber
index += nextNumberLen
# If end of string, exit
else:
print 'End of string, exit'
exit = True
# If only 1 missing found, return it
if missing:
print 'Returning ' + str(missing)
return missing
print 'Going to next number lenght'
# If no missing found, return -1
return -1
findMissingNumber('6768707172')
Giving as a result:
Input received: 6768707172
Have 6, expecting [7, 8] and got 7
Have 7, expecting [8, 9] and got 6
End of string, exit
Going to next number lenght
Have 67, expecting [68, 69] and got 68
Have 68, expecting [69, 70] and got 70
Found missing 69
Have 70, expecting [71, 72] and got 71
Have 71, expecting [72, 73] and got 72
Returning 69
Upvotes: 1
Reputation: 3329
Evaluate an algorithm using pseudo code. Identify important concepts and create functions with parameters. Give meaningful names. Run iteration with pencil and paper. If it works, refine algorithm. Identify data structures supporting your use case.
The current code is not made for human eyes, but you are asking humans for help.
Upvotes: 2