数字递减的最小数

2024-06-26 03:48:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我发现了一个问题,即在实现函数时,将一个正整数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))

Tags: the函数returnifintegerlengthnextint
2条回答

有许多相互交织的问题需要解决。这两个函数的命名令人困惑。如果我们遵循逻辑,那么函数increasing()应该被称为nondecreasing(),同样地,函数decreasing()应该被称为nonincreasing()。它是>;(大于)和>;=(大于或等于)之间的差值。你知道吗

下一个混淆是这些函数接受和返回的类型是什么?如果我们检查workingincreasing()函数返回的内容,我们会得到:

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)

修复此函数的关键是从倒数第二个if子句中删除return语句,改为修复数据并让它进入下一个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

然后确保两个实现的输出一致。你知道吗

您需要删除递归调用中完成的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)

相关问题 更多 >