-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalcohol.scala
95 lines (51 loc) · 2.34 KB
/
alcohol.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Part 2 about Alcohol-Consumption Worldwide
//============================================
object CW6b {
import scala.io.Source
import scala.util._
val url_alcohol =
"https://raw.githubusercontent.com/fivethirtyeight/data/master/alcohol-consumption/drinks.csv"
val file_population =
"population.csv"
def get_csv_page(url: String) : List[String] = {
val content = Source.fromURL(url).mkString
content.split("\n").toList
}
def get_csv_file(file: String) : List[String] = {
val content = Source.fromFile(file).mkString
content.split("\n").toList
}
//(2) Complete the functions that process the csv-lists. For
// process_alcs extract the country name (as String) and the
// pure alcohol consumption (as Double). For process_pops
// generate a Map of Strings (country names) to Long numbers
// (population sizes).
def process_alcs(lines: List[String]) : List[(String, Double)] = {
val newList = for(i<- lines) yield {
(i.split(",").take(1).mkString, i.split(",").takeRight(1).mkString.toDouble)
}
newList
}
def process_pops(lines: List[String]) : Map[String, Long] = {
val newList = for(i<- lines) yield {
(i.split(",").take(1).mkString, i.split(",").takeRight(1).mkString.toLong)
}
val newMap = newList.toMap
newMap
}
//(3) Calculate for each country the overall alcohol_consumption using
// the data from the alcohol list and the population sizes list. You
// should only include countries on the alcohol list that are also
// on the population sizes list with the exact same name. Note that
// the spelling of some names in the alcohol list differs from the
// population sizes list. You can ignore entries where the names differ.
// Sort the resulting list according to the country with the highest alcohol
// consumption to the country with the lowest alcohol consumption.
//def sorted_country_consumption() : List[(String, Long)] = ...
// Calculate the world consumption of pure alcohol of all countries, which
// should be the first element in the tuple below. The second element is
// the overall consumption of the first n countries in the sorted list
// from above; and finally the double should be the percentage of the
// first n countries drinking from the the world consumption of alcohol.
//def percentage(n: Int) : (Long, Long, Double) = ...
}