本文介绍了我想一步一步地理解它,这个功能如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

def fib_efficient(n, d):
    if n in d:
        return d[n]
    else:
        ans = fib_efficient(n-1, d) + fib_efficient(n-2, d)
        d[n] = ans
        return ans

d = {1:1, 2:2}

print(fib_efficient(6, d)





我的尝试:



它与斐波那契有关。

我想了解python如何处理它以给我们输出?



提前谢谢。



What I have tried:

It is relatd to fibonacci.
I would like to understand how python will handle it to give us the output?

Thank you in advance.

推荐答案

Quote:

我想了解python如何处理它以给我们输出?

I would like to understand how python will handle it to give us the output?



这是一个带有动态编程的递归反向fibonacci。

调试器是您查看此代码如何工作的首选工具。

-----

你不明白你的代码的行为!



有一个几乎通用的解决方案:运行你的调试器上的代码一步一步,检查变量。

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它不知道你应该做什么,它没有找到错误,它只是通过向你展示发生了什么来帮助你。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。

]



[]

[]

[]

[]

[]

调试器只向您展示你的代码正在做,你的任务是与它应该做的事情进行比较。


This is a recursive backward fibonacci with dynamic programming.
The debugger is your tool of choice to see how this code works.
-----
You don't understand the behavior of your code!

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
27.3. pdb — The Python Debugger — Python 3.6.1 documentation[^]
Debugging in Python | Python Conquers The Universe[^]
pdb – Interactive Debugger - Python Module of the Week[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于我想一步一步地理解它,这个功能如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 19:41