05-29-2012 04:08 PM
@crossrulz wrote:
@JW-L3CE wrote:
Ok, I know this is already solved, but I wanted to have fun.
A bit more obscure, but smaller.
Yeah, I can tell I've rubbed off on you at least a little bit. That was my original train of thought. I abandoned it however because I was trying to help the OP think through the problem.
Good point, I should explain for the OP.
It works by subtracting the current value from the previous value.
If you get a 0->1 transition, the subtraction results in a 1 and the current index is added (the "1" case).
If you get a 1->0 transition, the subtraction results in a -1 and the previous index is added (the "-1" case).
The last 0 is added to the array so that you can catch a 1 at the end of the array. If the last element is a 1, it will reach the end and subtract 0-1, get a "-1" result, and report the previous index (the REAL last index, not the added one)
The shift register is initialized to 0 so you can catch 1's at the start of the array. This would subtract 1-0 and get a "1" case.
05-29-2012 04:30 PM
@JW-L3CE wrote:
@crossrulz wrote:
@JW-L3CE wrote:
Ok, I know this is already solved, but I wanted to have fun.
A bit more obscure, but smaller.
Yeah, I can tell I've rubbed off on you at least a little bit. That was my original train of thought. I abandoned it however because I was trying to help the OP think through the problem.
Good point, I should explain for the OP.
It works by subtracting the current value from the previous value.
If you get a 0->1 transition, the subtraction results in a 1 and the current index is added (the "1" case).
If you get a 1->0 transition, the subtraction results in a -1 and the previous index is added (the "-1" case).
The last 0 is added to the array so that you can catch a 1 at the end of the array. If the last element is a 1, it will reach the end and subtract 0-1, get a "-1" result, and report the previous index (the REAL last index, not the added one)
The shift register is initialized to 0 so you can catch 1's at the start of the array. This would subtract 1-0 and get a "1" case.
There is a small (minor) issue with this, if you have a single 1 its index will be in the output twice (for the 0-1 and 1-0 transitions). Note that it migt be ok, it depend on what the OP wants.
Ben64
05-29-2012 04:33 PM
@ben64 wrote:
There is a small (minor) issue with this, if you have a single 1 its index will be in the output twice (for the 0-1 and 1-0 transitions). Note that it migt be ok, it depend on what the OP wants.
Ben64
Wow. Good eye. Kudos
05-29-2012 09:46 PM
Thanks all of you!!! that's very helpful!!!
Crossrulz .. now i'm trying to follow your solution , i've already subtracted the indices of o's by 1
could you give me the suggest after that about how to add the last column of index to array
05-30-2012 06:53 AM
The simple way is to append a 0 to the array before running through the loop. This way you will see a transition from 1 to 0 (grab the previous index) or 0 to 0 (which you don't care about).