aboutsummaryrefslogtreecommitdiff
path: root/challenge-207/duncan-c-white/perl/ch-2.pl
blob: 7d9eafa3f98b90006c51a0a6d1a9c1171f0bdd00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/perl
#
# Task 2: H-Index
# 
# 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:
# 
# 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.
# 
# MY NOTES: also pretty easy.  The Wikipedia entry sheds more light:
# 
# "First we order the citations from the largest to the lowest value.
#  Then, we look for the last position in which citation[h] >= h."
#  (This assumes array indexes start at 1, I think).
# 
# GUEST LANGUAGE: As a bonus, I also had a go at translating ch-2.pl into C
# (look in the C directory for that)
# 

use strict;
use warnings;
use feature 'say';
use Getopt::Long;
use Data::Dumper;
use List::Util qw(min max);

my $debug=0;
die "Usage: h-index [--debug] intlist\n"
	unless GetOptions( "debug"=>\$debug ) && @ARGV > 0;

my @list = split( /,/, join(',',@ARGV) );

say "list: ", join(',',@list) if $debug;

@list = sort { $b <=> $a } @list;

say "sorted list: ", join(',',@list) if $debug;

my $h = -1;

foreach my $pos (0..$#list)
{
	$h = $pos+1 if $list[$pos] >= $pos+1;
}

say $h;