【问题标题】:Is there a way I can compare the same variable and choose the higher one and print it?有没有办法可以比较相同的变量并选择较高的变量并打印它?
【发布时间】:2022-01-31 06:33:56
【问题描述】:

每次 while 循环继续时,y 的值都会发生变化,我尝试了观察点模块,但我需要一种方法来找出最高的 y 并打印它,我将打印我正在尝试的代码去上班。

import random
from watchpoints import watch

def alg(I):
    print(I)
    x = 1
    while i > 1:
        if (i % 2) == 0:
            i = int(i / 2)
            x = x + 1
        else:
            i = int(3 * i + 1)
            x = x + 1
        print(I)
        y = 1
        if i in range(1, 9):
            y = 1
        if i in range(10, 99):
            y = 2
        if i in range(100, 999):
            y = 3
    if i in range(1000, 9999):
        y = 4
    watch(a)#I want to know when y reachest the highest
    print(y, "is the max number of caracters")#then print it
    print("numero passaggi = ", str(x))


print("1: choice")
print("2: random")
type = int(input(" 1 or 2: "))
if type == 1:
    i = input("Enter a number: ")
    alg(int(I))
elif type == 2:
    i = random.randint(1, 100)  # 10^9
    alg(I)
else:
    print("Enter 1 or 2")

我想知道y什么时候达到最高点并在下面打印出来。

【问题讨论】:

  • 您可以创建一个变量max_y,如果新的y大于max_y,则每次更新该变量。您可以从那里跟踪。

标签: python python-3.x algorithm math watch


【解决方案1】:

有很多方法可以做到这一点,但由于您似乎有兴趣“观察”y 的值的变化,因此一个不错的选择可能是让您的 alg 函数成为生成 @ 值的生成器987654323@。然后,调用者可以对这些值执行任何操作,包括获取其中的 max

请注意,不要做这种事情来计算一个数字有多少位:

        if i in range(1, 9):
            y = 1
        if i in range(10, 99):
            y = 2
        if i in range(100, 999):
            y = 3
        if i in range(1000, 9999):
            y = 4

你可以这样做:

y = len(str(i))

即把它变成一个字符串,然后统计字符数。

def alg(i: int):
    x = 1
    while i > 1:
        if i % 2 == 0:
            i = i // 2
            x += 1
        else:
            i = 3 * i + 1
            x = x + 1
        print(f"i: {i}")
        yield len(str(i))
    print(f"numero passaggi = {x}")

print(f"Max number of digits: {max(alg(50))}")
i: 25
i: 76
i: 38
i: 19
i: 58
i: 29
i: 88
i: 44
i: 22
i: 11
i: 34
i: 17
i: 52
i: 26
i: 13
i: 40
i: 20
i: 10
i: 5
i: 16
i: 8
i: 4
i: 2
i: 1
numero passaggi = 25
Max number of digits: 2

【讨论】:

  • 不错的代码,它工作得很好,但我能问一下为什么在 {max(alg(50))} 中你使用 50,现在代码总是以 50 开头,我要写什么是随机的还是选择的?再次感谢
  • 没关系,找到了让它工作的方法,非常感谢。这是我的第一个项目,被这个问题困扰了好几天......谢谢
【解决方案2】:

最简单的方法是创建另一个变量来存储y 已达到的最大值,并在每个循环中运行检查以查看新的y 值是否大于先前的最大值。

这是一个例子:

def exampleFunc():
    i = 0
    y = yMax = 0
    while i < 20:
        y = random.randint(1, 100)
        if y > yMax:
            yMax = y
        i += 1
    print(yMax)

【讨论】:

    【解决方案3】:

    解决此类问题的最简单方法是将您的函数转换为返回所有中间值的函数,然后将它们聚合(在这种情况下,使用内置函数max())。

    from math import log10
    
    def collatz_seq(n):
        yield n
        while n > 1:
            if n % 2:
                n = 3 * n + 1
            else:
                n //= 2
            yield n
    
    def print_stats(n):
        seq = collatz_seq(n)
        idx, val = max(enumerate(seq), key=lambda x: x[1])
        digits = int(log10(val)) + 1
    
        print(f"{digits} is the max number digits")
        print(f"{idx} is the iteration number")
    

    这里我使用enumerate()给我每个值的索引,我使用math.log10()获取数字中的位数(减一)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-16
      • 2012-05-22
      • 1970-01-01
      • 2013-08-18
      相关资源
      最近更新 更多