templates/alcohol.scala
author Christian Urban <urbanc@in.tum.de>
Wed, 08 Nov 2017 02:06:54 +0000
changeset 129 1b0f1573c27c
parent 128 fd03a2f06286
child 135 e59aebee9770
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
128
fd03a2f06286 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
// Part 2 about Alcohol-Consumption Worldwide
fd03a2f06286 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
//============================================
fd03a2f06286 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
fd03a2f06286 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
object CW6b {
fd03a2f06286 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
fd03a2f06286 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
import io.Source
fd03a2f06286 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
import scala.util._
fd03a2f06286 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
fd03a2f06286 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
val url_alcohol = 
fd03a2f06286 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
  "https://raw.githubusercontent.com/fivethirtyeight/data/master/alcohol-consumption/drinks.csv"
fd03a2f06286 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
fd03a2f06286 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
val file_population = 
fd03a2f06286 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
  "population.csv"
fd03a2f06286 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
fd03a2f06286 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
129
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
    16
//(1) Complete the get_csv_page function below. It takes the URL-string
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
    17
//    as argument and generates a list of strings corresponing to each
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
    18
//    line in the csv list. The URL url_alcohol is one possible argument.
128
fd03a2f06286 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
129
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
    20
//def get_csv_page(url: String) : List[String] = ...
128
fd03a2f06286 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
fd03a2f06286 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
129
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
    23
//    Complete the get_csv_file function below. It takes a file-string
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
    24
//    as argument and reads the content of the given file. Like above
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
    25
//    it should generates a list of strings corresponing to each
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
    26
//    line in the csv-list. The filename file_population is one possible
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
    27
//    argument.
128
fd03a2f06286 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
129
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
    29
//def get_csv_file(file: String) : List[String] = ...
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
    30
128
fd03a2f06286 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
fd03a2f06286 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
129
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
    33
//(2) Complete the functions that process the csv-lists. For
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
    34
//    process_alcs extract the country name (as String) and the 
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
    35
//    pure alcohol consumption (as Double). For process_pops
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
    36
//    generate a Map of Strings (country names) to Long numbers 
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
    37
//    (population size). 
128
fd03a2f06286 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
129
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
    39
//def process_alcs(lines: List[String]) : List[(String, Double)] = ...
128
fd03a2f06286 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
129
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
    41
//def process_pops(lines: List[String]) : Map[String, Long] = ...
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
    42
128
fd03a2f06286 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
fd03a2f06286 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
129
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
    45
//(3) Calculate for each country the overall alcohol_consumption using
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
    46
//    the data from the alcohol list and the population sizes list. You
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
    47
//    should only include countries on the alcohol list that are also
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
    48
//    (with the exact name) on the population sizes list. Note that
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
    49
//    the spelling of some names in the alcohol list differs from the
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
    50
//    population sizes list. Sort the resulting list according to the 
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
    51
//    country with the highest alcohol consumption to the country 
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
    52
//    with the lowest alcohol consumption.
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
    53
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
    54
//def sorted_country_consumption() : List[(String, Long)] = ...
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
    55
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
    56
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
    57
//   Calculate the world consumption of pure alcohol of all countries, which 
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
    58
//   should be the first element in the tuple below. The second element is
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
    59
//   the overall consumption of the first n countries in the sorted list
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
    60
//   from above; and finally the double should be the percentage of the 
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
    61
//   first n countries of the the world consumption of alcohol.          
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
    62
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
    63
//def percentage(n: Int) : (Long, Long, Double) = ...
128
fd03a2f06286 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
fd03a2f06286 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
}