Skip to content

Instantly share code, notes, and snippets.

@jhcarr
Last active September 22, 2017 17:08
Show Gist options
  • Save jhcarr/e00707d7589a6355977538e568df1bec to your computer and use it in GitHub Desktop.
Save jhcarr/e00707d7589a6355977538e568df1bec to your computer and use it in GitHub Desktop.
Potential solution to question 3 from SDG student interview
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
@jhcarr
Copy link
Author

jhcarr commented Sep 22, 2017

Even cleaner if you add one more custom function:

zipMap fn a = zip (map fn a) a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment