繁体   English   中英

切换索引,使用显示的相同精确的9位数字创建下一个最高数字

[英]Switch indexes, create the next highest number using the same exact 9 digits presented

因此,我收到了一个挑战,陈述如下:“设计一个程序,输入一个9位数字,其中没有数字出现两次,并输出对应于下一个最高数字的相同9位数字作为输出。如果不存在这样的数字, ,算法应该指出这一点。因此,例如,如果输入为781623954,则输出将为781624359。”

因此,我想到了翻转索引的想法,因此请在检查前一个索引之前检查一下哪个索引更大,然后进行比较,然后在必要时翻转,但由于某些原因我的代码无法正常工作。 我只是检查最后两位数字而不是全部数字,因此,如果您可以帮助我并为我检查一下,并且您对解决此问题有更好的想法,请分享。

input = raw_input("Enter 9 Digits: ")
x = 9
while x>0:
    x-=1
    if input[8] > input[7]:
        temp = input[8]
        input[8] == input[7] 
        input[7] == temp
        print input
        break

这是使用14世纪印度数学家Narayana Pandita的算法的一种更有效的方法,该算法可以在Wikipedia上有关置换的文章中找到。 这种古老的算法仍然是已知的按顺序生成置换的最快方法之一,并且它非常健壮,因为它可以正确处理包含重复元素的置换。

下面的代码包括一个简单的test()函数,该函数生成有序数字字符串的所有排列。

#! /usr/bin/env python

''' Find the next permutation in lexicographic order after a given permutation

    This algorithm, due to Narayana Pandita, is from
    https://en.wikipedia.org/wiki/Permutation#Generation_in_lexicographic_order

    1. Find the largest index j such that a[j] < a[j + 1]. If no such index exists, 
    the permutation is the last permutation.
    2. Find the largest index k greater than j such that a[j] < a[k].
    3. Swap the value of a[j] with that of a[k].
    4. Reverse the sequence from a[j + 1] up to and including the final element a[n].

    Implemented in Python by PM 2Ring 2015.07.28
'''

import sys

def next_perm(a):
    ''' Advance permutation a to the next one in lexicographic order '''
    n = len(a) - 1
    #1. Find the largest index j such that a[j] < a[j + 1]
    for j in range(n-1, -1, -1):
        if a[j] < a[j + 1]:
            break
    else:
        #This must be the last permutation
        return False

    #2. Find the largest index k greater than j such that a[j] < a[k]
    v = a[j]
    for k in range(n, j, -1):
        if v < a[k]:
            break

    #3. Swap the value of a[j] with that of a[k].
    a[j], a[k] = a[k], a[j]

    #4. Reverse the tail of the sequence
    a[j+1:] = a[j+1:][::-1]

    return True


def test(n):
    ''' Print all permutations of an ordered numeric string (1-based) '''
    a = [str(i) for i in range(1, n+1)]
    i = 0
    while True:
        print('%2d: %s' % (i, ''.join(a)))
        i += 1
        if not next_perm(a):
            break


def main():
    s = sys.argv[1] if len(sys.argv) > 1 else '781623954'
    a = list(s)
    next_perm(a)
    print('%s -> %s' % (s, ''.join(a)))


if __name__ == '__main__':
    #test(4)
    main()

我不相信您会用数字翻转的方法来保证找到下一个最高的数字(至少在没有进一步检查的情况下)

这是一个简单的解决方案:只需增加输入数字并检查是否满足条件或找不到数字。

set()可用于获取数字中唯一的一组数字。

input_num = '781623954'
next_num = int(input_num) + 1
input_digits = set(input_num)
found = False
while not found:
    next_num += 1
    next_digits = set(str(next_num))
    found = len(next_digits) == 9 and input_digits == next_digits
    if next_num > 987654321:
        break

if found:
    print(next_num)
else:
    print("No number was found.")
input[8] == input[7]
input[7] == temp

你可能的意思是:

input[8] = input[7]
input[7] = temp

是不是

如注释中所述,它不能直接在字符串上工作,因为它在Python中是不可变的。 因此,作为第一步,您可以列出该字符串中的字符:

input = list(input)

最后,从修改后的列表中返回一个字符串:

input = ''.join(input)

顺便说一句,您可能想从Python元组解压缩中受益,它允许您交换两个变量而不必引入第三个变量:

input[7], input[8] = input[8], input[7]

暂无
暂无

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

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