4 object CW6b { | 
     4 object CW6b { | 
     5   | 
     5   | 
     6 import io.Source  | 
     6 import io.Source  | 
     7 import scala.util._  | 
     7 import scala.util._  | 
     8   | 
     8   | 
     9 def get_csv_page(url: String) : List[String] =   | 
         | 
    10   Source.fromURL(url)("ISO-8859-1").getLines.toList | 
         | 
    11   | 
         | 
    12 def get_csv_file(file: String) : List[String] =   | 
         | 
    13   Source.fromFile(file)("ISO-8859-1").getLines.toList | 
         | 
    14   | 
         | 
    15   | 
         | 
    16 val url_alcohol =   | 
     9 val url_alcohol =   | 
    17   "https://raw.githubusercontent.com/fivethirtyeight/data/master/alcohol-consumption/drinks.csv"  | 
    10   "https://raw.githubusercontent.com/fivethirtyeight/data/master/alcohol-consumption/drinks.csv"  | 
    18   | 
    11   | 
    19 val file_population =   | 
    12 val file_population =   | 
    20   "population.csv"  | 
    13   "population.csv"  | 
    21   | 
    14   | 
    22 get_csv_page(url_alcohol)  | 
         | 
    23 get_csv_file(file_population)  | 
         | 
    24   | 
    15   | 
    25 get_csv_page(url_alcohol).size  | 
    16 //(1) Complete the get_csv_page function below. It takes the URL-string  | 
    26 get_csv_file(file_population).size  | 
    17 //    as argument and generates a list of strings corresponing to each  | 
         | 
    18 //    line in the csv list. The URL url_alcohol is one possible argument.  | 
    27   | 
    19   | 
    28 val alcs = get_csv_page(url_alcohol)  | 
    20 //def get_csv_page(url: String) : List[String] = ...  | 
    29 val pops = get_csv_file(file_population)  | 
         | 
    30   | 
    21   | 
    31   | 
    22   | 
    32 def process_alcs(lines: List[String]) : List[(String, Double)] =  | 
    23 //    Complete the get_csv_file function below. It takes a file-string  | 
    33   for (l <- lines) yield { | 
    24 //    as argument and reads the content of the given file. Like above  | 
    34     val entries = l.split(",").toList  | 
    25 //    it should generates a list of strings corresponing to each  | 
    35     (entries(0), entries(4).toDouble)   | 
    26 //    line in the csv-list. The filename file_population is one possible  | 
    36   }  | 
    27 //    argument.  | 
    37   | 
    28   | 
    38 def process_pops(lines: List[String]) : Map[String, Long] =  | 
    29 //def get_csv_file(file: String) : List[String] = ...  | 
    39   (for (l <- lines) yield { | 
         | 
    40     val entries = l.split(",").toList  | 
         | 
    41     (entries(0), entries(1).toLong)  | 
         | 
    42   }).toMap  | 
         | 
    43   | 
    30   | 
    44   | 
    31   | 
    45 def sorted_country_consumption() : List[(String, Long)] = { | 
         | 
    46   val alcs2 = process_alcs(alcs.drop(1))  | 
         | 
    47   val pops2 = process_pops(pops.drop(1))  | 
         | 
    48   val cons_list =   | 
         | 
    49     for ((cname, cons) <- alcs2;   | 
         | 
    50 	 if pops2.isDefinedAt(cname)) yield (cname, (cons * pops2(cname)).toLong)  | 
         | 
    51   cons_list.sortBy(_._2).reverse  | 
         | 
    52 }  | 
         | 
    53   | 
    32   | 
    54 sorted_country_consumption().take(10)  | 
    33 //(2) Complete the functions that process the csv-lists. For  | 
    55 sorted_country_consumption().size  | 
    34 //    process_alcs extract the country name (as String) and the   | 
         | 
    35 //    pure alcohol consumption (as Double). For process_pops  | 
         | 
    36 //    generate a Map of Strings (country names) to Long numbers   | 
         | 
    37 //    (population size).   | 
    56   | 
    38   | 
    57 def percentage(n: Int) : (Long, Long, Double) = { | 
    39 //def process_alcs(lines: List[String]) : List[(String, Double)] = ...  | 
    58   val cons_list = sorted_country_consumption()  | 
    40   | 
    59   val sum_n = cons_list.take(n).map(_._2).sum  | 
    41 //def process_pops(lines: List[String]) : Map[String, Long] = ...  | 
    60   val sum_all = cons_list.map(_._2).sum  | 
         | 
    61   val perc = (sum_n.toDouble / sum_all.toDouble) * 100.0  | 
         | 
    62   (sum_all, sum_n, perc)  | 
         | 
    63 }  | 
         | 
    64   | 
    42   | 
    65   | 
    43   | 
    66 percentage(10)  | 
    44   | 
    67 percentage(164)  | 
    45 //(3) Calculate for each country the overall alcohol_consumption using  | 
         | 
    46 //    the data from the alcohol list and the population sizes list. You  | 
         | 
    47 //    should only include countries on the alcohol list that are also  | 
         | 
    48 //    (with the exact name) on the population sizes list. Note that  | 
         | 
    49 //    the spelling of some names in the alcohol list differs from the  | 
         | 
    50 //    population sizes list. Sort the resulting list according to the   | 
         | 
    51 //    country with the highest alcohol consumption to the country   | 
         | 
    52 //    with the lowest alcohol consumption.  | 
         | 
    53   | 
         | 
    54 //def sorted_country_consumption() : List[(String, Long)] = ...  | 
         | 
    55   | 
         | 
    56   | 
         | 
    57 //   Calculate the world consumption of pure alcohol of all countries, which   | 
         | 
    58 //   should be the first element in the tuple below. The second element is  | 
         | 
    59 //   the overall consumption of the first n countries in the sorted list  | 
         | 
    60 //   from above; and finally the double should be the percentage of the   | 
         | 
    61 //   first n countries of the the world consumption of alcohol.            | 
         | 
    62   | 
         | 
    63 //def percentage(n: Int) : (Long, Long, Double) = ...  | 
    68   | 
    64   | 
    69 }  | 
    65 }  |