<jupyter_start><jupyter_text>Solution to exercise 1.3.2- Generalise the code written for exercise 1.1.3 for finding which European countries have the largest population in 1952 and 2007 by creating a function that finds which country on a defined continent has the largest population for a given year. Provide default values for certain arguments.<jupyter_code>pop_1957 = 0
largest_eu_country_1957 = ''

pop_2007 = 0
largest_eu_country_2007 = ''

with open('../data/gapminder.csv') as f:
    for line in f:
        data = line.strip().split(',')
        if data[1] == "Europe":
            if data[2] == "1957" and int(data[4]) > pop_1957:
                pop_1957 = int(data[4])
                largest_eu_country_1957 = data[0]
            if data[2] == "2007" and int(data[4]) > pop_2007:
                pop_2007 = int(data[4])
                largest_eu_country_2007 = data[0]
            
print(largest_eu_country_1957, pop_1957)
print(largest_eu_country_2007, pop_2007)
import statistics as stats
import csv

def largest_country_by_continent_and_year(gapminder_filepath, continent='Europe', year='1952'):
    """
    Returns the largest country of the selected continent for a given year.

    gapminder_filepath --- gapminder file path with multi-continent and multi-year data
    continent --- continent for which data is extracted
    year --- year for which data is extracted
    """
    pop = 0
    largest_country = ''
    with open(gapminder_filepath) as f:
        reader = csv.DictReader(f, delimiter = ",")
        for data in reader: 
            if data['continent'] == continent and data['year'] == year:
                if int(data['pop']) > pop:
                    pop = int(data['pop'])
                    largest_country = data['country']
    print(continent, 'largest country in', year)
    return largest_country, pop
import os       
largest_country = largest_country_by_continent_and_year(os.path.join('..', 'data', 'gapminder.csv'))
print(largest_country)
import os       
largest_country = largest_country_by_continent_and_year(os.path.join('..', 'data', 'gapminder.csv'), 'Africa')
print(largest_country)
import os       
largest_country = largest_country_by_continent_and_year(os.path.join('..', 'data', 'gapminder.csv'), 'Africa', '2007')
print(largest_country)
import os       
largest_country = largest_country_by_continent_and_year(os.path.join('..', 'data', 'gapminder.csv'), 'Asia')
print(largest_country)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11191919191919192, 'fraction_numerical': 0.039595959595959594, 'mean_word_length': 3.351493848857645, 'pattern_counts': {'":': 1, '<': 5, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '31586', 'license': 'Unlicense', 'n_tokens_mistral': 835, 'n_tokens_neox': 734, 'n_words': 204, 'path': 'solutions/ex_13_2.ipynb', 'repo_name': 'abotzki/python-data-science', 'size': '2475'}