Problem_Set_03_Solutions_Code

Problem_Set_03_Solutions_Code - Discrete math PS-3;=...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
;;; Discrete math PS-3 ;;;=========================================================== ;;; Exercise 2 ;;;=========================================================== ;;; 4-peg Tower of Hanoi - toh4-sloppy ;;; if n is odd, the top-half gets an extra disk (define (toh4-sloppy n from to using1 using2) (let ((top-half (ceiling (/ n 2))) (bottom-half (floor (/ n 2)))) (cond ((= n 1) (display (list 'toh4-sloppy: 'move 'disk from '--> to)) (newline)) (else (toh4-sloppy top-half from using1 to using2) (toh4-sloppy bottom-half from to using2 using1) (toh4-sloppy top-half using1 to from using2))))) ;; works for n = 3 ;(toh4-sloppy 3 'from 'to 'using1 'using2) ;(toh4-sloppy: move disk from --> to) ;(toh4-sloppy: move disk from --> using1) ;(toh4-sloppy: move disk to --> using1) ;(toh4-sloppy: move disk from --> to) ;(toh4-sloppy: move disk using1 --> from) ;(toh4-sloppy: move disk using1 --> to) ;(toh4-sloppy: move disk from --> to) ;; works for n = 4 ;(toh4-sloppy 4 'from 'to 'using1 'using2) ;(toh4-sloppy: move disk from --> to) ;(toh4-sloppy: move disk from --> using1) ;(toh4-sloppy: move disk to --> using1) ;(toh4-sloppy: move disk from --> using2) ;(toh4-sloppy: move disk from --> to) ;(toh4-sloppy: move disk using2 --> to) ;(toh4-sloppy: move disk using1 --> from) ;(toh4-sloppy: move disk using1 --> to) ;(toh4-sloppy: move disk from --> to) ;; does not work for n = 8 ;(toh4-sloppy 8 'from 'to 'using1 'using2) ;(toh4-sloppy: move disk from --> using1) ;(toh4-sloppy: move disk from --> to) ;(toh4-sloppy: move disk using1 --> to) ;(toh4-sloppy: move disk from --> using2) ;(toh4-sloppy: move disk from --> using1) ;(toh4-sloppy: move disk using2 --> using1) ;(toh4-sloppy: move disk to --> from) ;(toh4-sloppy: move disk to --> using1) ;(toh4-sloppy: move disk from --> using1) ;(toh4-sloppy: move disk from --> to) ;(toh4-sloppy: move disk from --> using2) ;(toh4-sloppy: move disk to --> using2) ;(toh4-sloppy: move disk from --> using1) ;(toh4-sloppy: move disk from --> to) ;(toh4-sloppy: move disk using1 --> to)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
;(toh4-sloppy: move disk using2 --> from) ;(toh4-sloppy: move disk using2 --> to) ;(toh4-sloppy: move disk from --> to) ;(toh4-sloppy: move disk using1 --> to) ;(toh4-sloppy: move disk using1 --> from) ;(toh4-sloppy: move disk to --> from)
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 10/01/2011.

Page1 / 5

Problem_Set_03_Solutions_Code - Discrete math PS-3;=...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online