Skip to content

Instantly share code, notes, and snippets.

View nnabeyang's full-sized avatar

noriaki watanabe nnabeyang

View GitHub Profile
@nnabeyang
nnabeyang / diff.bash
Created December 3, 2017 12:28
ファイルの最後にヌルバイトを追加する。追加後と追加前で差異が無いかチェックする
#!/bin/bash
ls -l $1 | awk '{print $5,$10}'|
while read n f
do
if [ "$f" != "" ]; then
xxd -p "$2/$f" | fold -w2 | head -n $n | diff -u "$3/$f" -
fi
done
@nnabeyang
nnabeyang / main.js
Last active May 8, 2017 23:32
普通の電卓
//usage: node main.js "14 + 34 * 22 + (3 + 4) * 5"
var util = require('util'); var calc = calc || {};
calc.Parser = function() {
this.tokens = []; this.parens = []; this.pos = 0;
};
calc.Parser.prototype.next = function() {
var offset = this.parens[this.parens.length - 1];
if(offset + this.pos < this.tokens.length) return this.tokens[offset + this.pos++];
else return null;
};
@nnabeyang
nnabeyang / main.rb
Last active May 8, 2017 04:00
逆ポーランド記法の電卓
#usage: ruby main.rb
class Parser
def initialize
@stack = []
end
def parse(line)
scanner = Scanner.new(line)
while (token = scanner.next) do
add_token(token)
end
@nnabeyang
nnabeyang / Guardfile
Last active April 22, 2017 06:28
Ruby on Railsのrails new用のテンプレート
# Guardのマッチング規則を定義
guard :minitest, spring: "bin/rails test", all_on_start: false do
watch(%r{^test/(.*)/?(.*)_test\.rb$})
watch('test/test_helper.rb') { 'test' }
watch('config/routes.rb') { integration_tests }
watch(%r{^app/models/(.*?)\.rb$}) do |matches|
"test/models/#{matches[1]}_test.rb"
end
watch(%r{^app/controllers/(.*?)_controller\.rb$}) do |matches|
resource_tests(matches[1])
$ sqlite3 test.db
SQLite version 3.7.9 2011-11-01 00:52:41
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> create table fruits(name string, value integer);
sqlite> insert into fruits values('apple', 100);
sqlite> insert into fruits values('orange', 130);
sqlite> .q
@nnabeyang
nnabeyang / prime.hs
Created February 18, 2014 13:13
素数判定
ldf :: Int -> Int -> Maybe Int
ldf k n | k ^ 2 > n = Nothing
| rem n k == 0 = Just k
| otherwise = ldf (k+1) n
prime :: Int -> Bool
prime n = case (ldf 2 n) of
Just m -> m == n
Nothing -> True
@nnabeyang
nnabeyang / gemstring.hs
Last active August 29, 2015 13:56
『王女様の宝石パターンを見つけよう!』 https://codeiq.jp/ace/yuki_hiroshi/q684
import Parsing (parse, line)
import System.IO
import System.Environment
import Data.Char
import Data.Map as M
import Data.Time.Clock
import Control.Monad.State
-- http://d.hatena.ne.jp/mzp/20090308/bench
bench f = do from <- getCurrentTime
@nnabeyang
nnabeyang / hanoi.hs
Created January 30, 2014 07:53
左端のn段のハノイの塔を最小回数で右端に移動させるときの最初の一手について ref: http://qiita.com/nnabeyang/items/5339a3248284b0039255
import Control.Monad.Writer
size :: [[a]] -> Int
size = foldr (\ xs n -> length xs + n) 0
move :: [[Int]] -> (Int, Int) -> Writer [String] [[Int]]
move xss (from, to) = return([move_ xs y (from, to) i| (xs, i) <- zip xss [0..]])
where (y:_) = xss !! from
move_ :: [Int] -> Int -> (Int, Int) -> Int -> [Int]
move_ [] y (from, to) i = if i == to then [y] else []
move_ (x:xs) y (from, to) i |i == from = xs
@nnabeyang
nnabeyang / gcd.hs
Last active August 29, 2015 13:55
ユークリッド互除法の計算過程をControl.Monad.Writerで ref: http://qiita.com/nnabeyang/items/4667ed2ed5aa2a618e28
import Control.Monad.Writer
gcd' :: Int -> Int -> Writer [String] Int
gcd' a b
|b == 0 = do
tell["= " ++ show a]
return(a)
|otherwise = do
let nb = a `mod` b
expr = "gcd(" ++ show(b) ++ ", " ++ show(nb) ++ ")"
msg = show(a) ++ " = " ++ show(b) ++ " * " ++ show(a `div` b) ++ " + " ++ show(nb)
type Matrix = [[Int]]
dropAt :: Int -> [a] -> [a]
dropAt i xs = [x| (x, j) <- zip xs [0..], i /= j]
cofactor :: Int -> Int -> Matrix -> Matrix
cofactor i j m = [dropAt j v| (v, k) <- zip m [0..], i /= k]
det :: Matrix -> Int
type Matrix = [[Int]]
dropAt :: Int -> [a] -> [a]
dropAt i xs = as ++ bs where (as, _ :bs) = splitAt i xs
cofactor :: Int -> Int -> Matrix -> Matrix