This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import qualified Data.Map as M | |
amount = 18 | |
coins = [5, 4, 2, 1] | |
greedy a cs = fn a cs M.empty | |
where |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
isLeapYear y | |
| y `mod` 4 == 0 = if y `mod` 100 == 0 then (y `mod` 400 == 0) else True | |
| otherwise = False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |