Last active
September 22, 2017 17:08
-
-
Save jhcarr/e00707d7589a6355977538e568df1bec to your computer and use it in GitHub Desktop.
Potential solution to question 3 from SDG student interview
This file contains hidden or 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
module SdgStringSolver where | |
import Data.List | |
import Data.Char | |
help = "Run findTargetSubString against a test case such as test1 or test2." | |
target = "sdg" | |
test1 = ["SDGxx", "xxSDG", "xSDGx"] -- All results are true | |
test2 = ["SxDG", "S", "xxSD"] -- All results are false | |
findTargetSubString [] = [] | |
findTargetSubString a = zip (map (\s -> elem target (breakString s 3)) a) a | |
breakString :: String -> Int -> [String] | |
breakString [] _ = [] | |
breakString (a:as) n | |
| n < 1 = [] | |
| otherwise = take n (map toLower (a:as)) : breakString as n | |
----- Or if you're more familiar with Data.List and you like one-liners: ----- | |
alsoFindTargetSubstring a = zip (map (isInfixOf target . map toLower) a) a |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Even cleaner if you add one more custom function:
zipMap fn a = zip (map fn a) a