Created
March 18, 2015 14:06
-
-
Save phlipper/556a168a8a4f8dbbf450 to your computer and use it in GitHub Desktop.
TECH601-00 Day 5 Example - Lists of Countries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# an empty list of countries | |
countries = [] | |
# `push` a country on to the list | |
countries.push({ | |
"id" => "AGO", | |
"name" => "Angola", | |
"population" => 21_471_618, | |
"capital" => "Luanda", | |
"latitude" => -8.81155, | |
"longitude" => 13.242, | |
"income_level" => "Upper Middle", | |
"high_income" => false | |
}) | |
countries.push({ | |
"id" => "ATG", | |
"name" => "Antigua and Barbuda", | |
"population" => 89_985, | |
"capital" => "Saint John's", | |
"latitude" => 17.1175, | |
"longitude" => -61.8456, | |
"income_level" => "High", | |
"high_income" => true | |
}) | |
countries.push({ | |
"id" => "BLR", | |
"name" => "Belarus", | |
"population" => 9_466_000, | |
"capital" => "Minsk", | |
"latitude" => 53.9678, | |
"longitude" => 27.5766, | |
"income_level" => "Upper Middle", | |
"high_income" => false | |
}) | |
countries.push({ | |
"id" => "CAF", | |
"name" => "Central African Republic", | |
"population" => 4_616_417, | |
"capital" => "Bangui", | |
"latitude" => 5.63056, | |
"longitude" => 21.6407, | |
"income_level" => "Low", | |
"high_income" => false | |
}) | |
countries.push({ | |
"id" => "CHE", | |
"name" => "Switzerland", | |
"population" => 8_081_482, | |
"capital" => "Bern", | |
"latitude" => 46.948, | |
"longitude" => 7.44821, | |
"income_level" => "High", | |
"high_income" => true | |
}) | |
# Output a populated list of countries | |
# puts countries | |
# Output a subset list of countries | |
# puts countries.slice(1) | |
# puts countries[1] | |
# puts countries.slice(1..3) | |
# puts countries[1..3] | |
# puts countries.slice(1, 3) | |
# Reverse the list | |
# puts countries.reverse.last | |
# puts countries.reverse.first | |
# puts countries.reverse.first["name"] | |
# Sort the countries by population - lowest to highest | |
countries_by_population = countries.sort_by do |country| | |
country["population"] | |
end | |
# Output each country, ordered by population | |
countries_by_population.each do |country| | |
puts "#{country["name"]} population is #{country["population"]}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is the answer I got:
Antigua and Barbuda population is 89985
Central African Republic population is 4616417
Switzerland population is 8081482
Belarus population is 9466000
Angola population is 21471618