progs/alcohol.scala
changeset 128 166bb9b6b20a
parent 127 b4def82f3f9f
equal deleted inserted replaced
127:b4def82f3f9f 128:166bb9b6b20a
     1 // Part 2 about Alcohol-Consumption Worldwide
     1 // Part 2 about Alcohol-Consumption Worldwide
     2 //============================================
     2 //============================================
     3 
     3 
     4 
     4 object CW6b {
     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 
     5 
    18 import io.Source
     6 import io.Source
    19 import scala.util._
     7 import scala.util._
    20 
     8 
    21 def get_csv_page(url: String) : List[String] = 
     9 def get_csv_page(url: String) : List[String] = 
    27 
    15 
    28 val url_alcohol = 
    16 val url_alcohol = 
    29   "https://raw.githubusercontent.com/fivethirtyeight/data/master/alcohol-consumption/drinks.csv"
    17   "https://raw.githubusercontent.com/fivethirtyeight/data/master/alcohol-consumption/drinks.csv"
    30 
    18 
    31 val file_population = 
    19 val file_population = 
    32   "population.cvs"
    20   "population.csv"
    33 
    21 
    34 get_csv_page(url_alcohol)
    22 get_csv_page(url_alcohol)
    35 get_csv_file(file_population)
    23 get_csv_file(file_population)
    36 
    24 
    37 get_csv_page(url_alcohol).size
    25 get_csv_page(url_alcohol).size
    56 
    44 
    57 def sorted_country_consumption() : List[(String, Long)] = {
    45 def sorted_country_consumption() : List[(String, Long)] = {
    58   val alcs2 = process_alcs(alcs.drop(1))
    46   val alcs2 = process_alcs(alcs.drop(1))
    59   val pops2 = process_pops(pops.drop(1))
    47   val pops2 = process_pops(pops.drop(1))
    60   val cons_list = 
    48   val cons_list = 
    61     for ((cname, cons) <- alcs2; if pops2.isDefinedAt(cname)) yield (cname, (cons * pops2(cname)).toLong)
    49     for ((cname, cons) <- alcs2; 
       
    50 	 if pops2.isDefinedAt(cname)) yield (cname, (cons * pops2(cname)).toLong)
    62   cons_list.sortBy(_._2).reverse
    51   cons_list.sortBy(_._2).reverse
    63 }
    52 }
    64 
    53 
    65 sorted_country_consumption().take(10)
    54 sorted_country_consumption()(9)
    66 sorted_country_consumption().size
    55 sorted_country_consumption().size
    67 
       
    68 consumption.foreach { println } 
       
    69 
    56 
    70 def percentage(n: Int) : (Long, Long, Double) = {
    57 def percentage(n: Int) : (Long, Long, Double) = {
    71   val cons_list = sorted_country_consumption()
    58   val cons_list = sorted_country_consumption()
    72   val sum_n = cons_list.take(n).map(_._2).sum
    59   val sum_n = cons_list.take(n).map(_._2).sum
    73   val sum_all = cons_list.map(_._2).sum
    60   val sum_all = cons_list.map(_._2).sum
    74   val perc = (sum_n.toDouble / sum_all.toDouble) * 100.0
    61   val perc = (sum_n.toDouble / sum_all.toDouble) * 100.0
    75   (sum_all, sum_n, perc)
    62   (sum_all, sum_n, perc)
    76 }
    63 }
    77 
    64 
       
    65 
       
    66 percentage(10)
    78 percentage(164)
    67 percentage(164)
    79 
    68 
       
    69 assert(percentage(164) == (28771558364L, 28771558364L, 100.0))
       
    70 
       
    71 }
       
    72