I have a string validation function that I want to optimize. The string is of length 2n and composed of 0 and 1‘s, for example, str="100001". I want to test:
1) whether the number (has to be not less than 1) of 1‘s in oddly indexded positions in the string is equal to that in the evenly indexed positions
2) whether for every StringTake[str,2*i], i runs from 1 to n-1, the number of 1‘s in oddly indexded positions in the string is not equal to that in the evenly indexed positions.
In sum, I want to test whether the position 2n is the first time the number of 1‘s in oddly indexded positions in the string is equal to that in the evenly indexed positions.
"100001" and 101101 is a good string, but not 100100, 100000 nor 000000.
Many thanks.
This code does not test for invalid strings (characters not “0” or “1”, length not even).
Your examples:
Out[302]= {True, True, False, False, False}
There might be faster ways e.g. with NestList. Also if speed is a big issue and strings are likely to be long, you could split out the IntegerDigits[ToExpression[…]] in preprocessing and use Compile on the rest.
Daniel Lichtblau
Wolfram Research