Added Python notes
authorNeil Smith <neil.git@njae.me.uk>
Mon, 17 Sep 2018 18:04:55 +0000 (19:04 +0100)
committerNeil Smith <neil.git@njae.me.uk>
Mon, 17 Sep 2018 18:04:55 +0000 (19:04 +0100)
src/task1/task1.ipynb

index 61839d09131ddc34e049cd6b386f997b054226c2..a5632bb9b9a038d9720a2c98a02e8bc88e196870 100644 (file)
    "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."
    ]
   },
   {
    ]
   },
   {
-   "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."
    ]
   },
   {
     "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",
     "    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,