aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-03-18 21:55:19 +0000
committerGitHub <noreply@github.com>2023-03-18 21:55:19 +0000
commit561153c080466c0782c46598a1d0f41b5f37a779 (patch)
treec17ab752b5ef693632e6ef375717e70c4203f12a
parent5b415fb921db3b6482e5c2c04027fec92451024d (diff)
parentb99d2b310214e758f4cabd413bd17ba0815d8252 (diff)
downloadperlweeklychallenge-club-561153c080466c0782c46598a1d0f41b5f37a779.tar.gz
perlweeklychallenge-club-561153c080466c0782c46598a1d0f41b5f37a779.tar.bz2
perlweeklychallenge-club-561153c080466c0782c46598a1d0f41b5f37a779.zip
Merge pull request #7741 from simon-dueck/master
Added solutions for weeks 207 and 208
-rw-r--r--challenge-207/simon-dueck/README0
-rw-r--r--challenge-207/simon-dueck/fsharp/ch-1.fsx33
-rw-r--r--challenge-207/simon-dueck/fsharp/ch-2.fsx35
-rw-r--r--challenge-208/simon-dueck/README0
-rw-r--r--challenge-208/simon-dueck/fsharp/ch-1.fsx50
-rw-r--r--challenge-208/simon-dueck/fsharp/ch-2.fsx24
6 files changed, 142 insertions, 0 deletions
diff --git a/challenge-207/simon-dueck/README b/challenge-207/simon-dueck/README
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/challenge-207/simon-dueck/README
diff --git a/challenge-207/simon-dueck/fsharp/ch-1.fsx b/challenge-207/simon-dueck/fsharp/ch-1.fsx
new file mode 100644
index 0000000000..b7946394ab
--- /dev/null
+++ b/challenge-207/simon-dueck/fsharp/ch-1.fsx
@@ -0,0 +1,33 @@
+(*
+ You are given an array of words.
+ Write a script to print all the words in the given array that
+ can be types using alphabet on only one row of the keyboard.
+ Let us assume the keys are arranged as below:
+*)
+
+let keyboard: string list =
+ ["qwertyuiopQWERTYUIOP"; "asdfghjklASDFGHJKL"; "zxcvbnmZXCVBNM";]
+
+let rec contains_letter (l: char) (str: string) =
+ if str.Length = 0 then false
+ elif str.[0] = l then true
+ else contains_letter l str.[1..]
+
+let rec is_subset (letters: string) (word: string) =
+ if word.Length = 0 then
+ true
+ elif (contains_letter word.[0] letters) then
+ is_subset letters word.[1..]
+ else
+ false
+
+let single_row_words (words: string list) (layout: string list) =
+ let mutable output: list<string> = []
+ for word in words do
+ for row in layout do
+ if is_subset row word then
+ output <- word :: output
+ output
+
+let words = single_row_words ["Hello"; "Alaska"; "Dad"; "Peace"] keyboard
+printfn $"{words}"
diff --git a/challenge-207/simon-dueck/fsharp/ch-2.fsx b/challenge-207/simon-dueck/fsharp/ch-2.fsx
new file mode 100644
index 0000000000..72cb04f2ac
--- /dev/null
+++ b/challenge-207/simon-dueck/fsharp/ch-2.fsx
@@ -0,0 +1,35 @@
+(*
+ You are given an array of integers containing citations a researcher has received for each paper.
+ Write a script to compute the researcher’s H-Index.
+ For more information please checkout the wikipedia page.
+
+ The H-Index is the largest number h such that h articles have at least h citations each.
+ For example, if an author has five publications, with 9, 7, 6, 2, and 1 citations (ordered
+ from greatest to least), then the author’s h-index is 3, because the author has three
+ publications with 3 or more citations. However, the author does not have four publications
+ with 4 or more citations.
+*)
+
+let rec reverse (arr: int list): int list =
+ match arr with
+ | [x] -> [x]
+ | x::xs -> (reverse xs) @ [x]
+ | _ -> []
+
+let h_index (arr: int list) =
+ if arr.Length = 0 then
+ 0
+ else
+ let sorted = arr |> List.sort |> reverse
+
+ let rec find_index (arr: int list) (index: int): int =
+ match arr with
+ | [] -> index - 1
+ | x::xs when x >= index -> find_index xs (index + 1)
+ | _ -> index
+
+ find_index sorted 0
+
+//[1; 4; 5; 3; 6; 1; 2; 6; 9; 4; 0;] [3; 3; 3; 4; 5;]
+
+printfn $"{h_index [1; 4; 5; 3; 6; 1; 2; 6; 9; 4; 0;]}" \ No newline at end of file
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