@@ -311,34 +311,58 @@ hanoi n start temp end =
311
311
bottom-up solution p356
312
312
-}
313
313
genPerm :: String -> [String ]
314
- genPerm = genPermHelper [[] ]
314
+ genPerm chars = genPermHelper [[] ] listStr
315
+ where listStr = map (: [] ) chars
315
316
316
- -- Loop through all possible character
317
- genPermHelper :: [String ] -> String -> [String ]
317
+ -- Loop through all possible characters/strings
318
+ genPermHelper :: [String ] -> [ String ] -> [String ]
318
319
genPermHelper inp [] = inp
319
320
genPermHelper inp (x: xs) = genPermHelper newInp xs
320
- where newInp = appendPerm inp x
321
-
322
- -- Accumulate new permutations (not finished)
323
- appendPerm :: [String ] -> Char -> [String ]
324
- appendPerm [] _ = []
325
- appendPerm (x: xs) c = newPerm ++ appendPerm xs c
326
- where -- newPerm = appendPermHelper x c 0 -- with duplicates
327
- newPerm = appendPermHelper' x c 0 [] -- without duplicates
328
-
329
- -- Insert the character to every index of the input
330
- appendPermHelper :: String -> Char -> Nat -> [String ]
321
+ where newInp = appendPerm inp x []
322
+
323
+ -- Accumulate new permutations base on the string (not finished)
324
+ appendPerm :: [String ] -> String -> [String ] -> [String ]
325
+ appendPerm [] _ acc = acc
326
+ appendPerm (x: xs) c acc = appendPerm xs c (acc++ newPerm)
327
+ where -- newPerm = complement acc (appendPermHelper x c 0) -- with duplicates
328
+ newPerm = complement acc (appendPermHelper' x c 0 [] ) -- without duplicates
329
+
330
+ -- another check for duplicates in appendPerm level,
331
+ -- this need to work with appendPermHelper'
332
+ complement :: [String ] -> [String ] -> [String ]
333
+ complement _ [] = []
334
+ complement a (x: xs)
335
+ | x `elem` a = complement a xs
336
+ | otherwise = x: complement a xs
337
+
338
+ -- Insert the character/string to every index of the input
339
+ appendPermHelper :: String -> String -> Nat -> [String ]
331
340
appendPermHelper str c ind
332
341
| ind > length str = []
333
- | otherwise = (fstHalf ++ [c] ++ sndHalf): appendPermHelper str c (ind+ 1 )
342
+ | otherwise = (fstHalf ++ c ++ sndHalf): appendPermHelper str c (ind+ 1 )
334
343
where (fstHalf,sndHalf) = splitAt ind str
335
344
336
- -- Insert the character to every index of the input with no duplicates
337
- appendPermHelper' :: String -> Char -> Nat -> [String ] -> [String ]
345
+ -- Insert the character/string to every index of the input with no duplicates
346
+ appendPermHelper' :: String -> String -> Nat -> [String ] -> [String ]
338
347
appendPermHelper' str c ind acc
339
348
| ind > length str = acc
340
349
| newPerm `elem` acc = appendPermHelper' str c (ind+ 1 ) acc
341
350
| otherwise = appendPermHelper' str c (ind+ 1 ) (newPerm: acc)
342
351
where (fstHalf,sndHalf) = splitAt ind str
343
- newPerm = fstHalf ++ [c] ++ sndHalf
352
+ newPerm = fstHalf ++ c ++ sndHalf
353
+
354
+ {-
355
+ 8.9
356
+ Implement an algorithm to print all valid (eg: properly opened and closed)
357
+ combinations of n pairs of parentheses.
358
+
359
+ test case: genPerm' ["()","()","()"]
360
+
361
+ modify the genPerm from 8.7 and 8.8 to make it works for string, and remove
362
+ duplicates in two different levels.
363
+ -}
364
+ paren :: String
365
+ paren = " ()"
344
366
367
+ genPerm' :: [String ] -> [String ]
368
+ genPerm' = genPermHelper [[] ]
0 commit comments