Hi all,
I have a list of 200 numbers (not sequential). I need to get all the 2, 3 and 4 combinations of numbers in the list, for example
List = {153, 154, 155, 181, 182, 183, 184, 185, 208…}
2 = {{153,154}, {153,155}, {153,181}, {153,182}…} etc
3 = {{153,154, 155}, {153,155, 181}, {153,181, 182}, {153,182, 183}…} etc
(the number sequences should be in the order they are in the list - not every permutation)
I have so far come up with the following (I have commented out some of the group for reasons explained below)
script so
property myList : {153, 154, 155, 181, 182, 183, 184, 185, 208, 209, 210, 211, 212, 213, 236, 237, 238, 239, 240, 241, 242, 263, 264, 265, 266, 267, 268, 269, 270, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 318, 319, 320, 321, 322, 323, 324, 325, 326, 327, 328, 344, 345, 346, 347, 348, 349, 350, 351, 352, 353, 354, 355, 356, 357, 371, 372, 373, 374, 375, 376, 377, 378, 379, 380, 381, 382, 383, 384, 385, 398, 399, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 426, 427, 428, 429, 430, 431, 432, 433, 434, 435, 436, 437, 438, 439, 440, 441, 442, 456, 457, 458, 459, 460, 461, 462, 463, 464, 465, 466, 467, 468, 469, 470, 485, 486, 487, 488, 489, 490, 491, 492, 493, 494, 495, 496, 497}--, 515, 516, 517, 518, 519, 520, 521, 522, 523, 524, 525, 542, 543, 544, 545, 546, 547, 548, 549, 550, 551, 552, 571, 572, 573, 574, 575, 576, 577, 578, 579, 580, 600, 601, 602, 603, 604, 605, 606, 607, 629, 630, 631, 632, 633, 634, 635, 656, 657, 658, 659, 660, 661, 662, 686, 687, 688, 689, 690, 715}
end script
set allOptions to my getSets(so's myList, 2)
on getSets(myList, noOfInts)
set resultsList to {}
repeat with i from 1 to ((count myList) - (noOfInts - 1))
set baseValue to item i of myList
repeat with q from (i + 1) to ((count myList) - (noOfInts - 2))
set mySet to baseValue & items q thru (q + (noOfInts - 2)) of myList
set end of resultsList to mySet
end repeat
end repeat
return resultsList
end getSetss
A couple of things to note
a) it’s really slow. For sets of 2 on a list of ~140, it’s OK - takes about 1:40 and results in 9730 pairs, but then gets exponentially slower. On the full set, it still hadn’t finished after 10 minutes. My go to method of speeding up lists with a script object hasn’t helped.
b) it’s not even a full list of variants. In the end result should there should be more variation.
I realise the final result (especially for groups of 4) will be large, but is there a better/more efficient method for doing this?
Appreciate any help you can offer
Ian