progs/alcohol.scala
changeset 127 b4def82f3f9f
parent 18 87e55eb309ed
child 128 166bb9b6b20a
equal deleted inserted replaced
126:c40f364d87eb 127:b4def82f3f9f
       
     1 // Part 2 about Alcohol-Consumption Worldwide
       
     2 //============================================
       
     3 
       
     4 
       
     5 // (1) Complete the function that is given a list of floats
       
     6 // and calculuates the indices for when to buy the commodity 
       
     7 // and when to sell
       
     8 
       
     9 
       
    10 // (2) Complete the ``get webpage'' function that takes a
       
    11 // a stock symbol as argument and queries the Yahoo server
       
    12 // at
       
    13 //      http://ichart.yahoo.com/table.csv?s=<<insert stock symbol>>
       
    14 // 
       
    15 // This servive returns a CSV-list that needs to be separated into
       
    16 // a list of strings.
       
    17 
       
    18 import io.Source
       
    19 import scala.util._
       
    20 
       
    21 def get_csv_page(url: String) : List[String] = 
       
    22   Source.fromURL(url)("ISO-8859-1").getLines.toList
       
    23 
       
    24 def get_csv_file(file: String) : List[String] = 
       
    25   Source.fromFile(file)("ISO-8859-1").getLines.toList
       
    26 
       
    27 
       
    28 val url_alcohol = 
       
    29   "https://raw.githubusercontent.com/fivethirtyeight/data/master/alcohol-consumption/drinks.csv"
       
    30 
       
    31 val file_population = 
       
    32   "population.cvs"
       
    33 
       
    34 get_csv_page(url_alcohol)
       
    35 get_csv_file(file_population)
       
    36 
       
    37 get_csv_page(url_alcohol).size
       
    38 get_csv_file(file_population).size
       
    39 
       
    40 val alcs = get_csv_page(url_alcohol)
       
    41 val pops = get_csv_file(file_population)
       
    42 
       
    43 
       
    44 def process_alcs(lines: List[String]) : List[(String, Double)] =
       
    45   for (l <- lines) yield {
       
    46     val entries = l.split(",").toList 
       
    47     (entries(0), entries(4).toDouble) 
       
    48   }
       
    49 
       
    50 def process_pops(lines: List[String]) : Map[String, Long] =
       
    51   (for (l <- lines) yield {
       
    52     val entries = l.split(",").toList 
       
    53     (entries(0), entries(1).toLong)
       
    54   }).toMap
       
    55 
       
    56 
       
    57 def sorted_country_consumption() : List[(String, Long)] = {
       
    58   val alcs2 = process_alcs(alcs.drop(1))
       
    59   val pops2 = process_pops(pops.drop(1))
       
    60   val cons_list = 
       
    61     for ((cname, cons) <- alcs2; if pops2.isDefinedAt(cname)) yield (cname, (cons * pops2(cname)).toLong)
       
    62   cons_list.sortBy(_._2).reverse
       
    63 }
       
    64 
       
    65 sorted_country_consumption().take(10)
       
    66 sorted_country_consumption().size
       
    67 
       
    68 consumption.foreach { println } 
       
    69 
       
    70 def percentage(n: Int) : (Long, Long, Double) = {
       
    71   val cons_list = sorted_country_consumption()
       
    72   val sum_n = cons_list.take(n).map(_._2).sum
       
    73   val sum_all = cons_list.map(_._2).sum
       
    74   val perc = (sum_n.toDouble / sum_all.toDouble) * 100.0
       
    75   (sum_all, sum_n, perc)
       
    76 }
       
    77 
       
    78 percentage(164)
       
    79