@@ -117,7 +117,7 @@ permutateSeq existSeq (x:xs) = map (++ [x]) existSeq ++
117
117
(x, y): current position
118
118
(c, r): final position
119
119
constrs: cells are not available (can't include the start cell)
120
- -}
120
+ -}
121
121
move :: (Nat , Nat ) -> (Nat , Nat ) -> [(Nat , Nat )]-> [Maybe (Nat , Nat )]
122
122
move (x, y) (c, r) constrs
123
123
| x+ 1 <= c && y+ 1 <= r && xCell `notElem` constrs && yCell `notElem` constrs =
@@ -157,13 +157,13 @@ allPathsHelper (Just (x, y):xs) final constrs =
157
157
158
158
-- Return the Completed Heap
159
159
allPaths :: (Nat , Nat ) -> (Nat , Nat ) -> [(Nat , Nat )] -> [Maybe (Nat , Nat )]
160
- allPaths start end constrs =
160
+ allPaths start end constrs =
161
161
Just start: removeNothing (allPathsHelper [Just start] end constrs)
162
162
163
163
-- Return all unique paths index in a Heap
164
164
allPaths' :: (Nat , Nat ) -> (Nat , Nat ) -> [(Nat , Nat )] -> [[Nat ]]
165
165
allPaths' start end constrs = findAllPaths completedHeap [] (length completedHeap- 1 )
166
- where completedHeap =
166
+ where completedHeap =
167
167
Just start: removeNothing (allPathsHelper [Just start] end constrs)
168
168
169
169
-- Remove Nothing on the tail
@@ -178,13 +178,87 @@ isValidPath (x, y) (c, r) constrs
178
178
| x == c && y == r = True
179
179
| x > c || y > r = False
180
180
| (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
183
183
184
184
findOnePath :: (Nat , Nat ) -> (Nat , Nat ) -> [(Nat , Nat )] -> [(Nat , Nat )]
185
185
findOnePath (x, y) (c, r) constrs
186
- | isValidPath (x+ 1 , y) (c, r) constrs =
186
+ | isValidPath (x+ 1 , y) (c, r) constrs =
187
187
(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 =
189
189
(x, y+ 1 ): findOnePath(x, y+ 1 ) (c,r) constrs
190
190
| 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