1
$\begingroup$

enter image description here

The above full string is a combined form of two different strings named string-1 and string-2. Both strings has equal length of 32 characters. There is two other parameters interval and size. string-1 is broken into pieces of length = interval and string-2 is broken into pieces of length = size to form the full string. once all the pieces of string -2 is put in full string, pieces of string-1 is padded to the full string.

possible values for size variable are : 2, 4, 8, 16
possible values for interval variable are : any value from 1 to value of size variable

My requirement is to write a mathematical equation fetch index(in full string) of nth number of character of string-1 without separating the full string into its 2 original strings.

for e.g. In above full string, if I want to fetch 5th character of string-1 it will be '2' whose index in full string is 12. and for 6th character which is '1' has the index 15.

P.S. index of the character is counted from 0 to index, and the nth character is counted from 1 to n

1 Answers 1

1

Given character $n$ of string $1$, it will be in the $\lfloor \frac{n}{interval}\rfloor +1$ block. The reason I used floor instead of ceiling (which would get rid of the $+1$) is that integer divide gives the floor. There will be $(\lfloor \frac{n}{interval}\rfloor )size$ characters from string $2$ preceeding it, so it will be at position $(\lfloor \frac{n}{interval}\rfloor )size+n$ in the final string. Note that you are inconsistent in the referencing-you talk about the $5$th character as $2$, but it is in position $4$ if you index from $0$ and that you say both strings will have $32$ characters in contrast to the example. This will be correct as long as you don't run out of string $2$ characters. If you use all the string $2$ characters, the position will be $n+32$ if string $2$ is $32$ characters long.

  • 0
    In PHP i wrote : min(32,(floor($n/$interval)*$size))+$n It handles both the cases.2011-12-07