#Hanoi towers speed code#This is essentially what I did, that is, in the above code comment the line moveDisk() and uncomment the line callStack.push(). Using a stack will also preserve the order of the calls. After some thought, I was asking myself: Why not use a stack (callStack) to save the "from,to" call-parameters to moveDisk(). #Hanoi towers speed how to#But then how to find all these moveDisk() calls. In other words, every call to moveDisk() should issue (at the time when it is done) the next call to moveDisk(). But with JavaScript animation, somehow, we have to follow the pattern: animate moveDisk(), animate the next moveDisk(). It seems that JavaScript animation uses the pattern: if you start animation, do not do anything else.įor the tower-of-Hanoi algorithm, we want to animate one call to moveDisk(), return to the caller until we encounter the next call to moveDisk(), animate it, and so on. If we are to implement the delay by a spin-loop, it would bring things to a stand-still because the whole of JavaScript code executes on one thread. However, JavaScript lacks a "real" delay or sleep mechanism. One might suggest that the caller sleeps for a while to allow for animation to complete. A problem arises because code in the caller will continue executing and interfere (miss up shared data) with the animation. Suppose, in the above code, the call to the function moveDisk() is animated using setInterval(). Normally animation code can utilize JavaScript timer functions: setInterval() or setTimer(). Animation Handling in JavaScript is Problematic The above algorithm encodes the preceding description but with the parameters for the towers kept as variables instead of being specific values. After this, we can move the n-th disk from tower #0 to tower #2 and then move all the n-1 disks from the middle tower to the last tower using tower #0 as intermediate by the call Hanoi(n-1, 1,2,0). This moves the disks to the middle tower (#1) using the other tower (#2) as intermediate. The recursive algorithm for the tower of Hanoi is based on observing that the top n-1 disks at the "from" tower (together with the other two towers) represent a smaller-size instance of the original problem and, thus, can be solved by the call Hanoi(n-1, 0,1,2). Then express the solution for the original problem in terms of the solutions to the subproblems. Normally (if we denote the towers with 0,1,2), the initial call for n disks will be Hanoi(n,0,2,1).Ī recursive algorithm normally tries to find subproblems (instances of the original problem but with smaller problem size). via: the "via" tower is that used as an intermediate location as disks are moved between the towers from and to.to: the "to" tower is where the disks must be finally placed.from: the "from" tower is where the disks are placed.n: number of disks serves as the problem size for recursion.The input to the algorithm are 4 integer parameters, as follows: All rights reserved.// callStack.push() // save parameters to callStack array Lastly, both experiments provide evidence that first solving a problem mentally encouraged participants to use strategies similar to goal recursion on a second problem.Įxternal representations Internal representations Problem solving Strategy learning.Ĭopyright © 2022 Elsevier B.V. In addition, participants spent more time between moves when solving problems mentally, suggesting that external representations encourage speed while internal representations promote accuracy when solving recursion problems. Participants were better able to complete problems successfully when external representations were available but completed problems in fewer moves when relying on internal representations. They were randomly assigned to different conditions in which problems were either high in internal representation (mental) or high in external representation (computer). Undergraduate students (Experiment 1) or Prolific workers (Experiment 2) completed two TOH problems of varying difficulty (4-disk/5-disk). This study used TOH to examine how mode of presentation of a problem influences strategy use and transfer. The Tower of Hanoi (TOH) is a classic problem that can be solved via multiple strategies.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |