aboutsummaryrefslogtreecommitdiff
path: root/challenge-228
diff options
context:
space:
mode:
authorsimon-dueck <126712673+simon-dueck@users.noreply.github.com>2023-08-06 00:05:07 -0500
committerGitHub <noreply@github.com>2023-08-06 00:05:07 -0500
commitaa5da88fe33b3b849d28cd19d64b57cedcebe0fb (patch)
treed08ac3e61ca6628d83642f63300ab92c7bde89cf /challenge-228
parent10ec616c81e28fa2bdd7230718941fc16f28d49a (diff)
downloadperlweeklychallenge-club-aa5da88fe33b3b849d28cd19d64b57cedcebe0fb.tar.gz
perlweeklychallenge-club-aa5da88fe33b3b849d28cd19d64b57cedcebe0fb.tar.bz2
perlweeklychallenge-club-aa5da88fe33b3b849d28cd19d64b57cedcebe0fb.zip
Added solutions in F#
Diffstat (limited to 'challenge-228')
-rw-r--r--challenge-228/simon-dueck/fsharp/ch-1.fsx42
-rw-r--r--challenge-228/simon-dueck/fsharp/ch-2.fsx38
2 files changed, 80 insertions, 0 deletions
diff --git a/challenge-228/simon-dueck/fsharp/ch-1.fsx b/challenge-228/simon-dueck/fsharp/ch-1.fsx
new file mode 100644
index 0000000000..d1979902c9
--- /dev/null
+++ b/challenge-228/simon-dueck/fsharp/ch-1.fsx
@@ -0,0 +1,42 @@
+(*
+ You are given an array of integers.
+ Write a script to find out the sum of unique elements in the given array.
+*)
+
+let rec remove_dupes (arr: 'a list) =
+ match arr with
+ | x::xs when List.contains x xs -> remove_dupes <| List.except (seq {x}) xs
+ | x::xs -> x :: remove_dupes xs
+ | [] -> []
+
+
+let print_list arr =
+ printf "[ "
+ for item in arr do
+ printf $"{item}; "
+ printf "]"
+
+
+let a = [2; 1; 3; 2;]
+print_list a
+printfn " -> %d" (List.sum <| remove_dupes a)
+
+let b = [1; 1; 1; 1;]
+print_list b
+printfn " -> %d" (List.sum <| remove_dupes b)
+
+let c = [2; 1; 3; 4;]
+print_list c
+printfn " -> %d" (List.sum <| remove_dupes c)
+
+let d = [
+ 31; 41; 81; 91; 42; 11; 60; 12; 82; 61;
+ 14; 32; 71; 62; 34; 43; 13; 01; 50; 44;
+ 14; 32; 71; 62; 34; 43; 13; 01; 50; 44;
+ 72; 45; 21; 04; 63; 94; 02; 83; 93; 46;
+ 72; 45; 21; 04; 63; 94; 02; 83; 93; 46;
+ 19; 33; 15; 84; 92; 95; 03; 73; 47; 64;
+ 18; 17; 65; 16; 97; 96; 48; 10; 99; 00;
+ ]
+print_list d
+printfn " -> %d" (List.sum <| remove_dupes d) \ No newline at end of file
diff --git a/challenge-228/simon-dueck/fsharp/ch-2.fsx b/challenge-228/simon-dueck/fsharp/ch-2.fsx
new file mode 100644
index 0000000000..a9e1d7fce2
--- /dev/null
+++ b/challenge-228/simon-dueck/fsharp/ch-2.fsx
@@ -0,0 +1,38 @@
+(*
+ You are given an array of integers in which all elements are unique.
+ Write a script to perform the following operations until the array is empty and return the total count of operations.
+*)
+
+let operate arr =
+
+ let rec loop count arr =
+ match arr with
+ | x::xs when x = List.min arr -> loop (count + 1) xs
+ | x::xs -> loop (count + 1) (xs @ [x])
+ | [] -> count
+
+ loop 0 arr
+
+let print_list arr =
+ printf "[ "
+ for item in arr do
+ printf $"{item}; "
+ printf "]"
+
+let a = [3; 4; 2;]
+print_list a
+printfn " -> %d" (operate a)
+
+let b = [1; 2; 3;]
+print_list b
+printfn " -> %d" (operate b)
+
+let c = [
+ 31; 41; 81; 91; 42; 11; 60; 12; 82; 61;
+ 14; 32; 71; 62; 34; 43; 13; 01; 50; 44;
+ 72; 45; 21; 04; 63; 94; 02; 83; 93; 46;
+ 19; 33; 15; 84; 92; 95; 03; 73; 47; 64;
+ 18; 17; 65; 16; 97; 96; 48; 10; 99; 00;
+ ]
+print_list c
+printfn " -> %d" (operate c) \ No newline at end of file