Skip to content

Instantly share code, notes, and snippets.

@kotapiku
kotapiku / buchi_empty.py
Last active August 2, 2020 14:43
emptiness check of a buchi automaton
import pydot
import sys
# This script checks whether the input buchi automaton given by a dot file is empty.
# Labels of initial states should be init.
# Labels of accepting states should be accept.
if len(sys.argv) != 2:
print("usuage: buchi_empty.py path_to_dotfile")
sys.exit()
; toffoliのみを用いて可逆fulladderを作る
def not(x) = if x == 1 then 0 else 1
def toffoli(x, y, z) = if x == 1 && y == 1 then not(z) else z
def or(x, y) =
let nx = toffoli(x, 1, 1) in
let ny = toffoli(y, 1, 1) in
toffoli(nx, ny, 1)
import Data.List
import Data.Maybe
-- 31
isqrt :: Int -> Int
isqrt = floor . sqrt . fromIntegral
isprime :: Int -> Bool
isprime n
| n <= 1 = False
rot :: Int -> [Char] -> [Char]
rot 0 a = a
rot n a = rot (n-1) [rotsucc x | x <- a]
rotsucc :: Char -> Char
rotsucc 'z' = 'a'
rotsucc 'Z' = 'A'
rotsucc a = if elem a (['a'..'z'] ++ ['A'..'Z']) then (succ a) else a
myzipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
max' :: Int -> Int -> Int
max' a b
| a <= b = b
| otherwise = a
replicate' :: Int -> a -> [a]
replicate' 0 a = []
replicate' n a = a:(replicate' (n-1) a)
take' :: Int -> [a] -> [a]
@kotapiku
kotapiku / set_card_game.py
Created September 24, 2017 16:37
カードゲームsetでsetができない最大枚数を探った
import itertools
import random
def oneset(a,b,c):
for j in range(4):
if (a[j] + b[j] + c[j]) % 3 != 0:
return False
return True
def isnotset(field,card):
@kotapiku
kotapiku / haskell_excersize_5.hs
Last active April 2, 2018 14:32
kokuyouwind/haskell-exercises chapter5(途中まで)の解答
-- 基本的な高階関数(chapter5)
applyPair :: (a -> b) -> (a, a) -> (b, b)
applyPair f t = ((f.fst) t, (f.snd) t)
applyN :: (a -> a) -> Int -> a -> a
applyN f 1 x = f x
applyN f n x = f $ applyN f (n-1) x
squares :: Int -> [Int]
squares n = takeWhile (<=n) $ map (^2) [1..]
@kotapiku
kotapiku / haskell_exercise_4.hs
Last active September 23, 2017 05:07
kokuyouwind/haskell-exercises chapter4の解答
-- 再帰(chapter4)
tri_number :: Int -> Int
tri_number n
| n == 0 = 0
| otherwise = n + tri_number (n-1)
tetration :: Integer -> Integer -> Integer
tetration n m
| m == 0 = 1
| otherwise = (n^) $ tetration n (m-1)
@kotapiku
kotapiku / haskell_excercise_1_3.hs
Created September 13, 2017 14:15
kokuyouwind/haskell-exercises chapter1,3の解答
-- マンハッタン距離(chapter1)
manlen :: (Int, Int) -> (Int, Int) -> Int
manlen p1 p2 = abs ((fst p1)-(fst p2)) + abs ((snd p1)-(snd p2))
points :: Int -> [(Int, Int)]
points x = [(x1,x2) | x1 <- [-1*x..x], x2 <- [-1*x..x]]
mancircle :: Int -> [(Int, Int)]
mancircle x = [p | p <- points x, manlen p (0,0) == x]
#多変数多項式の整序 (辞書式順序ver)
# ex) ((x**2)y+x(y**2)+y**2)÷(xy-1,y**2-1) -> [[2,1,1],[1,2,1],[0,2,1]] [[[1,1,1],[0,0,-1]],[[0,2,1],[0,0,-1]]]
def multikxver():
f_in,g_in = input().split()
f = []
g = []
for i in f_in[2:-2].split("],["):
f.append(list(map(int,i.split(","))))
for ii in g_in[3:-3].split("]],[["):