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