(defun list-group (lst n / res item)
(while lst
(setq item nil)
(repeat n (setq item (cons (car lst) item) lst (cdr lst)))
(setq res (cons (reverse item) res)))
(reverse res))
Some sample code:_$ (list-group '(1 2 3 4 5 6 7 8 9) 3)
((1 2 3) (4 5 6) (7 8 9))
_$ (list-group '(1 2 3 4 5 6 7 8 9 10) 3)
((1 2 3) (4 5 6) (7 8 9) (10 nil nil))
Clearly if the length of the list is not divisible by the group-by number, you're going to get those nils at the end.(defun list-group-i (lst n / item res i j)
(setq i (length lst))
(while (>= (setq i (1- i)) 0)
(setq j n item nil)
(while (>= (setq j (1- j)) 0)
(setq item (cons (nth (+ i j) lst) item)))
(setq res (cons item res) i (- i n -1)))
res)
(defun list-group1 (lst n / res item)
(setq lst (reverse lst))
(repeat (- n (rem (length lst) n)) (setq lst (cons nil lst)))
(while lst
(setq item nil)
(repeat n (setq item (cons (car lst) item) lst (cdr lst)))
(setq res (cons item res)))
res)
Benchmarking ..........Elapsed milliseconds / relative speed for 128 iteration(s):
(LIST-GROUP1 LST 5)......1030 / 6.53 <fastest>
(LIST-GROUP LST 5).......1061 / 6.34
(LIST-GROUP-I LST 5).....6723 / 1 <slowest>
Clearly the indexed version is way slower than the others. Anyone have any other ideas?
(defun _SubLists ( lst n / sublist result )
;; Given: a list of (1 2 3 4 5 6 7 8)
;;
;; and n values of 2, 3 and 4
;;
;; Return: ((1 2) (3 4) (5 6) (7 8))
;; ((1 2 3) (4 5 6) (7 8))
;; ((1 2 3 4) (5 6 7 8))
;;
;; respectively.
(foreach x lst
(if (eq n (length (setq sublist (cons x sublist))))
(setq
result (cons (reverse sublist) result)
sublist nil
)
)
)
(reverse
(if sublist
(cons (reverse sublist) result)
result
)
)
)
... I'm not saying it's faster ...Strange that you should mention that ... or perhaps not :whistle:
Benchmarking ...........Elapsed milliseconds / relative speed for 256 iteration(s):
(_SUBLISTS LST 5)...........1716 / 1.15 <fastest>
(LIST-GROUP1 LST 5).........1903 / 1.03
(LM:GROUPBYNUM-R LST 5).....1950 / 1.01
(LIST-GROUP LST 5)..........1966 / 1
(LM:GROUPBYNUM LST 5).......1966 / 1 <slowest>
Obviously I axed the embarrassing index version :ugly: _$ (_Sublists '(1 2 3 4 5 6 7 8 9 10) 3)
((1 2 3) (4 5 6) (7 8 9) (10))
Effectively the same thing isn't it?Challenge thread:Typical!
http://www.theswamp.org/index.php?topic=32428.0 (http://www.theswamp.org/index.php?topic=32428.0)
Challenge thread 2:
http://www.theswamp.org/index.php?topic=5108.0 (http://www.theswamp.org/index.php?topic=5108.0)
(defun list-group (lst n / sub res)
(setq lst (reverse lst))
(repeat (- n (rem (length lst) n)) (setq lst (cons nil lst)))
(foreach x lst
(if (eq n (length (setq sub (cons x sub))))
(setq res (cons sub res) sub nil)))
(if sub
(cons sub res)
res))
Benchmarking ............Elapsed milliseconds / relative speed for 512 iteration(s):
(GBN_1_EV LST 5)...................1622 / 3.11 <fastest>
(LIST-GROUP LST 5).................3947 / 1.28
(GROUPBYNUM_MAC4 LST 5)............4025 / 1.25
(_SUBLISTS LST 5)..................4072 / 1.24
(GROUPBYNUM_MAC3 LST 5)............4228 / 1.19
(LIST-GROUP-STEPREVERSE LST 5).....4555 / 1.11
(LM:GROUPBYNUM LST 5)..............4633 / 1.09
(LM:GROUPBYNUM-R LST 5)............4633 / 1.09
(GROUPBYNUM_MAC2 LST 5)............4633 / 1.09
(GBN LST 5)........................4696 / 1.07
(LIST-GROUP-STEP LST 5)............4711 / 1.07
(WIZ-GRP LST 5)....................4789 / 1.05
(GROUPBYNUM_VK LST 5)..............4790 / 1.05
(GROUPBYNUM_MAC LST 5).............4820 / 1.05
(SPLIT LST 5)......................5039 / 1 <slowest>
Should've remembered from my programming lectures: "Divide and conquer is what makes Quick Sort live up to its name".Looks like my attempt did not make the cut. :(Sorry, missed yours from that huge list of attempts.
http://www.theswamp.org/index.php?topic=5108.msg61975#msg61975 (http://www.theswamp.org/index.php?topic=5108.msg61975#msg61975)
Benchmarking ............Elapsed milliseconds / relative speed for 512 iteration(s):
(GBN_1_EV LST 5)...................1654 / 3.1 <fastest>
(LIST-GROUP LST 5).................3947 / 1.3
(_SUBLISTS LST 5)..................4056 / 1.27
(GROUPBYNUM_MAC4 LST 5)............4134 / 1.24
(GROUPBYNUM_MAC3 LST 5)............4259 / 1.2
(LIST-GROUP-STEPREVERSE LST 5).....4524 / 1.13
(LM:GROUPBYNUM LST 5)..............4649 / 1.1
(LM:GROUPBYNUM-R LST 5)............4649 / 1.1
(GBN LST 5)........................4649 / 1.1
(GROUPBYNUM_MAC2 LST 5)............4665 / 1.1
(LIST-GROUP-STEP LST 5)............4680 / 1.1
(GROUPBYNUM_MAC LST 5).............4851 / 1.06
(GROUPBYNUM_VK LST 5)..............4852 / 1.06
(WIZ-GRP LST 5)....................4882 / 1.05
(SPLIT LST 5)......................5023 / 1.02
(GROUPITEMS LST 5).................5132 / 1 <slowest>
Oops ... I think I could've lied and said yours "just" didn't make the cut! :pissed:
Benchmarking ............Elapsed milliseconds / relative speed for 512 iteration(s):
(LIST-GBN LST 5).....1591 / 1.05 <fastest>
(GBN_1_EV LST 5).....1669 / 1 <slowest>
Also tried to make the original a bit more "elegant" ... though I'm still not happy with those 4 setq's.
$ (list-gbn '(1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10) 5)
((1 2 3 4 5) (6 7 8 9 10) (1 2 3 4 5) (6 7 8 9 10) (nil nil nil nil nil))
Here's the fix:Benchmarking ............Elapsed milliseconds / relative speed for 512 iteration(s):
(LIST-GBN LST 5).....1591 / 1.04 <fastest>
(GBN_1_EV LST 5).....1653 / 1 <slowest>
Still a whisker faster! :kewl:
Nice reworking of Evgeniy's algorithm Irne :)Thanks! After re-reading his code several times I finally figured out what was going on ... and then realized: "But this can be combined into a loop can't it?"
Benchmarking ...........Elapsed milliseconds / relative speed for 256 iteration(s):
(LIST-GBN LST 5)...................1232 / 2.55 <fastest>
(GBN_1_EV LST 5)...................1294 / 2.42
(GROUPBYNUM_MAC3 LST 5)............1763 / 1.78
(LIST-GROUP-STEPREVERSE LST 5).....1794 / 1.75
(GROUPBYNUM_MAC4 LST 5)............2590 / 1.21
(LIST-GROUP LST 5).................2621 / 1.2
(_SUBLISTS LST 5)..................2652 / 1.18
(GBN2 LST 5).......................3027 / 1.04
(LM:GROUPBYNUM-R LST 5)............3120 / 1.01
(LM:GROUPBYNUM LST 5)..............3136 / 1
(GBN LST 5)........................3136 / 1 <slowest>
Not bad ... made the top 10!(setq lst nil n 0)
(repeat 5000 (setq lst (cons n lst) n (1+ n)))
The two PC's are extremely close to being identical (both i7-2600, both 16GB ram, both Win7 Pro, both Vanilla 2012). Yet there's some discrepancies in the benchmarking. I've run the benchmarking 5 times now and get similar results each time. Must be something strange running on the background in one but not the other.Benchmarking ............Elapsed milliseconds / relative speed for 512 iteration(s):
(LIST-GBN LST 5)............1029 / 3.5 <fastest>
(GBN_1_EV LST 5)............1233 / 2.92
(GROUPBYNUM_MAC4 LST 5).....2090 / 1.72
(GROUPBYNUM_MAC3 LST 5).....2184 / 1.65
(LIST-GBN2 LST 5)...........2386 / 1.51
(LM:GROUPBYNUM-R LST 5).....2605 / 1.38
(SPLIT LST 5)...............2636 / 1.37
(WIZ-GRP LST 5).............2793 / 1.29
(_SUBLISTS LST 5)...........2823 / 1.28
(GROUPITEMS LST 5)..........2871 / 1.26
(LM:GROUPBYNUM LST 5).......2964 / 1.22
(LIST-GROUP-STEP LST 5).....2964 / 1.22
(GROUPBYNUM_MAC2 LST 5).....3229 / 1.12
(GROUPBYNUM_MAC LST 5)......3323 / 1.08
(GBN2 LST 5)................3604 / 1 <slowest>
Benchmarking .............Elapsed milliseconds / relative speed for 1024 iteration(s):
(LIST-GBN LST 5)............1919 / 3.83 <fastest>
(LM:GROUPBYNUM-R LST 5).....1950 / 3.77
(LIST-GROUP-STEP LST 5).....1965 / 3.74
(LM:GROUPBYNUM LST 5).......1966 / 3.74
(GROUPBYNUM_MAC2 LST 5).....1981 / 3.71
(GBN_1_EV LST 5)............2013 / 3.65
(GBN2 LST 5)................2137 / 3.44
(_SUBLISTS LST 5)...........2153 / 3.41
(LIST-GBN2 LST 5)...........2293 / 3.2
(GROUPBYNUM_MAC4 LST 5).....2745 / 2.68
(GROUPBYNUM_MAC3 LST 5).....2933 / 2.5
(GROUPITEMS LST 5)..........3245 / 2.26
(SPLIT LST 5)...............3978 / 1.85
(WIZ-GRP LST 5).............4118 / 1.78
(GROUPBYNUM_MAC LST 5)......7347 / 1 <slowest>
Benchmarking .............Elapsed milliseconds / relative speed for 1024 iteration(s):
(LIST-GBN LST 5)............1934 / 3.19 <fastest>
(LM:GROUPBYNUM-R LST 5).....1966 / 3.14
(GROUPBYNUM_MAC2 LST 5).....1981 / 3.12
(LM:GROUPBYNUM LST 5).......1981 / 3.12
(LIST-GROUP-STEP LST 5).....1997 / 3.09
(GBN_1_EV LST 5)............2028 / 3.05
(GBN2 LST 5)................2153 / 2.87
(_SUBLISTS LST 5)...........2153 / 2.87
(LIST-GBN2 LST 5)...........2309 / 2.68
(GROUPBYNUM_MAC4 LST 5).....2777 / 2.22
(GROUPBYNUM_MAC3 LST 5).....2901 / 2.13
(GROUPITEMS LST 5)..........3260 / 1.9
(SPLIT LST 5)...............3526 / 1.75
(WIZ-GRP LST 5).............3713 / 1.66
(GROUPBYNUM_MAC LST 5)......6178 / 1 <slowest>
Benchmarking ..............Elapsed milliseconds / relative speed for 2048 iteration(s):
(GROUPBYNUM_MAC2 LST 5)......1903 / 5.89 <fastest>
(LIST-GROUP-STEP LST 5)......1935 / 5.79
(LM:GROUPBYNUM LST 5)........1950 / 5.74
(LM:GROUPBYNUM-R LST 5)......2012 / 5.57
(GBN2 LST 5).................2309 / 4.85
(GROUPBYNUM_MAC4 LST 5)......2324 / 4.82
(_SUBLISTS LST 5)............2356 / 4.75
(LIST-GBN2 LST 5)............2387 / 4.69
(GROUPBYNUM_MAC3 LST 5)......2449 / 4.57
(GROUPITEMS LST 5)...........2668 / 4.2
(SPLIT LST 5)................3463 / 3.23
(LIST-GBN LST 5).............3697 / 3.03
(WIZ-GRP LST 5)..............4399 / 2.55
(GBN_1_EV LST 5).............4493 / 2.49
(GROUPBYNUM_MAC LST 5)......11201 / 1 <slowest>
Command: (load "Benchmark")
BENCHMARK
Command: (load "BenchTest")
Elapsed milliseconds / relative speed for 1024 iteration(s):
(LIST-GBN LST 5)............1591 / 3.84 <fastest>
(GBN_1_EV LST 5)............1654 / 3.7
(LIST-GBN2 LST 5)...........2434 / 2.51
(LIST-GROUP-STEP LST 5).....2667 / 2.29
(GROUPBYNUM_MAC2 LST 5).....2684 / 2.28
(LM:GROUPBYNUM LST 5).......2715 / 2.25
(GROUPBYNUM_MAC LST 5)......2776 / 2.2
(GBN2 LST 5)................2823 / 2.17
(GROUPBYNUM_MAC4 LST 5).....2855 / 2.14
(LM:GROUPBYNUM-R LST 5).....2870 / 2.13
(GROUPBYNUM_MAC3 LST 5).....2948 / 2.07
(_SUBLISTS LST 5)...........3214 / 1.9
(GROUPITEMS LST 5)..........3245 / 1.88
(SPLIT LST 5)...............3744 / 1.63
(WIZ-GRP LST 5).............3806 / 1.61
(GROUPBYNUM_VK LST 5).......6115 / 1 <slowest>
Elapsed milliseconds / relative speed for 1024 iteration(s):
(LIST-GBN LST 5)............1560 / 4.14 <fastest>
(GBN_1_EV LST 5)............1591 / 4.06
(GROUPBYNUM_MAC2 LST 5).....1762 / 3.67
(LIST-GROUP-STEP LST 5).....1778 / 3.63
(LM:GROUPBYNUM LST 5).......1779 / 3.63
(_SUBLISTS LST 5)...........1919 / 3.37
(GBN2 LST 5)................1934 / 3.34
(LM:GROUPBYNUM-R LST 5).....2043 / 3.16
(LIST-GBN2 LST 5)...........2075 / 3.11
(GROUPBYNUM_MAC LST 5)......2075 / 3.11
(GROUPBYNUM_MAC4 LST 5).....2574 / 2.51
(GROUPBYNUM_MAC3 LST 5).....2637 / 2.45
(GROUPITEMS LST 5)..........2949 / 2.19
(WIZ-GRP LST 5).............3432 / 1.88
(SPLIT LST 5)...............3495 / 1.85
(GROUPBYNUM_VK LST 5).......6458 / 1 <slowest>
Elapsed milliseconds / relative speed for 1024 iteration(s):
(GBN_1_EV LST 5)............1592 / 3.95 <fastest>
(LIST-GBN LST 5)............1607 / 3.91
(GROUPBYNUM_MAC2 LST 5).....1763 / 3.57
(LM:GROUPBYNUM LST 5).......1763 / 3.57
(LIST-GROUP-STEP LST 5).....1778 / 3.54
(_SUBLISTS LST 5)...........1919 / 3.28
(GBN2 LST 5)................2028 / 3.1
(GROUPBYNUM_MAC LST 5)......2059 / 3.05
(LM:GROUPBYNUM-R LST 5).....2059 / 3.05
(LIST-GBN2 LST 5)...........2433 / 2.58
(GROUPBYNUM_MAC4 LST 5).....2574 / 2.44
(GROUPBYNUM_MAC3 LST 5).....2667 / 2.36
(GROUPITEMS LST 5)..........3011 / 2.09
(SPLIT LST 5)...............3338 / 1.88
(WIZ-GRP LST 5).............3432 / 1.83
(GROUPBYNUM_VK LST 5).......6287 / 1 <slowest>
Elapsed milliseconds / relative speed for 2048 iteration(s):
(GROUPBYNUM_MAC2 LST 5).....1591 / 2.96 <fastest>
(LM:GROUPBYNUM LST 5).......1591 / 2.96
(LIST-GROUP-STEP LST 5).....1623 / 2.9
(_SUBLISTS LST 5)...........1966 / 2.4
(GBN2 LST 5)................1997 / 2.36
(LIST-GBN2 LST 5)...........2060 / 2.29
(GROUPBYNUM_MAC4 LST 5).....2106 / 2.24
(LM:GROUPBYNUM-R LST 5).....2106 / 2.24
(GROUPBYNUM_MAC3 LST 5).....2137 / 2.2
(GROUPBYNUM_MAC LST 5)......2137 / 2.2
(GROUPITEMS LST 5)..........2340 / 2.01
(LIST-GBN LST 5)............3042 / 1.55
(GBN_1_EV LST 5)............3104 / 1.52
(SPLIT LST 5)...............3183 / 1.48
(WIZ-GRP LST 5).............3619 / 1.3
(GROUPBYNUM_VK LST 5).......4711 / 1 <slowest>
you can't fool the compiler :)Quote(GROUPBYNUM_MAC2 LST 5).....1591 / 2.96 <fastest>
(LM:GROUPBYNUM LST 5).......1591 / 2.96
(LIST-GROUP-STEP LST 5).....1623 / 2.9
(_SUBLISTS LST 5)...........1966 / 2.4
(GBN2 LST 5)................1997 / 2.36
(LIST-GBN2 LST 5)...........2060 / 2.29
(GROUPBYNUM_MAC4 LST 5).....2106 / 2.24
(LM:GROUPBYNUM-R LST 5).....2106 / 2.24
(GROUPBYNUM_MAC3 LST 5).....2137 / 2.2
(GROUPBYNUM_MAC LST 5)......2137 / 2.2
(GROUPITEMS LST 5)..........2340 / 2.01
(LIST-GBN LST 5)............3042 / 1.55
(GBN_1_EV LST 5)............3104 / 1.52
(SPLIT LST 5)...............3183 / 1.48
(WIZ-GRP LST 5).............3619 / 1.3
(GROUPBYNUM_VK LST 5).......4711 / 1 <slowest>