Author: Remi Meier Branch: c7 Changeset: r702:ff1022e19989 Date: 2014-02-05 10:57 +0100 http://bitbucket.org/pypy/stmgc/changeset/ff1022e19989/
Log: simple n-queens demo for duhton diff --git a/duhton/demo/nqueens.duh b/duhton/demo/nqueens.duh new file mode 100644 --- /dev/null +++ b/duhton/demo/nqueens.duh @@ -0,0 +1,85 @@ + + + + + +(setq count (container 0)) + +(defun abs (i) + (if (<= 0 i) + i + (- 0 i))) + +(defun attacks (hist col i j) + (|| (== (get hist j) i) + (== (abs (- (get hist j) i)) + (- col j))) + ) + +(defun print_solution (hist n) + (print (quote solution) n) + (setq i 0) + (while (< i n) + (setq line (list)) + (setq j 0) + (while (< j n) + (if (== j (get hist i)) + (append line (quote Q)) + (if (== 0 (% (+ i j) 2)) + (append line (quote .)) + (append line (quote ,)) + ) + ) + (setq j (+ j 1)) + ) + + (print line) + (setq i (+ i 1)) + ) + ) + +(defun solve (n col hist) + (if (== col n) + (progn + (set count (+ (get count) 1)) + (print_solution hist n) + ) + + ;; else + (setq i 0) + (while (< i n) + (setq j 0) + (while (&& (< j col) + (not (attacks hist col i j))) + (setq j (+ j 1)) + ) + + (if (>= j col) + (progn + (set hist col i) + (solve n (+ col 1) hist) + )) + + (setq i (+ i 1)) + ) + ) + ) + + + +(defun clean_list (n) + (setq i n) + (setq res (list)) + (while (> i 0) + (append res 0) + (setq i (- i 1)) + ) + res + ) + + + +(setq n 8) +(solve n 0 (clean_list n)) +(print (quote solutions:) (get count)) + diff --git a/duhton/demo/sort.duh b/duhton/demo/sort.duh --- a/duhton/demo/sort.duh +++ b/duhton/demo/sort.duh @@ -173,7 +173,7 @@ (setq current (time)) (print (quote before-random)) -(setq cs (random_list 200000)) +(setq cs (random_list 300000)) (print (quote time-random:) (- (time) current)) ;; (print_list cs) _______________________________________________ pypy-commit mailing list [email protected] https://mail.python.org/mailman/listinfo/pypy-commit
