Skip to content

Instantly share code, notes, and snippets.

View samidarko's full-sized avatar

Sami Darko samidarko

View GitHub Profile
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 / 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
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 / 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
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
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]
import qualified Data.List as L
l1 = [(1, 3), (2, 4), (5, 7), (6, 8), (10, 15)]
l2 = [(6, 8), (1, 9), (2, 4), (4, 7)]
sort' :: Ord b => [(b, b1)] -> [(b, b1)]
sort' = L.sortOn fst
mergeIntervals [] = []
mergeIntervals ((s, e):xs) = fn ((s,e):[]) $ xs
@samidarko
samidarko / polygon-area.hs
Created March 14, 2018 05:48
Warning: this code is in progress and only works for triangles
import System.IO
-- lst = processInput ["1043 770","551 990","681 463"]
lst = processInput ["458 695","621 483","877 469","1035 636","1061 825","875 1023","645 1033","
485 853"]
-- area :: [Double] -> Double
area d@(a:b:c:[]) = let s = sum d / 2 in round $ sqrt $ (s * (s-a) * (s-b) * (s-c) )
processInput = map (tuplintfy . words)
@samidarko
samidarko / isLeapYear.hs
Last active March 18, 2018 12:29
calculate leap yers
isLeapYear y
| y `mod` 4 == 0 = if y `mod` 100 == 0 then (y `mod` 400 == 0) else True
| otherwise = False
main :: IO ()
main = do
n_k <- getLine
let n_k_a = map read (words "4 1") :: [Int]
let k = n_k_a !! 1
items_temp <- getLine
let items = map read (words items_temp) :: [Int]
b_temp <- getLine
let charged = read b_temp :: Int
let actual = (sum items - items !! k) `div` 2