-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.hs
38 lines (31 loc) · 1.06 KB
/
solution.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
-- Said Kadrioski <said@kadrioski.de>
--
names = ["Sheldon", "Leonard", "Penny", "Rajesh", "Howard"]
-- Solution works with double entries as well.
-- names = ["Rajesh", "Howard", "Sheldon", "Sheldon", "Leonhard", "Penny", "Penny"]
-- The Problem at hand.
doubleCola :: Int -> [String] -> [String]
doubleCola n [] = []
doubleCola 0 (x:xs) = x:xs
doubleCola n (x:xs) = doubleCola (n-1) $ xs ++ [x, x]
-- How to get name after nth iteration?
--
-- Using BruteForce
getCola :: Int -> [String] -> String
getCola n l = head $ doubleCola n l
-- Using my mathematical formula.
getColaa :: Int -> [String] -> String
getColaa n l = l !! formi n (length l)
--
-- This is my important formula
formi :: Int -> Int -> Int
formi n m = d2 - m
where g = n+m
d0 = div g m
d1 = floor $ logBase 2 (fromIntegral d0)
d2 = div g (2^d1)
-- Test. Is not a proof, just visual "confirmation" that my formula works.
test = and $ zipWith (==) listA listB
where longList = [1..2222]
listA = map (`getCola` names) longList
listB = map (`getColaa` names) longList