Created
May 22, 2013 05:46
-
-
Save t-miya/5625488 to your computer and use it in GitHub Desktop.
ループなしのリスト逆順化
再帰的に左右のペアとなるインデックス同士を入れ替える。
奇数長の場合は入れ替え先と元のインデックスが同値、
偶数長の場合はインデックスがリスト長の半分を超えた時点で終了 pythonの場合はスライスを使った方がスマートかもしれない
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def rev(l, index=0): | |
dest = len(l)-index-1 | |
if dest == index or index+1 > len(l)/2: | |
return l | |
tmp = l[index] | |
l[index] = l[dest] | |
l[dest] = tmp | |
return rev(l, index+1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment