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
# coding: utf-8 | |
import itertools as I | |
def it(): | |
yield 1 | |
yield 0 | |
yield 1 | |
a, b, c = it(), I.islice(it(), 1, None), I.islice(it(), 2, None) | |
for i in I.imap(int.__add__, I.imap(int.__mul__, a, b), c): |
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
↓正解↓ | |
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
class Memoize: | |
""" | |
Example usage: | |
>>> @Memoize | |
... def fib(n): | |
... if n <= 1: | |
... return n | |
... else: | |
... return fib(n - 1) + fib(n - 2) | |
>>> fib(300) |
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
import itertools | |
class SingleMix(object): | |
def __init__(self, x): self._x = x | |
def __repr__(self): return "%s(%r)" % (self.__class__.__name__, self._x) | |
class InfixMix(object): | |
def __init__(self, left, right): | |
self._left = left | |
self._right = right |
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
class once: | |
def __init__(self, f): | |
self.f = f | |
self.memo = {} | |
def __call__(self, *args): | |
if args in self.memo: | |
return self.memo[args] | |
else: | |
result = self.f(*args) |
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
{- | |
ふえぇ言語のインタプリタ(ver0.1) | |
構文: | |
ふ -> S | |
ぇ -> K | |
[F]え[G] -> [F][G] | |
[F]…[G] -> [G][F] | |
[F]、[G]は任意の式 | |
-} |
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
import Prelude | |
fibs = : 1 (: 1 (zipWith + fibs (tail fibs))) | |
main = putStr (unlines (map showInteger fibs)) |
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
data Token = Brace Int | Angle Int | Error | T String | |
transform :: [Token] -> [Int] -> [Token] | |
transform (Angle n:ts) (m:ms) | |
| m == n = T ";" : (transform ts (m:ms)) | |
| n < m = T "}" : (transform (Angle n:ts) ms) | |
transform (Angle n:ts) ms = transform ts ms | |
transform (Brace n:ts) (m:ms) | n > m = T "{" : transform ts (n:m:ms) | |
transform (Brace n:ts) [] | n > 0 = T "{" : transform ts [n] | |
transform (Brace n:ts) ms = T "{" : T "}" : transform (Angle n:ts) ms |
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
{- HQ9FSI+ interpreter -} | |
import Data.Char (toUpper) | |
import Control.Monad ((>=>)) | |
import Data.List (intersperse) | |
import System.Environment (getArgs) | |
type Program = String -> (String, String) | |
step :: Char -- operation | |
-> String -- entire program |
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
import sys | |
data = open(sys.argv[1], "r").read() | |
print data.replace("$", chr(34) + data.replace("\n", "\\n") + chr(34)).strip("\n") |