Skip to content

Instantly share code, notes, and snippets.

View YuRen-tw's full-sized avatar
🐢
Rawr

Yu-Ren YuRen-tw

🐢
Rawr
View GitHub Profile
import re
def repl_map(xs, ys, none='', take=0):
mapping = dict(zip(xs, ys))
def f(matchobj):
item = matchobj.group(take)
return mapping.get(item, none)
return f
def replace_pipe(script, replacements):
@YuRen-tw
YuRen-tw / prerequisites.hs
Created May 26, 2018 16:16
FLOLAC 2018 0-p: Prerequisites
{- 1 -}
myFst :: (a, b) -> a
myFst (a, b) = a
{- 2 -}
myOdd :: Int -> Bool
myOdd = (== 1) . flip mod 2
@YuRen-tw
YuRen-tw / caesar.hs
Last active May 28, 2018 07:04
FLOLAC 2018 0-4: Caesar Cipher & Decipher
import Data.Char (ord, chr)
import Data.List
import Data.Function (on)
encode :: Int -> String -> String
encode n = map (shift n)
decode :: String -> (String, Int)
decode = aarrr mkPair (flip minimumBy [0..25] . compareOnDiff . count)
where aarrr f g x = f x $ g x
@YuRen-tw
YuRen-tw / goldbach.hs
Created May 13, 2018 18:01
FLOLAC 2018 0-3: Goldbach's conjecture
goldbach :: Int -> Maybe (Int, Int)
goldbach = listToMaybe . goldbachFull
where listToMaybe = foldr (const . Just) Nothing
goldbachFull :: Int -> [(Int, Int)]
goldbachFull n = mkPairs . filter isNegPrime . takeHalfN $ primes
where mkPairs = map (\p -> (p, n-p))
isNegPrime p = isPrime (n - p)
takeHalfN = takeWhile (\p -> p*2 <= n)
@YuRen-tw
YuRen-tw / histogram.hs
Created May 6, 2018 17:36
FLOLAC 2018 0-2
import Data.List
histogram :: [Int] -> String
histogram = unlines . reverse . putNumber . transpose . mkLines
where putNumber = ("0123456789" :) . ("==========" :)
mkLines :: [Int] -> [String]
mkLines = graph . group . sort . ([0..9] ++)
graph :: [[Int]] -> [String]
@YuRen-tw
YuRen-tw / nub.hs
Last active April 27, 2018 07:25
FLOLAC 2018 0-1
nub :: [Int] -> [Int]
nub [] = []
nub (x:xs) = x:(nub $ filter (/= x) xs)
import enum
FULL_LINE_COMMENT = ';'
class CHAR(enum.Enum):
LEFT = 1
RIGHT = 2
SYMBOL = 3
DIGIT = 4
SPACE = 5
@YuRen-tw
YuRen-tw / mkInfFuncToggle.js
Created March 18, 2017 16:49
Infinite Loop
function mkInfFuncToggle(func) {
var X = { x : 0 };
function infFunc() {
func();
if (X.x) { setTimeout(infFunc, 1); }
}
function infFuncToggle() {
if (X.x) { X.x = 0; }
else {
X.x = 1;
def Fibonacci(length) :
a, b = 1, 1
for _ in range(length) :
yield a
a, b = b, a + b