aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-04-02 23:52:12 +0100
committerGitHub <noreply@github.com>2023-04-02 23:52:12 +0100
commit87bad2a480ca75cba828cf56b7f649045b449b0f (patch)
treeaf9ce8905d92920b7c17e31b000440c8abced371
parent8915a66de2cb2a724aee5e55ddfc15580cfdf1d5 (diff)
parent95c661a12b81513cdddc3977c02262668a58a68c (diff)
downloadperlweeklychallenge-club-87bad2a480ca75cba828cf56b7f649045b449b0f.tar.gz
perlweeklychallenge-club-87bad2a480ca75cba828cf56b7f649045b449b0f.tar.bz2
perlweeklychallenge-club-87bad2a480ca75cba828cf56b7f649045b449b0f.zip
Merge pull request #7818 from simon-dueck/master
Added solutions for weeks 209 and 210
-rw-r--r--challenge-209/simon-dueck/fsharp/ch-1.fsx18
-rw-r--r--challenge-210/simon-dueck/fsharp/ch-2.fsx48
2 files changed, 66 insertions, 0 deletions
diff --git a/challenge-209/simon-dueck/fsharp/ch-1.fsx b/challenge-209/simon-dueck/fsharp/ch-1.fsx
new file mode 100644
index 0000000000..d376088f2f
--- /dev/null
+++ b/challenge-209/simon-dueck/fsharp/ch-1.fsx
@@ -0,0 +1,18 @@
+(*
+ You are given an array of binary bits that ends with 0.
+ Valid sequences in the bit string are:
+ [0] -decodes-to-> "a"
+ [1, 0] -> "b"
+ [1, 1] -> "c"
+ Write a script to print 1 if the last character is an “a” otherwise print 0.
+*)
+
+let rec decode (arr): int =
+ match arr with
+ | [x] when not x -> 1 // last character decodes to 'a'
+ | x::y::xs when x -> decode xs // list begins with 'b' or 'c'
+ | x::xs when not x -> decode xs // list begins with 'a'
+ | _ -> 0
+
+printfn $"{decode [true; true; false; false; true; false; false;]}"
+printfn $"{decode [true; true; false; false; true; false;]}" \ No newline at end of file
diff --git a/challenge-210/simon-dueck/fsharp/ch-2.fsx b/challenge-210/simon-dueck/fsharp/ch-2.fsx
new file mode 100644
index 0000000000..1dde08cc63
--- /dev/null
+++ b/challenge-210/simon-dueck/fsharp/ch-2.fsx
@@ -0,0 +1,48 @@
+(*
+ You are given an array of integers which can move in right
+ direction if it is positive and left direction when negative.
+
+ If two numbers collide then the smaller one will explode.
+ And if both are same then they both explode.
+ We take the absolute value in consideration when comparing.
+
+ All numbers move at the same speed, therefore any 2 numbers
+ moving in the same direction will never collide.
+
+ Write a script to find out who survives the collision.
+*)
+
+let abs a = if a > 0 then a else 0 - a
+
+let collide a b: int option =
+ if (abs a) = (abs b) then None
+ elif (abs a) > (abs b) then Some(a)
+ else Some(b)
+
+let number_collision arr: int list =
+
+ let rec advance = function
+ | x::y::xs when x > 0 && y < 0 ->
+ match collide x y with
+ | Some(w) when w > 0 -> advance (w :: xs)
+ | Some(w) -> w :: (advance xs)
+ | None -> advance xs
+ | x::y::xs when x > 0 && y > 0 || x < 0 && y < 0 ->
+ x :: advance (y::xs)
+ | x -> x
+
+ let rec repeat arr =
+ let advanced = advance arr
+ if advanced = arr then advanced
+ else repeat advanced
+
+ repeat arr
+
+let a = [2; 3; -1;]
+printfn $"{number_collision a}"
+
+let b = [3; 2; -4;]
+printfn $"{number_collision b}"
+
+let c = [1; -1;]
+printfn $"{number_collision c}" \ No newline at end of file