2017-11-03 6 views
2

昇順に並べられた数字は、文字列として入力として与えられます。指定された入力から欠落した数字を印刷する

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 

マイクエリ:

私のプログラムは、文字列の形式で入力として与えられた2つのまたは4桁の数字の場合は欠番を見つけることができません。

input 6768707172 
output of my program 677 
expected output 69 

文字列に2桁または3桁または4桁の数字が含まれているかどうかをチェックし、それに応じて分割する方法を教えてください。

+0

それは14を返しては...私はちょうど私がちょうどcode.Justが新しいをチェックし更新した –

+0

をチェックします入力してください! –

+0

これは一般的なインタビューの質問です。あなたはこのリンクのロジックを見つけることができます。http://www.geeksforgeeks.org/find-missing-number-string-numbers-no-separator/ –

答えて

1

私はこのアプローチを面白くしました。それはうまくいくようです(私は悪い入力例外を制御しておらず、確かに改善することができます)。私は基本的に1文字の長さから始まり、私が正しい場合に試してみます。

結果として与える
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') 

::私はそれが役に立てば幸い

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 
2

擬似コードを使用してアルゴリズムを評価します。重要な概念を特定し、パラメータを使用して関数を作成する。意味のある名前をつけてください。鉛筆と紙で反復を実行します。それが動作する場合は、アルゴリズムを絞り込みます。ユースケースをサポートするデータ構造を特定する。

現在のコードは人間の目には作成されていませんが、人間にヘルプを求めています。

関連する問題