Last active
August 29, 2015 14:21
-
-
Save Dubhead/a51904c9e55908c747d0 to your computer and use it in GitHub Desktop.
「ソフトウェアエンジニアならば1時間以内に解けなければいけない5つの問題」の5問目を Pure で解いてみた
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
数式の文字列を eval できる言語ならどれでも大差なく解けそうなもの。 | |
というわけで Pure (http://purelang.bitbucket.org/) で解いてみた。 | |
#!/usr/bin/pure | |
using system; // for 'puts' | |
add100 acc 9 | |
= puts acc if eval acc == 100; | |
= _ otherwise; | |
add100 acc n = map f ["+", "-", ""] | |
with f s = add100 (acc + s + str(n+1)) (n+1); | |
end; | |
add100 "1" 1; | |
// eof | |
実行結果: | |
% ./add100.pure | |
1+2+3-4+5+6+78+9 | |
1+2+34-5+67-8+9 | |
1+23-4+5+6+78-9 | |
1+23-4+56+7+8+9 | |
12+3+4+5-6-7+89 | |
12+3-4+5+67+8+9 | |
12-3-4+5-6+7+89 | |
123+4-5+67-89 | |
123+45-67+8-9 | |
123-4-5-6-7+8-9 | |
123-45-67+89 | |
% |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
参考: http://blog.64p.org/entry/2015/05/27/095844