我需要一个接受n并返回2 n -1的函数。听起来很简单,但是该函数必须是递归的。到目前为止,我只有2 n:
def required_steps(n):
if n == 0:
return 1
return 2 * req_steps(n-1)
Run Code Online (Sandbox Code Playgroud)
练习指出:“您可以假设参数n始终为正整数且大于0”
我面临一个问题,即递归和使用循环似乎都是自然的解决方案.对于像这样的案件,是否有惯例或"首选方法"?(显然它不像下面那么简单)
Item Search(string desired, Scope scope) {
foreach(Item item in scope.items)
if(item.name == desired)
return item;
return scope.Parent ? Search(desired, scope.Parent) : null;
}
Run Code Online (Sandbox Code Playgroud)
Item Search(string desired, Scope scope) {
for(Scope cur = scope; cur != null; cur = cur.Parent)
foreach(Item item in cur.items)
if(item.name == desired)
return item;
return null;
}
Run Code Online (Sandbox Code Playgroud) 我正在寻找一个C#特定的开源(或源代码可用)递归或深度对象比较的实现.
我目前有两个活动对象图,我希望相互比较,比较的结果是图中的一组差异.对象是在运行时已知的一组类的实例化(但不一定在编译时).
有一个特定的要求是能够从图中的差异映射回到包含差异的对象.
这最近打击了我正在进行的一个项目.大多数人都熟悉属性递归:
public int Test
{
get { return this.test; }
set { this.Test = value; }
}
private int test;
Run Code Online (Sandbox Code Playgroud)
你不小心把一个大写的T放在这个setter中,你已经把自己打开了StackoverflowException.更糟糕的是,如果你没有定义它,通常visual studio会自动纠正套管,使你进入无效状态.
我最近在构造函数中做了类似的事情:
public TestClass(int test)
{
this.Test = Test;
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,你没有得到StackOverflowException,现在你遇到了编程错误.在我的情况下,这个值被传递给一个WebService,而后者使用了一个默认值(不是0),这导致我错过了我错误地分配它的事实.集成测试全部通过,因为该服务没有说
"嘿,你忘记了这个非常重要的领域!"
我可以采取哪些措施来避免这种行为?我一直被建议不要像以下那样定义变量,我个人不喜欢它们,但我想不出任何其他选择:
private int _test;
private int mTest;
Run Code Online (Sandbox Code Playgroud)
编辑
通常我可以想到的下划线或m前缀不合适的原因是:
我正在打印一个我认为是列表的值,但我得到的输出是:
[...]
Run Code Online (Sandbox Code Playgroud)
这代表什么?我该如何测试呢?我试过了:
myVar.__repr__() != '[...]'
Run Code Online (Sandbox Code Playgroud)
和
myVar.__repr_() != Ellipsis
Run Code Online (Sandbox Code Playgroud)
但没有骰子......
这是给出问题的代码的缩减:
def buildPaths(graph, start, end, path=[], totalPaths=[]):
"""
returns list of all possible paths from start node to the end node
"""
path = path + [start]
if start == end:
return path
for nextNode in graph.childrenOf(start):
if nextNode not in path:
newPath = buildPaths(graph, nextNode, end, path, totalPaths)
if newPath != []: # test
totalPaths.append(newPath)
return totalPaths
Run Code Online (Sandbox Code Playgroud)
totalPaths包含很多所谓的递归列表,但我不明白为什么.我在#test修改了测试以防止这种情况发生.
我也尝试过:
def buildPaths(graph, thisNode, end, path=[], totalPaths=None):
"""
returns list …Run Code Online (Sandbox Code Playgroud) 尾递归是函数式语言中一个重要的性能优化策略,因为它允许递归调用消耗常量堆栈(而不是O(n)).
是否有任何问题根本无法以尾递归方式编写,或者总是可以将天真递归函数转换为尾递归函数?
如果是这样,有一天功能编译器和解释器可能足够智能以自动执行转换?
这是一些递归反转字符串的Java代码.
有人可以解释它是如何工作的吗?
public static String reverse(String str) {
if ((null == str) || (str.length() <= 1)) {
return str;
}
return reverse(str.substring(1)) + str.charAt(0);
}
Run Code Online (Sandbox Code Playgroud)
我不明白这是如何工作的.
我试过put -r directory/*,只上传文件而不上传文件夹.给我错误,不能Couldn't canonicalise.
任何帮助将不胜感激.
我有这段自称的代码:
def get_input():
my_var = input('Enter "a" or "b": ')
if my_var != "a" and my_var != "b":
print('You didn\'t type "a" or "b". Try again.')
get_input()
else:
return my_var
print('got input:', get_input())
Run Code Online (Sandbox Code Playgroud)
现在,如果我输入"a"或"b",一切都很好.输出是:
Type "a" or "b": a
got input: a
Run Code Online (Sandbox Code Playgroud)
但是,如果我输入其他内容然后输入"a"或"b",我会得到:
Type "a" or "b": purple
You didn't type "a" or "b". Try again.
Type "a" or "b": a
got input: None
Run Code Online (Sandbox Code Playgroud)
我不知道为什么get_input()要回来None,因为它应该只返回my_var.print语句显示None正确的值,但函数由于某种原因不返回该值.
这是代码:
function repeat(operation, num) {
return function() {
if (num <= 0) return
operation()
return repeat(operation, --num)
}
}
function trampoline(fn) {
while(fn && typeof fn === 'function') {
fn = fn()
}
}
module.exports = function(operation, num) {
trampoline(function() {
return repeat(operation, num)
})
}
Run Code Online (Sandbox Code Playgroud)
我已经读过蹦床用于处理溢出问题,因此该函数不仅会保持调用自身和堆栈.
但是这个片段的功能如何呢?特别是trampoline功能?它究竟做了什么while以及它是如何实现其目标的?
感谢您的任何帮助 :)
recursion ×10
python ×3
c# ×2
comparison ×1
console ×1
ellipsis ×1
function ×1
java ×1
javascript ×1
linux ×1
loops ×1
properties ×1
return ×1
reverse ×1
sftp ×1
string ×1
trampolines ×1
upload ×1