aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsimon-dueck <126712673+simon-dueck@users.noreply.github.com>2023-03-17 02:09:06 -0500
committerGitHub <noreply@github.com>2023-03-17 02:09:06 -0500
commitb99d2b310214e758f4cabd413bd17ba0815d8252 (patch)
treeecbb140da64187ddade7f1e63db64355e7c155f1
parentd4868ee4ef7d1f08b23b8384b220a6e82c217e5d (diff)
downloadperlweeklychallenge-club-b99d2b310214e758f4cabd413bd17ba0815d8252.tar.gz
perlweeklychallenge-club-b99d2b310214e758f4cabd413bd17ba0815d8252.tar.bz2
perlweeklychallenge-club-b99d2b310214e758f4cabd413bd17ba0815d8252.zip
Added week 207 solutions in F#
-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
3 files changed, 68 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