Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jul 24, 2023. It is now read-only.

Commit 71661bb

Browse files
author
cd155
committed
add 8.9 valid parentheses
1 parent d99df7c commit 71661bb

File tree

1 file changed

+42
-18
lines changed

1 file changed

+42
-18
lines changed

‎src/Recursion.hs

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -311,34 +311,58 @@ hanoi n start temp end =
311311
bottom-up solution p356
312312
-}
313313
genPerm :: String -> [String]
314-
genPerm = genPermHelper [[]]
314+
genPerm chars = genPermHelper [[]] listStr
315+
where listStr = map (: []) chars
315316

316-
-- Loop through all possible character
317-
genPermHelper :: [String] -> String -> [String]
317+
-- Loop through all possible characters/strings
318+
genPermHelper :: [String] -> [String] -> [String]
318319
genPermHelper inp [] = inp
319320
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]
331340
appendPermHelper str c ind
332341
| ind > length str = []
333-
| otherwise = (fstHalf ++ [c] ++ sndHalf): appendPermHelper str c (ind+1)
342+
| otherwise = (fstHalf ++ c ++ sndHalf): appendPermHelper str c (ind+1)
334343
where (fstHalf,sndHalf) = splitAt ind str
335344

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]
338347
appendPermHelper' str c ind acc
339348
| ind > length str = acc
340349
| newPerm `elem` acc = appendPermHelper' str c (ind+1) acc
341350
| otherwise = appendPermHelper' str c (ind+1) (newPerm:acc)
342351
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 = "()"
344366

367+
genPerm' :: [String] -> [String]
368+
genPerm' = genPermHelper [[]]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /