-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactorial.html
More file actions
540 lines (485 loc) · 17.6 KB
/
factorial.html
File metadata and controls
540 lines (485 loc) · 17.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
<html>
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"
integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"
integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo"
crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"
integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI"
crossorigin="anonymous"></script>
<style>
body {
padding: 5px 10px;
}
.flex-container {
display: grid;
grid-template-rows: 425px 425px;
grid-template-columns: 40% 60%;
}
.code-div {
grid-row: 1;
grid-column: 1;
}
.visualization-div {
grid-row: 1;
grid-column: 2;
}
.stack-div {
grid-row: 2;
grid-column: 1;
}
.tree-div {
grid-row: 2;
grid-column: 2;
}
.button-span {
float: right;
}
.highlight {
background-color: yellow;
}
.call-stack-grid-container {
display: grid;
grid-template-columns: 100%;
grid-template-rows: 38px 38px 38px 38px 38px 38px 38px 38px 38px 38px;
width: 70%;
border: 1px solid black;
margin-left: auto;
margin-right: auto;
text-align: center;
font-size: 30px;
}
.call-stack-item {
border-top: 1px solid black;
}
.tree-canvas {
display: block;
}
</style>
</head>
<body>
<h1>
Factorial
<span class="button-span">
<div class="btn-group">
<button class="btn btn-info" disabled="true">Speed:</button>
<div class="btn-group btn-group-toggle" data-toggle="buttons">
<label class="btn btn-info active">
<input type="radio" name="speed" id="option1" value="1" checked> 1x
</label>
<label class="btn btn-info">
<input type="radio" name="speed" id="option2" value="2"> 2x
</label>
<label class="btn btn-info">
<input type="radio" name="speed" id="option3" value="4"> 4x
</label>
<label class="btn btn-info">
<input type="radio" name="speed" id="option4" value="8"> 8x
</label>
</div>
</div>
<button id="play_button" class="btn btn-success">
<svg class="bi bi-play-fill" width="1em" height="1em" viewBox="0 0 16 16" fill="currentColor"
xmlns="http://www.w3.org/2000/svg">
<path
d="M11.596 8.697l-6.363 3.692c-.54.313-1.233-.066-1.233-.697V4.308c0-.63.692-1.01 1.233-.696l6.363 3.692a.802.802 0 0 1 0 1.393z" />
</svg>
Play
</button>
<button id="pause_button" class="btn btn-warning" style="display: none;">
<svg class="bi bi-pause-fill" width="1em" height="1em" viewBox="0 0 16 16" fill="currentColor"
xmlns="http://www.w3.org/2000/svg">
<path
d="M5.5 3.5A1.5 1.5 0 0 1 7 5v6a1.5 1.5 0 0 1-3 0V5a1.5 1.5 0 0 1 1.5-1.5zm5 0A1.5 1.5 0 0 1 12 5v6a1.5 1.5 0 0 1-3 0V5a1.5 1.5 0 0 1 1.5-1.5z" />
</svg>
Pause
</button>
<button id="reset_button" class="btn btn-danger">
<svg class="bi bi-arrow-clockwise" width="1em" height="1em" viewBox="0 0 16 16" fill="currentColor"
xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd"
d="M3.17 6.706a5 5 0 0 1 7.103-3.16.5.5 0 1 0 .454-.892A6 6 0 1 0 13.455 5.5a.5.5 0 0 0-.91.417 5 5 0 1 1-9.375.789z" />
<path fill-rule="evenodd"
d="M8.147.146a.5.5 0 0 1 .707 0l2.5 2.5a.5.5 0 0 1 0 .708l-2.5 2.5a.5.5 0 1 1-.707-.708L10.293 3 8.147.854a.5.5 0 0 1 0-.708z" />
</svg>
Reset
</button>
<button id="backward_button" class="btn btn-primary">
<svg class="bi bi-chevron-left" width="1em" height="1em" viewBox="0 0 16 16" fill="currentColor"
xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd"
d="M11.354 1.646a.5.5 0 0 1 0 .708L5.707 8l5.647 5.646a.5.5 0 0 1-.708.708l-6-6a.5.5 0 0 1 0-.708l6-6a.5.5 0 0 1 .708 0z" />
</svg>
Step Backward
</button>
<button id="forward_button" class="btn btn-primary">
Step Forward
<svg class="bi bi-chevron-right" width="1em" height="1em" viewBox="0 0 16 16" fill="currentColor"
xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd"
d="M4.646 1.646a.5.5 0 0 1 .708 0l6 6a.5.5 0 0 1 0 .708l-6 6a.5.5 0 0 1-.708-.708L10.293 8 4.646 2.354a.5.5 0 0 1 0-.708z" />
</svg>
</button>
</span>
</h1>
<div class="flex-container">
<div class="code-div">
<h2>Code</h2>
<pre style="font-size: 12px;">
# Main entry for program
<span id="main">def main(*args):</span>
<span id="solve_init">solve(5)</span>
<!-- <span id="main_return">}</span> -->
# Main entry point to solve
<span id="solve">def solve(number):</span>
<span id="base_case_if">if number == 1:</span>
<span id="base_case_return">return number</span>
<span id="recursive">return number * solve(number - 1)</span>
# Prevent running on import.
if __name__ == "__main__":
main(*sys.argv[1:])
</pre>
</div>
<div class="visualization-div">
<h2>Return Code</h2>
<div>
<h1 id="factorial_return"> </h1>
</div>
<h2>Value</h2>
<div>
<h1 id="factorial_value"></h1>
</div>
</div>
<div class="stack-div">
<h2>Call Stack</h2>
<div id="call_stack_grid_container" class="call-stack-grid-container">
<div class="call-stack-item call-stack-main-item" style="grid-row: 10; grid-column: 1;">main</div>
<div class="call-stack-item call-stack-main-item" style="grid-row: 9; grid-column: 1;">solve</div>
</div>
</div>
<div class="tree-div">
<h2>Call Tree</h2>
<canvas id="tree_canvas" class="tree-canvas" height="425" width="950"></canvas>
</div>
</div>
<script>
$(document).ready(function () {
var tree_canvas, tree_ctx, play_timeouts;
var code_steps, tree_code, current_step, processed_step;
var clear_everything, play_all_steps, draw, is_null;
var create_code_steps, get_current_tree_step_stacks, get_call_stack_div;
is_null = function (value) {
return (typeof value === 'undefined' || value === null);
}
tree_canvas = document.getElementById('tree_canvas');
tree_ctx = tree_canvas.getContext('2d');
tree_ctx.font = '14px courier';
output_values = [
'0',
'1',
'2',
'6',
'24',
'120',
];
return_values = [
' ',
'return 1;',
'return 2 * 1;',
'return 3 * 2;',
'return 4 * 6;',
'return 5 * 24;',
]
tree_code = [
// Level 1
{
tree_code: 'solve(5)',
tree_code_x: 0,
tree_code_y: 100,
},
// Level 2
{
tree_code: 'solve(4)',
tree_code_x: 200,
tree_code_y: 100,
tree_line_start_x: 75,
tree_line_start_y: 100,
tree_line_end_x: 200,
tree_line_end_y: 100,
},
// Level 3
{
tree_code: 'solve(3)',
tree_code_x: 400,
tree_code_y: 100,
tree_line_start_x: 275,
tree_line_start_y: 100,
tree_line_end_x: 400,
tree_line_end_y: 100,
},
// Level 4
{
tree_code: 'solve(2)',
tree_code_x: 600,
tree_code_y: 100,
tree_line_start_x: 475,
tree_line_start_y: 100,
tree_line_end_x: 600,
tree_line_end_y: 100,
},
// Level 5
{
tree_code: 'solve(1)',
tree_code_x: 800,
tree_code_y: 100,
tree_line_start_x: 675,
tree_line_start_y: 100,
tree_line_end_x: 800,
tree_line_end_y: 100,
},
];
create_code_steps = function () {
var steps = [];
steps.push('main');
steps.push('solve_init');
var recursion = function (n) {
steps.push('solve');
steps.push('base_case_if');
if (n === 1) {
steps.push('base_case_return');
return n;
}
steps.push('recursive');
var return_val = n * recursion(n - 1);
steps.push('recursive_return');
return return_val;
}
recursion(5);
steps.push('solve_init_return');
return steps;
}
get_current_tree_step_stacks = function (step_number) {
var current_tree_step_stack = [];
var current_red_text_stack = [];
var current_tree_step = -1;
for (var i = 0; i <= step_number; i++) {
var code_step = code_steps[i];
if (code_step === 'solve') {
// Increase current_tree_step
current_tree_step++;
// Add step number to step stack
current_tree_step_stack.push(current_tree_step);
current_red_text_stack.push(current_tree_step);
}
if (
code_step === 'recursive_return'
|| code_step === 'solve_init_return'
) {
current_red_text_stack.pop();
}
}
return {
tree_step_stack: current_tree_step_stack,
red_text_step_stack: current_red_text_stack,
};
};
get_current_value_step = function (step_number) {
var current_value_step = 0;
for (var i = 0; i <= step_number; i++) {
var code_step = code_steps[i];
if (
code_step === 'recursive_return'
|| code_step === 'solve_init_return'
) {
current_value_step++;
}
}
return current_value_step;
}
get_current_return_step = function (step_number) {
var current_return_step = 0;
for (var i = 0; i <= step_number; i++) {
var code_step = code_steps[i];
if (
code_step === 'base_case_return'
|| code_step === 'recursive_return'
) {
current_return_step++;
}
}
return current_return_step;
}
get_call_stack_div = function (call_stack_number, code_line) {
return '<div class="call-stack-item call-stack-main-item" style="grid-row: ' + call_stack_number + '; grid-column: 1;">' + code_line + '</div>'
};
draw = function (step_number) {
//console.log("Step Number " + step_number)
// Get Tree Data
var tree_data = get_current_tree_step_stacks(step_number);
var tree_step_stack = tree_data.tree_step_stack;
var red_text_step_stack = tree_data.red_text_step_stack;
var tree_step = tree_step_stack[tree_step_stack.length - 1];
var red_text_step = red_text_step_stack[red_text_step_stack.length - 1];
// Highlight line of code
var code_step = code_steps[step_number];
//console.log("Step " + code_step);
code_step = (code_step === 'recursive_return') ? 'recursive' : code_step;
code_step = (code_step === 'solve_init_return') ? 'solve_init' : code_step;
$('#' + code_step).addClass('highlight');
// Draw call stack boxes
var call_stack_number = 10;
var call_stack_div = get_call_stack_div(call_stack_number--, 'main');
$('#call_stack_grid_container').append(call_stack_div);
//console.log(red_text_step_stack);
for (var i = 0; i < red_text_step_stack.length; i++) {
// Add stack div to call stack grid.
var step = tree_code[red_text_step_stack[i]];
call_stack_div = get_call_stack_div(call_stack_number--, step.tree_code);
$('#call_stack_grid_container').append(call_stack_div);
}
// Draw call tree based on above collected tree data.
for (var i = 0; i <= tree_step; i++) {
var step = tree_code[i];
//console.log('Tree Step Index ' + i);
//console.log('Tree Step ' + step);
text_color = '#000000';
line_color = '#000000';
if (i === red_text_step) {
text_color = '#ff0000';
}
// Draw call tree code if the data to do so exists.
if (
!is_null(step.tree_code)
&& !is_null(step.tree_code_x)
&& !is_null(step.tree_code_y)
) {
// Show the call tree steps
tree_ctx.fillStyle = text_color
tree_ctx.fillText(step.tree_code, step.tree_code_x, step.tree_code_y);
}
// Add a line between the code calls if the data to do so exists.
if (
!is_null(step.tree_line_start_x)
&& !is_null(step.tree_line_start_y)
&& !is_null(step.tree_line_end_x)
&& !is_null(step.tree_line_end_y)
) {
tree_ctx.strokeStyle = line_color;
tree_ctx.beginPath();
tree_ctx.moveTo(step.tree_line_start_x, step.tree_line_start_y);
tree_ctx.lineTo(step.tree_line_end_x, step.tree_line_end_y);
tree_ctx.stroke();
}
}
var return_step = get_current_return_step(step_number);
$('#factorial_return').html(return_values[return_step]);
// Show the current value
var value_step = get_current_value_step(step_number);
$('#factorial_value').html(output_values[value_step]);
};
clear_everything = function () {
// clear all things on screen
//console.log('Clear Everything');
$('#main').removeClass('highlight');
$('#solve_init').removeClass('highlight');
$('#solve').removeClass('highlight');
$('#base_case_if').removeClass('highlight');
$('#base_case_return').removeClass('highlight');
$('#recursive').removeClass('highlight');
$('#call_stack_grid_container').html('');
tree_ctx.clearRect(0, 0, tree_canvas.width, tree_canvas.height);
};
$('#play_button').click(function () {
// Time for each step
var step_time = 2000;
var speed = 1;
var speed_radios = $("[name='speed']");
for (var i = 0, len = speed_radios.length; i < len; i++) {
if (speed_radios[i].checked) {
speed = $(speed_radios[i]).val();
}
}
step_time = (step_time / speed);
// Disable play button
$('#play_button').hide();
$('#pause_button').show();
$('#reset_button').prop('disabled', true);
$('#forward_button').prop('disabled', true);
$('#backward_button').prop('disabled', true);
// Reinitialize play_timeouts array
play_timeouts = [];
// For each step, set a timeout.
processed_step = (processed_step === code_steps.length) ? 0 : processed_step;
// Calculate offset for when paused in middle.
var offset = processed_step * step_time;
//console.log(processed_step);
for (current_step = processed_step; current_step < code_steps.length; current_step++) {
// Calculate timeout
var timeout = (step_time * current_step) - offset;
//console.log("Current Step " + current_step);
//console.log("Timeout " + timeout);
timeout_id = setTimeout(function (current_step) {
clear_everything();
draw(current_step);
processed_step++;
}, timeout, current_step);
// Add timeout to array of timeouts
play_timeouts.push(timeout_id);
}
// Add a timeout to re-enable the play button
var button_timeout = (step_time * code_steps.length) - offset;
timeout_id = setTimeout(function () {
//$('#play_button').prop('disabled', false);
$('#pause_button').hide();
$('#play_button').show();
$('#reset_button').prop('disabled', false);
$('#forward_button').prop('disabled', false);
$('#backward_button').prop('disabled', false);
}, button_timeout);
play_timeouts.push(timeout_id);
});
$('#pause_button').click(function () {
for (i = 0; i < play_timeouts.length; i++) {
clearTimeout(play_timeouts[i]);
}
$('#pause_button').hide();
$('#play_button').show();
$('#reset_button').prop('disabled', false);
$('#forward_button').prop('disabled', false);
$('#backward_button').prop('disabled', false);
});
$('#reset_button').click(function () {
processed_step = 0;
current_step = 0;
clear_everything();
draw(current_step);
});
$('#forward_button').click(function () {
if (processed_step + 1 < code_steps.length) {
current_step = current_step + 1;
processed_step = processed_step + 1;
}
clear_everything();
draw(processed_step);
});
$('#backward_button').click(function () {
if (processed_step - 1 >= 0) {
current_step = current_step - 1;
processed_step = processed_step - 1;
}
clear_everything();
draw(processed_step);
});
code_steps = create_code_steps();
//console.log(code_steps);
// Setup the inital display.
processed_step = 0;
current_step = 0;
clear_everything();
draw(current_step);
});
</script>
</body>
</html>