127
|
1 |
// Part 2 about Alcohol-Consumption Worldwide
|
|
2 |
//============================================
|
18
|
3 |
|
128
|
4 |
object CW6b {
|
18
|
5 |
|
|
6 |
import io.Source
|
|
7 |
import scala.util._
|
|
8 |
|
127
|
9 |
def get_csv_page(url: String) : List[String] =
|
|
10 |
Source.fromURL(url)("ISO-8859-1").getLines.toList
|
|
11 |
|
|
12 |
def get_csv_file(file: String) : List[String] =
|
|
13 |
Source.fromFile(file)("ISO-8859-1").getLines.toList
|
|
14 |
|
|
15 |
|
|
16 |
val url_alcohol =
|
|
17 |
"https://raw.githubusercontent.com/fivethirtyeight/data/master/alcohol-consumption/drinks.csv"
|
18
|
18 |
|
127
|
19 |
val file_population =
|
128
|
20 |
"population.csv"
|
127
|
21 |
|
|
22 |
get_csv_page(url_alcohol)
|
|
23 |
get_csv_file(file_population)
|
18
|
24 |
|
127
|
25 |
get_csv_page(url_alcohol).size
|
|
26 |
get_csv_file(file_population).size
|
|
27 |
|
|
28 |
val alcs = get_csv_page(url_alcohol)
|
|
29 |
val pops = get_csv_file(file_population)
|
18
|
30 |
|
|
31 |
|
127
|
32 |
def process_alcs(lines: List[String]) : List[(String, Double)] =
|
|
33 |
for (l <- lines) yield {
|
|
34 |
val entries = l.split(",").toList
|
|
35 |
(entries(0), entries(4).toDouble)
|
|
36 |
}
|
18
|
37 |
|
127
|
38 |
def process_pops(lines: List[String]) : Map[String, Long] =
|
|
39 |
(for (l <- lines) yield {
|
|
40 |
val entries = l.split(",").toList
|
|
41 |
(entries(0), entries(1).toLong)
|
|
42 |
}).toMap
|
18
|
43 |
|
|
44 |
|
127
|
45 |
def sorted_country_consumption() : List[(String, Long)] = {
|
|
46 |
val alcs2 = process_alcs(alcs.drop(1))
|
|
47 |
val pops2 = process_pops(pops.drop(1))
|
|
48 |
val cons_list =
|
128
|
49 |
for ((cname, cons) <- alcs2;
|
|
50 |
if pops2.isDefinedAt(cname)) yield (cname, (cons * pops2(cname)).toLong)
|
127
|
51 |
cons_list.sortBy(_._2).reverse
|
18
|
52 |
}
|
|
53 |
|
128
|
54 |
sorted_country_consumption()(9)
|
127
|
55 |
sorted_country_consumption().size
|
18
|
56 |
|
127
|
57 |
def percentage(n: Int) : (Long, Long, Double) = {
|
|
58 |
val cons_list = sorted_country_consumption()
|
|
59 |
val sum_n = cons_list.take(n).map(_._2).sum
|
|
60 |
val sum_all = cons_list.map(_._2).sum
|
|
61 |
val perc = (sum_n.toDouble / sum_all.toDouble) * 100.0
|
|
62 |
(sum_all, sum_n, perc)
|
|
63 |
}
|
18
|
64 |
|
128
|
65 |
|
|
66 |
percentage(10)
|
127
|
67 |
percentage(164)
|
18
|
68 |
|
128
|
69 |
assert(percentage(164) == (28771558364L, 28771558364L, 100.0))
|
|
70 |
|
|
71 |
}
|
|
72 |
|