aboutsummaryrefslogtreecommitdiff
path: root/challenge-207/simon-dueck/fsharp/ch-2.fsx
diff options
context:
space:
mode:
authorboblied <boblied@gmail.com>2023-03-20 08:28:53 -0500
committerboblied <boblied@gmail.com>2023-03-20 08:28:53 -0500
commite679e218ca3ba0ba4da25e28bf5ebe740a3842ae (patch)
tree11de20a63c0abdfca453334dd35570831f237ab6 /challenge-207/simon-dueck/fsharp/ch-2.fsx
parent1c868a3d5e041651b5847bbec416133f15ad8a3c (diff)
parent9c5cd2108a8f6cf8b793c28051fdf8d767a4c8a9 (diff)
downloadperlweeklychallenge-club-e679e218ca3ba0ba4da25e28bf5ebe740a3842ae.tar.gz
perlweeklychallenge-club-e679e218ca3ba0ba4da25e28bf5ebe740a3842ae.tar.bz2
perlweeklychallenge-club-e679e218ca3ba0ba4da25e28bf5ebe740a3842ae.zip
Merge branch 'master' of https://github.com/boblied/perlweeklychallenge-club
Diffstat (limited to 'challenge-207/simon-dueck/fsharp/ch-2.fsx')
-rw-r--r--challenge-207/simon-dueck/fsharp/ch-2.fsx35
1 files changed, 35 insertions, 0 deletions
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