1 #!/usr/bin/perl 2 # https://theweeklychallenge.org/blog/perl-weekly-challenge-207/#TASK2 3 # 4 # Task 2: H-Index 5 # =============== 6 # 7 # You are given an array of integers containing citations a researcher has 8 # received for each paper. 9 # 10 # Write a script to compute the researcher’s H-Index. For more information 11 # please checkout the wikipedia page. 12 # 13 ### The H-Index is the largest number h such that h articles have at least h 14 ### citations each. For example, if an author has five publications, with 9, 15 ### 7, 6, 2, and 1 citations (ordered from greatest to least), then the 16 ### author’s h-index is 3, because the author has three publications with 3 17 ### or more citations. However, the author does not have four publications 18 ### with 4 or more citations. 19 # 20 # 21 ## Example 1 22 ## 23 ## Input: @citations = (10,8,5,4,3) 24 ## Output: 4 25 ## 26 ## Because the 4th publication has 4 citations and the 5th has only 3. 27 # 28 ## Example 2 29 ## 30 ## Input: @citations = (25,8,5,3,3) 31 ## Output: 3 32 ## 33 ## The H-Index is 3 because the fourth paper has only 3 citations. 34 # 35 ############################################################ 36 ## 37 ## discussion 38 ## 39 ############################################################ 40 # 41 # Basically you count from 1 to the number of publications and 42 # check the number of citations for each. If the number of 43 # citations drops below the current index, you found the H-Index 44 45 use strict; 46 use warnings; 47 use feature 'say'; 48 49 h_index(10,8,5,4,3); 50 h_index(25,8,5,3,3); 51 h_index(7,6,5,6,7,5,5); 52 h_index(7,6,5,6); 53 54 sub h_index { 55 my @citations = sort {$b <=> $a} @_; 56 say "Input: (" . join(",", @citations) . ")"; 57 my $h = 0; 58 foreach my $c (@citations) { 59 $h++; 60 if($h > $c) { 61 $h--; 62 last; 63 } 64 } 65 say "Output $h"; 66 }