What is the most efficient way to splice a list into another? I wrote this, but am thinking there must be a more efficient way.
q)a:1 2 3 7 8 9q)b:4 5 6q){x:(0,y)_x; x[0],z,x[1]}[a;3;b]1 2 3 4 5 6 7 8 9
What is the most efficient way to splice a list into another? I wrote this, but am thinking there must be a more efficient way.
q)a:1 2 3 7 8 9q)b:4 5 6q){x:(0,y)_x; x[0],z,x[1]}[a;3;b]1 2 3 4 5 6 7 8 9
Maybe this (didn’t test it)
(3#a),b,3_a
And you could generalize using bin to find the index (offsetting by 1) on sorted big a, b lists.
Hey Victor, the original question is a little vague:
Are you assuming that you know where to cut and you don’t care about the order of the output list?? Or is it that the first value in the second list (4) has to slot in after the first number that is less than it (3)?
The example shown may be misleading
My apologies for the ambiguity. I am assuming I know where to cut and that the order of both lists are preserved in the output - the inputs are not necessarily sorted.
Right. Then walters solution should give you what you’re looking for, along with plenty of other alternatives
q)a:1000000?100
q)
q)b:1000?100
q)\ts do[100;f1:{x:(0,y)_x; x[0],z,x[1]}[a;3;b]]
1085 33555136
q)\ts do[100;f2:{(y#x),z,y _ x}[a;3;b]]
1069 25166208
q)\ts do[100;f3:{raze @[(0;y) cut x;0;,;z]}[a;3;b]]
602 16786016
q)\ts do[100;f4:{raze @[;0 2 1] ((0;y) _ x),enlist z}[a;3;b]]
598 16777984
q)
q)f1~f2
1b
q)f2~f3
1b
q)f3~f4
1b
Thank you very much. I didn’t know that raze was so much more efficient.
Much appreciated,
Victor