evantianx / CodeWars-Haskell

0 stars 0 forks source link

Count the smiley faces! #5

Open evantianx opened 5 years ago

evantianx commented 5 years ago

Given an array (arr) as an argument complete the function countSmileys that should return the total number of smiling faces.

Rules for a smiling face:

Valid smiley face examples:

:) :D ;-D :~)

Invalid smiley faces:

;( :> :} :]

Example cases:

countSmileys([':)', ';(', ';}', ':-D']);       // should return 2;
countSmileys([';D', ':-(', ':-)', ';~)']);     // should return 3;
countSmileys([';]', ':[', ';*', ':$', ';-D']); // should return 1;

Note: In case of an empty array return 0. You will not be tested with invalid input (input will always be an array). Order of the face (eyes, nose, mouth) elements will always be the same

evantianx commented 5 years ago

Solutions

countSmileys :: [String] -> Int
countSmileys = length . filter correct

correct [e,m] = e `elem` eyes && m `elem` mouth
correct [e,n,m] = n `elem` nose && correct [e,m]
correct _ = False

eyes = ":;"
nose = "-~"
mouth = ")D"