数字减少的最小数字

时间:2019-02-17 07:38:57

标签: python recursion

我发现实现一个函数的问题,该函数采用一个正整数n作为输入并返回大于n的最小正整数(其位数正在减少),并且类似地,该函数返回一个大于n的最小正整数(其位数正在增加) 。我认为增加功能可以正常工作。但是函数减少有什么错误呢?对于减少的输入(100),它返回11而不是110。

# the next integer whose digits are increasing.
def increasing(n):
    asastring = str(n)
    length = len(asastring)
    if asastring == "9"*length:
        return "1"*(length+1)
    if length == 1:
        return int(n)+1

    if length >= 2:
        firstcharacter = asastring[0]
        secondcharacter = asastring[1]
        if int(firstcharacter) > int(secondcharacter):
            return int(str(firstcharacter)*length)
        if firstcharacter == secondcharacter:
             return firstcharacter+str(increasing(int(asastring[1:])))
        if int(firstcharacter) < int(secondcharacter):
            if secondcharacter == "9":
                return str(int(firstcharacter)+1) * len(str(n))
            return firstcharacter+str(increasing(int(asastring[1:])))

# the next integer whose digits are decreasing.
def decreasing(n):
    asastring = str(n)
    length = len(asastring)
# First the case where we need to add a digit.
    if asastring == "9"*length:
        return "1"+"0"*length
# Now we know that the next integer has the same number of digits as the original number.
    if length == 1:
        return int(n)+1
    if length >= 2:
        firstcharacter = asastring[0]
        secondcharacter = asastring[1]
        if int(firstcharacter) > int(secondcharacter):
            endpart = str(((asastring[1:3])))
            value = firstcharacter + str(decreasing(int(asastring[1:])))
            return str(firstcharacter) + str(decreasing(int(asastring[1:])))
        if int(firstcharacter) == int(secondcharacter):
            return decreasing(firstcharacter+str(decreasing(int(asastring[1:]))))
        if int(firstcharacter) < int(secondcharacter):
            return str(int(firstcharacter)+1)+'0'*(length-1)

i=100
print(increasing(i))
print(decreasing(i))

2 个答案:

答案 0 :(得分:1)

您需要删除在递归调用中完成的int类型转换,因为int('00')将您的数字转换为零(基本上删除了所有起始的零)并缩短了字符串的长度。只需删除该转换即可。.其余代码工作正常:

def decreasing(n):
    asastring = str(n)
    length = len(asastring)
# First the case where we need to add a digit.
    if asastring == "9"*length:
        return "1"+"0"*length
# Now we know that the next integer has the same number of digits as the original number.
    if length == 1:
        return int(n)+1
    if length >= 2:
        firstcharacter = asastring[0]
        secondcharacter = asastring[1]
        if int(firstcharacter) > int(secondcharacter):
            return str(firstcharacter) + str(decreasing(asastring[1:]))
        if int(firstcharacter) == int(secondcharacter):
            return decreasing(firstcharacter+str(decreasing(asastring[1:])))
        if int(firstcharacter) < int(secondcharacter):
            return str(int(firstcharacter)+1)+'0'*(length-1)

答案 1 :(得分:0)

有多个相互交织的问题需要解决。这两个功能的命名令人困惑。如果遵循逻辑,则函数increasing()应该被称为nondecreasing(),类似地,函数decreasing()应该被称为nonincreasing()。是>(大于)和> =(大于或等于)之间的区别。

接下来的困惑是这些函数接受返回是什么类型?如果我们检查 working increasing()函数返回的内容,则会得到:

str return "1"*(length+1)
int return int(n)+1
int return int(str(firstcharacter)*length)
str return firstcharacter+str(increasing(int(asastring[1:])))
str return str(int(firstcharacter)+1) * len(str(n))
str return firstcharacter+str(increasing(int(asastring[1:])))

如果我们类似地研究increasing()如何处理其自身的内部递归调用,以查看其认为接受和返回的内容,我们将得到:

int -> int  return firstcharacter+str(increasing(int(asastring[1:])))
int -> int  return firstcharacter+str(increasing(int(asastring[1:])))

因此,这是对increasing()(又名nondecreasing())的尝试返工,它试图使其始终如一地接受int并返回int

def nondecreasing(n):  # aka increasing()
    as_string = str(n)
    length = len(as_string)

    if as_string == "9" * length:
        return int("1" * (length + 1))

    if length == 1:
        return int(n) + 1

    first_digit, second_digit, second_digit_onward = as_string[0], as_string[1], as_string[1:]

    if first_digit > second_digit:
        return int(first_digit * length)

    if first_digit == second_digit:
        return int(first_digit + str(nondecreasing(int(second_digit_onward))))

    if as_string == first_digit + "9" * (length - 1):
        return int(str(int(first_digit) + 1) * length)

    return int(first_digit + str(nondecreasing(int(second_digit_onward))))

decreasing()(也称为nonincreasing())的功能存在更多问题。它依靠其在内部调用上接受intstr的能力来解决问题。

讨论这类问题,而不是让其他程序员重新发现它们,是代码注释的全部内容。

我不认为上述问题会阻止nonincreasing()持续返回一个int

def nonincreasing(n):  # aka decreasing()
    as_string = str(n)
    length = len(as_string)

    if as_string == "9" * length:
        return int("1" + "0" * length)

    if length == 1:
        return int(n) + 1

    first_digit, second_digit, second_digit_onward = as_string[0], as_string[1], as_string[1:]

    if first_digit > second_digit:
        return int(first_digit + str(nonincreasing(second_digit_onward)))

    if first_digit == second_digit:
        remaining_digits = str(nonincreasing(second_digit_onward))
        second_digit = remaining_digits[0]
        n = first_digit + remaining_digits

    if first_digit < second_digit:
        return int(str(int(first_digit) + 1) + '0' * (length - 1))

    return int(n)

修复此功能的关键是从倒数第二个return子句中删除if语句,然后修复数据并使其进入下一个if子句以查看结果是否需要修复。

我相信@ devender22对int()转换的见解是至关重要的,但是我不认为随附的解决方案是有效的,因为它会产生大量错误的结果(例如990到998都应在应有的时候变成1000只是被1撞倒了。

为了检查我的nonincreasing()函数的所有情况,我使用完全不同的Python运算符编写了一个效率较低的非递归解决方案,没有单独的情况:

def nonincreasing(n):

    def is_increasing(n):
        string = str(n)

        return any(map(lambda x, y: y > x, string, string[1:]))

    while is_increasing(n + 1):
        n += 1

    return n + 1

然后确保两个实现在其输出上达成共识。