Added more tour shape images
[ou-summer-of-code-2017.git] / 06-tour-shapes / tour-creation-for-background.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {},
6 "source": [
7 "Given a sequence of {F|L|R}, each of which is \"move forward one step\", \"turn left, then move forward one step\", \"turn right, then move forward one step\":\n",
8 "1. which tours are closed?\n",
9 "2. what is the area enclosed by the tour?"
10 ]
11 },
12 {
13 "cell_type": "code",
14 "execution_count": 1,
15 "metadata": {
16 "collapsed": true
17 },
18 "outputs": [],
19 "source": [
20 "import collections\n",
21 "import enum\n",
22 "import random\n",
23 "import os\n",
24 "\n",
25 "import matplotlib.pyplot as plt\n",
26 "%matplotlib inline"
27 ]
28 },
29 {
30 "cell_type": "code",
31 "execution_count": 2,
32 "metadata": {
33 "collapsed": true
34 },
35 "outputs": [],
36 "source": [
37 "class Direction(enum.Enum):\n",
38 " UP = 1\n",
39 " RIGHT = 2\n",
40 " DOWN = 3\n",
41 " LEFT = 4\n",
42 " \n",
43 "turn_lefts = {Direction.UP: Direction.LEFT, Direction.LEFT: Direction.DOWN,\n",
44 " Direction.DOWN: Direction.RIGHT, Direction.RIGHT: Direction.UP}\n",
45 "\n",
46 "turn_rights = {Direction.UP: Direction.RIGHT, Direction.RIGHT: Direction.DOWN,\n",
47 " Direction.DOWN: Direction.LEFT, Direction.LEFT: Direction.UP}\n",
48 "\n",
49 "def turn_left(d):\n",
50 " return turn_lefts[d]\n",
51 "\n",
52 "def turn_right(d):\n",
53 " return turn_rights[d]\n"
54 ]
55 },
56 {
57 "cell_type": "code",
58 "execution_count": 3,
59 "metadata": {
60 "collapsed": true
61 },
62 "outputs": [],
63 "source": [
64 "Step = collections.namedtuple('Step', ['x', 'y', 'dir'])\n",
65 "Mistake = collections.namedtuple('Mistake', ['i', 'step'])"
66 ]
67 },
68 {
69 "cell_type": "code",
70 "execution_count": 4,
71 "metadata": {
72 "collapsed": true
73 },
74 "outputs": [],
75 "source": [
76 "def advance(step, d):\n",
77 " if d == Direction.UP:\n",
78 " return Step(step.x, step.y+1, d)\n",
79 " elif d == Direction.DOWN:\n",
80 " return Step(step.x, step.y-1, d)\n",
81 " elif d == Direction.LEFT:\n",
82 " return Step(step.x-1, step.y, d)\n",
83 " elif d == Direction.RIGHT:\n",
84 " return Step(step.x+1, step.y, d)"
85 ]
86 },
87 {
88 "cell_type": "code",
89 "execution_count": 5,
90 "metadata": {
91 "collapsed": true
92 },
93 "outputs": [],
94 "source": [
95 "def trace_tour(tour, startx=0, starty=0, startdir=Direction.RIGHT):\n",
96 " current = Step(startx, starty, startdir)\n",
97 " trace = [current]\n",
98 " for s in tour:\n",
99 " if s == 'F':\n",
100 " current = advance(current, current.dir)\n",
101 " elif s == 'L':\n",
102 " current = advance(current, turn_left(current.dir))\n",
103 " elif s == 'R':\n",
104 " current = advance(current, turn_right(current.dir))\n",
105 " trace += [current]\n",
106 " return trace "
107 ]
108 },
109 {
110 "cell_type": "code",
111 "execution_count": 6,
112 "metadata": {
113 "collapsed": true
114 },
115 "outputs": [],
116 "source": [
117 "def positions(trace):\n",
118 " return [(s.x, s.y) for s in trace]"
119 ]
120 },
121 {
122 "cell_type": "code",
123 "execution_count": 7,
124 "metadata": {
125 "collapsed": true
126 },
127 "outputs": [],
128 "source": [
129 "def valid(trace):\n",
130 " return (trace[-1].x == 0 \n",
131 " and trace[-1].y == 0 \n",
132 " and len(set(positions(trace))) + 1 == len(trace))"
133 ]
134 },
135 {
136 "cell_type": "code",
137 "execution_count": 8,
138 "metadata": {
139 "collapsed": true
140 },
141 "outputs": [],
142 "source": [
143 "def chunks(items, n=2):\n",
144 " return [items[i:i+n] for i in range(len(items) - n + 1)]"
145 ]
146 },
147 {
148 "cell_type": "markdown",
149 "metadata": {},
150 "source": [
151 "Using the [Shoelace formula](https://en.wikipedia.org/wiki/Shoelace_formula)"
152 ]
153 },
154 {
155 "cell_type": "code",
156 "execution_count": 9,
157 "metadata": {
158 "collapsed": true
159 },
160 "outputs": [],
161 "source": [
162 "def shoelace(trace):\n",
163 " return abs(sum(s.x * t.y - t.x * s.y for s, t in chunks(trace, 2))) // 2"
164 ]
165 },
166 {
167 "cell_type": "code",
168 "execution_count": 10,
169 "metadata": {
170 "collapsed": true
171 },
172 "outputs": [],
173 "source": [
174 "def step(s, current):\n",
175 " if s == 'F':\n",
176 " return advance(current, current.dir)\n",
177 " elif s == 'L':\n",
178 " return advance(current, turn_left(current.dir))\n",
179 " elif s == 'R':\n",
180 " return advance(current, turn_right(current.dir))\n",
181 " else:\n",
182 " raise ValueError"
183 ]
184 },
185 {
186 "cell_type": "code",
187 "execution_count": 11,
188 "metadata": {
189 "collapsed": true
190 },
191 "outputs": [],
192 "source": [
193 "def valid_prefix(tour):\n",
194 " current = Step(0, 0, Direction.RIGHT)\n",
195 " prefix = []\n",
196 " posns = []\n",
197 " for s in tour:\n",
198 " current = step(s, current)\n",
199 " prefix += [s]\n",
200 " if (current.x, current.y) in posns:\n",
201 " return ''\n",
202 " elif current.x == 0 and current.y == 0: \n",
203 " return ''.join(prefix)\n",
204 " posns += [(current.x, current.y)]\n",
205 " if current.x == 0 and current.y == 0:\n",
206 " return ''.join(prefix)\n",
207 " else:\n",
208 " return ''"
209 ]
210 },
211 {
212 "cell_type": "code",
213 "execution_count": 12,
214 "metadata": {
215 "collapsed": true
216 },
217 "outputs": [],
218 "source": [
219 "def mistake_positions(trace, debug=False):\n",
220 " mistakes = []\n",
221 " current = trace[0]\n",
222 " posns = [(0, 0)]\n",
223 " for i, current in enumerate(trace[1:]):\n",
224 " if (current.x, current.y) in posns:\n",
225 " if debug: print(i, current)\n",
226 " mistakes += [Mistake(i+1, current)]\n",
227 " posns += [(current.x, current.y)]\n",
228 " if (current.x, current.y) == (0, 0):\n",
229 " return mistakes[:-1]\n",
230 " else:\n",
231 " return mistakes + [Mistake(len(trace)+1, current)]"
232 ]
233 },
234 {
235 "cell_type": "code",
236 "execution_count": 13,
237 "metadata": {
238 "collapsed": true
239 },
240 "outputs": [],
241 "source": [
242 "def returns_to_origin(mistake_positions):\n",
243 " return [i for i, m in mistake_positions\n",
244 " if (m.x, m.y) == (0, 0)]"
245 ]
246 },
247 {
248 "cell_type": "code",
249 "execution_count": 14,
250 "metadata": {
251 "collapsed": true
252 },
253 "outputs": [],
254 "source": [
255 "def random_walk(steps=1000):\n",
256 " return ''.join(random.choice('FFLR') for _ in range(steps))"
257 ]
258 },
259 {
260 "cell_type": "code",
261 "execution_count": 15,
262 "metadata": {
263 "collapsed": true
264 },
265 "outputs": [],
266 "source": [
267 "def bounds(trace):\n",
268 " return (max(s.x for s in trace),\n",
269 " max(s.y for s in trace),\n",
270 " min(s.x for s in trace),\n",
271 " min(s.y for s in trace))"
272 ]
273 },
274 {
275 "cell_type": "code",
276 "execution_count": 1,
277 "metadata": {},
278 "outputs": [
279 {
280 "ename": "NameError",
281 "evalue": "name 'Direction' is not defined",
282 "output_type": "error",
283 "traceback": [
284 "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
285 "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
286 "\u001b[0;32m<ipython-input-1-f5ea591d6161>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m plot_wh = {Direction.UP: (0, 1), Direction.LEFT: (-1, 0),\n\u001b[0m\u001b[1;32m 2\u001b[0m Direction.DOWN: (0, -1), Direction.RIGHT: (1, 0)}\n",
287 "\u001b[0;31mNameError\u001b[0m: name 'Direction' is not defined"
288 ]
289 }
290 ],
291 "source": [
292 "plot_wh = {Direction.UP: (0, 1), Direction.LEFT: (-1, 0),\n",
293 " Direction.DOWN: (0, -1), Direction.RIGHT: (1, 0)}"
294 ]
295 },
296 {
297 "cell_type": "code",
298 "execution_count": 16,
299 "metadata": {
300 "collapsed": true
301 },
302 "outputs": [],
303 "source": [
304 "def plot_trace(trace, colour='k', xybounds=None, fig=None, subplot_details=None, filename=None):\n",
305 " plt.axis('on')\n",
306 " plt.axes().set_aspect('equal')\n",
307 " for s, t in chunks(trace, 2):\n",
308 " w, h = plot_wh[t.dir]\n",
309 " plt.arrow(s.x, s.y, w, h, head_width=0.1, head_length=0.1, fc=colour, ec=colour, length_includes_head=True)\n",
310 " xh, yh, xl, yl = bounds(trace)\n",
311 " if xybounds is not None: \n",
312 " bxh, byh, bxl, byl = xybounds\n",
313 " plt.xlim([min(xl, bxl)-1, max(xh, bxh)+1])\n",
314 " plt.ylim([min(yl, byl)-1, max(yh, byh)+1])\n",
315 " else:\n",
316 " plt.xlim([xl-1, xh+1])\n",
317 " plt.ylim([yl-1, yh+1])\n",
318 " if filename:\n",
319 " plt.savefig(filename)"
320 ]
321 },
322 {
323 "cell_type": "code",
324 "execution_count": 17,
325 "metadata": {
326 "collapsed": true
327 },
328 "outputs": [],
329 "source": [
330 "def trim_loop(tour):\n",
331 " trace = trace_tour(tour)\n",
332 " mistakes = mistake_positions(trace)\n",
333 " end_mistake_index = 0\n",
334 "# print('end_mistake_index {} pointing to trace position {}; {} mistakes and {} in trace; {}'.format(end_mistake_index, mistakes[end_mistake_index].i, len(mistakes), len(trace), mistakes))\n",
335 " # while this mistake extends to the next step in the trace...\n",
336 " while (mistakes[end_mistake_index].i + 1 < len(trace) and \n",
337 " end_mistake_index + 1 < len(mistakes) and\n",
338 " mistakes[end_mistake_index].i + 1 == \n",
339 " mistakes[end_mistake_index + 1].i):\n",
340 "# print('end_mistake_index {} pointing to trace position {}; {} mistakes and {} in trace'.format(end_mistake_index, mistakes[end_mistake_index].i, len(mistakes), len(trace), mistakes))\n",
341 " # push this mistake finish point later\n",
342 " end_mistake_index += 1\n",
343 " mistake = mistakes[end_mistake_index]\n",
344 " \n",
345 " # find the first location that mentions where this mistake ends (which the point where the loop starts)\n",
346 " mistake_loop_start = max(i for i, loc in enumerate(trace[:mistake.i])\n",
347 " if (loc.x, loc.y) == (mistake.step.x, mistake.step.y))\n",
348 "# print('Dealing with mistake from', mistake_loop_start, 'to', mistake.i, ', trace has len', len(trace))\n",
349 " \n",
350 " # direction before entering the loop\n",
351 " direction_before = trace[mistake_loop_start].dir\n",
352 " \n",
353 " # find the new instruction to turn from heading before the loop to heading after the loop\n",
354 " new_instruction = 'F'\n",
355 " if (mistake.i + 1) < len(trace):\n",
356 " if turn_left(direction_before) == trace[mistake.i + 1].dir:\n",
357 " new_instruction = 'L'\n",
358 " if turn_right(direction_before) == trace[mistake.i + 1].dir:\n",
359 " new_instruction = 'R'\n",
360 "# if (mistake.i + 1) < len(trace):\n",
361 "# print('turning from', direction_before, 'to', trace[mistake.i + 1].dir, 'with', new_instruction )\n",
362 "# else:\n",
363 "# print('turning from', direction_before, 'to BEYOND END', 'with', new_instruction )\n",
364 " return tour[:mistake_loop_start] + new_instruction + tour[mistake.i+1:]\n",
365 "# return mistake, mistake_loop_start, trace[mistake_loop_start-2:mistake_loop_start+8]"
366 ]
367 },
368 {
369 "cell_type": "code",
370 "execution_count": 18,
371 "metadata": {
372 "collapsed": true
373 },
374 "outputs": [],
375 "source": [
376 "def trim_all_loops(tour, mistake_reduction_attempt_limit=10):\n",
377 " trace = trace_tour(tour)\n",
378 " mistake_limit = 1\n",
379 " if trace[-1].x == 0 and trace[-1].y == 0:\n",
380 " mistake_limit = 0\n",
381 " mistakes = mistake_positions(trace)\n",
382 " \n",
383 " old_mistake_count = len(mistakes)\n",
384 " mistake_reduction_tries = 0\n",
385 " \n",
386 " while len(mistakes) > mistake_limit and mistake_reduction_tries < mistake_reduction_attempt_limit:\n",
387 " tour = trim_loop(tour)\n",
388 " trace = trace_tour(tour)\n",
389 " mistakes = mistake_positions(trace)\n",
390 " if len(mistakes) < old_mistake_count:\n",
391 " old_mistake_count = len(mistakes)\n",
392 " mistake_reduction_tries = 0\n",
393 " else:\n",
394 " mistake_reduction_tries += 1\n",
395 " if mistake_reduction_tries >= mistake_reduction_attempt_limit:\n",
396 " return ''\n",
397 " else:\n",
398 " return tour"
399 ]
400 },
401 {
402 "cell_type": "code",
403 "execution_count": 19,
404 "metadata": {
405 "collapsed": true
406 },
407 "outputs": [],
408 "source": [
409 "def reverse_tour(tour):\n",
410 " def swap(tour_step):\n",
411 " if tour_step == 'R':\n",
412 " return 'L'\n",
413 " elif tour_step == 'L':\n",
414 " return 'R'\n",
415 " else:\n",
416 " return tour_step\n",
417 " \n",
418 " return ''.join(swap(s) for s in reversed(tour))"
419 ]
420 },
421 {
422 "cell_type": "code",
423 "execution_count": 20,
424 "metadata": {
425 "collapsed": true
426 },
427 "outputs": [],
428 "source": [
429 "def wander_near(locus, current, limit=10):\n",
430 " valid_proposal = False\n",
431 " while not valid_proposal:\n",
432 " s = random.choice('FFFRL')\n",
433 " if s == 'F':\n",
434 " proposed = advance(current, current.dir)\n",
435 " elif s == 'L':\n",
436 " proposed = advance(current, turn_left(current.dir))\n",
437 " elif s == 'R':\n",
438 " proposed = advance(current, turn_right(current.dir))\n",
439 " if abs(proposed.x - locus.x) < limit and abs(proposed.y - locus.y) < limit:\n",
440 " valid_proposal = True\n",
441 "# print('At {} going to {} by step {} to {}'.format(current, locus, s, proposed))\n",
442 " return s, proposed"
443 ]
444 },
445 {
446 "cell_type": "code",
447 "execution_count": 21,
448 "metadata": {
449 "collapsed": true
450 },
451 "outputs": [],
452 "source": [
453 "def seek(goal, current):\n",
454 " dx = current.x - goal.x\n",
455 " dy = current.y - goal.y\n",
456 "\n",
457 " if dx < 0 and abs(dx) > abs(dy): # to the left\n",
458 " side = 'left'\n",
459 " if current.dir == Direction.RIGHT:\n",
460 " s = 'F'\n",
461 " elif current.dir == Direction.UP:\n",
462 " s = 'R'\n",
463 " else:\n",
464 " s = 'L'\n",
465 " elif dx > 0 and abs(dx) > abs(dy): # to the right\n",
466 " side = 'right'\n",
467 " if current.dir == Direction.LEFT:\n",
468 " s = 'F'\n",
469 " elif current.dir == Direction.UP:\n",
470 " s = 'L'\n",
471 " else:\n",
472 " s = 'R'\n",
473 " elif dy > 0 and abs(dx) <= abs(dy): # above\n",
474 " side = 'above'\n",
475 " if current.dir == Direction.DOWN:\n",
476 " s = 'F'\n",
477 " elif current.dir == Direction.RIGHT:\n",
478 " s = 'R'\n",
479 " else:\n",
480 " s = 'L'\n",
481 " else: # below\n",
482 " side = 'below'\n",
483 " if current.dir == Direction.UP:\n",
484 " s = 'F'\n",
485 " elif current.dir == Direction.LEFT:\n",
486 " s = 'R'\n",
487 " else:\n",
488 " s = 'L'\n",
489 " if s == 'F':\n",
490 " proposed = advance(current, current.dir)\n",
491 " elif s == 'L':\n",
492 " proposed = advance(current, turn_left(current.dir))\n",
493 " elif s == 'R':\n",
494 " proposed = advance(current, turn_right(current.dir))\n",
495 " \n",
496 "# print('At {} going to {}, currently {}, by step {} to {}'.format(current, goal, side, s, proposed))\n",
497 "\n",
498 " return s, proposed"
499 ]
500 },
501 {
502 "cell_type": "code",
503 "execution_count": 22,
504 "metadata": {
505 "collapsed": true
506 },
507 "outputs": [],
508 "source": [
509 "def guided_walk(loci, locus_limit=5, wander_limit=10, seek_step_limit=20):\n",
510 " trail = ''\n",
511 " current = Step(0, 0, Direction.RIGHT) \n",
512 " l = 0\n",
513 " finished = False\n",
514 " while not finished:\n",
515 " if abs(current.x - loci[l].x) < locus_limit and abs(current.y - loci[l].y) < locus_limit:\n",
516 " l += 1\n",
517 " if l == len(loci) - 1:\n",
518 " finished = True\n",
519 " s, proposed = wander_near(loci[l], current, limit=wander_limit)\n",
520 " trail += s\n",
521 " current = proposed\n",
522 "# print('!! Finished loci')\n",
523 " seek_steps = 0\n",
524 " while not (current.x == loci[l].x and current.y == loci[l].y) and seek_steps < seek_step_limit:\n",
525 "# error = max(abs(current.x - loci[l].x), abs(current.y - loci[l].y))\n",
526 "# s, proposed = wander_near(loci[l], current, limit=error+1)\n",
527 " s, proposed = seek(loci[l], current)\n",
528 " trail += s\n",
529 " current = proposed\n",
530 " seek_steps += 1\n",
531 " if seek_steps >= seek_step_limit:\n",
532 " return ''\n",
533 " else:\n",
534 " return trail"
535 ]
536 },
537 {
538 "cell_type": "code",
539 "execution_count": 24,
540 "metadata": {
541 "collapsed": true
542 },
543 "outputs": [],
544 "source": [
545 "def square_tour(a=80):\n",
546 " \"a is width of square\"\n",
547 " return ('F' * a + 'L') * 4"
548 ]
549 },
550 {
551 "cell_type": "code",
552 "execution_count": 25,
553 "metadata": {
554 "collapsed": true
555 },
556 "outputs": [],
557 "source": [
558 "def cross_tour(a=50, b=40):\n",
559 " \"a is width of cross arm, b is length of cross arm\"\n",
560 " return ('F' * a + 'L' + 'F' * b + 'R' + 'F' * b + 'L') * 4"
561 ]
562 },
563 {
564 "cell_type": "code",
565 "execution_count": 26,
566 "metadata": {
567 "collapsed": true
568 },
569 "outputs": [],
570 "source": [
571 "def quincunx_tour(a=60, b=30, c=50):\n",
572 " \"a is length of indent, b is indent/outdent distance, c is outdent outer length\"\n",
573 " return ('F' * a + 'R' + 'F' * b + 'L' + 'F' * c + 'L' + 'F' * c + 'L' + 'F' * b + 'R') * 4\n"
574 ]
575 },
576 {
577 "cell_type": "code",
578 "execution_count": 27,
579 "metadata": {
580 "collapsed": true
581 },
582 "outputs": [],
583 "source": [
584 "heart_points = [Step(60, 50, Direction.UP), Step(50, 90, Direction.UP),\n",
585 " Step(20, 70, Direction.UP), \n",
586 " Step(-40, 90, Direction.UP), Step(-60, 80, Direction.UP), \n",
587 " Step(0, 0, Direction.RIGHT)]\n",
588 "\n",
589 "heart_tour = ''\n",
590 "current = Step(0, 0, Direction.RIGHT)\n",
591 "\n",
592 "for hp in heart_points:\n",
593 " while not (current.x == hp.x and current.y == hp.y):\n",
594 " s, proposed = seek(hp, current)\n",
595 " heart_tour += s\n",
596 " current = proposed\n",
597 "\n",
598 "def heart_tour_func(): return heart_tour"
599 ]
600 },
601 {
602 "cell_type": "code",
603 "execution_count": 28,
604 "metadata": {
605 "collapsed": true
606 },
607 "outputs": [],
608 "source": [
609 "# success_count = 0\n",
610 "# while success_count <= 20:\n",
611 "# lc = trace_tour(square_tour(a=10))\n",
612 "# rw = guided_walk(lc, wander_limit=4, locus_limit=2)\n",
613 "# if rw:\n",
614 "# rw_trimmed = trim_all_loops(rw)\n",
615 "# if len(rw_trimmed) > 10:\n",
616 "# with open('small-squares.txt', 'a') as f:\n",
617 "# f.write(rw_trimmed + '\\n')\n",
618 "# success_count += 1"
619 ]
620 },
621 {
622 "cell_type": "code",
623 "execution_count": 29,
624 "metadata": {
625 "collapsed": true
626 },
627 "outputs": [],
628 "source": [
629 "# success_count = 0\n",
630 "# while success_count <= 20:\n",
631 "# lc = trace_tour(square_tour())\n",
632 "# rw = guided_walk(lc)\n",
633 "# if rw:\n",
634 "# rw_trimmed = trim_all_loops(rw)\n",
635 "# if len(rw_trimmed) > 10:\n",
636 "# with open('large-squares.txt', 'a') as f:\n",
637 "# f.write(rw_trimmed + '\\n')\n",
638 "# success_count += 1"
639 ]
640 },
641 {
642 "cell_type": "code",
643 "execution_count": 30,
644 "metadata": {
645 "collapsed": true
646 },
647 "outputs": [],
648 "source": [
649 "# success_count = 0\n",
650 "# while success_count <= 20:\n",
651 "# lc = trace_tour(cross_tour())\n",
652 "# rw = guided_walk(lc)\n",
653 "# if rw:\n",
654 "# rw_trimmed = trim_all_loops(rw)\n",
655 "# if len(rw_trimmed) > 10:\n",
656 "# with open('cross.txt', 'a') as f:\n",
657 "# f.write(rw_trimmed + '\\n')\n",
658 "# success_count += 1"
659 ]
660 },
661 {
662 "cell_type": "code",
663 "execution_count": 31,
664 "metadata": {
665 "collapsed": true
666 },
667 "outputs": [],
668 "source": [
669 "# success_count = 0\n",
670 "# while success_count <= 20:\n",
671 "# lc = trace_tour(quincunx_tour())\n",
672 "# rw = guided_walk(lc)\n",
673 "# if rw:\n",
674 "# rw_trimmed = trim_all_loops(rw)\n",
675 "# if len(rw_trimmed) > 10:\n",
676 "# with open('quincunx.txt', 'a') as f:\n",
677 "# f.write(rw_trimmed + '\\n')\n",
678 "# success_count += 1"
679 ]
680 },
681 {
682 "cell_type": "code",
683 "execution_count": 32,
684 "metadata": {
685 "collapsed": true
686 },
687 "outputs": [],
688 "source": [
689 "patterns = [square_tour, cross_tour, quincunx_tour, heart_tour_func]\n",
690 "tours_filename = 'tours.txt'\n",
691 "\n",
692 "try:\n",
693 " os.remove(tours_filename)\n",
694 "except OSError:\n",
695 " pass\n",
696 "\n",
697 "success_count = 0\n",
698 "while success_count < 100:\n",
699 " lc = trace_tour(random.choice(patterns)())\n",
700 " rw = guided_walk(lc)\n",
701 " if rw:\n",
702 " rw_trimmed = trim_all_loops(rw)\n",
703 " if len(rw_trimmed) > 10:\n",
704 " with open(tours_filename, 'a') as f:\n",
705 " f.write(rw_trimmed + '\\n')\n",
706 " success_count += 1"
707 ]
708 },
709 {
710 "cell_type": "code",
711 "execution_count": null,
712 "metadata": {
713 "collapsed": true
714 },
715 "outputs": [],
716 "source": []
717 }
718 ],
719 "metadata": {
720 "kernelspec": {
721 "display_name": "Python 3",
722 "language": "python",
723 "name": "python3"
724 },
725 "language_info": {
726 "codemirror_mode": {
727 "name": "ipython",
728 "version": 3
729 },
730 "file_extension": ".py",
731 "mimetype": "text/x-python",
732 "name": "python",
733 "nbconvert_exporter": "python",
734 "pygments_lexer": "ipython3",
735 "version": "3.5.2+"
736 }
737 },
738 "nbformat": 4,
739 "nbformat_minor": 2
740 }