Skip to content

Instantly share code, notes, and snippets.

View samidarko's full-sized avatar

Sami Darko samidarko

View GitHub Profile
def main(arr):
arr.sort(key=lambda x: x[0])
if arr:
stack = [arr[0]]
else:
return []
for start, end in arr[1:]:
interval = stack[-1]
data HotelRank = HotelRank { idh :: Integer, rank :: Integer } deriving (Show, Eq)
instance Ord HotelRank where
compare (HotelRank i r) (HotelRank i' r')
| r == r' = compare i i'
| otherwise = if compare r r' == GT then LT else GT
l = [(4000, 8), (3000, 7), (2000, 8), (1000, 5), (2000, 5), (4000, 5), (3000, 2)]
rankings = map (\(i, r) -> (HotelRank i r)) l
@samidarko
samidarko / greedy.hs
Created March 9, 2018 03:41
naive greedy algorithm in Haskell
import qualified Data.Map as M
amount = 18
coins = [5, 4, 2, 1]
greedy a cs = fn a cs M.empty
where
import qualified Data.Map as M
l = [5, 3, 7, 0, 1, 4, 2]
m = M.fromList [(x,x)| x <- l]
searchSumInList xs s =
let m = M.fromList [(x,x)| x <- xs]
in foldl fn [] $ zip [0..] l
@samidarko
samidarko / binary-search.hs
Created March 8, 2018 10:06
binary search function in Haskell for Int lists
binarySearch :: Ord a => a -> [a] -> Maybe Int
binarySearch s xs = let left = 0
right = length xs - 1
in fn left right
where fn l r = if l > r
then Nothing
else let m = (l + r) `div` 2
e = xs !! m
in if e < s
then fn (m+1) r
l = [25626, 25757, 24367, 24267, 16, 100, 2, 7277]
deltaEncoding :: (Ord a, Num a) => [a] -> [a]
deltaEncoding xs = head xs : fn (xs)
where fn (x:y:ys) = let n = y - x -- : fn (y:ys)
in if isSingleByte n
then n : fn (y:ys)
else -128 : n :fn (y:ys)
fn (x:[]) = []
isSingleByte b = -127 <= b && b <= 127
@samidarko
samidarko / polygon.hs
Last active March 8, 2018 09:53
dentify whether four sides (given by four integers) can form a square, a rectangle, or neither.
import Data.List.Split (splitOn)
-- polygons.txt
-- 36 30 36 30
-- 15 15 15 15
-- 46 96 90 100
-- 86 86 86 86
-- 100 200 100 200
-- -100 200 -100 200
@samidarko
samidarko / search_sum_of_pair_in_array.py
Last active March 8, 2018 06:37
Find if sum a of pair of integer exists in an array
def brute_force(arr, s):
for i in arr:
for j in arr:
if i + j == s:
print("{} + {} = {}".format(i, j, s))
# brute_force([5, 3, 7, 0, 1, 4, 2], 5)
data Expr = Val Int | Div Expr Expr deriving Show
eval :: Expr -> Int
eval (Val x) = x
eval (Div x y) = eval x `div` eval y
safeDiv :: Int -> Int -> Maybe Int
safeDiv n m = if m == 0 then Nothing else Just (n `div` m)
safeEval :: Expr -> Maybe Int
@samidarko
samidarko / start.sh
Created October 1, 2017 03:52
zec/start.sh
#!/usr/bin/env bash
cd ${HOME}/zec
SRV="eu1-zcash.flypool.org:3333"
ADDRESS=""
RIG=`hostname`