【问题标题】:Printing missing numbers from given input从给定的输入中打印缺失的数字
【发布时间】:2017-11-03 13:32:04
【问题描述】:

按升序排列的数字作为输入作为字符串给出。程序应该打印丢失的数字?

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

我的代码

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 位数字并相应地分解它们?

【问题讨论】:

  • 它正在返回 14 ...我刚刚检查过
  • 我刚刚更新了代码。请检查新的输入!
  • 这是一个流行的面试问题。您可以在此链接中找到逻辑geeksforgeeks.org/…
  • @SubhrajyotiDas,我熟悉python,不是C++

标签: python python-3.x


【解决方案1】:

使用伪代码评估算法。识别重要概念并创建带有参数的函数。给出有意义的名字。用铅笔和纸运行迭代。如果可行,请改进算法。确定支持您的用例的数据结构。

当前的代码不是为人眼编写的,但您是在向人类寻求帮助。

【讨论】:

    【解决方案2】:

    我只是为了好玩而采用这种方法,它似乎有效(我没有控制错误的输入异常,它肯定可以改进)。我基本上从 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
    

    【讨论】:

      猜你喜欢
      • 2019-03-21
      • 2022-12-03
      • 2017-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-27
      • 1970-01-01
      相关资源
      最近更新 更多