diff options
| -rw-r--r-- | 5-kyu/hashtag-generator.hs | 24 | ||||
| -rw-r--r-- | 6-kyu/does-my-number-look-big-in-this.hs | 16 | ||||
| -rw-r--r-- | 6-kyu/duplicate-encoder.hs | 18 | ||||
| -rw-r--r-- | 6-kyu/equal-side-of-an-array.hs | 11 | ||||
| -rw-r--r-- | 6-kyu/pyramid-array.rkt | 14 | ||||
| -rw-r--r-- | 6-kyu/valid-braces.hs | 22 | ||||
| -rw-r--r-- | 7-kyu/a-rule-of-divisibility-by-7.rkt | 15 | ||||
| -rw-r--r-- | 7-kyu/sum-of-odd-numbers.hs | 6 |
8 files changed, 126 insertions, 0 deletions
diff --git a/5-kyu/hashtag-generator.hs b/5-kyu/hashtag-generator.hs new file mode 100644 index 0000000..2da598f --- /dev/null +++ b/5-kyu/hashtag-generator.hs @@ -0,0 +1,24 @@ +-- https://www.codewars.com/kata/52449b062fb80683ec000024 + +module Codewars.Kata.Hashtag where + +import Data.Char +import Control.Monad + +capitalized w = (toUpper $ head w) : (map toLower $ tail w) + +processCapChar s = + join $ map (\x -> if isLower x then [x] else " "++[x]) s + +generateHashtag :: String -> Maybe String +generateHashtag s = impl (processCapChar s) + where + impl s + | length (words s) == 0 = Nothing + | otherwise = + let result = "#" ++ (join $ map capitalized $ words s) + in + if (length result) > 140 then + Nothing + else + Just result diff --git a/6-kyu/does-my-number-look-big-in-this.hs b/6-kyu/does-my-number-look-big-in-this.hs new file mode 100644 index 0000000..3de21d8 --- /dev/null +++ b/6-kyu/does-my-number-look-big-in-this.hs @@ -0,0 +1,16 @@ +-- https://www.codewars.com/kata/5287e858c6b5a9678200083c + +module Narcissistic where + +splitNum n = reverse $ impl n + where + impl n + | n < 10 = [n] + | otherwise = (n `mod` 10):(impl (n `div` 10)) + +narcissistic :: Integral n => n -> Bool +narcissistic n + | (sum $ map (^ (length splited)) splited) == n = True + | otherwise = False + where + splited = splitNum n diff --git a/6-kyu/duplicate-encoder.hs b/6-kyu/duplicate-encoder.hs new file mode 100644 index 0000000..1cb38eb --- /dev/null +++ b/6-kyu/duplicate-encoder.hs @@ -0,0 +1,18 @@ +-- https://www.codewars.com/kata/54b42f9314d9229fd6000d9c + +module Dups where + +import Data.Char + +count s c + | s == [] = 0 + | c == (head s) = 1 + count (tail s) c + | otherwise = count (tail s) c + +convert s c + | count s c > 1 = ')' + | otherwise = '(' + +duplicateEncode :: String -> String +duplicateEncode s = map (convert ls) ls + where ls = map toLower s diff --git a/6-kyu/equal-side-of-an-array.hs b/6-kyu/equal-side-of-an-array.hs new file mode 100644 index 0000000..00717d7 --- /dev/null +++ b/6-kyu/equal-side-of-an-array.hs @@ -0,0 +1,11 @@ +-- https://www.codewars.com/kata/5679aa472b8f57fb8c000047 + +module Codewars.G964.FindEven where + +findEvenIndex :: [Int] -> Int +findEvenIndex arr = findEvenIndexImpl [] 0 arr + +findEvenIndexImpl left n right + | right == [] = -1 + | sum left == sum (tail right) = n + | otherwise = findEvenIndexImpl (left ++ [head right]) (n + 1) (tail right) diff --git a/6-kyu/pyramid-array.rkt b/6-kyu/pyramid-array.rkt new file mode 100644 index 0000000..0d6a00b --- /dev/null +++ b/6-kyu/pyramid-array.rkt @@ -0,0 +1,14 @@ +#lang racket + +;; https://www.codewars.com/kata/515f51d438015969f7000013 + +(provide pyramid) + +(define (pyramid n) + (define (loop ret level cur) + (define next (cons 1 cur)) + (if (= level n) + (reverse ret) + (loop (cons next ret) (+ level 1) next))) + (loop '() 0 '())) + diff --git a/6-kyu/valid-braces.hs b/6-kyu/valid-braces.hs new file mode 100644 index 0000000..1df6f5c --- /dev/null +++ b/6-kyu/valid-braces.hs @@ -0,0 +1,22 @@ +-- https://www.codewars.com/kata/5277c8a221e209d3f6000b56 + +module Codewars.Kata.Braces where + +validBraces :: String -> Bool +validBraces xs = impl 0 0 0 xs where + impl cnt1 cnt2 cnt3 str = + if str == [] then + cnt1 == 0 && cnt2 == 0 && cnt3 == 0 + else let + x = head str + xs = tail str + in + if cnt1 < 0 || cnt2 < 0 || cnt3 < 0 then + False + else if x == '(' then impl (cnt1 + 1) cnt2 cnt3 xs + else if x == ')' then impl (cnt1 - 1) cnt2 cnt3 xs + else if x == '[' then impl cnt1 (cnt2 + 1) cnt3 xs + else if x == ']' then impl cnt1 (cnt2 - 1) cnt3 xs + else if x == '{' then impl cnt1 cnt2 (cnt3 + 1) xs + else if x == '}' then impl cnt1 cnt2 (cnt3 - 1) xs + else False diff --git a/7-kyu/a-rule-of-divisibility-by-7.rkt b/7-kyu/a-rule-of-divisibility-by-7.rkt new file mode 100644 index 0000000..6859107 --- /dev/null +++ b/7-kyu/a-rule-of-divisibility-by-7.rkt @@ -0,0 +1,15 @@ +#lang racket + +;; https://www.codewars.com/kata/55e6f5e58f7817808e00002e + +(provide seven) + +(define (seven m) + (define (impl cur steps) + (define x (quotient cur 10)) + (define y (modulo cur 10)) + (define next (- x (* 2 y))) + (if (< cur 100) + (cons cur steps) + (impl next (+ 1 steps)))) + (impl m 0)) diff --git a/7-kyu/sum-of-odd-numbers.hs b/7-kyu/sum-of-odd-numbers.hs new file mode 100644 index 0000000..11bb86e --- /dev/null +++ b/7-kyu/sum-of-odd-numbers.hs @@ -0,0 +1,6 @@ +-- https://www.codewars.com/kata/55fd2d567d94ac3bc9000064 + +module Codewars.SumOddNumbers where + +rowSumOddNumbers :: Integer -> Integer +rowSumOddNumbers x = x * x * x |
