diff options
| author | boblied <boblied@gmail.com> | 2023-03-06 17:23:59 -0600 |
|---|---|---|
| committer | boblied <boblied@gmail.com> | 2023-03-06 17:23:59 -0600 |
| commit | 15567a7f4f11d04f5ae8e9d1d4dc9c92b6e1c8e2 (patch) | |
| tree | 49849fb88cf94f161157c5ebe9e257b73d9fcca8 | |
| parent | a7426dfa56f1ed4e603dbbc3e48aca51140edb35 (diff) | |
| download | perlweeklychallenge-club-15567a7f4f11d04f5ae8e9d1d4dc9c92b6e1c8e2.tar.gz perlweeklychallenge-club-15567a7f4f11d04f5ae8e9d1d4dc9c92b6e1c8e2.tar.bz2 perlweeklychallenge-club-15567a7f4f11d04f5ae8e9d1d4dc9c92b6e1c8e2.zip | |
Week 207 Task 2
| -rw-r--r-- | challenge-207/bob-lied/perl/ch-2.pl | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/challenge-207/bob-lied/perl/ch-2.pl b/challenge-207/bob-lied/perl/ch-2.pl new file mode 100644 index 0000000000..027d3aa90f --- /dev/null +++ b/challenge-207/bob-lied/perl/ch-2.pl @@ -0,0 +1,59 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# ch-2.pl Perl Weekly Challenge Week 207 Task 2 H-Index +#============================================================================= +# Copyright (c) 2023, Bob Lied +#============================================================================= +# 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. https://en.wikipedia.org/wiki/H-index +# +# 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. +# +# Example 1 Input: @citations = (10,8,5,4,3) Output: 4 +# Because the 4th publication has 4 citations and the 5th has only 3. +# Example 2 Input: @citations = (25,8,5,3,3) Output: 3 +# The H-Index is 3 because the fourth paper has only 3 citations. +# +# What this amounts to is finding the largest i in a sorted array such +# that x[i] >= i +#============================================================================= + +use v5.36; + +use List::Util qw/first/; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +say H_index( @ARGV ); + +sub H_index(@cite) +{ + my @sorted = sort { $b <=> $a } @cite; + my $h = first { $sorted[$_] < $_+1 } 0 .. $#sorted; + return $h; +} + +sub runTest +{ + use Test2::V0; + + is( H_index(10,8,5,4,3), 4, "Example 1"); + is( H_index(25,8,5,3,3), 3, "Example 2"); + is( H_index( 1,1,1,1,1), 1, "Example 2"); + + done_testing; +} + |
