Merge pull request #172 from seanyu4296/add-purescript-solution-for-chapter-1-chapter-2
Add purescript solution for chapter 1 chapter 2
This commit is contained in:
39
01_introduction_to_algorithms/Purescript/BinarySearch.purs
Normal file
39
01_introduction_to_algorithms/Purescript/BinarySearch.purs
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
module GrokkingAlgos.BinarySearch where
|
||||||
|
|
||||||
|
import Prelude
|
||||||
|
import Data.Array ((!!))
|
||||||
|
import Data.Array as Array
|
||||||
|
import Data.Maybe (Maybe(..))
|
||||||
|
import Effect (Effect)
|
||||||
|
import Effect.Class.Console (logShow)
|
||||||
|
|
||||||
|
-- | Binary Search - input is a sorted list of elements
|
||||||
|
-- | Big o notation - log n
|
||||||
|
-- | Traveling salesman - O (n!)
|
||||||
|
binarysearch :: Int -> Array Int -> Int -> Int -> Maybe Int
|
||||||
|
binarysearch x arr low high
|
||||||
|
| low > high = Nothing
|
||||||
|
| otherwise =
|
||||||
|
let
|
||||||
|
mid = (high + low) / 2
|
||||||
|
in
|
||||||
|
arr !! mid
|
||||||
|
>>= case _ of
|
||||||
|
item
|
||||||
|
| item == x -> Just mid
|
||||||
|
item
|
||||||
|
| item > x -> binarysearch x arr low (mid - 1)
|
||||||
|
item
|
||||||
|
| item < x -> binarysearch x arr (mid + 1) high
|
||||||
|
_ -> Nothing
|
||||||
|
|
||||||
|
find :: Int -> Array Int -> Maybe Int
|
||||||
|
find x arr = binarysearch x arr low high
|
||||||
|
where
|
||||||
|
low = 0
|
||||||
|
|
||||||
|
high = (Array.length arr) - 1
|
||||||
|
|
||||||
|
main :: Effect Unit
|
||||||
|
main = do
|
||||||
|
logShow $ find 20 [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20 ]
|
||||||
6
01_introduction_to_algorithms/Purescript/README.md
Normal file
6
01_introduction_to_algorithms/Purescript/README.md
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
# Getting Started
|
||||||
|
1. Install `spago` and `purescript` through `yarn` or `npm` (e.g. `yarn global add spago` and `yarn global add purescript`)
|
||||||
|
2. Run file through `spago run --watch --main <module-name>` (e.g. `spago run --watch --main GrokkingAlgos.SelectionSort`)
|
||||||
|
|
||||||
|
# Main Repo
|
||||||
|
- https://github.com/seanyu4296/grokking-algo-in-purs
|
||||||
8
01_introduction_to_algorithms/Purescript/packages.dhall
Normal file
8
01_introduction_to_algorithms/Purescript/packages.dhall
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
let upstream =
|
||||||
|
https://github.com/purescript/package-sets/releases/download/psc-0.13.8-20200724/packages.dhall sha256:bb941d30820a49345a0e88937094d2b9983d939c9fd3a46969b85ce44953d7d9
|
||||||
|
|
||||||
|
let overrides = {=}
|
||||||
|
|
||||||
|
let additions = {=}
|
||||||
|
|
||||||
|
in upstream // overrides // additions
|
||||||
9
01_introduction_to_algorithms/Purescript/spago.dhall
Normal file
9
01_introduction_to_algorithms/Purescript/spago.dhall
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
{-
|
||||||
|
Welcome to a Spago project!
|
||||||
|
You can edit this file as you like.
|
||||||
|
-}
|
||||||
|
{ name = "my-project"
|
||||||
|
, dependencies = [ "console", "effect", "psci-support", "arrays", "debug", "lists", "ordered-collections", "strings"]
|
||||||
|
, packages = ./packages.dhall
|
||||||
|
, sources = [ "src/**/*.purs", "test/**/*.purs" ]
|
||||||
|
}
|
||||||
6
02_selection_sort/Purescript/README.md
Normal file
6
02_selection_sort/Purescript/README.md
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
# Getting Started
|
||||||
|
1. Install `spago` and `purescript` through `yarn` or `npm` (e.g. `yarn global add spago` and `yarn global add purescript`)
|
||||||
|
2. Run file through `spago run --watch --main <module-name>` (e.g. `spago run --watch --main GrokkingAlgos.SelectionSort`)
|
||||||
|
|
||||||
|
# Main Repo
|
||||||
|
- https://github.com/seanyu4296/grokking-algo-in-purs
|
||||||
17
02_selection_sort/Purescript/SelectionSort.purs
Normal file
17
02_selection_sort/Purescript/SelectionSort.purs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
module GrokkingAlgos.SelectionSort where
|
||||||
|
|
||||||
|
import Prelude
|
||||||
|
import Data.Foldable (minimum)
|
||||||
|
import Data.List (List(..), delete, (:))
|
||||||
|
import Data.Maybe (Maybe(..))
|
||||||
|
import Effect (Effect)
|
||||||
|
import Effect.Class.Console (logShow)
|
||||||
|
|
||||||
|
selectionsort :: List Int -> List Int
|
||||||
|
selectionsort l = case minimum l of
|
||||||
|
Nothing -> Nil
|
||||||
|
Just min -> min : selectionsort (delete min l)
|
||||||
|
|
||||||
|
main :: Effect Unit
|
||||||
|
main = do
|
||||||
|
logShow $ selectionsort $ 1 : 2 : 3 : 5 : 4 : Nil
|
||||||
8
02_selection_sort/Purescript/packages.dhall
Normal file
8
02_selection_sort/Purescript/packages.dhall
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
let upstream =
|
||||||
|
https://github.com/purescript/package-sets/releases/download/psc-0.13.8-20200724/packages.dhall sha256:bb941d30820a49345a0e88937094d2b9983d939c9fd3a46969b85ce44953d7d9
|
||||||
|
|
||||||
|
let overrides = {=}
|
||||||
|
|
||||||
|
let additions = {=}
|
||||||
|
|
||||||
|
in upstream // overrides // additions
|
||||||
9
02_selection_sort/Purescript/spago.dhall
Normal file
9
02_selection_sort/Purescript/spago.dhall
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
{-
|
||||||
|
Welcome to a Spago project!
|
||||||
|
You can edit this file as you like.
|
||||||
|
-}
|
||||||
|
{ name = "my-project"
|
||||||
|
, dependencies = [ "console", "effect", "psci-support", "arrays", "debug", "lists", "ordered-collections", "strings"]
|
||||||
|
, packages = ./packages.dhall
|
||||||
|
, sources = [ "src/**/*.purs", "test/**/*.purs" ]
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user