From 0472940614d79c440f45236ca5aec715ec8a3cca Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Mon, 17 Sep 2018 19:04:55 +0100 Subject: [PATCH] Added Python notes --- src/task1/task1.ipynb | 67 +++++++++++++++++++++++++++++-------------- 1 file changed, 45 insertions(+), 22 deletions(-) diff --git a/src/task1/task1.ipynb b/src/task1/task1.ipynb index 61839d0..a5632bb 100644 --- a/src/task1/task1.ipynb +++ b/src/task1/task1.ipynb @@ -107,27 +107,16 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# Part 2" + "# Part 2\n", + "\n", + "I have the `mower` as an \"object\" storing its state. (In this case, the mower is a `dict` and the state is just its location and direction.) As each instruction is executed, the mower is updated." ] }, { - "cell_type": "code", - "execution_count": 7, + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "def move(mower, distance):\n", - " if mower['d'] == 0:\n", - " mower['y'] += distance\n", - " elif mower['d'] == 90:\n", - " mower['x'] += distance\n", - " elif mower['d'] == 180:\n", - " mower['y'] -= distance\n", - " elif mower['d'] == 270:\n", - " mower['x'] -= distance\n", - " else:\n", - " raise ValueError\n", - " return mower" + "An initial mower. The initial location and direction don't matter in this case, so I choose a location that's easy and an arbitrary location." ] }, { @@ -141,13 +130,10 @@ ] }, { - "cell_type": "code", - "execution_count": 16, + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "def mowmaster_distance(mw):\n", - " return abs(mw['x']) + abs(mw['y'])" + "Execute the instructions. If it starts `C` or `A`, turn; if it starts `F`, move forward. Ignore all other instructions." ] }, { @@ -159,7 +145,7 @@ "def execute(mowmaster, instructions, debug=False):\n", " for instruction in instructions:\n", " if instruction == 'C':\n", - " mowmaster['d'] = (mowmaster['d'] + 90) % 360\n", + " mowmaster['d'] = (mowmaster['d'] + 90) % 360 # Use the modul\n", " elif instruction == 'A':\n", " mowmaster['d'] = (mowmaster['d'] - 90) % 360\n", " elif instruction.startswith('F'):\n", @@ -169,6 +155,43 @@ " return mowmaster" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The " + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "def move(mower, distance):\n", + " if mower['d'] == 0:\n", + " mower['y'] += distance\n", + " elif mower['d'] == 90:\n", + " mower['x'] += distance\n", + " elif mower['d'] == 180:\n", + " mower['y'] -= distance\n", + " elif mower['d'] == 270:\n", + " mower['x'] -= distance\n", + " else:\n", + " raise ValueError\n", + " return mower" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [], + "source": [ + "def mowmaster_distance(mw):\n", + " return abs(mw['x']) + abs(mw['y'])" + ] + }, { "cell_type": "code", "execution_count": 18, -- 2.34.1