| 128 |      1 | // Part 2 about Alcohol-Consumption Worldwide
 | 
|  |      2 | //============================================
 | 
|  |      3 | 
 | 
|  |      4 | object CW6b {
 | 
|  |      5 | 
 | 
|  |      6 | import io.Source
 | 
|  |      7 | import scala.util._
 | 
|  |      8 | 
 | 
|  |      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 | 
 | 
|  |     19 | val file_population = 
 | 
|  |     20 |   "population.csv"
 | 
|  |     21 | 
 | 
|  |     22 | get_csv_page(url_alcohol)
 | 
|  |     23 | get_csv_file(file_population)
 | 
|  |     24 | 
 | 
|  |     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)
 | 
|  |     30 | 
 | 
|  |     31 | 
 | 
|  |     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 |   }
 | 
|  |     37 | 
 | 
|  |     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
 | 
|  |     43 | 
 | 
|  |     44 | 
 | 
|  |     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 = 
 | 
|  |     49 |     for ((cname, cons) <- alcs2; 
 | 
|  |     50 | 	 if pops2.isDefinedAt(cname)) yield (cname, (cons * pops2(cname)).toLong)
 | 
|  |     51 |   cons_list.sortBy(_._2).reverse
 | 
|  |     52 | }
 | 
|  |     53 | 
 | 
|  |     54 | sorted_country_consumption().take(10)
 | 
|  |     55 | sorted_country_consumption().size
 | 
|  |     56 | 
 | 
|  |     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 | }
 | 
|  |     64 | 
 | 
|  |     65 | 
 | 
|  |     66 | percentage(10)
 | 
|  |     67 | percentage(164)
 | 
|  |     68 | 
 | 
|  |     69 | }
 |