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