2022.05.17 10:14 AM - edited 2022.05.17 10:18 AM
Hi All,
Given a binary list of any length e.g:
test:00001111000111001101000b
Is there some way to iterate through this with 2 values - e.g input:3 , output:2
So that every time the input is 1 at least 3 times in a row (input param) , we let the result equal 1 and only when there are more consecutive 0's than the output param, do we reset.
Example output for test above:
00000011110001111111110b
Another sample:
Input: 110010111001111b
Output: 000000001111111b
I tried to use msum so if input is 3:
3 msum test
Gives the points where 3 consecutive 1's have been detected however can't seem to carry this value for the next two iterations.
Any help appreciated!
2022.05.17 03:44 PM
This becomes easier by splitting the problem in 2 parts.
These are the only indexes that matter. Then a prototype list can be populated with nulls before having the important indexes overlaid. Then the prevailing values are carried forward by fills.
q)f:{o:count[z]#0N;o:@[o;where x=x msum z;:;1];y:y+1;"b"$0^fills @[o;where y=y msum not z;:;0]}
q)f[3;2] 00001111000111001101000b
00000011110001111111110b
q)f[3;2] 110010111001111b
000000001111111b
2022.05.17 03:44 PM
This becomes easier by splitting the problem in 2 parts.
These are the only indexes that matter. Then a prototype list can be populated with nulls before having the important indexes overlaid. Then the prevailing values are carried forward by fills.
q)f:{o:count[z]#0N;o:@[o;where x=x msum z;:;1];y:y+1;"b"$0^fills @[o;where y=y msum not z;:;0]}
q)f[3;2] 00001111000111001101000b
00000011110001111111110b
q)f[3;2] 110010111001111b
000000001111111b
2022.05.18 12:16 AM
Nice solution, thanks!
2022.05.18 07:06 AM
Rian’s excellent logic can be refactored to use the Over iterator. (You might not be familiar with Over with a ternary or quaternary function.)
q)g:{"b"$0^fills@/[;(x,y+1){where x=x msum y}'1 not\z;:;1 0]count[z]#0N}
q)g[3;2] 00001111000111001101000b
00000011110001111111110b
q)g[3;2] 110010111001111b
000000001111111b
The key concept here is that the first argument of Amend At Over @/
is the initial state: count[z]#0N
. The other (right) arguments are same-length lists, or atoms. Over works through the argument lists in succession.
Once again we see the Zen monks as a point-free alternative to writing (z;not z)
.
The refactoring here doesn’t save much time, but spotting opportunities like this improves your ability to find iterator solutions, some of which will save you significant CPU.
EMEA
Tel: +44 (0)28 3025 2242
AMERICAS
Tel: +1 (212) 447 6700
APAC
Tel: +61 (0)2 9236 5700
KX. All Rights Reserved.
KX and kdb+ are registered trademarks of KX Systems, Inc., a subsidiary of FD Technologies plc.