Specification: Find the name of the continent with the highest average population by country.
Example input/output:
Input:
countries
name | population | continent |
---|---|---|
USA | 328 | North America |
Canada | 37 | North America |
Ethiopia | 109 | Africa |
Kenya | 51 | Africa |
Output:
North America
Python - Imperative
def continent_by_population(countries): continent_stats = defaultdict(lambda: [0, 0]) for country in countries: continent = country['continent'] continent_stats[continent][0] += country['population'] continent_stats[continent][1] += 1 max_continent = None max_average = None for continent, [total, count] in continent_stats.items(): average = total / count if max_average is None or max_average < average: max_average = average max_continent = continent return max_continent
Python - Functional
def continent_by_population(countries): continents = set([c['continent'] for c in countries]) populations_by_continent = [ (continent, [c['population'] for c in countries if c['continent'] == continent]) for continent in continents ] averages = [ (continent, sum(pops) / len(pops)) for continent, pops in populations_by_continent ] return max(averages, key=lambda t: t[1])[0]
Python - Pandas
def continent_by_population(countries): mean_pop = countries.groupby('continent').population.mean() return mean_pop.index[mean_pop.argmax()]
R - Tidyverse
continent_by_population <- function(countries) { countries %>% group_by(continent) %>% summarize(mean_pop = mean(population)) %>% slice(which.max(mean_pop)) %>% .$continent }
SQL - SQLite
SELECT continent FROM countries GROUP BY continent ORDER BY AVG(population) DESC LIMIT 1
Datalog - Souffle
.decl average_population(Continent:symbol, Avg:number) average_population(Continent, Avg) :- countries(Continent, _, _), Total = sum P : countries(Continent, _, P), Num_countries = count : countries(Continent, _, _), Avg = Total / Num_countries. continent_by_population(Continent) :- countries(Continent, _, _), average_population(Continent, Max_avg), Max_avg = max A : { countries(C, _, _), average_population(C, A) }.
Q - kdb+
averages: select avg(population) by continent from countries; continent_by_population: (first select[>population] continent from averages) `continent