connect strings works!

This commit is contained in:
nora 2021-04-11 21:43:29 +02:00
parent 40a4b61733
commit ca2beccab4

View file

@ -102,21 +102,20 @@ fizzBuzzSingle n
| n `mod` 3 == 0 = "Fizz" | n `mod` 3 == 0 = "Fizz"
| otherwise = show n | otherwise = show n
-- connect a string list so that it overlaps the strings -- connect a string list so that it overlaps the strings
-- ["hi", "india", "ares", "resolution"] -> "hindiaresolution" -- ["hi", "india", "ares", "resolution"] -> "hindiaresolution"
connectStrings :: [String] -> String connectStrings :: [String] -> String
connectStrings [] = [] connectStrings [] = []
connectStrings [x] = x connectStrings [x] = x
connectStrings (x : y : []) = "" connectStrings [x, y] = connect2Strings x y
connectStrings _ = "not implemented yet" connectStrings (x : xs) = connect2Strings x $ connectStrings xs
connectStrings2 :: String -> String -> String connect2Strings :: String -> String -> String
connectStrings2 x y = [] connect2Strings = connect2StringsH []
getCommonPart :: String -> String -> String -- w: the part already cut off from x, x: the first string, y: the second string
getCommonPart [] _ = [] connect2StringsH :: String -> String -> String -> String
getCommonPart x y connect2StringsH _ [] _ = []
| x `isPrefixOf` y = x connect2StringsH w x y
| otherwise = getCommonPart (tail x) y | x `isPrefixOf` y = w ++ y
| otherwise = connect2StringsH (w ++ [head x]) (tail x) y