18 "def find_comment(text):\n",
19 " start = text.find('<')\n",
21 " end = text.find('>', start + 1)\n",
23 " return start, end\n",
33 "def decomment_no_re(text):\n",
34 " s, e = find_comment(text)\n",
35 " while s is not None:\n",
36 " text = text[:s] + text[e+1:]\n",
37 " s, e = find_comment(text)\n",
47 "def find_expansion_mark(text):\n",
48 " start = text.find(':')\n",
50 " mid = text.find(':', start + 1)\n",
52 " end = text.find(':', mid + 1)\n",
54 " length = int(text[start+1:mid])\n",
55 " times = int(text[mid+1:end])\n",
56 " if length > start:\n",
58 " return start, end, length, times\n",
59 " return None, None, None, None"
68 "def expand_no_re(text, expansion_limit=None):\n",
70 " s, e, l, n = find_expansion_mark(text)\n",
71 " while s and (expansion_limit is None or (expansion_limit is not None and i <= expansion_limit)):\n",
72 " text = text[:s-l] + text[s-l:s] * n + text[e+1:]\n",
73 " s, e, l, n = find_expansion_mark(text)\n",
84 "data_p_t = [l.strip() for l in open('../../data/05-instructions.txt')]"
93 "data_p = [decomment_no_re(l) for l in data_p_t]\n",
94 "data = [expand_no_re(l) for l in data_p]"
108 "execution_count": 8,
110 "output_type": "execute_result"
114 "len([c for c in ''.join(data_p) if c not in string.whitespace])"
119 "execution_count": 9,
128 "execution_count": 9,
130 "output_type": "execute_result"
134 "len([c for c in ''.join(data) if c not in string.whitespace])"
139 "execution_count": 10,
143 "data_s_p_t = open('../../data/05-instructions.txt').read()\n",
144 "data_s_p = decomment_no_re(data_s_p_t)\n",
145 "data_s = expand_no_re(data_s_p)"
150 "execution_count": 11,
159 "execution_count": 11,
161 "output_type": "execute_result"
165 "sum(1 for c in data_s_p if c not in string.whitespace)"
170 "execution_count": 12,
179 "execution_count": 12,
181 "output_type": "execute_result"
185 "sum(1 for c in data_s if c not in string.whitespace)"
190 "execution_count": 13,
195 "output_type": "stream",
197 "23.7 ms ± 1.79 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
203 "data_p = [decomment_no_re(l) for l in data_p_t]\n",
204 "data = [expand_no_re(l) for l in data_p]"
209 "execution_count": 14,
214 "output_type": "stream",
216 "360 ms ± 6.67 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
222 "data_s_p = decomment_no_re(data_s_p_t)\n",
223 "data_s = expand_no_re(data_s_p)"
228 "execution_count": null,
236 "display_name": "Python 3",
237 "language": "python",
245 "file_extension": ".py",
246 "mimetype": "text/x-python",
248 "nbconvert_exporter": "python",
249 "pygments_lexer": "ipython3",