h-index

他の言語で書いてみても面白いかも。Haskellとかね。

こういうの?

import List
import Test.HUnit

hindex :: [Int] -> Int
hindex lst = length $ takeWhile cond (zip sorted [1..])
  where
    sorted = sortBy (flip compare) lst
    cond (val, rank) = val >= rank
 
testData :: [Test]
testData = [t 4 [0, 10, 20, 30, 40],
            t 4 [10, 20, 30, 40],
            t 3 [20, 30, 40],
            t 2 [30, 40],
            t 1 [40],
            t 2 [1, 2, 3],
            t 3 [3, 3, 3],
            t 2 [3, 3],
            t 1 [3],
            t 0 [0, 0],
            t 0 [0]]
  where
    t n ls = n ~=? hindex ls

main :: IO Counts
main = runTestTT (test testData)