Skip to content

Commit

Permalink
split out answers
Browse files Browse the repository at this point in the history
  • Loading branch information
philtweir committed Sep 4, 2021
1 parent 779bafc commit 021574d
Show file tree
Hide file tree
Showing 5 changed files with 358 additions and 1,102 deletions.
174 changes: 167 additions & 7 deletions 002-basic-control-structures/Answers.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
"cells": [
{
"cell_type": "markdown",
"id": "5db4ea72",
"id": "18edd6b8",
"metadata": {},
"source": [
"# 002: Basic Control Structures"
]
},
{
"cell_type": "markdown",
"id": "67a80e4b",
"id": "b81f64c7",
"metadata": {},
"source": [
"## Strung together"
Expand All @@ -19,7 +19,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "5c82c39d",
"id": "0bbef854",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -39,7 +39,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "92953c8a",
"id": "042fe0fe",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -59,7 +59,7 @@
},
{
"cell_type": "markdown",
"id": "02f31524",
"id": "0aa57294",
"metadata": {},
"source": [
"## Zipping Up"
Expand All @@ -68,7 +68,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "7a8e5af9",
"id": "8346f014",
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -98,7 +98,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "4cb9272c",
"id": "280668c6",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -107,6 +107,166 @@
" new_dct[key] = value\n",
"new_dct"
]
},
{
"cell_type": "markdown",
"id": "fc7335c1",
"metadata": {},
"source": [
"# 002: Basic Control Structures II"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3f97f8a7",
"metadata": {},
"outputs": [],
"source": [
"def dict_to_string_map(dct):\n",
" new_dct = {}\n",
" \n",
" for key, value in dct.items():\n",
" if type(value) is list:\n",
" new_dct[key] = ', '.join(value)\n",
" else:\n",
" new_dct[key] = str(value)\n",
" \n",
" return new_dct\n",
"\n",
"dict_to_string_map(my_meetup_dot_com_profile)"
]
},
{
"cell_type": "markdown",
"id": "ddfefa3e",
"metadata": {},
"source": [
"## Yes but no, but yes, but no"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "92ae43cd",
"metadata": {},
"outputs": [],
"source": [
"# ANANSWER\n",
"my_dictionary = {\n",
" 'hoho': 1,\n",
" 'hehe': 2,\n",
" 'myhaha': 3\n",
"}\n",
"# hoho, hehe, myhaha are fine; anything else gives a KeyError, unless it contains the text \"haha\"\n",
"\n",
"def check_dictionary(key, dct):\n",
" try:\n",
" dct[key]\n",
" except KeyError as e:\n",
" if 'haha' in key:\n",
" raise RuntimeError(\"Now you're just having a laugh\")\n",
" raise e\n",
" \n",
"check_dictionary('myhaha', my_dictionary) # fine\n",
"check_dictionary('hohoho', my_dictionary) # KeyError\n",
"check_dictionary('hohohaha', my_dictionary) # RuntimeError"
]
},
{
"cell_type": "markdown",
"id": "00e18caa",
"metadata": {},
"source": [
"## Three's a crowd"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6a9c5d24",
"metadata": {},
"outputs": [],
"source": [
"filter_function = lambda n: (n % 3 > 0) and (n < 1000) # INSERT FUNCTION HERE - SHOULD EVALUATE TO TRUE or FALSE\n",
"list(filter(filter_function, [1, 234, 129123, 15, 34, 41991, 14]))"
]
},
{
"cell_type": "markdown",
"id": "18e1be05",
"metadata": {},
"source": [
"## Calling a halt"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "afde3db9",
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"with open('halts.geojson', 'r') as halts_f:\n",
" halts_geojson = json.load(halts_f)\n",
"\n",
"all_halts = halts_geojson['features']\n",
"halt_names = map(lambda feature: feature['properties']['Station'], all_halts)\n",
"belfast_halts = filter(lambda name: name.startswith('BELFAST'), halt_names) # replace this line\n",
"belfast_halts = map(lambda name: name.replace('BELFAST - ', '').replace(' RAIL HALT', ''), belfast_halts)\n",
"\n",
"sorted(belfast_halts)"
]
},
{
"cell_type": "markdown",
"id": "de885134",
"metadata": {},
"source": [
"## End of the line"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0a53f4bb",
"metadata": {},
"outputs": [],
"source": [
"with open('halts.geojson', 'r') as halts_f:\n",
" halts_geojson = json.load(halts_f)\n",
"\n",
"halts_dictionary = {h['properties']['Station']: h['geometry']['coordinates'] for h in halts_geojson['features']}\n",
"\n",
"halts_dictionary if halts_dictionary['BELFAST - ADELAIDE RAIL HALT'] != [-5.955437428473338, 54.57871560269907] else \"🥳\""
]
},
{
"cell_type": "markdown",
"id": "58874cf8",
"metadata": {},
"source": [
"## Optimal Prime"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2dcc13e8",
"metadata": {},
"outputs": [],
"source": [
"def is_prime(n):\n",
" if n == 2:\n",
" return True\n",
" \n",
" for y in primes():\n",
" if y > int(n ** .5):\n",
" return True\n",
" if n % y == 0:\n",
" return False"
]
}
],
"metadata": {
Expand Down
Loading

0 comments on commit 021574d

Please sign in to comment.