Skip to content

Instantly share code, notes, and snippets.

View rhaseven7h's full-sized avatar
🙊

Gabriel Medina rhaseven7h

🙊
View GitHub Profile
@rhaseven7h
rhaseven7h / README.md
Created July 28, 2016 06:15
HackerRank.com - Functional Programming - Haskell - Super Digit

Super Digit

We define super digit of an integer x using the following rules:

  • If x has only 1 digit, then its super digit is x.
  • Otherwise, the super digit of x is equal to the super digit of the digit-sum of x. Here, digit-sum of a number is defined as the sum of its digits.

For example, super digit of 9875 will be calculated as:

@rhaseven7h
rhaseven7h / README.md
Created July 28, 2016 03:10
HackerRank.com - Functional Programming - Haskell - Filter Elements

Filter Elements

Given a list of N integers A = [a1, a2, ..., aN], you have to find those integers which are repeated at least K times. In case no such element exists you have to print -1.

If there are multiple elements in A which are repeated at least K times, then print these elements ordered by their first occurrence in the list.

Let's say A = [4, 5, 2, 5, 4, 3, 1, 3, 4] and K = 2. Then the output is

4 5 3
@rhaseven7h
rhaseven7h / README.md
Last active July 28, 2016 00:47
HackerRank.com - Functional Programming - Haskell - Sequence Full Of Colors

Sequence Full Of Colors

You are given a sequence of N balls in 4 colors: red, green, yellow and blue. The sequence is full of colors if and only if all of the following conditions are true:

  • There are as many red balls as green balls.
  • There are as many yellow balls as blue balls.
  • Difference between the number of red balls and green balls in every prefix of the sequence is at most 1.
  • Difference between the number of yellow balls and blue balls in every prefix of the sequence is at most 1.

Your task is to write a program, which for a given sequence prints True if it is full of colors, otherwise it prints False.

@rhaseven7h
rhaseven7h / README.md
Created July 26, 2016 22:00
HackerRank.com - Functional Programming - Haskell - Captain Prime

Captain Prime

Captain Prime is going on a trip to Primeland and needs support of his troops to make this voyage successful. To prevent the wrath of evil spirits, he has to throw out some people from his troop into the sea. This decision will depend on the identification number of the troop member.

His ship is divided into three parts: Left, right, and central. Every person on the ship is assigned an identification number (referred as id), and according to their id they get to work in one part of the ship, or end up getting thrown out of the ship.


A person's fate depends on the following conditions:

@rhaseven7h
rhaseven7h / a_compute_the_area_of_a_polygon.hs
Created July 23, 2016 08:49
HackerRank.com - Functional Programming - Haskell - Compute The Area Of A Polygon
main = do
raw <- getContents -- readFile "input.txt"
let toInts a = map (\e -> read e :: Int) a
noded = map toInts $ map words $ tail $ lines raw
nodec = length noded
xdata = map head noded
ydata = map last noded
sum1x = xdata
sum1y = (tail ydata) ++ [head ydata]
sum2x = (tail xdata) ++ [head xdata]
@rhaseven7h
rhaseven7h / a_prefix_compression.hs
Created July 23, 2016 06:35
HackerRank.com - Functional Programming - Prefix Compression - Haskell
slv n "" _ = n
slv n _ "" = n
slv n a b
| fa == fb = slv (n + 1) (tail a) (tail b)
| otherwise = n
where fa = head a
fb = head b
main = do
raw <- readFile "input.txt"
@rhaseven7h
rhaseven7h / a_functions_or_not.hs
Created July 23, 2016 05:53
HackerRank.com - Functional Programming - Functions or Not? - Haskell
import qualified Data.Map.Strict as MS
import Data.List
-- Basically a valid function is that which
-- always return the same output for a given input,
-- that is, a function that one time returns 3 for f(1)
-- and the next returns 5 for the same f(1) is NOT
-- a valid function
--
-- This algorithm checks we have unique outputs for
@rhaseven7h
rhaseven7h / a_kundu_and_bubble_wrap.hs
Created July 21, 2016 08:43
HackerRank.com - Functional Programming - Haskell - Kundu And Bubble Wrap
solve n m = sum times
where oSize = fromIntegral $ n * m
oIndices = [ 1 .. oSize ]
singleProb = (\x -> x / oSize)
singleTime = (\n -> 1 / (singleProb n))
times = map singleTime oIndices
main = do
raw <- readFile "input.txt"
let inStrArr = words $ head $ lines raw
@rhaseven7h
rhaseven7h / a_remove_duplicates.hs
Created July 21, 2016 06:03
HackerRank - Functional Programming - Haskell - Remove Duplicate Characters From String
---- Remove Duplicates and return characters in the order they were found
import Data.List
solve :: String -> String -> String
solve b "" = b
solve b (s:st)
| isInfixOf (s : "") b = solve b st
| otherwise = solve (s : b) st
@rhaseven7h
rhaseven7h / a_string_rotate.hs
Last active July 21, 2016 05:39
HackerRank.com - Functional Programming - Haskell - Return all rotations of a string
-- Rotate Strings
import Data.List
rotate s r = drop r s ++ take r s
solve s = intercalate " " (tail rss ++ [ head rss ])
where rss = [ rs | r <- [ 0 .. (length s - 1) ], let rs = rotate s r ]
main = do