简体   繁体   English

Python-使用唯一数字查找最近的更大数字

[英]Python- Find nearest greater number with unique digits

I came across this problem in which you will take an integer as a input from the user and then return the nearest greater number with unique digits .我遇到了这个问题,您将从用户那里获取一个整数作为输入,然后返回最近的具有唯一数字的更大数字
First it seem like an easy one and i wrote it's code which gave me an desired outputs but for some inputs returns an integer with repeating digits or a unique number with higher value then expected,首先它看起来很简单,我写了它的代码,它给了我一个想要的输出,但对于某些输入返回一个带有重复数字的整数或一个具有比预期更高值的唯一数字,
I want to know why my code is showing different behaviour than expected and what would be the right answer to this problem.我想知道为什么我的代码显示出与预期不同的行为,以及这个问题的正确答案是什么。 Also i don't know what to do when a digit will become two digit number how to make it unique eg.另外我不知道当一个数字变成两位数字时该怎么办,例如如何使它独一无二。 9999 9999

  • code代码
    n = int(input("enter a no.:"))+1         #taking input adding 1 to make sure if the input is 
                                         #unique it should return higher no. with uniq 
    a =[]                                    #creating an empty list
    while n!=0:                              #loop to store digits in a list
        a.append(n%10)                       #at index 0 ones digit is stored
        n = int(n/10)                        #at index 1 tens digit is stored an so on...


    while len(set(a)) != len(a):             #checking if the list is unique
        for i in range(0,len(a)):            #
            occur = a.count(a[i])            #taking occurence of a list item
            if occur != 1:                   #if no. is repeated
                a[i]+=occur-1                #incrementing the repeating digit.

    a.reverse()                              #converting list to integer and printing
    n = 0
    for i in a:
        n = n*10+i
    print(n)
  • Behaviour行为
    1.Printing repeating digits for some inputs 1.打印某些输入的重复数字
    2.Printing Higher values than expected for some inputs 2.打印某些输入的值高于预期
    3.When a digit becomes a two digit no. 3.当一位数变成两位数时it treats it like a single digit它把它当作一个数字
  • Some outputs一些输出
enter a no.:1233  
output:  1234            #desired output: 1234

enter a no.:7885
output:  7896            #desired output: 7890

enter a no.:7886
output:  8008             #desired output: 7890

enter a no.:999
output:  2013             #desired output: 1023

You're probably over complicating this for yourself.你可能把这件事复杂化了。

Would something like this work rather than doing all of the conversions?这样的事情会起作用而不是进行所有转换吗?

n = int(input("enter a no.:"))+1   #taking input adding 1 to make 
                                   #sure if the input is unique then 
                                   #program does't return the input itself

a = str(n)                         # simply convert to a string

while len(set(a)) != len(a):       #checking if the string is unique
    n += 1
    a = str(n)

print(n)

Why not simply increment the number until you find one with unique digits?为什么不简单地增加数字直到找到具有唯一数字的数字?

def next_uniq(n):
  a = str(n)  
  while len(set(a)) != len(a):
    a = str(int(a) + 1)
  return a

for i in [1233, 7885, 7886, 999]:
  print(next_uniq(i))

# 1234, 7890, 7890, 1023

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM