|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Josephus Problem" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "markdown", |
| 12 | + "metadata": {}, |
| 13 | + "source": [ |
| 14 | + "The Josephus Problem is a counting problem, named after a historical event described by Flavius Josephus.\n", |
| 15 | + "In the original form, 42 objects are placed in a circle; and starting from Object #1, every 3d object is removed.\n", |
| 16 | + "We want to preserve the order in which the objects are removed; more specifically, \n", |
| 17 | + "we want to know which objects are the second-to-last and last are removed." |
| 18 | + ] |
| 19 | + }, |
| 20 | + { |
| 21 | + "cell_type": "code", |
| 22 | + "execution_count": null, |
| 23 | + "metadata": {}, |
| 24 | + "outputs": [], |
| 25 | + "source": [ |
| 26 | + "list(range(1,42))" |
| 27 | + ] |
| 28 | + }, |
| 29 | + { |
| 30 | + "cell_type": "code", |
| 31 | + "execution_count": null, |
| 32 | + "metadata": {}, |
| 33 | + "outputs": [], |
| 34 | + "source": [ |
| 35 | + "# pseudo-code\n", |
| 36 | + "\n", |
| 37 | + "# create three lists, one for circle_of_objects, one for removed_objects, and one for the skip\n", |
| 38 | + "circle_of_objects, removed_objects, skip = range(1,42), [], range(3)\n", |
| 39 | + "\n", |
| 40 | + "# while circle_of_objects == True\n", |
| 41 | + "while circle_of_objects:\n", |
| 42 | + "\n", |
| 43 | + " # for i <- skip-list reversed\n", |
| 44 | + " for i in skip[::-1]:\n", |
| 45 | + "\n", |
| 46 | + " # dequeue the next object\n", |
| 47 | + "\n", |
| 48 | + " # the logical structure of the following\n", |
| 49 | + " # if-else clause is not pythonic.\n", |
| 50 | + " # in python, we prefer: if someCollection:\n", |
| 51 | + " # meaning \"True as long as someCollection\n", |
| 52 | + " # contains an item\"\n", |
| 53 | + " # let's refactor to clause.\n", |
| 54 | + " \n", |
| 55 | + " # if i == 0\n", |
| 56 | + "\n", |
| 57 | + " # append next object to removed_objects list\n", |
| 58 | + " \n", |
| 59 | + " # else\n", |
| 60 | + " \n", |
| 61 | + " # append next object to circle_of_objects list\n", |
| 62 | + " \n", |
| 63 | + " \n", |
| 64 | + "# print removed_objects list\n", |
| 65 | + "removed_objects" |
| 66 | + ] |
| 67 | + }, |
| 68 | + { |
| 69 | + "cell_type": "code", |
| 70 | + "execution_count": null, |
| 71 | + "metadata": {}, |
| 72 | + "outputs": [], |
| 73 | + "source": [ |
| 74 | + "for i in skip[::-1]:\n", |
| 75 | + " print(i)" |
| 76 | + ] |
| 77 | + }, |
| 78 | + { |
| 79 | + "cell_type": "code", |
| 80 | + "execution_count": 7, |
| 81 | + "metadata": {}, |
| 82 | + "outputs": [ |
| 83 | + { |
| 84 | + "data": { |
| 85 | + "text/html": [ |
| 86 | + "\n", |
| 87 | + " <iframe\n", |
| 88 | + " width=\"950\"\n", |
| 89 | + " height=\"800\"\n", |
| 90 | + " src=\"https://mathworld.wolfram.com/images/eps-gif/Josephus41-3_1000.gif\"\n", |
| 91 | + " frameborder=\"0\"\n", |
| 92 | + " allowfullscreen\n", |
| 93 | + " ></iframe>\n", |
| 94 | + " " |
| 95 | + ], |
| 96 | + "text/plain": [ |
| 97 | + "<IPython.lib.display.IFrame at 0x7f4f78309a90>" |
| 98 | + ] |
| 99 | + }, |
| 100 | + "execution_count": 7, |
| 101 | + "metadata": {}, |
| 102 | + "output_type": "execute_result" |
| 103 | + } |
| 104 | + ], |
| 105 | + "source": [ |
| 106 | + "from IPython.display import IFrame\n", |
| 107 | + "IFrame('https://mathworld.wolfram.com/images/eps-gif/Josephus41-3_1000.gif', width=950, height=800)" |
| 108 | + ] |
| 109 | + }, |
| 110 | + { |
| 111 | + "cell_type": "code", |
| 112 | + "execution_count": null, |
| 113 | + "metadata": {}, |
| 114 | + "outputs": [], |
| 115 | + "source": [ |
| 116 | + "from IPython.display import IFrame\n", |
| 117 | + "IFrame('https://en.wikipedia.org/wiki/Josephus_problem', width=950, height=800)" |
| 118 | + ] |
| 119 | + } |
| 120 | + ], |
| 121 | + "metadata": { |
| 122 | + "kernelspec": { |
| 123 | + "display_name": "Python 3 (ipykernel)", |
| 124 | + "language": "python", |
| 125 | + "name": "python3" |
| 126 | + }, |
| 127 | + "language_info": { |
| 128 | + "codemirror_mode": { |
| 129 | + "name": "ipython", |
| 130 | + "version": 3 |
| 131 | + }, |
| 132 | + "file_extension": ".py", |
| 133 | + "mimetype": "text/x-python", |
| 134 | + "name": "python", |
| 135 | + "nbconvert_exporter": "python", |
| 136 | + "pygments_lexer": "ipython3", |
| 137 | + "version": "3.12.5" |
| 138 | + } |
| 139 | + }, |
| 140 | + "nbformat": 4, |
| 141 | + "nbformat_minor": 4 |
| 142 | +} |
0 commit comments