diff options
| author | simon-dueck <126712673+simon-dueck@users.noreply.github.com> | 2023-03-17 02:07:34 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-17 02:07:34 -0500 |
| commit | d4868ee4ef7d1f08b23b8384b220a6e82c217e5d (patch) | |
| tree | 80306c31efc8416a22ab1d428585f8d80bf27e27 | |
| parent | 0f4a809c0bead5e3fd8bc4616e1c011c9d6fb8a9 (diff) | |
| download | perlweeklychallenge-club-d4868ee4ef7d1f08b23b8384b220a6e82c217e5d.tar.gz perlweeklychallenge-club-d4868ee4ef7d1f08b23b8384b220a6e82c217e5d.tar.bz2 perlweeklychallenge-club-d4868ee4ef7d1f08b23b8384b220a6e82c217e5d.zip | |
Added week 208 solutions in F#
| -rw-r--r-- | challenge-208/simon-dueck/README | 0 | ||||
| -rw-r--r-- | challenge-208/simon-dueck/fsharp/ch-1.fsx | 50 | ||||
| -rw-r--r-- | challenge-208/simon-dueck/fsharp/ch-2.fsx | 24 |
3 files changed, 74 insertions, 0 deletions
diff --git a/challenge-208/simon-dueck/README b/challenge-208/simon-dueck/README new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/challenge-208/simon-dueck/README diff --git a/challenge-208/simon-dueck/fsharp/ch-1.fsx b/challenge-208/simon-dueck/fsharp/ch-1.fsx new file mode 100644 index 0000000000..62d8a4361d --- /dev/null +++ b/challenge-208/simon-dueck/fsharp/ch-1.fsx @@ -0,0 +1,50 @@ +(*
+ You are given two arrays of strings.
+ Write a script to find out all common strings in the given two arrays with minimum index sum.
+ If no common strings found returns an empty list.
+*)
+
+let rec contains arr elem =
+ match arr with
+ | x::xs when x = elem -> true
+ | [] -> false
+ | _ -> contains arr.Tail elem
+
+let find_common arr_a arr_b =
+ let rec inner arr_a arr_b acc =
+ if arr_b = [] then
+ acc
+ elif contains arr_a arr_b.Head then
+ inner arr_a arr_b.Tail (arr_b.Head :: acc)
+ else
+ inner arr_a arr_b.Tail acc
+ inner arr_a arr_b []
+
+let index_of arr item =
+ let rec index_of_rec arr item index =
+ match arr with
+ | x::xs when x = item -> index
+ | x::xs -> index_of_rec xs item (index + 1)
+ | _ -> -1
+ index_of_rec arr item 0
+
+let index_sum item arr_a arr_b =
+ index_of arr_a item + index_of arr_b item
+
+let min_index_sum arr_a arr_b =
+
+ let rec inner arr_a arr_b common min acc =
+ match common with
+ | [] -> acc
+ | _ ->
+ let curr = index_sum (common.Head) arr_a arr_b
+ match curr with
+ | x when x < min -> inner arr_a arr_b common.Tail curr [common.Head;]
+ | x when x = min -> inner arr_a arr_b common.Tail curr (common.Head :: acc)
+ | _ -> acc
+
+ inner arr_a arr_b (find_common arr_a arr_b) (arr_a.Length) []
+
+let arr_a = ["A"; "B"; "C";]
+let arr_b = ["C"; "A"; "B";]
+printfn $"arrays: {arr_a} {arr_b}\nminimum sum index : {min_index_sum arr_a arr_b}"
\ No newline at end of file diff --git a/challenge-208/simon-dueck/fsharp/ch-2.fsx b/challenge-208/simon-dueck/fsharp/ch-2.fsx new file mode 100644 index 0000000000..7f4b18450b --- /dev/null +++ b/challenge-208/simon-dueck/fsharp/ch-2.fsx @@ -0,0 +1,24 @@ +(*
+ You are given an array of integers in sequence with one missing and one duplicate.
+
+ Write a script to find the duplicate and missing integer in the given array.
+ Return -1 if none found.
+
+ For the sake of this task, let us assume the array
+ contains no more than one duplicate and missing.
+*)
+
+let rec find_duplicate arr = // this is assuming the duplicates are consecutive
+ match arr with
+ | x::y::_ when x = y -> y
+ | x::xs -> find_duplicate xs
+ | _ -> -1
+
+let rec find_missing arr =
+ match arr with
+ | x::y::_ when y - x = 2 -> y - 1
+ | x::xs -> find_missing xs
+ | _ -> -1
+
+let arr = [1; 2; 3; 3; 4; 6; 7; 8; 9;]
+printfn $"duplicate : {find_duplicate arr}\nmissing : {find_missing arr}"
\ No newline at end of file |
