2015-04-07 78 views

回答

1

的「下限」這裏是當傳遞的字符串(或列表或元組...)是空的 - 在這種情況下,你只返回,這將結束遞歸:

def recdup(seq): 
    if not seq: 
     return seq 
    head, tail = seq[0:1], seq[1:] 
    return (head * 2) + recdup(tail) 
+0

這是完美的,但我不知道「如果不是seq:」在做什麼。如果該語句是空字符串,則該語句返回true,如果該字符串是字符串,則返回false? – Slizzard73

+0

這真的是Python 101 - https://docs.python.org/2/reference/expressions.html#boolean-operations –

0
#iterate through x, store each value twice in y, print y joined by nothing 
x = 'abc' 
y=[i*2 for i in x] 
print ''.join(y) 
+0

這工作,但它不像遞歸呢?我認爲這個解決方案是迭代的。 – Slizzard73

+0

當然是最pythonic的解決方案,但OP要求遞歸的一個... –

相關問題