--- a/templates/alcohol.scala Tue Nov 07 14:17:21 2017 +0000
+++ b/templates/alcohol.scala Wed Nov 08 02:06:54 2017 +0000
@@ -6,64 +6,60 @@
import io.Source
import scala.util._
-def get_csv_page(url: String) : List[String] =
- Source.fromURL(url)("ISO-8859-1").getLines.toList
-
-def get_csv_file(file: String) : List[String] =
- Source.fromFile(file)("ISO-8859-1").getLines.toList
-
-
val url_alcohol =
"https://raw.githubusercontent.com/fivethirtyeight/data/master/alcohol-consumption/drinks.csv"
val file_population =
"population.csv"
-get_csv_page(url_alcohol)
-get_csv_file(file_population)
-get_csv_page(url_alcohol).size
-get_csv_file(file_population).size
+//(1) Complete the get_csv_page function below. It takes the URL-string
+// as argument and generates a list of strings corresponing to each
+// line in the csv list. The URL url_alcohol is one possible argument.
-val alcs = get_csv_page(url_alcohol)
-val pops = get_csv_file(file_population)
+//def get_csv_page(url: String) : List[String] = ...
-def process_alcs(lines: List[String]) : List[(String, Double)] =
- for (l <- lines) yield {
- val entries = l.split(",").toList
- (entries(0), entries(4).toDouble)
- }
+// Complete the get_csv_file function below. It takes a file-string
+// as argument and reads the content of the given file. Like above
+// it should generates a list of strings corresponing to each
+// line in the csv-list. The filename file_population is one possible
+// argument.
-def process_pops(lines: List[String]) : Map[String, Long] =
- (for (l <- lines) yield {
- val entries = l.split(",").toList
- (entries(0), entries(1).toLong)
- }).toMap
+//def get_csv_file(file: String) : List[String] = ...
+
-def sorted_country_consumption() : List[(String, Long)] = {
- val alcs2 = process_alcs(alcs.drop(1))
- val pops2 = process_pops(pops.drop(1))
- val cons_list =
- for ((cname, cons) <- alcs2;
- if pops2.isDefinedAt(cname)) yield (cname, (cons * pops2(cname)).toLong)
- cons_list.sortBy(_._2).reverse
-}
+//(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 size).
-sorted_country_consumption().take(10)
-sorted_country_consumption().size
+//def process_alcs(lines: List[String]) : List[(String, Double)] = ...
-def percentage(n: Int) : (Long, Long, Double) = {
- val cons_list = sorted_country_consumption()
- val sum_n = cons_list.take(n).map(_._2).sum
- val sum_all = cons_list.map(_._2).sum
- val perc = (sum_n.toDouble / sum_all.toDouble) * 100.0
- (sum_all, sum_n, perc)
-}
+//def process_pops(lines: List[String]) : Map[String, Long] = ...
+
-percentage(10)
-percentage(164)
+//(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
+// (with the exact name) on the population sizes list. Note that
+// the spelling of some names in the alcohol list differs from the
+// population sizes list. 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 of the the world consumption of alcohol.
+
+//def percentage(n: Int) : (Long, Long, Double) = ...
}