Author Topic: how would you shift items in a list?  (Read 1623 times)

0 Members and 1 Guest are viewing this topic.

Shay Gaghe

  • Newt
  • Posts: 89
how would you shift items in a list?
« on: July 15, 2015, 08:46:53 AM »
hi

how would you shift items of a list?

_$ (setq lst '((0)(1)(2)(3)(4)(5)(6)(7)))

((0)(1)(2)(3)(4)(5)(6)(7)))
((1)(2)(3)(4)(5)(6)(7)(0)))
((2)(3)(4)(5)(6)(7)(0)(1)))
((3)(4)(5)(6)(7)(0)(1)(2)))

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: how would you shift items in a list?
« Reply #1 on: July 15, 2015, 09:04:41 AM »
hi

how would you shift items of a list?

_$ (setq lst '((0)(1)(2)(3)(4)(5)(6)(7)))
Simplest I can think of:
Code - Auto/Visual Lisp: [Select]
  1. (defun rotate-list (lst)
  2.   (reverse (cons (car lst) (reverse (cdr lst)))))


Not necessarily the most efficient.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

ronjonp

  • Needs a day job
  • Posts: 7529
Re: how would you shift items in a list?
« Reply #2 on: July 15, 2015, 09:07:44 AM »
Here's another ( not so efficient ) for the list you provided.

Code - Auto/Visual Lisp: [Select]
  1. (defun _shift (l n) (append (member (nth n l) l) (reverse (cdr (member (nth n l) (reverse l))))))
  2. (_shift '((0) (1) (2) (3) (4) (5) (6) (7)) 3)
  3. ;; ((3) (4) (5) (6) (7) (0) (1) (2))
« Last Edit: July 15, 2015, 10:11:20 AM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ymg

  • Guest
Re: how would you shift items in a list?
« Reply #3 on: July 15, 2015, 09:24:58 AM »
From Reini's std-lib

Code: [Select]
(defun rot- (l)
  (append (cdr l) (list (car l))))

Shay Gaghe

  • Newt
  • Posts: 89
Re: how would you shift items in a list?
« Reply #4 on: July 15, 2015, 10:04:57 AM »
what about  the most efficient code for the task?

ronjonp

  • Needs a day job
  • Posts: 7529
Re: how would you shift items in a list?
« Reply #5 on: July 15, 2015, 10:13:57 AM »
what about  the most efficient code for the task?
Not mine  ;D  (unless you want to jump to something in the middle without having to iterate)


Quote
Benchmarking ...................Elapsed milliseconds / relative speed for 65536 iteration(s):


    (ROTATE-LIST L).....1281 / 1.16 <fastest>
    (ROT- L)............1312 / 1.13
    (_SHIFT L 1)........1484 / 1.00 <slowest>

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ymg

  • Guest
Re: how would you shift items in a list?
« Reply #6 on: July 15, 2015, 10:49:45 AM »
shay,

It depends on the size of the list.

For short list up to about 50 items Irneb's is a bit more efficient.

On long one the advantage goes to rot-

Here benchmark for 10, 50, 500 and 5000 items list.

Quote
10 items
 (benchmark '((rotate-list pl) (rot- pl)))
Benchmarking .................Elapsed milliseconds / relative speed for 16384 iteration(s):

    (ROTATE-LIST PL).....1513 / 1.01 <fastest>
    (ROT- PL)............1529 / 1.00 <slowest>

50 items
(benchmark '((rotate-list pl) (rot- pl)))
Benchmarking .................Elapsed milliseconds / relative speed for 16384 iteration(s):

    (ROTATE-LIST PL).....1529 / 1.00 <fastest>
    (ROT- PL)............1529 / 1.00 <slowest>

500 items:
(benchmark '((rotate-list pl) (rot- pl)))
Benchmarking .................Elapsed milliseconds / relative speed for 16384 iteration(s):

    (ROT- PL)............1685 / 1.09 <fastest>
    (ROTATE-LIST PL).....1841 / 1.00 <slowest>

5000 items:
(benchmark '((rotate-list pl) (rot- pl)))
Benchmarking ..............Elapsed milliseconds / relative speed for 2048 iteration(s):

    (ROT- PL)............1233 / 1.99 <fastest>
    (ROTATE-LIST PL).....2449 / 1.00 <slowest>