--- a/progs/alcohol.scala Tue Nov 07 13:08:18 2017 +0000
+++ b/progs/alcohol.scala Tue Nov 07 14:17:21 2017 +0000
@@ -1,19 +1,7 @@
// Part 2 about Alcohol-Consumption Worldwide
//============================================
-
-// (1) Complete the function that is given a list of floats
-// and calculuates the indices for when to buy the commodity
-// and when to sell
-
-
-// (2) Complete the ``get webpage'' function that takes a
-// a stock symbol as argument and queries the Yahoo server
-// at
-// http://ichart.yahoo.com/table.csv?s=<<insert stock symbol>>
-//
-// This servive returns a CSV-list that needs to be separated into
-// a list of strings.
+object CW6b {
import io.Source
import scala.util._
@@ -29,7 +17,7 @@
"https://raw.githubusercontent.com/fivethirtyeight/data/master/alcohol-consumption/drinks.csv"
val file_population =
- "population.cvs"
+ "population.csv"
get_csv_page(url_alcohol)
get_csv_file(file_population)
@@ -58,15 +46,14 @@
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)
+ for ((cname, cons) <- alcs2;
+ if pops2.isDefinedAt(cname)) yield (cname, (cons * pops2(cname)).toLong)
cons_list.sortBy(_._2).reverse
}
-sorted_country_consumption().take(10)
+sorted_country_consumption()(9)
sorted_country_consumption().size
-consumption.foreach { println }
-
def percentage(n: Int) : (Long, Long, Double) = {
val cons_list = sorted_country_consumption()
val sum_n = cons_list.take(n).map(_._2).sum
@@ -75,5 +62,11 @@
(sum_all, sum_n, perc)
}
+
+percentage(10)
percentage(164)
+assert(percentage(164) == (28771558364L, 28771558364L, 100.0))
+
+}
+