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.Strict as Map | |
import Data.List (inits, tails, any) | |
match "" "" _ = True | |
match "" _ _ = False | |
match _ "" _ = False | |
match expr text matches = let | |
possibilities = zip (tail $ inits text) (tail $ tails text) | |
pattern = head expr | |
expr' = tail expr |
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 Control.Applicative | |
import Control.Monad | |
import System.IO | |
import Data.List (foldl') | |
import Data.Maybe | |
main :: IO () | |
main = do | |
t <- fmap read getLine :: IO Int | |
lns <- mapM (const getLine) [1..t] |
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.Strict as Map | |
import qualified Data.Set as Set | |
import Data.Map.Strict ((!)) | |
import Data.List (partition) | |
getNeighbours list i j = let | |
possible = [(i-1, j-1),(i-1, j), (i-1, j+1), (i, j-1), (i, j+1), (i+1, j-1), (i+1, j), (i+1, j+1)] | |
actual = filter (\(x, y) -> x >=0 && y >= 0 && x < length list && y < length (head list)) possible |
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
sherlock :: Int -> Int -> Int -> [Int] -> Bool | |
sherlock left current sm [] = | |
if left == sm | |
then True | |
else False | |
sherlock left current sm rest = | |
case compare left sm of | |
LT -> let | |
left' = left + current |
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 Data.List (groupBy, sortBy) | |
import Data.Ord (comparing) | |
data DisjointSet = DisjointSet {index :: Int, value :: Int, parent :: Int} deriving Show | |
find :: [DisjointSet] -> Int -> Int | |
find ls i = let | |
djs = ls !! (i - 1) | |
ix = index djs | |
p = parent djs |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 Control.Monad.Trans.State | |
import Data.Map.Strict as Map | |
import Data.Set as Set | |
triangle n = sum [1..n] | |
factors n = [f | f <- [1..n], n `mod` f == 0 ] | |
addFactors :: Int -> Int -> Map.Map Int (Set.Set Int) -> Map.Map Int (Set.Set Int) | |
addFactors n i factorMap | |
| n == i = Map.insert n (Set.fromList (factors n)) factorMap |
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
-- Slow solution that fails for n>19 due to integer overflow | |
import Data.List | |
import Data.Ord | |
maxPair n = sortBy (comparing snd) [(x,y) | x <- [0,3..n], y <- [0,5..(n-x)], x+y==n] | |
gen [] = -1 | |
gen ((x,y):_) = let | |
noOf5s = (replicate x '5') | |
noOf3s = (replicate y '3') | |
in read (noOf5s ++ noOf3s) :: Int |