2013-04-11

计算编程高手的平均年龄


## Problem

What's the average age of StackOverflow top users?

## Solution 1

    URL='https://api.stackexchange.com/2.1/users?order=desc&sort=reputation&site=stackoverflow'

    curl -s $URL |
      gunzip |
        jsawk 'return $$.items' |
          jsawk 'return $$.age' -a 'return $$.reduce(function(x,y){return x+y})/$$.length'

## Solution 2

    URL='https://api.stackexchange.com/2.1/users?order=desc&sort=reputation&site=stackoverflow'

    curl -s $URL |
      gunzip |
        jq '[.items[].age | select(.)] | add/length'

## Result

    36.72

## Links:

- https://github.com/micha/jsawk
- http://stedolan.github.io/jq/manual/