Author Topic: Army Ants Recruit Training (lisp challenge)  (Read 7640 times)

0 Members and 1 Guest are viewing this topic.

pkohut

  • Guest
Re: Army Ants Recruit Training (lisp challenge)
« Reply #15 on: June 20, 2009, 06:01:29 AM »
Thanks Cab, those updates look very helpful.  The setCols function that was posted
is now cleaned up and renamed to setMxColsFromArry and there is now
a setMxRowFromArray function.  Test1 and test2 drives some test data for the
functions.

Thanks,
Paul

Code: [Select]
(defun c:test1 ( / mx lst m)
  (setq mx (list (list 1 2 3 4 5) (list 6 7 8 9 10) (list 11 12 13 14 15) (list 16 17 18 19 20) (list 21 22 23 24 25)))
  (setq lst (list 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116))
 
  (setq m (setMxColsFromArray mx 0 0 4 lst 0))
  (setq m (setMxColsFromArray mx 1 0 2 lst 5))
  (setq m (setMxColsFromArray mx 2 2 4 lst 5))
  (setq m (setMxColsFromArray mx 3 1 3 lst 4))
  (princ)
)

(defun c:test2 ( / mx lst m)
  (setq mx (list (list 1 2 3 4 5) (list 6 7 8 9 10) (list 11 12 13 14 15) (list 16 17 18 19 20) (list 21 22 23 24 25)))
  (setq lst (list 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116))
 
  (setq m (setMxRowsFromArray mx 0 0 4 lst 0))
  (setq m (setMxRowsFromArray mx 1 0 2 lst 5))
  (setq m (setMxRowsFromArray mx 2 2 4 lst 5))
  (setq m (setMxRowsFromArray mx 3 1 3 lst 4))
  (princ)
)

;;; Substitutes input matrix items on row# along fromColumn to toColumn, from items in input array
;;; beginning at lstStartPos (position).
;;; Returns modified input matrix.
;;; zero based, no bounds or error checking
(defun setMxColsFromArray (mxIn row fromColumn toColumn arrayIn lstStartPos / i j mxNew lstTmp lstMxRow)
  (setq i 0
j 0
mxNew nil
  )
  (while (< j (length mxIn))
    (setq lstMxRow (nth j mxIn)
  i  0
  lstTmp nil
    )
    (while (< i (length lstMxRow))
      (if (= row j)
(if (and
      (>= i fromColumn)
      (<= i toColumn)
      )
  (setq lstTmp (append lstTmp (list (nth (+ i lstStartPos) arrayIn))))
  (setq lstTmp (append lstTmp (list (nth i lstMxRow))))
)
(setq lstTmp (append lstTmp (list (nth i lstMxRow))))
      )
      (setq i (1+ i))
    )
    (setq mxNew (append mxNew (list lstTmp)))
    (setq j (1+ j))
  )
  mxNew
)

;;; Substitutes input matrix items at column# in rows fromRow to toRow, from items in input array
;;; beginning at lstStartPos (position).
;;; Returns modified input matrix.
;;; zero based, no bounds or error checking
(defun setMxRowsFromArray (mxIn column fromRow toRow arrayIn lstStartPos / i j k mxNew lstTmp lstMxRow)
  (setq i 0
j 0
mxNew nil
k 0
  )
  (while (< j (length mxIn))
    (setq lstMxRow (nth j mxIn)
  i  0
  lstTmp nil
    )
    (while (< i (length lstMxRow))
      (if (= column i)
  (if (and
(>= j fromRow)
(<= j toRow)
      )
    (setq lstTmp (append lstTmp (list (nth (+ k lstStartPos) arrayIn)))
  k (1+ k))
    (setq lstTmp (append lstTmp (list (nth i lstMxRow)))
  k (1+ k))
  )
  (setq lstTmp (append lstTmp (list (nth i lstMxRow))))
)
      (setq i (1+ i))
    )
    (setq mxNew (append mxNew (list lstTmp)))
    (setq j (1+ j))
  )
  mxNew
)