Skip to content

Instantly share code, notes, and snippets.

View yoshitsugu's full-sized avatar
🐐
bleat

Kota Yoshitsugu yoshitsugu

🐐
bleat
View GitHub Profile
@yoshitsugu
yoshitsugu / software-design-202103-dp.hs
Created February 22, 2021 13:22
Software Design 2021年3月号「パズルで鍛えるアルゴリズム力」第8回をHaskellで実装してみる
-- https://myuon.github.io/posts/haskell-atcoder/ を参考にしつつ競プロっぽい書き方にできないか試す
{-# LANGUAGE Strict #-}
module Main where
import qualified Data.ByteString.Char8 as BS
import qualified Data.Vector.Unboxed as VU
checkMax :: BS.ByteString -> BS.ByteString -> VU.Vector Int -> Int -> Int -> Int -> Int -> Int
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
eval$s=%q(eval(%w(s=%(eval$s=%q(#{$s}));print(s);sleep(0.5);
q=9608.chr("utf-8");print("\e[?25l\e[1G\e[37m\e[23F");24.tim
es.each{puts(q*6 0)};w=->{sleep(0 .1)};w[];f=->(n,* as
){print(as.map { |a|"\e[#{a}"}* " "+q*n);w[];};pr i nt
("\e[2F\e[4 C\e [31m");14.t ime s{f[2,"1A"," 2D" ];
};f[3,"1 F","2C "];f[3," 1F","5 C"];f[3," 1F","8 C"
];f[3 ,"1F","11 C"];f [2,"1F"," 14C"]; f[3,"1F", "1
6C "];5.times{f [2 ,"1B","2D"]; }; f[4,"1B","2D" ];
4. times{f[3,"1A "];};f[2,"1A"]; f[3,"1A"];5.time s{
f[ 2,"1B","2D"];};f[4,"1B","2D"];4.times{f[3,"1A"];};f[ 2,
eval$s=%w(s=%(eval$s=%w(#{$s})*"")
;f=->*a{m=1 ;puts(a. map{|n|( m
==0)?(m= 1; 32.ch r* n):(m =0 ;
s.sli ce!(0 ,n ))}.j oi n)};m =
1; f[34];f[ 11,2,8,2 ,8,2,m]; f
[8 ,2,2,m,5,2,2,m,5,2,2,m,m];f[5 ,
2, 5,m,2,2,5, m,2 ,2, 5,m,m];f[2 ,
2, 8,2,8,2,8,m ,m ]; f[2,m,29,m, m
]; f[2,m,10,m,3,m,3,m,10,m,m];f[ 2
,m ,11,m,2,m, 2,m,10+ m,m,m];f[2 ,
extern crate rusty_machine;
extern crate rand;
extern crate gnuplot;
use rusty_machine::linalg::{Matrix, BaseMatrix};
use rusty_machine::learning::k_means::KMeansClassifier;
use rusty_machine::learning::UnSupModel;
use rand::thread_rng;
use rand::distributions::IndependentSample;
chunk :: (Eq b) => [a] -> (a -> b) -> [(b, [a])]
chunk [] _ = []
chunk (x:xs) f = let fx = f x
(h, t) = span ((==) fx . f) xs
in [(fx,(x:h))] ++ chunk t f
data ChunkType = Word | Blank deriving (Show, Eq)
nwSqrt :: Int -> Double
nwSqrt n = let s = sqrts $ fromIntegral n in
snd $ last $ takeWhile inThreshold $ zip s (tail s)
where
sqrts :: Double -> [Double]
sqrts n = 1.0:[x - (x ^ 2 - n) / (2 * x) | x <- sqrts n]
inThreshold :: (Double,Double) -> Bool
inThreshold (a,b) = abs (a - b) > 0.00000001
import Text.Parsec
data Exp = And Exp Exp | Or Exp Exp | Not Exp | T | F deriving Show
expr :: Parsec String st Exp
expr = do
t <- term
Or t <$> (string "or" *> expr) <|> pure t
term :: Parsec String st Exp
@yoshitsugu
yoshitsugu / ReaderSample.hs
Created August 23, 2015 08:59
reader sample code
import Control.Monad.Reader
data Config = Config { verbose :: Int, debug :: Bool }
configToLevel :: Config -> Int
configToLevel config
| debug config = 10
| otherwise = verbose config
@yoshitsugu
yoshitsugu / monad_sample_20150823.hs
Last active August 29, 2015 14:27
((->) Int ) 型もモナドとなる
import Control.Applicative
hoge :: Int -> Int
hoge = (*) 2
fuga :: Int -> Int
fuga = (+) 1
piyo :: Int -> Int
piyo = (+) <$> hoge <*> fuga