【问题标题】:Implement the recursion in pythonic way以pythonic方式实现递归
【发布时间】:2019-01-15 16:35:20
【问题描述】:

我有一个递归:

C(n) = min{
  C(n/3) + 1 if n ⋮ 3,
  C(n/2) + 1 if n ⋮ 2,
  C(n-1) + 1
}

基本情况

C(n) = 0 for n <= 1

如何以 Python 的方式实现这种递归? 这是解决给定问题的尝试,我能够成功解决,但我觉得也需要实现递归解决方案。

Problem 1: Primitive Calculator
You are given a primitive calculator that can perform the following three operations with the current number x: multiply x by 2, multiply x by 3, or add 1 to x. Your goal is given a positive integer n, find the minimum number of operations needed to obtain the number n starting from the number 1.

Problem Description

Task. Given an integer n, compute the minimum number of operations needed to obtain the number n starting from the number 1.

Output Format. In the first line, output the minimum number k of operations needed to get n from 1. In the second line output a sequence of intermediate numbers. That is, the second line should contain positive integers a0, a2,…, a(k-1) such that a0 =1, a(k-1) =n and for all 0≤i<k-1, ai+1 is equal to either ai + 1, 2 x ai, or 3 x ai. If there are many such sequences, output any one of them.

Sample 1.

Input: 5

Output:

3

1 2 4 5

Explanation:
Here, we first multiply 1 by 2 two times and then add 1 ( ((1 x 2) x 2) + 1). Another possibility is to first multiply by 3 and then add 1 two times. Hence “1 3 4 5” is also a valid output in this case.

Sample 2:

Input: 96234

Output:

14

1 3 9 10 11 22 66 198 594 1782 5346 16038 16039 32078 96234

Explanation:
Again, another valid output in this case is “1 3 9 10 11 33 99 297 891 2673 8019 16038 16039 48117 96234”.

Your goal is to design and implement a dynamic programming solution for this problem. A natural subproblem in this case is the following: C(n) is the minimum number of operations required to obtain n from 1 (using the three primitive operations). How to express C(n) through C(n/3), C(n/2), C(n-1)?

【问题讨论】:

  • 我不认识 符号。你能解释清楚点吗?
  • n ⋮ 3 表示如果 n%3 == 0
  • min 如何用这些条件元素来解释?如果它可以被 3 整除(但不能被 2 整除),则返回 C(n/3) + 1 和 C(n-1) + 1 的最小值?
  • 您自己编写这段代码时遇到了什么问题?似乎大部分内容转换为 Python 代码应该是相当简单的(如果您了解 Python,也就是说 - 如果您不了解,您可以在网上找到大量教程和代码示例来帮助您)。如果您不知道其中的特定部分应该是什么样子,那么提出这个问题可能会导致一个对其他人更有用的问题,并且您会得到更专注于解释您不理解的部分的答案(而不是仅仅得到“这里是代码”的答案)。
  • 我尝试的是使用一个全局变量来存储总计数和局部变量来存储来自潜在解决方案的结果,这些解决方案总是导致最终答案为零。我无法弄清楚原因。我知道我的问题应该更具体

标签: python python-3.x algorithm recursion dynamic-programming


【解决方案1】:
def C(n):
    if n <= 1:
        return 0

    m= C(n-1)
    if n % 3 == 0:
        m= min(m, C(n/3))
    if n % 2 == 0:
        m= min(m, C(n/2))
    return m + 1

可能值得考虑记忆化。

Cache= {}

def C(n):
    global Cache

    if n <= 1:
        return 0

    try:
        return Cache[n]

    except:
        m= C(n-1)
        if n % 3 == 0:
            m= min(m, C(n/3))
        if n % 2 == 0:
            m= min(m, C(n/2))
        m+= 1
        Cache[n]= m
        return m

我不确定是否最好先测试n &lt;= 1

【讨论】:

  • 谢谢。我确实使用了记忆,但在使用递归时遇到了问题。
猜你喜欢
  • 2015-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-27
  • 2014-09-13
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
相关资源
最近更新 更多