无法获取用于计算迭代次数的Python函数以正常工作

2024-04-27 05:36:30 发布

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

我最近开始学习Python。我在做一些关于课程控制结构部分的练习,一切都很顺利,直到最后一道题被难倒。我已经研究了三天了,我很确定解决办法是显而易见的。以下是练习中的描述:

Start with any number. If the number is divisible by 3, divide it by 3. Otherwise, add 2 to the number. Eventually, no matter what number you begin with, this series will run into 1. For example, imagine we started with the number 5: 5 is not divisible by 3, so 5 + 2 = 7 7 is not divisible by 3, so 7 + 2 = 9 9 is divisible by 3, so 9 / 3 = 3 3 is divisible by 3, so 3 / 3 = 1

Start with 5, this sequence converges on 1 in 4 iterations: 5 -> 7, 7 -> 9, 9 -> 3, 3 -> 1.

Write a function called joyner. joyner should have one parameter, an integer. It should return the number of iterations required to reach 1 for the first time.

我在一个页面上运行了我写的函数,用不同的整数测试我的函数。每次它给我一个错误(说多少迭代是预期的和多少我的程序计算),我设法修复该整数的程序,但下面测试的整数会再次给我一个错误。下面的代码是我决定来这里寻求帮助之前的最后一次重写。你知道吗

def joyner(num):
    count = 0
    while num % 3 != 0:
        # print("num not divisible by 3")
        num = num + 2
        # print("added 2")
        count += 1
        # print(count)
        if num % 3 == 0:
            # print("num is divisible by 3")
            num /= 3
            # print("divided by 3")
            count += 1
            # print(count)


    while num % 3 == 0:
        # print("num is divisible by 3")
        num /= 3
        # print("divided by 3")
        count += 1
        # print(count)

    return count

结果如下:

We found the following problems with your submission:

We tested your code with num = 15. We expected joyner to return the int 5. However, it returned the int 1.

We tested your code with num = 29. We expected joyner to return the int 10. However, it returned the int 3.

We tested your code with num = 65. We expected joyner to return the int 8. However, it returned the int 3.

We tested your code with num = 12. We expected joyner to return the int 3. However, it returned the int 1.

We tested your code with num = 32. We expected joyner to return the int 6. However, it returned the int 4.

Note that these errors may have prevented us from checking your submission in other ways, so fixing these problems may cause other problems to show up next time. Keep trying! This is what debugging is all about.


The following things were correct about your submission:

We expected to find a function named joyner in your code, and we did.

We tested your code with num = 5. We expected joyner to return the int 4, and it did.

We tested your code with num = 27. We expected joyner to return the int 3, and it did.

We tested your code with num = 16. We expected joyner to return the int 3, and it did.


Tags: thetoyourbyreturniswithcode
2条回答

你的任务的前提是不正确的:

Start with any number. If the number is divisible by 3, divide it by 3. Otherwise, add 2 to the number. Eventually, no matter what number you begin with, this series will run into 1.

number = 4

4 + 2 = 6        # 4 is not divisible by 3, add 2
6 / 3 = 2        # 6 is     divisible by 3 -> 2
2 + 2 = 4        # 2 is not divisible by 3, add 2
4 + 2 = 6        # 4 is not divisible by 3, add 2   > cycle - no solution.

如果有解决方案,就会发现:

def joyner(num): 
    count = 0
    seen = set()
    while num != 1:
        if num in seen: 
            return None
        else:
            seen.add(num)
        if num % 3 == 0:
            num = num // 3
        else:
            num += 2
        count += 1

    return count

测试它:

i = 0    
for k,c in d.items():
    if c is None:
        continue
    print(k,":",c, end="     ")
    i += 1
    if i % 6 == 0:
        print()
        i = 0

从1到99的溶液输出:

 1 : 0       3 : 1      5 : 4      7 : 3     9 : 2      11 : 7     
13 : 6      15 : 5     17 : 6     19 : 5     21 : 4     23 : 5     
25 : 4      27 : 3     29 : 10    31 : 9     33 : 8     35 : 9     
37 : 8      39 : 7     41 : 8     43 : 7     45 : 6     47 : 9     
49 : 8      51 : 7     53 : 8     55 : 7     57 : 6     59 : 7     
61 : 6      63 : 5     65 : 8     67 : 7     69 : 6     71 : 7     
73 : 6      75 : 5     77 : 6     79 : 5     81 : 4     83 : 13     
85 : 12     87 : 11    89 : 12    91 : 11    93 : 10    95 : 11     
97 : 10     99 : 9     

不可行:

[ 2,  4,  6,  8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 
 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 
 80, 82, 84, 86, 88, 90, 92, 94, 96, 98]   

你的算法只适用于奇数。你知道吗

We tested your code with num = 12. We expected joyner to return the int 3. However, it returned the int 1.

根本不起作用

代码中的问题是,它假设当一个数字不再可以被3整除时,它达到了1,也就是说关于第二个循环,例如15可以被3整除,它变成了5,这是不可整除的,但也不是1。你知道吗

总的来说,这样想,一个数可能是可除的,也可能不是一次可除的,所以它应该在一个循环中发生,当你达到1时应该停止。你知道吗

相关问题 更多 >