# HG changeset patch # User Christian Urban # Date 1510064241 0 # Node ID 166bb9b6b20a4def4ebd7adacdf35490d1241610 # Parent b4def82f3f9fbf32b3bde9ef163feebfd13090ac updated diff -r b4def82f3f9f -r 166bb9b6b20a progs/alcohol.scala --- 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=<> -// -// 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)) + +} + diff -r b4def82f3f9f -r 166bb9b6b20a progs/population.csv --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/progs/population.csv Tue Nov 07 14:17:21 2017 +0000 @@ -0,0 +1,216 @@ +country,population_size +Afghanistan,32758020 +Albania,2889104 +Algeria,39113313 +American Samoa,55437 +Andorra,79223 +Angola,26920466 +Antigua and Barbuda,98875 +Argentina,42981515 +Armenia,2906220 +Aruba,103795 +Australia,23460694 +Austria,8541575 +Azerbaijan,9535079 +Bahamas,382169 +Bahrain,1336397 +Bangladesh,159405279 +Barbados,283385 +Belarus,9474511 +Belgium,11209057 +Belize,351694 +Benin,10286712 +Bermuda,65139 +Bhutan,776448 +Bolivia,10562159 +Bosnia and Herzegovina,3566002 +Botswana,2168573 +Brazil,204213133 +British Virgin Islands,29588 +Brunei Darussalam,411704 +Bulgaria,7223938 +Burkina Faso,17585977 +Burundi,9891790 +Cabo Verde,526437 +Cambodia,15270790 +Cameroon,22239904 +Canada,35544564 +Cayman Islands,59172 +Central African Republic,4515392 +Chad,13569438 +Channel Islands,162969 +Chile,17613798 +China,1364270000 +Colombia,47791911 +Comoros,759385 +Congo,73722860 +Costa Rica,4757575 +Cote d'Ivoire,22531350 +Croatia,4238389 +Cuba,11439767 +Curacao,155909 +Cyprus,1152309 +Czech Republic,10525347 +Denmark,5643475 +Djibouti,912164 +Dominica,72778 +Dominican Republic,10405844 +Ecuador,15903112 +Egypt,91812566 +El Salvador,6281189 +Equatorial Guinea,1129424 +Estonia,1314545 +Ethiopia,97366774 +Faroe Islands,48842 +Fiji,885806 +Finland,5461512 +France,66331957 +French Polynesia,275484 +Gabon,1875713 +Gambia,1917852 +Georgia,3727000 +Germany,80982500 +Ghana,26962563 +Gibraltar,34038 +Greece,10892413 +Greenland,56295 +Grenada,106360 +Guam,160967 +Guatemala,15923559 +Guinea,11805509 +Guinea-Bissau,1725744 +Guyana,763393 +Haiti,10572466 +Honduras,8809216 +Hong Kong SAR,7241700 +Hungary,9866468 +Iceland,327386 +India,1293859294 +Indonesia,255131116 +Iran,78411092 +Iraq,35006080 +Ireland,4617225 +Isle of Man,82590 +Israel,8215700 +Italy,60789140 +Jamaica,2862087 +Japan,127276000 +Jordan,8809306 +Kazakhstan,17289224 +Kenya,46024250 +Kiribati,110458 +North Korea,25116363 +South Korea,50746659 +Kosovo,1821800 +Kuwait,3782450 +Kyrgyz Republic,5835500 +Lao PDR,6576397 +Latvia,1993782 +Lebanon,5603279 +Lesotho,2145785 +Liberia,4390737 +Libya,6204108 +Liechtenstein,37127 +Lithuania,2932367 +Luxembourg,556319 +Macao SAR,588781 +Macedonia,2077495 +Madagascar,23589801 +Malawi,17068838 +Malaysia,30228017 +Maldives,401000 +Mali,16962846 +Malta,427364 +Marshall Islands,52898 +Mauritania,4063920 +Mauritius,1260934 +Mexico,124221600 +Micronesia,104015 +Moldova,3556397 +Monaco,38132 +Mongolia,2923896 +Montenegro,621810 +Morocco,34318082 +Mozambique,27212382 +Myanmar,51924182 +Namibia,2370992 +Nauru,11853 +Nepal,28323241 +Netherlands,16865008 +New Caledonia,268000 +New Zealand,4509700 +Nicaragua,6013997 +Niger,19148219 +Nigeria,176460502 +Northern Mariana Islands,54468 +Norway,5137232 +Oman,3960925 +Pakistan,185546257 +Palau,21094 +Panama,3903986 +Papua New Guinea,7755785 +Paraguay,6552584 +Peru,30973354 +Philippines,100102249 +Poland,38011735 +Portugal,10401062 +Puerto Rico,3534874 +Qatar,2374419 +Romania,19908979 +Russian Federation,143819666 +Rwanda,11345357 +Samoa,192290 +San Marino,32657 +Sao Tome and Principe,191266 +Saudi Arabia,30776722 +Senegal,14546111 +Serbia,7130576 +Seychelles,91359 +Sierra Leone,7079162 +Singapore,5469724 +Sint Maarten (Dutch part),37685 +Slovak Republic,5418649 +Slovenia,2061980 +Solomon Islands,575504 +Somalia,13513125 +South Africa,54146734 +South Sudan,11530971 +Spain,46480882 +Sri Lanka,20771000 +St. Kitts and Nevis,53739 +St. Lucia,176421 +St. Martin (French part),31530 +St. Vincent and the Grenadines,109357 +Sudan,37737913 +Suriname,547928 +Swaziland,1295097 +Sweden,9696110 +Switzerland,8188649 +Syrian Arab Republic,19203090 +Tajikistan,8362745 +Tanzania,52234869 +Thailand,68416772 +Timor-Leste,1212814 +Togo,7228915 +Tonga,105782 +Trinidad and Tobago,1354493 +Tunisia,11143908 +Turkey,77030628 +Turkmenistan,5466241 +Turks and Caicos Islands,33739 +Tuvalu,10908 +Uganda,38833338 +Ukraine,45271947 +United Arab Emirates,9070867 +United Kingdom,64613160 +United States,318563456 +Uruguay,3419546 +Uzbekistan,30757700 +Vanuatu,258850 +Venezuela,30738378 +Vietnam,90728900 +Virgin Islands (U.S.),104170 +West Bank and Gaza,4294682 +Yemen,26246327 +Zambia,15620974 +Zimbabwe,15411675 diff -r b4def82f3f9f -r 166bb9b6b20a progs/population.cvs --- a/progs/population.cvs Tue Nov 07 13:08:18 2017 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,216 +0,0 @@ -country,population_size -Afghanistan,32758020 -Albania,2889104 -Algeria,39113313 -American Samoa,55437 -Andorra,79223 -Angola,26920466 -Antigua and Barbuda,98875 -Argentina,42981515 -Armenia,2906220 -Aruba,103795 -Australia,23460694 -Austria,8541575 -Azerbaijan,9535079 -Bahamas,382169 -Bahrain,1336397 -Bangladesh,159405279 -Barbados,283385 -Belarus,9474511 -Belgium,11209057 -Belize,351694 -Benin,10286712 -Bermuda,65139 -Bhutan,776448 -Bolivia,10562159 -Bosnia and Herzegovina,3566002 -Botswana,2168573 -Brazil,204213133 -British Virgin Islands,29588 -Brunei Darussalam,411704 -Bulgaria,7223938 -Burkina Faso,17585977 -Burundi,9891790 -Cabo Verde,526437 -Cambodia,15270790 -Cameroon,22239904 -Canada,35544564 -Cayman Islands,59172 -Central African Republic,4515392 -Chad,13569438 -Channel Islands,162969 -Chile,17613798 -China,1364270000 -Colombia,47791911 -Comoros,759385 -Congo,73722860 -Costa Rica,4757575 -Cote d'Ivoire,22531350 -Croatia,4238389 -Cuba,11439767 -Curacao,155909 -Cyprus,1152309 -Czech Republic,10525347 -Denmark,5643475 -Djibouti,912164 -Dominica,72778 -Dominican Republic,10405844 -Ecuador,15903112 -Egypt,91812566 -El Salvador,6281189 -Equatorial Guinea,1129424 -Estonia,1314545 -Ethiopia,97366774 -Faroe Islands,48842 -Fiji,885806 -Finland,5461512 -France,66331957 -French Polynesia,275484 -Gabon,1875713 -Gambia,1917852 -Georgia,3727000 -Germany,80982500 -Ghana,26962563 -Gibraltar,34038 -Greece,10892413 -Greenland,56295 -Grenada,106360 -Guam,160967 -Guatemala,15923559 -Guinea,11805509 -Guinea-Bissau,1725744 -Guyana,763393 -Haiti,10572466 -Honduras,8809216 -Hong Kong SAR,7241700 -Hungary,9866468 -Iceland,327386 -India,1293859294 -Indonesia,255131116 -Iran,78411092 -Iraq,35006080 -Ireland,4617225 -Isle of Man,82590 -Israel,8215700 -Italy,60789140 -Jamaica,2862087 -Japan,127276000 -Jordan,8809306 -Kazakhstan,17289224 -Kenya,46024250 -Kiribati,110458 -North Korea,25116363 -South Korea,50746659 -Kosovo,1821800 -Kuwait,3782450 -Kyrgyz Republic,5835500 -Lao PDR,6576397 -Latvia,1993782 -Lebanon,5603279 -Lesotho,2145785 -Liberia,4390737 -Libya,6204108 -Liechtenstein,37127 -Lithuania,2932367 -Luxembourg,556319 -Macao SAR,588781 -Macedonia,2077495 -Madagascar,23589801 -Malawi,17068838 -Malaysia,30228017 -Maldives,401000 -Mali,16962846 -Malta,427364 -Marshall Islands,52898 -Mauritania,4063920 -Mauritius,1260934 -Mexico,124221600 -Micronesia,104015 -Moldova,3556397 -Monaco,38132 -Mongolia,2923896 -Montenegro,621810 -Morocco,34318082 -Mozambique,27212382 -Myanmar,51924182 -Namibia,2370992 -Nauru,11853 -Nepal,28323241 -Netherlands,16865008 -New Caledonia,268000 -New Zealand,4509700 -Nicaragua,6013997 -Niger,19148219 -Nigeria,176460502 -Northern Mariana Islands,54468 -Norway,5137232 -Oman,3960925 -Pakistan,185546257 -Palau,21094 -Panama,3903986 -Papua New Guinea,7755785 -Paraguay,6552584 -Peru,30973354 -Philippines,100102249 -Poland,38011735 -Portugal,10401062 -Puerto Rico,3534874 -Qatar,2374419 -Romania,19908979 -Russian Federation,143819666 -Rwanda,11345357 -Samoa,192290 -San Marino,32657 -Sao Tome and Principe,191266 -Saudi Arabia,30776722 -Senegal,14546111 -Serbia,7130576 -Seychelles,91359 -Sierra Leone,7079162 -Singapore,5469724 -Sint Maarten (Dutch part),37685 -Slovak Republic,5418649 -Slovenia,2061980 -Solomon Islands,575504 -Somalia,13513125 -South Africa,54146734 -South Sudan,11530971 -Spain,46480882 -Sri Lanka,20771000 -St. Kitts and Nevis,53739 -St. Lucia,176421 -St. Martin (French part),31530 -St. Vincent and the Grenadines,109357 -Sudan,37737913 -Suriname,547928 -Swaziland,1295097 -Sweden,9696110 -Switzerland,8188649 -Syrian Arab Republic,19203090 -Tajikistan,8362745 -Tanzania,52234869 -Thailand,68416772 -Timor-Leste,1212814 -Togo,7228915 -Tonga,105782 -Trinidad and Tobago,1354493 -Tunisia,11143908 -Turkey,77030628 -Turkmenistan,5466241 -Turks and Caicos Islands,33739 -Tuvalu,10908 -Uganda,38833338 -Ukraine,45271947 -United Arab Emirates,9070867 -United Kingdom,64613160 -United States,318563456 -Uruguay,3419546 -Uzbekistan,30757700 -Vanuatu,258850 -Venezuela,30738378 -Vietnam,90728900 -Virgin Islands (U.S.),104170 -West Bank and Gaza,4294682 -Yemen,26246327 -Zambia,15620974 -Zimbabwe,15411675 diff -r b4def82f3f9f -r 166bb9b6b20a templates/alcohol.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/templates/alcohol.scala Tue Nov 07 14:17:21 2017 +0000 @@ -0,0 +1,69 @@ +// Part 2 about Alcohol-Consumption Worldwide +//============================================ + +object CW6b { + +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 + +val alcs = get_csv_page(url_alcohol) +val pops = get_csv_file(file_population) + + +def process_alcs(lines: List[String]) : List[(String, Double)] = + for (l <- lines) yield { + val entries = l.split(",").toList + (entries(0), entries(4).toDouble) + } + +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 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 +} + +sorted_country_consumption().take(10) +sorted_country_consumption().size + +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) +} + + +percentage(10) +percentage(164) + +} diff -r b4def82f3f9f -r 166bb9b6b20a templates/population.csv --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/templates/population.csv Tue Nov 07 14:17:21 2017 +0000 @@ -0,0 +1,216 @@ +country,population_size +Afghanistan,32758020 +Albania,2889104 +Algeria,39113313 +American Samoa,55437 +Andorra,79223 +Angola,26920466 +Antigua and Barbuda,98875 +Argentina,42981515 +Armenia,2906220 +Aruba,103795 +Australia,23460694 +Austria,8541575 +Azerbaijan,9535079 +Bahamas,382169 +Bahrain,1336397 +Bangladesh,159405279 +Barbados,283385 +Belarus,9474511 +Belgium,11209057 +Belize,351694 +Benin,10286712 +Bermuda,65139 +Bhutan,776448 +Bolivia,10562159 +Bosnia and Herzegovina,3566002 +Botswana,2168573 +Brazil,204213133 +British Virgin Islands,29588 +Brunei Darussalam,411704 +Bulgaria,7223938 +Burkina Faso,17585977 +Burundi,9891790 +Cabo Verde,526437 +Cambodia,15270790 +Cameroon,22239904 +Canada,35544564 +Cayman Islands,59172 +Central African Republic,4515392 +Chad,13569438 +Channel Islands,162969 +Chile,17613798 +China,1364270000 +Colombia,47791911 +Comoros,759385 +Congo,73722860 +Costa Rica,4757575 +Cote d'Ivoire,22531350 +Croatia,4238389 +Cuba,11439767 +Curacao,155909 +Cyprus,1152309 +Czech Republic,10525347 +Denmark,5643475 +Djibouti,912164 +Dominica,72778 +Dominican Republic,10405844 +Ecuador,15903112 +Egypt,91812566 +El Salvador,6281189 +Equatorial Guinea,1129424 +Estonia,1314545 +Ethiopia,97366774 +Faroe Islands,48842 +Fiji,885806 +Finland,5461512 +France,66331957 +French Polynesia,275484 +Gabon,1875713 +Gambia,1917852 +Georgia,3727000 +Germany,80982500 +Ghana,26962563 +Gibraltar,34038 +Greece,10892413 +Greenland,56295 +Grenada,106360 +Guam,160967 +Guatemala,15923559 +Guinea,11805509 +Guinea-Bissau,1725744 +Guyana,763393 +Haiti,10572466 +Honduras,8809216 +Hong Kong SAR,7241700 +Hungary,9866468 +Iceland,327386 +India,1293859294 +Indonesia,255131116 +Iran,78411092 +Iraq,35006080 +Ireland,4617225 +Isle of Man,82590 +Israel,8215700 +Italy,60789140 +Jamaica,2862087 +Japan,127276000 +Jordan,8809306 +Kazakhstan,17289224 +Kenya,46024250 +Kiribati,110458 +North Korea,25116363 +South Korea,50746659 +Kosovo,1821800 +Kuwait,3782450 +Kyrgyz Republic,5835500 +Lao PDR,6576397 +Latvia,1993782 +Lebanon,5603279 +Lesotho,2145785 +Liberia,4390737 +Libya,6204108 +Liechtenstein,37127 +Lithuania,2932367 +Luxembourg,556319 +Macao SAR,588781 +Macedonia,2077495 +Madagascar,23589801 +Malawi,17068838 +Malaysia,30228017 +Maldives,401000 +Mali,16962846 +Malta,427364 +Marshall Islands,52898 +Mauritania,4063920 +Mauritius,1260934 +Mexico,124221600 +Micronesia,104015 +Moldova,3556397 +Monaco,38132 +Mongolia,2923896 +Montenegro,621810 +Morocco,34318082 +Mozambique,27212382 +Myanmar,51924182 +Namibia,2370992 +Nauru,11853 +Nepal,28323241 +Netherlands,16865008 +New Caledonia,268000 +New Zealand,4509700 +Nicaragua,6013997 +Niger,19148219 +Nigeria,176460502 +Northern Mariana Islands,54468 +Norway,5137232 +Oman,3960925 +Pakistan,185546257 +Palau,21094 +Panama,3903986 +Papua New Guinea,7755785 +Paraguay,6552584 +Peru,30973354 +Philippines,100102249 +Poland,38011735 +Portugal,10401062 +Puerto Rico,3534874 +Qatar,2374419 +Romania,19908979 +Russian Federation,143819666 +Rwanda,11345357 +Samoa,192290 +San Marino,32657 +Sao Tome and Principe,191266 +Saudi Arabia,30776722 +Senegal,14546111 +Serbia,7130576 +Seychelles,91359 +Sierra Leone,7079162 +Singapore,5469724 +Sint Maarten (Dutch part),37685 +Slovak Republic,5418649 +Slovenia,2061980 +Solomon Islands,575504 +Somalia,13513125 +South Africa,54146734 +South Sudan,11530971 +Spain,46480882 +Sri Lanka,20771000 +St. Kitts and Nevis,53739 +St. Lucia,176421 +St. Martin (French part),31530 +St. Vincent and the Grenadines,109357 +Sudan,37737913 +Suriname,547928 +Swaziland,1295097 +Sweden,9696110 +Switzerland,8188649 +Syrian Arab Republic,19203090 +Tajikistan,8362745 +Tanzania,52234869 +Thailand,68416772 +Timor-Leste,1212814 +Togo,7228915 +Tonga,105782 +Trinidad and Tobago,1354493 +Tunisia,11143908 +Turkey,77030628 +Turkmenistan,5466241 +Turks and Caicos Islands,33739 +Tuvalu,10908 +Uganda,38833338 +Ukraine,45271947 +United Arab Emirates,9070867 +United Kingdom,64613160 +United States,318563456 +Uruguay,3419546 +Uzbekistan,30757700 +Vanuatu,258850 +Venezuela,30738378 +Vietnam,90728900 +Virgin Islands (U.S.),104170 +West Bank and Gaza,4294682 +Yemen,26246327 +Zambia,15620974 +Zimbabwe,15411675 diff -r b4def82f3f9f -r 166bb9b6b20a testing/alcohol.csv --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/testing/alcohol.csv Tue Nov 07 14:17:21 2017 +0000 @@ -0,0 +1,194 @@ +country,beer_servings,spirit_servings,wine_servings,total_litres_of_pure_alcohol +Afghanistan,0,0,0,0.0 +Albania,89,132,54,4.9 +Algeria,25,0,14,0.7 +Andorra,245,138,312,12.4 +Angola,217,57,45,5.9 +Antigua & Barbuda,102,128,45,4.9 +Argentina,193,25,221,8.3 +Armenia,21,179,11,3.8 +Australia,261,72,212,10.4 +Austria,279,75,191,9.7 +Azerbaijan,21,46,5,1.3 +Bahamas,122,176,51,6.3 +Bahrain,42,63,7,2.0 +Bangladesh,0,0,0,0.0 +Barbados,143,173,36,6.3 +Belarus,142,373,42,14.4 +Belgium,295,84,212,10.5 +Belize,263,114,8,6.8 +Benin,34,4,13,1.1 +Bhutan,23,0,0,0.4 +Bolivia,167,41,8,3.8 +Bosnia-Herzegovina,76,173,8,4.6 +Botswana,173,35,35,5.4 +Brazil,245,145,16,7.2 +Brunei,31,2,1,0.6 +Bulgaria,231,252,94,10.3 +Burkina Faso,25,7,7,4.3 +Burundi,88,0,0,6.3 +Cote d'Ivoire,37,1,7,4.0 +Cabo Verde,144,56,16,4.0 +Cambodia,57,65,1,2.2 +Cameroon,147,1,4,5.8 +Canada,240,122,100,8.2 +Central African Republic,17,2,1,1.8 +Chad,15,1,1,0.4 +Chile,130,124,172,7.6 +China,79,192,8,5.0 +Colombia,159,76,3,4.2 +Comoros,1,3,1,0.1 +Congo,76,1,9,1.7 +Cook Islands,0,254,74,5.9 +Costa Rica,149,87,11,4.4 +Croatia,230,87,254,10.2 +Cuba,93,137,5,4.2 +Cyprus,192,154,113,8.2 +Czech Republic,361,170,134,11.8 +North Korea,0,0,0,0.0 +DR Congo,32,3,1,2.3 +Denmark,224,81,278,10.4 +Djibouti,15,44,3,1.1 +Dominica,52,286,26,6.6 +Dominican Republic,193,147,9,6.2 +Ecuador,162,74,3,4.2 +Egypt,6,4,1,0.2 +El Salvador,52,69,2,2.2 +Equatorial Guinea,92,0,233,5.8 +Eritrea,18,0,0,0.5 +Estonia,224,194,59,9.5 +Ethiopia,20,3,0,0.7 +Fiji,77,35,1,2.0 +Finland,263,133,97,10.0 +France,127,151,370,11.8 +Gabon,347,98,59,8.9 +Gambia,8,0,1,2.4 +Georgia,52,100,149,5.4 +Germany,346,117,175,11.3 +Ghana,31,3,10,1.8 +Greece,133,112,218,8.3 +Grenada,199,438,28,11.9 +Guatemala,53,69,2,2.2 +Guinea,9,0,2,0.2 +Guinea-Bissau,28,31,21,2.5 +Guyana,93,302,1,7.1 +Haiti,1,326,1,5.9 +Honduras,69,98,2,3.0 +Hungary,234,215,185,11.3 +Iceland,233,61,78,6.6 +India,9,114,0,2.2 +Indonesia,5,1,0,0.1 +Iran,0,0,0,0.0 +Iraq,9,3,0,0.2 +Ireland,313,118,165,11.4 +Israel,63,69,9,2.5 +Italy,85,42,237,6.5 +Jamaica,82,97,9,3.4 +Japan,77,202,16,7.0 +Jordan,6,21,1,0.5 +Kazakhstan,124,246,12,6.8 +Kenya,58,22,2,1.8 +Kiribati,21,34,1,1.0 +Kuwait,0,0,0,0.0 +Kyrgyzstan,31,97,6,2.4 +Laos,62,0,123,6.2 +Latvia,281,216,62,10.5 +Lebanon,20,55,31,1.9 +Lesotho,82,29,0,2.8 +Liberia,19,152,2,3.1 +Libya,0,0,0,0.0 +Lithuania,343,244,56,12.9 +Luxembourg,236,133,271,11.4 +Madagascar,26,15,4,0.8 +Malawi,8,11,1,1.5 +Malaysia,13,4,0,0.3 +Maldives,0,0,0,0.0 +Mali,5,1,1,0.6 +Malta,149,100,120,6.6 +Marshall Islands,0,0,0,0.0 +Mauritania,0,0,0,0.0 +Mauritius,98,31,18,2.6 +Mexico,238,68,5,5.5 +Micronesia,62,50,18,2.3 +Monaco,0,0,0,0.0 +Mongolia,77,189,8,4.9 +Montenegro,31,114,128,4.9 +Morocco,12,6,10,0.5 +Mozambique,47,18,5,1.3 +Myanmar,5,1,0,0.1 +Namibia,376,3,1,6.8 +Nauru,49,0,8,1.0 +Nepal,5,6,0,0.2 +Netherlands,251,88,190,9.4 +New Zealand,203,79,175,9.3 +Nicaragua,78,118,1,3.5 +Niger,3,2,1,0.1 +Nigeria,42,5,2,9.1 +Niue,188,200,7,7.0 +Norway,169,71,129,6.7 +Oman,22,16,1,0.7 +Pakistan,0,0,0,0.0 +Palau,306,63,23,6.9 +Panama,285,104,18,7.2 +Papua New Guinea,44,39,1,1.5 +Paraguay,213,117,74,7.3 +Peru,163,160,21,6.1 +Philippines,71,186,1,4.6 +Poland,343,215,56,10.9 +Portugal,194,67,339,11.0 +Qatar,1,42,7,0.9 +South Korea,140,16,9,9.8 +Moldova,109,226,18,6.3 +Romania,297,122,167,10.4 +Russian Federation,247,326,73,11.5 +Rwanda,43,2,0,6.8 +St. Kitts & Nevis,194,205,32,7.7 +St. Lucia,171,315,71,10.1 +St. Vincent & the Grenadines,120,221,11,6.3 +Samoa,105,18,24,2.6 +San Marino,0,0,0,0.0 +Sao Tome & Principe,56,38,140,4.2 +Saudi Arabia,0,5,0,0.1 +Senegal,9,1,7,0.3 +Serbia,283,131,127,9.6 +Seychelles,157,25,51,4.1 +Sierra Leone,25,3,2,6.7 +Singapore,60,12,11,1.5 +Slovakia,196,293,116,11.4 +Slovenia,270,51,276,10.6 +Solomon Islands,56,11,1,1.2 +Somalia,0,0,0,0.0 +South Africa,225,76,81,8.2 +Spain,284,157,112,10.0 +Sri Lanka,16,104,0,2.2 +Sudan,8,13,0,1.7 +Suriname,128,178,7,5.6 +Swaziland,90,2,2,4.7 +Sweden,152,60,186,7.2 +Switzerland,185,100,280,10.2 +Syria,5,35,16,1.0 +Tajikistan,2,15,0,0.3 +Thailand,99,258,1,6.4 +Macedonia,106,27,86,3.9 +Timor-Leste,1,1,4,0.1 +Togo,36,2,19,1.3 +Tonga,36,21,5,1.1 +Trinidad & Tobago,197,156,7,6.4 +Tunisia,51,3,20,1.3 +Turkey,51,22,7,1.4 +Turkmenistan,19,71,32,2.2 +Tuvalu,6,41,9,1.0 +Uganda,45,9,0,8.3 +Ukraine,206,237,45,8.9 +United Arab Emirates,16,135,5,2.8 +United Kingdom,219,126,195,10.4 +Tanzania,36,6,1,5.7 +USA,249,158,84,8.7 +Uruguay,115,35,220,6.6 +Uzbekistan,25,101,8,2.4 +Vanuatu,21,18,11,0.9 +Venezuela,333,100,3,7.7 +Vietnam,111,2,1,2.0 +Yemen,6,0,0,0.1 +Zambia,32,19,4,2.5 +Zimbabwe,64,18,4,4.7 diff -r b4def82f3f9f -r 166bb9b6b20a testing/alcohol.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/testing/alcohol.scala Tue Nov 07 14:17:21 2017 +0000 @@ -0,0 +1,69 @@ +// Part 2 about Alcohol-Consumption Worldwide +//============================================ + +object CW6b { + +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 + +val alcs = get_csv_page(url_alcohol) +val pops = get_csv_file(file_population) + + +def process_alcs(lines: List[String]) : List[(String, Double)] = + for (l <- lines) yield { + val entries = l.split(",").toList + (entries(0), entries(4).toDouble) + } + +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 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 +} + +sorted_country_consumption().take(10) +sorted_country_consumption().size + +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) +} + + +percentage(10) +percentage(164) + +} diff -r b4def82f3f9f -r 166bb9b6b20a testing/alcohol_test --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/testing/alcohol_test Tue Nov 07 14:17:21 2017 +0000 @@ -0,0 +1,114 @@ +#!/bin/bash + +# to make the script fail safely +set -euo pipefail + + +out=${1:-output} + +echo "" > $out + +echo "Below is the feedback for your submission for alcohol.scala" >> $out +echo "" >> $out + + +# compilation tests + +function scala_compile { + (ulimit -t 30 -m 1024000 ; scala "$1" 2>> $out 1>> $out) +} + +# functional tests + +function scala_assert { + (ulimit -t 30 -m 1024000 ; scala -i "$1" "$2" -e "") # 2> /dev/null 1> /dev/null) +} + +# purity test + +function scala_vars { + (egrep '\bvar\b|\breturn\b|\.par|ListBuffer|mutable' "$1" 2> /dev/null 1> /dev/null) +} + + +# var, .par return, ListBuffer test +# +echo "alcohol.scala does not contain vars, returns etc?" >> $out + +if (scala_vars alcohol.scala) +then + echo " --> fail" >> $out + tsts0=$(( 1 )) +else + echo " --> success" >> $out + tsts0=$(( 0 )) +fi + +### compilation test + + +if [ $tsts0 -eq 0 ] +then + echo "alcohol.scala runs?" >> $out + + if (scala_compile alcohol.scala) + then + echo " --> success" >> $out + tsts=$(( 0 )) + else + echo " --> scala did not run alcohol.scala" >> $out + tsts=$(( 1 )) + fi +else + tsts=$(( 1 )) +fi + +### alcohol tests + +if [ $tsts -eq 0 ] +then + echo "get_csv tests:" >> $out + echo " get_csv_page(url_alcohol).size == 194" >> $out + echo " get_csv_file(file_population).size == 216" >> $out + + if (scala_assert "alcohol.scala" "alcohol_test1.scala") + then + echo " --> success" >> $out + else + echo " --> one of the tests failed" >> $out + fi +fi + +### alcohol processing tests + +if [ $tsts -eq 0 ] +then + echo "processing tests:" >> $out + echo " process_alcs(alcs_list.drop(1))(0) == (\"Afghanistan\", 0.0)" >> $out + echo " process_pops(pops_list.drop(1))(\"Micronesia\") == 104015" >> $out + + if (scala_assert "alcohol.scala" "alcohol_test2.scala") + then + echo " --> success" >> $out + else + echo " --> one of the tests failed" >> $out + fi +fi + +### alcohol percentage tests + +if [ $tsts -eq 0 ] +then + echo "calculation tests:" >> $out + echo " sorted_country_consumption().size == 177" >> $out + echo " sorted_country_consumption()(9) == (\"United Kingdom\", 671976864)" >> $out + echo " percentage(164) == (28771558364L, 28771558364L, 100.0)" >> $out + + if (scala_assert "alcohol.scala" "alcohol_test3.scala") + then + echo " --> success" >> $out + else + echo " --> one of the tests failed" >> $out + fi +fi + diff -r b4def82f3f9f -r 166bb9b6b20a testing/alcohol_test1.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/testing/alcohol_test1.scala Tue Nov 07 14:17:21 2017 +0000 @@ -0,0 +1,12 @@ + +val url_alcohol_test = + "https://raw.githubusercontent.com/fivethirtyeight/data/master/alcohol-consumption/drinks.csv" + +val file_population_test = + "population.csv" + +assert(CW6b.get_csv_page(url_alcohol_test).size == 194) + +assert(CW6b.get_csv_file(file_population_test).size == 216) + + diff -r b4def82f3f9f -r 166bb9b6b20a testing/alcohol_test2.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/testing/alcohol_test2.scala Tue Nov 07 14:17:21 2017 +0000 @@ -0,0 +1,17 @@ + +import io.Source +import scala.util._ + +val file_population_test = "population.csv" +val file_alcohol_test = "alcohol.csv" + + +def get_csv_file_test(file: String) : List[String] = + Source.fromFile(file)("ISO-8859-1").getLines.toList + +val alcs_test = get_csv_file_test(file_alcohol_test) +val pops_test = get_csv_file_test(file_population_test) + +assert(CW6b.process_alcs(alcs_test.drop(1))(0) == ("Afghanistan", 0.0)) + +assert(CW6b.process_pops(pops_test.drop(1))("Micronesia") == 104015) diff -r b4def82f3f9f -r 166bb9b6b20a testing/alcohol_test3.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/testing/alcohol_test3.scala Tue Nov 07 14:17:21 2017 +0000 @@ -0,0 +1,7 @@ + + +assert(CW6b.sorted_country_consumption().size == 177) + +assert(CW6b.sorted_country_consumption()(9) == ("United Kingdom", 671976864)) + +assert(CW6b.percentage(164) == (28771558364L, 28771558364L, 100.0)) diff -r b4def82f3f9f -r 166bb9b6b20a testing/collatz_test --- a/testing/collatz_test Tue Nov 07 13:08:18 2017 +0000 +++ b/testing/collatz_test Tue Nov 07 14:17:21 2017 +0000 @@ -8,7 +8,7 @@ echo "" > $out -echo "Below is the feedback for your submission" >> $out +echo "Below is the feedback for your submission collatz.scala" >> $out echo "" >> $out diff -r b4def82f3f9f -r 166bb9b6b20a testing/mark --- a/testing/mark Tue Nov 07 13:08:18 2017 +0000 +++ b/testing/mark Tue Nov 07 14:17:21 2017 +0000 @@ -4,5 +4,6 @@ trap "exit" INT ./collatz_test output1 +./alcohol_test output2 diff -r b4def82f3f9f -r 166bb9b6b20a testing/population.csv --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/testing/population.csv Tue Nov 07 14:17:21 2017 +0000 @@ -0,0 +1,216 @@ +country,population_size +Afghanistan,32758020 +Albania,2889104 +Algeria,39113313 +American Samoa,55437 +Andorra,79223 +Angola,26920466 +Antigua and Barbuda,98875 +Argentina,42981515 +Armenia,2906220 +Aruba,103795 +Australia,23460694 +Austria,8541575 +Azerbaijan,9535079 +Bahamas,382169 +Bahrain,1336397 +Bangladesh,159405279 +Barbados,283385 +Belarus,9474511 +Belgium,11209057 +Belize,351694 +Benin,10286712 +Bermuda,65139 +Bhutan,776448 +Bolivia,10562159 +Bosnia and Herzegovina,3566002 +Botswana,2168573 +Brazil,204213133 +British Virgin Islands,29588 +Brunei Darussalam,411704 +Bulgaria,7223938 +Burkina Faso,17585977 +Burundi,9891790 +Cabo Verde,526437 +Cambodia,15270790 +Cameroon,22239904 +Canada,35544564 +Cayman Islands,59172 +Central African Republic,4515392 +Chad,13569438 +Channel Islands,162969 +Chile,17613798 +China,1364270000 +Colombia,47791911 +Comoros,759385 +Congo,73722860 +Costa Rica,4757575 +Cote d'Ivoire,22531350 +Croatia,4238389 +Cuba,11439767 +Curacao,155909 +Cyprus,1152309 +Czech Republic,10525347 +Denmark,5643475 +Djibouti,912164 +Dominica,72778 +Dominican Republic,10405844 +Ecuador,15903112 +Egypt,91812566 +El Salvador,6281189 +Equatorial Guinea,1129424 +Estonia,1314545 +Ethiopia,97366774 +Faroe Islands,48842 +Fiji,885806 +Finland,5461512 +France,66331957 +French Polynesia,275484 +Gabon,1875713 +Gambia,1917852 +Georgia,3727000 +Germany,80982500 +Ghana,26962563 +Gibraltar,34038 +Greece,10892413 +Greenland,56295 +Grenada,106360 +Guam,160967 +Guatemala,15923559 +Guinea,11805509 +Guinea-Bissau,1725744 +Guyana,763393 +Haiti,10572466 +Honduras,8809216 +Hong Kong SAR,7241700 +Hungary,9866468 +Iceland,327386 +India,1293859294 +Indonesia,255131116 +Iran,78411092 +Iraq,35006080 +Ireland,4617225 +Isle of Man,82590 +Israel,8215700 +Italy,60789140 +Jamaica,2862087 +Japan,127276000 +Jordan,8809306 +Kazakhstan,17289224 +Kenya,46024250 +Kiribati,110458 +North Korea,25116363 +South Korea,50746659 +Kosovo,1821800 +Kuwait,3782450 +Kyrgyz Republic,5835500 +Lao PDR,6576397 +Latvia,1993782 +Lebanon,5603279 +Lesotho,2145785 +Liberia,4390737 +Libya,6204108 +Liechtenstein,37127 +Lithuania,2932367 +Luxembourg,556319 +Macao SAR,588781 +Macedonia,2077495 +Madagascar,23589801 +Malawi,17068838 +Malaysia,30228017 +Maldives,401000 +Mali,16962846 +Malta,427364 +Marshall Islands,52898 +Mauritania,4063920 +Mauritius,1260934 +Mexico,124221600 +Micronesia,104015 +Moldova,3556397 +Monaco,38132 +Mongolia,2923896 +Montenegro,621810 +Morocco,34318082 +Mozambique,27212382 +Myanmar,51924182 +Namibia,2370992 +Nauru,11853 +Nepal,28323241 +Netherlands,16865008 +New Caledonia,268000 +New Zealand,4509700 +Nicaragua,6013997 +Niger,19148219 +Nigeria,176460502 +Northern Mariana Islands,54468 +Norway,5137232 +Oman,3960925 +Pakistan,185546257 +Palau,21094 +Panama,3903986 +Papua New Guinea,7755785 +Paraguay,6552584 +Peru,30973354 +Philippines,100102249 +Poland,38011735 +Portugal,10401062 +Puerto Rico,3534874 +Qatar,2374419 +Romania,19908979 +Russian Federation,143819666 +Rwanda,11345357 +Samoa,192290 +San Marino,32657 +Sao Tome and Principe,191266 +Saudi Arabia,30776722 +Senegal,14546111 +Serbia,7130576 +Seychelles,91359 +Sierra Leone,7079162 +Singapore,5469724 +Sint Maarten (Dutch part),37685 +Slovak Republic,5418649 +Slovenia,2061980 +Solomon Islands,575504 +Somalia,13513125 +South Africa,54146734 +South Sudan,11530971 +Spain,46480882 +Sri Lanka,20771000 +St. Kitts and Nevis,53739 +St. Lucia,176421 +St. Martin (French part),31530 +St. Vincent and the Grenadines,109357 +Sudan,37737913 +Suriname,547928 +Swaziland,1295097 +Sweden,9696110 +Switzerland,8188649 +Syrian Arab Republic,19203090 +Tajikistan,8362745 +Tanzania,52234869 +Thailand,68416772 +Timor-Leste,1212814 +Togo,7228915 +Tonga,105782 +Trinidad and Tobago,1354493 +Tunisia,11143908 +Turkey,77030628 +Turkmenistan,5466241 +Turks and Caicos Islands,33739 +Tuvalu,10908 +Uganda,38833338 +Ukraine,45271947 +United Arab Emirates,9070867 +United Kingdom,64613160 +United States,318563456 +Uruguay,3419546 +Uzbekistan,30757700 +Vanuatu,258850 +Venezuela,30738378 +Vietnam,90728900 +Virgin Islands (U.S.),104170 +West Bank and Gaza,4294682 +Yemen,26246327 +Zambia,15620974 +Zimbabwe,15411675