Skip to content

Instantly share code, notes, and snippets.

View zaneli's full-sized avatar
🤔
...?

Shunsuke Otani zaneli

🤔
...?
View GitHub Profile
@zaneli
zaneli / p11.clj
Last active August 29, 2015 13:59
「ClojureでNinety-Nine Lisp Problems(P11~15)」ブログ用
(defn my-encode
"Run-length encoding of a list."
[xs]
(map #(list (count %) (first %)) (partition-by identity xs))
)
(defn my-encode-modified
"Modified run-length encoding."
[xs]
(map #(let [[cnt elem] %] (if (== cnt 1) elem (list cnt elem))) (my-encode xs))
@zaneli
zaneli / p06-1.clj
Last active August 29, 2015 13:59
「ClojureでNinety-Nine Lisp Problems(P06~10)」ブログ用
(defn my-palindrome?
"Find out whether a list is a palindrome."
[word]
(let [middle (/ (count word) 2), word1 (take middle word), word2 (reverse (drop middle word))]
(every? #(= (first %) (second %)) (map list word1 word2))
)
)
@zaneli
zaneli / p01-1.clj
Last active August 29, 2015 13:58
「ClojureでNinety-Nine Lisp Problems(P01~05)」ブログ用
(defn my-last
"Find the last box of a list."
[xs]
(if (>= 1 (count xs))
xs
(my-last (rest xs))
)
)
@zaneli
zaneli / euler40.hs
Last active August 29, 2015 13:58
「HaskellでProject Euler(Problem 40~42)」ブログ用
import Data.Char (digitToInt)
main = print $ product $ pickUpChampernowne $ map (10^) [0..6]
pickUpChampernowne :: [Int] -> [Int]
pickUpChampernowne targets = pickUp 1 targets 0 []
where
pickUp _ [] _ nums = nums
pickUp n targets@(x:xs) count nums
| inBetween = pickUp (n+1) xs count' (targetNum:nums)
@zaneli
zaneli / euler37-1.hs
Last active August 29, 2015 13:57
「HaskellでProject Euler(Problem 37~39)」ブログ用
import Zaneli.Euler (numToList, listToNum)
main = print $ sum $ trunPrimes 11
trunPrimes :: Int -> [Int]
trunPrimes limit = trunPrimes' 3 [2] []
where
trunPrimes' n primes result
| length result >= limit = result
| isPrime n = trunPrimes' (n + 2) (primes ++ [n]) result'
@zaneli
zaneli / euler34.hs
Last active August 29, 2015 13:57
「HaskellでProject Euler(Problem 34~36)」ブログ用
import Data.Array (listArray, (!))
import Data.List (find)
import Zaneli.Euler (numToList)
main = print $ sum digitFactorials
digitFactorials :: [Int]
digitFactorials = [n | n <- [3..limit], let ns = map (facts !) $ numToList n, (sum ns) == n]
where
facts = let (minIdx, maxIdx) = (0, 9) in listArray (minIdx, maxIdx) $ map fact [minIdx..maxIdx] -- 一桁の値の階乗を予め作っておく
@zaneli
zaneli / euler31-1.hs
Last active August 29, 2015 13:56
「HaskellでProject Euler(Problem 31~33)」ブログ用
main = print $ length $ coinSums 200
coinSums :: Integer -> [(Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer)]
coinSums n = [ (po1, po2, po5, po10, po20, po50, pe1, pe2) |
po1 <- [0,1..n],
po2 <- [0,2..n-po1],
po5 <- [0,5..n-(po1+po2)],
po10 <- [0,10..n-(po1+po2+po5)],
po20 <- [0,20..n-(po1+po2+po5+po10)],
po50 <- [0,50..n-(po1+po2+po5+po10+po20)],
@zaneli
zaneli / euler28-1.hs
Last active August 29, 2015 13:56
「HaskellでProject Euler(Problem 28~30)」ブログ用
main = print $ 1 + addDiags [2..1001*1001] 0 1 0
addDiags :: (Num a, Num b, Eq b) => [a] -> a -> Int -> b -> a
addDiags [] sum _ _ = sum
addDiags list sum step count = let (n:ns) = drop step' list in
addDiags ns (sum + n) step' count'
where
(step', count') | count == 4 = (step + 2, 1)
| otherwise = (step, count + 1)
@zaneli
zaneli / euler25-1.hs
Created January 25, 2014 10:09
「HaskellでProject Euler(Problem 25~27)」ブログ用
import Zaneli.Euler (fib)
main = print $ fst $ head $ dropWhile (\(_, n) -> n < 10^(1000-1)) $ zip [1..] $ map fib [1..]
@zaneli
zaneli / MyType.hs
Last active January 3, 2016 22:59
すごいHaskellたのしく学ぼう輪読会用
import Data.Maybe (catMaybes)
data MyType = I Int | F Float | C Char
deriving (Show)
-- 再帰を使う
filterC :: [MyType] -> [MyType]
filterC [] = []
filterC (x@(C _):xs) = x:filterC xs
filterC (_:xs) = filterC xs