Sysop: | Amessyroom |
---|---|
Location: | Fayetteville, NC |
Users: | 23 |
Nodes: | 6 (0 / 6) |
Uptime: | 52:43:55 |
Calls: | 583 |
Files: | 1,139 |
D/L today: |
179 files (27,921K bytes) |
Messages: | 111,617 |
(defun rob-st-amant (list)
(loop for element in list
if (plusp element)
collect element into positives
else if (minusp element)
collect (abs element) into minuses
else collect element into zeroes
finally (return (list positives zeroes minuses))))
Slobodan Blazeski wrote:
(defun rob-st-amant (list)
(loop for element in list
if (plusp element)
collect element into positives
else if (minusp element)
collect (abs element) into minuses
else collect element into zeroes
finally (return (list positives zeroes minuses))))
Given a list of integers, segregate them according to
whether they are negative, positive, or zero.
The absolute values of the negative numbers will be saved.
The function shown above is the fastest one that he tested.
(defconstant cll '(-2 0 -4 8 -6 35 0 42 -26 75 -88 92))
Checking output:
(rob-st-amant cll)
===>
((8 35 42 75 92) (0 0) (2 4 6 26 88))
Using SBCL.
Timing (fastest run shown here):
(time (dotimes (i 1000000) (rob-st-amant cll)))
0.562 seconds of real time
0.562500 seconds of total run time (0.562500 user, 0.000000 system)
[Run times consist of 0.031 seconds GC time, and 0.532 seconds non-GC time.] 100.00% CPU
1,038,368,540 processor cycles
143,998,192 bytes consed
(defun b-pym (seq)
(do ((pos '())
(zer '())
(neg '()))
((not seq) (list pos zer neg))
(let ((e (pop seq)))
(cond ((minusp e) (push (abs e) neg))
((plusp e) (push e pos))
(t (push e zer))))))
Checking output:
(b-pym cll)
===>
((92 75 42 35 8) (0 0) (88 26 6 4 2))
Timing (fastest run shown here):
(time (dotimes (i 1000000) (b-pym cll)))
0.391 seconds of real time
0.390625 seconds of total run time (0.390625 user, 0.000000 system)
100.00% CPU
716,289,211 processor cycles
119,997,064 bytes consed