Skip to content

Commit

Permalink
update formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
jessepisel committed Jun 9, 2023
1 parent 944bfc0 commit 919dc76
Showing 1 changed file with 38 additions and 48 deletions.
86 changes: 38 additions & 48 deletions index.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@
"outputs": [],
"source": [
"# Run this cell without changes\n",
"world_cup_file = open(\"data/world_cup_2018.json\", encoding='utf8')\n",
"world_cup_file = open(\"data/world_cup_2018.json\", encoding=\"utf8\")\n",
"population_file = open(\"data/country_populations.csv\")"
]
},
Expand All @@ -201,7 +201,7 @@
"outputs": [],
"source": [
"# __SOLUTION__\n",
"world_cup_file = open(\"data/world_cup_2018.json\", encoding='utf8')\n",
"world_cup_file = open(\"data/world_cup_2018.json\", encoding=\"utf8\")\n",
"population_file = open(\"data/country_populations.csv\")"
]
},
Expand Down Expand Up @@ -261,7 +261,7 @@
"assert type(world_cup_data) == dict\n",
"\n",
"# Check that the dictionary has 2 keys, 'name' and 'rounds'\n",
"assert list(world_cup_data.keys()) == ['name', 'rounds']"
"assert list(world_cup_data.keys()) == [\"name\", \"rounds\"]"
]
},
{
Expand All @@ -276,7 +276,7 @@
"assert type(world_cup_data) == dict\n",
"\n",
"# Check that the dictionary has 2 keys, 'name' and 'rounds'\n",
"assert list(world_cup_data.keys()) == ['name', 'rounds']"
"assert list(world_cup_data.keys()) == [\"name\", \"rounds\"]"
]
},
{
Expand Down Expand Up @@ -338,6 +338,7 @@
"# (csv.DictReader interface differs slightly by Python version;\n",
"# either a dict or an OrderedDict is fine here)\n",
"from collections import OrderedDict\n",
"\n",
"assert type(population_data[0]) == dict or type(population_data[0]) == OrderedDict"
]
},
Expand All @@ -356,6 +357,7 @@
"# (csv.DictReader interface differs slightly by Python version;\n",
"# either a dict or an OrderedDict is fine here)\n",
"from collections import OrderedDict\n",
"\n",
"assert type(population_data[0]) == dict or type(population_data[0]) == OrderedDict"
]
},
Expand Down Expand Up @@ -651,7 +653,7 @@
" round_matches = None\n",
" # Add them to the overall list of matches\n",
" None\n",
" \n",
"\n",
"matches[0]"
]
},
Expand Down Expand Up @@ -699,7 +701,7 @@
" round_matches = round_[\"matches\"]\n",
" # Add them to the overall list of matches\n",
" matches.extend(round_matches)\n",
" \n",
"\n",
"matches[0]"
]
},
Expand Down Expand Up @@ -805,7 +807,7 @@
" None\n",
" # Add team2 name value to teams_set\n",
" None\n",
" \n",
"\n",
"teams = sorted(list(teams_set))\n",
"print(teams)"
]
Expand All @@ -832,7 +834,7 @@
" teams_set.add(match[\"team1\"][\"name\"])\n",
" # Add team2 value to teams_set\n",
" teams_set.add(match[\"team2\"][\"name\"])\n",
" \n",
"\n",
"teams = sorted(list(teams_set))\n",
"print(teams)"
]
Expand Down Expand Up @@ -966,7 +968,7 @@
"combined_data = {}\n",
"for team in teams:\n",
" combined_data[team] = {\"wins\": 0}\n",
" \n",
"\n",
"# Dictionary comprehension:\n",
"# (This is preferable once you're familiar with the\n",
"# syntax, but don't worry too much about using this\n",
Expand Down Expand Up @@ -1072,6 +1074,7 @@
"source": [
"# Replace None with appropriate code\n",
"\n",
"\n",
"def find_winner(match):\n",
" \"\"\"\n",
" Given a dictionary containing information about a match,\n",
Expand All @@ -1088,14 +1091,15 @@
"source": [
"# __SOLUTION__\n",
"\n",
"\n",
"def find_winner(match):\n",
" \"\"\"\n",
" Given a dictionary containing information about a match,\n",
" return the name of the winner (or None in the case of a tie)\n",
" \"\"\"\n",
" score_1 = match[\"score1\"]\n",
" score_2 = match[\"score2\"]\n",
" \n",
"\n",
" if score_1 > score_2:\n",
" return match[\"team1\"][\"name\"]\n",
" elif score_2 > score_1:\n",
Expand Down Expand Up @@ -1153,7 +1157,7 @@
" if winner:\n",
" # Add 1 to the associated count of wins\n",
" None\n",
" \n",
"\n",
"# Visually inspect the output to ensure the wins are\n",
"# different for different countries\n",
"combined_data"
Expand Down Expand Up @@ -1217,7 +1221,7 @@
" if winner:\n",
" # Add 1 to the associated count of wins\n",
" combined_data[winner][\"wins\"] += 1\n",
" \n",
"\n",
"# Visually inspect the output to ensure the wins are\n",
"# different for different countries\n",
"combined_data"
Expand Down Expand Up @@ -1522,8 +1526,8 @@
"for record in population_data:\n",
" # Add record to population_data_filtered if relevant\n",
" None\n",
" \n",
"len(population_data_filtered) # 27"
"\n",
"len(population_data_filtered) # 27"
]
},
{
Expand Down Expand Up @@ -1551,7 +1555,7 @@
" # Add record to population_data_filtered if relevant\n",
" if (record[\"Country Name\"] in teams) and (record[\"Year\"] == \"2018\"):\n",
" population_data_filtered.append(record)\n",
" \n",
"\n",
"len(population_data_filtered)"
]
},
Expand Down Expand Up @@ -1669,12 +1673,13 @@
" \"Egypt, Arab Rep.\": \"Egypt\",\n",
" \"Iran, Islamic Rep.\": \"Iran\",\n",
" \"Korea, Rep.\": \"South Korea\",\n",
" \"United Kingdom\": \"England\"\n",
" \"United Kingdom\": \"England\",\n",
" }\n",
" # The .get method returns the corresponding value from\n",
" # the dict if present, otherwise returns country_name\n",
" return name_sub_dict.get(country_name, country_name)\n",
"\n",
"\n",
"# Example where normalized location is different\n",
"print(normalize_location(\"Russian Federation\"))\n",
"# Example where normalized location is the same\n",
Expand Down Expand Up @@ -1707,12 +1712,13 @@
" \"Egypt, Arab Rep.\": \"Egypt\",\n",
" \"Iran, Islamic Rep.\": \"Iran\",\n",
" \"Korea, Rep.\": \"South Korea\",\n",
" \"United Kingdom\": \"England\"\n",
" \"United Kingdom\": \"England\",\n",
" }\n",
" # The .get method returns the corresponding value from\n",
" # the dict if present, otherwise returns country_name\n",
" return name_sub_dict.get(country_name, country_name)\n",
"\n",
"\n",
"# Example where normalized location is different\n",
"print(normalize_location(\"Russian Federation\"))\n",
"# Example where normalized location is the same\n",
Expand Down Expand Up @@ -1745,8 +1751,8 @@
" None\n",
" # Append to list\n",
" None\n",
" \n",
"len(population_data_filtered) # 32"
"\n",
"len(population_data_filtered) # 32"
]
},
{
Expand Down Expand Up @@ -1779,7 +1785,7 @@
" record[\"Country Name\"] = normalized_name\n",
" # Append to list\n",
" population_data_filtered.append(record)\n",
" \n",
"\n",
"len(population_data_filtered)"
]
},
Expand Down Expand Up @@ -1848,7 +1854,7 @@
"for record in population_data_filtered:\n",
" # Convert the population value from str to int\n",
" None\n",
" \n",
"\n",
"# Look at the last record to make sure the population\n",
"# value is an int\n",
"population_data_filtered[-1]"
Expand Down Expand Up @@ -1879,7 +1885,7 @@
"for record in population_data_filtered:\n",
" # Convert the population value from str to int\n",
" record[\"Value\"] = int(record[\"Value\"])\n",
" \n",
"\n",
"# Look at the last record to make sure the population\n",
"# value is an int\n",
"population_data_filtered[-1]"
Expand Down Expand Up @@ -2018,7 +2024,7 @@
" population = None\n",
" # Add this information to combined_data\n",
" None\n",
" \n",
"\n",
"# Look combined_data\n",
"combined_data"
]
Expand Down Expand Up @@ -2079,7 +2085,7 @@
" population = record[\"Value\"]\n",
" # Add this information to combined_data\n",
" combined_data[country][\"population\"] = population\n",
" \n",
"\n",
"# Look combined_data\n",
"combined_data"
]
Expand Down Expand Up @@ -2339,21 +2345,17 @@
"fig, ax = plt.subplots(figsize=(8, 5))\n",
"\n",
"# Basic scatter plot\n",
"ax.scatter(\n",
" x=populations,\n",
" y=wins,\n",
" color=\"gray\", alpha=0.5, s=100\n",
")\n",
"ax.scatter(x=populations, y=wins, color=\"gray\", alpha=0.5, s=100)\n",
"ax.set_xlabel(\"2018 Population\")\n",
"ax.set_ylabel(\"2018 World Cup Wins\")\n",
"ax.set_title(\"Population vs. World Cup Wins\")\n",
"\n",
"# Add annotations for specific points of interest\n",
"highlighted_points = {\n",
" \"Belgium\": 2, # Numbers are the index of that\n",
" \"Belgium\": 2, # Numbers are the index of that\n",
" \"Brazil\": 3, # country in populations & wins\n",
" \"France\": 10,\n",
" \"Nigeria\": 17\n",
" \"Nigeria\": 17,\n",
"}\n",
"for country, index in highlighted_points.items():\n",
" # Get x and y position of data point\n",
Expand All @@ -2364,11 +2366,7 @@
" xtext = x - (1.25e6 * len(country))\n",
" ytext = y - 0.5\n",
" # Annotate with relevant arguments\n",
" ax.annotate(\n",
" text=country,\n",
" xy=(x, y),\n",
" xytext=(xtext, ytext)\n",
" )"
" ax.annotate(text=country, xy=(x, y), xytext=(xtext, ytext))"
]
},
{
Expand Down Expand Up @@ -2396,21 +2394,17 @@
"fig, ax = plt.subplots(figsize=(8, 5))\n",
"\n",
"# Basic scatter plot\n",
"ax.scatter(\n",
" x=populations,\n",
" y=wins,\n",
" color=\"gray\", alpha=0.5, s=100\n",
")\n",
"ax.scatter(x=populations, y=wins, color=\"gray\", alpha=0.5, s=100)\n",
"ax.set_xlabel(\"2018 Population\")\n",
"ax.set_ylabel(\"2018 World Cup Wins\")\n",
"ax.set_title(\"Population vs. World Cup Wins\")\n",
"\n",
"# Add annotations for specific points of interest\n",
"highlighted_points = {\n",
" \"Belgium\": 2, # Numbers are the index of that\n",
" \"Belgium\": 2, # Numbers are the index of that\n",
" \"Brazil\": 3, # country in populations & wins\n",
" \"France\": 10,\n",
" \"Nigeria\": 17\n",
" \"Nigeria\": 17,\n",
"}\n",
"for country, index in highlighted_points.items():\n",
" # Get x and y position of data point\n",
Expand All @@ -2421,11 +2415,7 @@
" xtext = x - (1.25e6 * len(country))\n",
" ytext = y - 0.5\n",
" # Annotate with relevant arguments\n",
" ax.annotate(\n",
" text=country,\n",
" xy=(x, y),\n",
" xytext=(xtext, ytext)\n",
" )"
" ax.annotate(text=country, xy=(x, y), xytext=(xtext, ytext))"
]
},
{
Expand Down

0 comments on commit 919dc76

Please sign in to comment.