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 e123505

Browse files
author
cd155
committed
solution for getting any power set
1 parent 2c68424 commit e123505

File tree

1 file changed

+81
-7
lines changed

1 file changed

+81
-7
lines changed

‎src/Recursion.hs

Lines changed: 81 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ permutateSeq existSeq (x:xs) = map (++ [x]) existSeq ++
117117
(x, y): current position
118118
(c, r): final position
119119
constrs: cells are not available (can't include the start cell)
120-
-}
120+
-}
121121
move :: (Nat, Nat) -> (Nat, Nat) -> [(Nat, Nat)]-> [Maybe (Nat, Nat)]
122122
move (x, y) (c, r) constrs
123123
| x+1 <= c && y+1 <= r && xCell `notElem` constrs && yCell `notElem` constrs =
@@ -157,13 +157,13 @@ allPathsHelper (Just (x, y):xs) final constrs =
157157

158158
-- Return the Completed Heap
159159
allPaths :: (Nat, Nat) -> (Nat, Nat) -> [(Nat, Nat)] -> [Maybe(Nat, Nat)]
160-
allPaths start end constrs =
160+
allPaths start end constrs =
161161
Just start: removeNothing (allPathsHelper [Just start] end constrs)
162162

163163
-- Return all unique paths index in a Heap
164164
allPaths' :: (Nat, Nat) -> (Nat, Nat) -> [(Nat, Nat)] -> [[Nat]]
165165
allPaths' start end constrs = findAllPaths completedHeap [] (length completedHeap-1)
166-
where completedHeap =
166+
where completedHeap =
167167
Just start: removeNothing (allPathsHelper [Just start] end constrs)
168168

169169
-- Remove Nothing on the tail
@@ -178,13 +178,87 @@ isValidPath (x, y) (c, r) constrs
178178
| x == c && y == r = True
179179
| x > c || y > r = False
180180
| (x, y) `elem` constrs = False
181-
| otherwise = isValidPath (x+1, y) (c, r) constrs ||
182-
isValidPath (x, y+1) (c, r) constrs
181+
| otherwise = isValidPath (x+1, y) (c, r) constrs ||
182+
isValidPath (x, y+1) (c, r) constrs
183183

184184
findOnePath :: (Nat, Nat) -> (Nat, Nat) -> [(Nat, Nat)] -> [(Nat, Nat)]
185185
findOnePath (x, y) (c, r) constrs
186-
| isValidPath (x+1, y) (c, r) constrs =
186+
| isValidPath (x+1, y) (c, r) constrs =
187187
(x+1, y): findOnePath(x+1, y) (c,r) constrs
188-
| isValidPath (x, y+1) (c, r) constrs =
188+
| isValidPath (x, y+1) (c, r) constrs =
189189
(x, y+1): findOnePath(x, y+1) (c,r) constrs
190190
| otherwise = []
191+
192+
{-
193+
8.3
194+
A magic index in an array A [0 ... n-1] is defined to be an index such
195+
that A[i] = i. Given a sorted array of distinct integers, write a
196+
method to find a magic index, if one exists, in array A.
197+
198+
For example:
199+
|0 |1 |2 |3 |4 |5 |6
200+
|-40 |-20 |0 |1 |4 |10 |12
201+
202+
Solution 1: brutal force
203+
Solution 2: binary search
204+
-}
205+
206+
findMagicIndex :: [Int] -> Nat -> Int
207+
findMagicIndex [] _ = error "No magic index in this array."
208+
findMagicIndex arr preInd
209+
| arr !! mid == (preInd + mid) = preInd + mid
210+
| arr !! mid > (preInd + mid) = findMagicIndex fstHalf preInd
211+
| otherwise = findMagicIndex sndHalf (preInd+mid)
212+
where mid = length arr `div` 2
213+
(fstHalf, sndHalf) = splitAt mid arr
214+
215+
{-
216+
8.3 with a sorted array of non-distinct integers
217+
|0 |1 |2 |3 |4 |5 |6 |7 |8 |9 |10
218+
|-10 |-5 |2 |2 |2 |3 |4 |7 |9 |12 |13
219+
-}
220+
221+
{-
222+
8.4
223+
Write a method to return all subsets of a set.
224+
225+
input: a = [1,3,4,6,10]
226+
output: the power set of a
227+
228+
test case = powerSetOf [3,3,7,10,1,15]
229+
-}
230+
powerSetOf :: [a] -> [[a]]
231+
powerSetOf xs = map (map (xs!!)) resultInInd
232+
where resultInInd = powerSetHelper initalSeq (length xs) 0
233+
initalSeq = powerSetOfInd 0 (length xs)
234+
235+
-- Initialize the list of index for pairing
236+
-- It is better to use index for paring because the index is a unique ID
237+
powerSetOfInd :: Nat -> Nat -> [[Nat]]
238+
powerSetOfInd i max
239+
| i < max = [i]: powerSetOfInd (i+1) max
240+
| otherwise = []
241+
242+
{-
243+
Copy the current set, and accumulate with new set
244+
245+
inp: give list of seq,
246+
len: length of original set,
247+
count: count number (start with 0) indicate when to stop the loop
248+
-}
249+
powerSetHelper :: [[Nat]] -> Nat -> Nat -> [[Nat]]
250+
powerSetHelper inp len count
251+
| count < len = inp ++ powerSetHelper newInp len (count+1)
252+
| otherwise = []
253+
where newInp = newPair inp len
254+
255+
-- Accumulate all new pair in the list
256+
newPair :: [[Nat]] -> Nat -> [[Nat]]
257+
newPair [] len = []
258+
newPair (x:xs) len =
259+
newPairHelper x [(last x +1) .. len-1] ++ newPair (xs) len
260+
261+
-- Give a new seq append new possible pairing to it
262+
newPairHelper :: [Nat] -> [Nat] -> [[Nat]]
263+
newPairHelper _ [] = []
264+
newPairHelper a (x:xs) = (a ++ [x]): newPairHelper a xs

0 commit comments

Comments
(0)

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