Skip to content

Instantly share code, notes, and snippets.

View draftcode's full-sized avatar

Masaya Suzuki draftcode

View GitHub Profile
import Data.Char
positions :: (Eq a) => a -> [a] -> [Int]
positions t xs = [n | (x,n) <- zip xs [0..(length xs)-1], x == t]
count :: Char -> String -> Int
count c (x:xs) | c == x = 1 + count c xs
| otherwise = count c xs
count _ [] = 0
replicate :: Int -> a -> [a]
replicate n x = [x | _ <- [1..n]]
pyths :: Int -> [(Int, Int, Int)]
pyths n = [(x,y,z) | x <- [1..n], y <- [1..n], z <- [1..n], x^2 + y^2 == z^2]
factors :: Int -> [Int]
factors n = [x | x <- [1..n], n `mod` x == 0]
perfects :: Int -> [Int]
#!/usr/bin/ruby
# レーベンシュタイン距離を算出するスクリプト
def LevenshteinDistance(s1, s2)
arr = Array.new(s1.size+1)
(0..(s1.size)).each do |i|
arr[i] = Array.new(s2.size+1)
arr[i][0] = i
end
(0..(s2.size)).each do |i|
#!/usr/bin/ruby
# ISBN13のコードをISBN10に変換して読書メーターのページを開くスクリプト
def isbn13_to_isbn10(s)
i = 10
sum = 0
s[3..-2].split(//).each do |c|
sum += c.to_i * i
i -= 1
end
#include <stdio.h>
#include <IOKit/IOCFPlugIn.h>
#include <IOKit/hid/IOHIDKeys.h>
#include <CoreFoundation/CoreFoundation.h>
int main(void)
{
IOReturn result = kIOReturnSuccess;
io_iterator_t hidObjectIterator = 0;
io_object_t hidDevice = IO_OBJECT_NULL;
List foldLeft := method(
context := Object clone prependProto(call sender)
if(call sender hasLocalSlot("self"),
context setSlot("self", call sender self)
)
initValue := call message evalArg(0)
eName := call message argAt(1) name
eResult := call message argAt(2) name
sum_two_dim := method(ll,
ll map(l, l sum) sum
)
sum_two_dim(list(list(1,2,3), list(4,5,6), list(7,8,9,10))) println
sum_dim_flat := method(ll,
ll map(l, if(l isKindOf(List), sum_dim_flat(l), l)) sum
)
Number divOrig := Number getSlot("/")
Number "/" := method(k, if(k == 0, 0, self divOrig(k)))
Vector := Sequence clone setItemType("float32")
iters := 1000
size := 1024
ops := iters * size
v1 := Vector clone setSize(size) rangeFill
v2 := Vector clone setSize(size) rangeFill
dt := Date secondsToRun(
Y := block(m,
block(x,
(m call(Y call(m))) call(x)
)
)
f := Y call(
block(m,
block(x,
if (x == 1, 1, x * m call(x - 1))