|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "0738d385-d3ac-4240-a9c8-404501ec320e", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Requirements" |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "code", |
| 13 | + "execution_count": 47, |
| 14 | + "id": "90e02852-f5a9-4f2b-ade4-6381350e59c1", |
| 15 | + "metadata": {}, |
| 16 | + "outputs": [], |
| 17 | + "source": [ |
| 18 | + "import matplotlib.pyplot as plt\n", |
| 19 | + "import re" |
| 20 | + ] |
| 21 | + }, |
| 22 | + { |
| 23 | + "cell_type": "markdown", |
| 24 | + "id": "dbba3d84-6d7b-47dc-b3f7-f9e3a934bcbf", |
| 25 | + "metadata": {}, |
| 26 | + "source": [ |
| 27 | + "# Problem" |
| 28 | + ] |
| 29 | + }, |
| 30 | + { |
| 31 | + "cell_type": "markdown", |
| 32 | + "id": "11156389-33b0-4c13-aab6-b6eb5ea6f2ac", |
| 33 | + "metadata": {}, |
| 34 | + "source": [ |
| 35 | + "The aim is to generate Conway's look-and-say sequence:\n", |
| 36 | + "\n", |
| 37 | + " * 1\n", |
| 38 | + " * 11\n", |
| 39 | + " * 21\n", |
| 40 | + " * 1211\n", |
| 41 | + " * 111221\n", |
| 42 | + " * 312211\n", |
| 43 | + " * 13112221\n", |
| 44 | + " * 1113213211\n", |
| 45 | + " * 31131211131221\n", |
| 46 | + " * ...\n", |
| 47 | + "\n", |
| 48 | + "The sequence is generated by looking at the string, determining the length of sequences of identical characters, and replacing those by the count and the character." |
| 49 | + ] |
| 50 | + }, |
| 51 | + { |
| 52 | + "cell_type": "markdown", |
| 53 | + "id": "13fc6c45-2eab-4f99-a0e1-c0399b2c27af", |
| 54 | + "metadata": {}, |
| 55 | + "source": [ |
| 56 | + "# Implementation" |
| 57 | + ] |
| 58 | + }, |
| 59 | + { |
| 60 | + "cell_type": "markdown", |
| 61 | + "id": "55166a40-75a1-4542-8d24-4a3151142b81", |
| 62 | + "metadata": {}, |
| 63 | + "source": [ |
| 64 | + "It is easy to split a string into slices that consist of one or more of the same characters using regular expressions. The first part of the expression matches a digit and stores the match for future use (`(\\d)`). The second part matches zero or more occurences of the digit that was just matched, and stores it as well (`(\\1*)`). The resultting sequence of tuples contain a digit, followed by the remainder of the sequence, so the next string in Conway's look-and-say sequence is easy to construct." |
| 65 | + ] |
| 66 | + }, |
| 67 | + { |
| 68 | + "cell_type": "code", |
| 69 | + "execution_count": 33, |
| 70 | + "id": "0036b458-9e3a-47f5-aefb-0dd0bb0c6a79", |
| 71 | + "metadata": {}, |
| 72 | + "outputs": [], |
| 73 | + "source": [ |
| 74 | + "def look_say(sequence):\n", |
| 75 | + " new_sequance = ''\n", |
| 76 | + " for match in re.findall(r'(\\d)(\\1*)', sequence):\n", |
| 77 | + " new_sequance += f'{str(1 + len(match[1]))}{match[0]}'\n", |
| 78 | + " return new_sequance" |
| 79 | + ] |
| 80 | + }, |
| 81 | + { |
| 82 | + "cell_type": "markdown", |
| 83 | + "id": "8805d91c-2e6c-4bc5-b1e0-e5fb8574b138", |
| 84 | + "metadata": {}, |
| 85 | + "source": [ |
| 86 | + "For convenience, a generator is defined to generate the sequence up to any number of elements." |
| 87 | + ] |
| 88 | + }, |
| 89 | + { |
| 90 | + "cell_type": "code", |
| 91 | + "execution_count": 38, |
| 92 | + "id": "2d957a66-2464-4fbd-bf89-47944d01828b", |
| 93 | + "metadata": {}, |
| 94 | + "outputs": [], |
| 95 | + "source": [ |
| 96 | + "def look_and_say_sequence():\n", |
| 97 | + " s = '1'\n", |
| 98 | + " yield s\n", |
| 99 | + " while True:\n", |
| 100 | + " s = look_say(s)\n", |
| 101 | + " yield s" |
| 102 | + ] |
| 103 | + }, |
| 104 | + { |
| 105 | + "cell_type": "code", |
| 106 | + "execution_count": 46, |
| 107 | + "id": "b1033bcb-d1e4-4f66-970e-759516325233", |
| 108 | + "metadata": {}, |
| 109 | + "outputs": [ |
| 110 | + { |
| 111 | + "name": "stdout", |
| 112 | + "output_type": "stream", |
| 113 | + "text": [ |
| 114 | + "1\n", |
| 115 | + "11\n", |
| 116 | + "21\n", |
| 117 | + "1211\n", |
| 118 | + "111221\n", |
| 119 | + "312211\n", |
| 120 | + "13112221\n", |
| 121 | + "1113213211\n", |
| 122 | + "31131211131221\n", |
| 123 | + "13211311123113112211\n" |
| 124 | + ] |
| 125 | + } |
| 126 | + ], |
| 127 | + "source": [ |
| 128 | + "seq = look_and_say_sequence()\n", |
| 129 | + "for _ in range(10):\n", |
| 130 | + " print(next(seq))" |
| 131 | + ] |
| 132 | + }, |
| 133 | + { |
| 134 | + "cell_type": "markdown", |
| 135 | + "id": "03ee3ba4-3e64-4017-a701-827c5499cef8", |
| 136 | + "metadata": {}, |
| 137 | + "source": [ |
| 138 | + "We can check how many digits the sequence has for the first 200 elements in the sequence." |
| 139 | + ] |
| 140 | + }, |
| 141 | + { |
| 142 | + "cell_type": "code", |
| 143 | + "execution_count": null, |
| 144 | + "id": "33d9f2f0-50ad-4592-8318-1e80e51941ab", |
| 145 | + "metadata": {}, |
| 146 | + "outputs": [], |
| 147 | + "source": [ |
| 148 | + "nr_elements = 50" |
| 149 | + ] |
| 150 | + }, |
| 151 | + { |
| 152 | + "cell_type": "code", |
| 153 | + "execution_count": null, |
| 154 | + "id": "835dab2b-f877-4673-9f90-221eefc91b29", |
| 155 | + "metadata": {}, |
| 156 | + "outputs": [], |
| 157 | + "source": [ |
| 158 | + "lengths = []\n", |
| 159 | + "seq = look_and_say_sequence()\n", |
| 160 | + "for _ in rangenr_elements):\n", |
| 161 | + " lengths.append(len(next(seq)))" |
| 162 | + ] |
| 163 | + }, |
| 164 | + { |
| 165 | + "cell_type": "code", |
| 166 | + "execution_count": null, |
| 167 | + "id": "3d812c68-27e7-4a55-a89f-3a117ccab9df", |
| 168 | + "metadata": {}, |
| 169 | + "outputs": [], |
| 170 | + "source": [ |
| 171 | + "plt.plot(list(range(1, 201)), lengths)" |
| 172 | + ] |
| 173 | + }, |
| 174 | + { |
| 175 | + "cell_type": "code", |
| 176 | + "execution_count": null, |
| 177 | + "id": "1e22377c-d2d7-4abd-8b94-259956416be1", |
| 178 | + "metadata": {}, |
| 179 | + "outputs": [], |
| 180 | + "source": [] |
| 181 | + } |
| 182 | + ], |
| 183 | + "metadata": { |
| 184 | + "kernelspec": { |
| 185 | + "display_name": "Python 3 (ipykernel)", |
| 186 | + "language": "python", |
| 187 | + "name": "python3" |
| 188 | + }, |
| 189 | + "language_info": { |
| 190 | + "codemirror_mode": { |
| 191 | + "name": "ipython", |
| 192 | + "version": 3 |
| 193 | + }, |
| 194 | + "file_extension": ".py", |
| 195 | + "mimetype": "text/x-python", |
| 196 | + "name": "python", |
| 197 | + "nbconvert_exporter": "python", |
| 198 | + "pygments_lexer": "ipython3", |
| 199 | + "version": "3.12.3" |
| 200 | + } |
| 201 | + }, |
| 202 | + "nbformat": 4, |
| 203 | + "nbformat_minor": 5 |
| 204 | +} |
0 commit comments