At least h with at least h
Python, 52
f=lambda s,n=0:n<sum(n<x for x in s)and f(s,n+1)or n
A recursive solution. Run this in Stackless Python if you're worried about overflows.
Starting from n=0
, checks whether at least n+1
of the numbers are at least n+1
. If so, increments n
and starts again. If not, outputs n
.
The conditional is done using Python's short-circuiting for Booleans. The expression sum(n<x for x in s)
counts the number of values in s
that are greater than n
by adding the indicator Booleans, which are treated as 0
or 1
.
For comparison, the iterative equivalent is 2 chars longer. It requires Python 2.
s=input()
n=0
while n<sum(n<x for x in s):n+=1
print n
Unfortunately, the input need to be saved for a variable before being iterated over or else Python will try to read the input repeatedly.
Pyth, 13 10 bytes
tf<l-QUTT1
Input in a form such as [22,33,1,2,4]
on STDIN.
Try it here.
How it works:
-QUT
is all of the numbers in the input (Q
) at least as large as the number being checked, T
.
<l-QUTT
is true if the the length of that list is less than T
.
f<l-QUTT1
finds the first integer which returns true for the inner check, starting at 1
and going up.
tf<l-QUTT1
decrements that by one, giving the largest value for which the condition is false, which is the h-index.
Starting at 1 ensures that 0
is returned when the test is always true, such as in the first test case.
Python 2, 49
Input should be typed in the same format as the examples.
i=0
for z in sorted(input())[::-1]:i+=z>i
print i