Skip to content

Instantly share code, notes, and snippets.

@mike-neck
Created February 22, 2024 05:35
Show Gist options
  • Save mike-neck/19d4fffba7fada757e7390857958fc39 to your computer and use it in GitHub Desktop.
Save mike-neck/19d4fffba7fada757e7390857958fc39 to your computer and use it in GitHub Desktop.
日本国民の(一人当たりGDPの)偏差値は60を超えるのか確認するスクリプト
@Grab('org.jsoup:jsoup:1.17.2')
import org.jsoup.*
import groovy.transform.*
@ToString
@EqualsAndHashCode
@Sortable(includes = ['gdpPerPerson'], reversed = true)
class Gdp implements Iterable<Long> {
final int country
final String name
final long gdpPerPerson
final long gdpTotal
Gdp(int country, String name, long gdpPerPerson, long gdpTotal) {
this.country = country
this.name = name
this.gdpPerPerson = gdpPerPerson
this.gdpTotal = gdpTotal
}
long getCount() { gdpTotal / gdpPerPerson }
@Override Iterator<Long> iterator() {
def list = []
this.count.each { list << gdpPerPerson }
return list.iterator()
}
}
def gdpPerPersonDoc = Jsoup.connect('https://www.globalnote.jp/post-1339.html').get()
def gdpPerPersonElements = gdpPerPersonDoc.select('main div#post_data article div.content_data table tbody tr.value')
def gdpPerPersonMap = gdpPerPersonElements.collectEntries { elem ->
def link = elem.select('td.country a')
def matcher = link.attr('href') =~ /c_code=(\d+)/
matcher.find() ? [matcher.group(1) as int, [name:link.text(), value:elem.select('td.bar span.num').text().replace(',','') as long]]: null
}
def gdpDoc = Jsoup.connect('https://www.globalnote.jp/post-1409.html').get()
def gdpElements = gdpDoc.select('main div#post_data article div.content_data table tbody tr.value')
def gdpList = gdpElements.collect { elem ->
def link = elem.select('td.country a')
def matcher = link.attr('href') =~ /c_code=(\d+)/
if (!matcher.find()) {
return null
}
def id = matcher.group(1) as int
def gdp = (elem.select('td.bar span.num').text().replace(',','') as long) * 1_000_000
def personal = gdpPerPersonMap[id]
if (personal == null) {
null
} else {
new Gdp(id, personal.name, personal.value, gdp)
}
}.findAll().sort()
def calcAverage = { List<Gdp> list ->
long total = list.sum { it.gdpPerPerson * it.count }
long all = list.sum { it.count }
return (total as double) / all
}
@ToString
class Calc {
final double powered
final double mean
final long num
Calc(double pow, double mean, long num) {
this.powered = pow
this.mean = mean
this.num = num
}
Calc append(long gdpPerPerson) {
def newNum = num + 1
double delta = gdpPerPerson - mean
def m = mean + delta / newNum
double d = gdpPerPerson - m
def newPow = powered + delta * d
return new Calc(newPow, m, newNum)
}
double getVariance() { Math.sqrt(powered / (num - 1)) }
}
def avg = calcAverage(gdpList)
def variance = gdpList.inject(new Calc(0.0, 0.0, 0)) { Calc calc, Gdp gdp -> gdp.inject(calc) { Calc c, long g -> c.append(g) } }
println "1人あたりGDP平均: ${avg}"
println "分散: ${variance.powered / (variance.num - 1)}"
def jp = gdpList.find { it.name == '日本' }?.gdpPerPerson
println "日本 : ${jp}"
println "偏差値: ${ (jp - avg) * 10 / variance.variance + 50 }"
def nor = gdpList.find { it.name == 'ノルウェー' }?.gdpPerPerson
println "ノルウェー: ${ nor }"
println "偏差値: ${ (nor - avg) * 10 / variance.variance + 50 }"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment