Skip to content

Instantly share code, notes, and snippets.

@ria3100
Last active May 29, 2017 09:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ria3100/fc7ebf6c5d37357919f2 to your computer and use it in GitHub Desktop.
Save ria3100/fc7ebf6c5d37357919f2 to your computer and use it in GitHub Desktop.
紙を折った回数Nに応じて山折り、谷折りの数をカウントする
# coding: utf-8
'''
紙を上記のように折る回数Nが与えられるので
紙を折って広げたあとの山折り谷折りの折り目を計算する
プログラムを作成してください。
http://paiza.hatenablog.com/entry/2016/02/15/ITエンジニアを目指す学生が、就活を有利に進める
'''
'''
備考: 標準入力で紙を折る回数Nを取得し、谷折りを 0 山折りを 1 として出力する
'''
def sort(temp):
ret = temp + [0] + temp
ret[len(temp) + 1 + len(temp)//2] = 1
return ret
def recursive(N, temp=[0]):
if N == 1:
return temp
return recursive(N-1, sort(temp))
N = int(input())
print(recursive(N))
@ria3100
Copy link
Author

ria3100 commented Feb 16, 2016

最初の折り目が谷
それ以降、再帰処理で (N-1) + (最初の谷折り) + (N-1の中央を山折りにしたもの) が出力される
1.
0
2.
0 0 1
3.
001 0 011
4.
0010011 0 0011011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment