diff --git a/src/interviewing.io/2023-09-16-question1.hs b/src/interviewing.io/2023-09-16-question1.hs new file mode 100644 index 0000000..0a550d1 --- /dev/null +++ b/src/interviewing.io/2023-09-16-question1.hs @@ -0,0 +1,49 @@ +import qualified Data.List as List +import qualified Data.Maybe as Maybe +import Data.Set (Set) +import qualified Data.Set as Set + +{- +interviewing.io question: + + +Given a list of whole positive numbers, how would you go about finding two numbers in that list that sum up to a given target number. Assume no duplicates in list. + +-} + + +wholePositiveNumbers :: Set Integer +wholePositiveNumbers = Set.fromAscList [1..1_000_000] + +-- findTargetSumIn xs n = +-- (if abs (x-n) `member` xs +-- && abs (x-n) + x == n +-- then Just (abs (x-n), x) +-- else Nothing) : findTargetSumIn xs n +-- where +-- x = +ans lst sum = List.nubBy isMirror $ Maybe.catMaybes $ Set.elems $ Set.map (\x -> + let x' = abs (x-sum) + in + if x' `Set.member` lst + && x' + x == sum + then Just (x',x) + else Nothing + ) lst + +isMirror (a,a') (b,b') + = a == b' + && b == a' + +example = Set.fromList [14,13,6,7,8,10,1,2] +example' = Set.fromList [14,13,6,7,8,10,1] +example'' = Set.fromList [1,14,13,3,6,7,2,8,10,4] + +main = do + print "Quick 3 answers:" + print $ ans example 3 + print $ ans example' 3 + print $ ans example'' 5 + + print "Long answer" + print $ ans wholePositiveNumbers 3