From b99d2b310214e758f4cabd413bd17ba0815d8252 Mon Sep 17 00:00:00 2001 From: simon-dueck <126712673+simon-dueck@users.noreply.github.com> Date: Fri, 17 Mar 2023 02:09:06 -0500 Subject: Added week 207 solutions in F# --- challenge-207/simon-dueck/fsharp/ch-1.fsx | 33 +++++++++++++++++++++++++++++ challenge-207/simon-dueck/fsharp/ch-2.fsx | 35 +++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 challenge-207/simon-dueck/fsharp/ch-1.fsx create mode 100644 challenge-207/simon-dueck/fsharp/ch-2.fsx (limited to 'challenge-207/simon-dueck/fsharp') 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 = [] + 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 -- cgit