Skip to content

Instantly share code, notes, and snippets.

@t-miya
Created May 22, 2013 05:46
Show Gist options
  • Save t-miya/5625488 to your computer and use it in GitHub Desktop.
Save t-miya/5625488 to your computer and use it in GitHub Desktop.
ループなしのリスト逆順化 再帰的に左右のペアとなるインデックス同士を入れ替える。 奇数長の場合は入れ替え先と元のインデックスが同値、 偶数長の場合はインデックスがリスト長の半分を超えた時点で終了 pythonの場合はスライスを使った方がスマートかもしれない
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