Mapping all numbers in a set to a particular number This is not homework, I am a programmer and I encountered this problem in the implementation of an algorithm of mine.
I was wondering if the following can be done without the use of any auxiliary data structure (such as arrays) and without if statements or variable length loops, just by using some smart mathematical approach. I also thought about interpolation, but that seems to beat the purpose for this set of data.
Auxiliary variables can be used, but only such that they can contain an integer at most.

Is there a way to map all the numbers in the sets to the indicated value through the use of some function ? Maybe modulo operations ?
$S_0 = \text{{0, 3,6,27,30,33,54,57,60}} \rightarrow 0$
$S_1 = \text{{1, 4,7,28,31,34,55,58,61}} \rightarrow 1$
$S_2 = \text{{2, 5,8,29,32,35,56,59,62}} \rightarrow 2$
$S_3 =\text{{9, 12,15,36,39,42,63,66,69}} \rightarrow 3$
$S_4 =\text{{10, 13,16,37,40,43,64,67,70}} \rightarrow 4$
$S_5 =\text{{11, 14,17,38,41,44,65,68,71}} \rightarrow 5$
$S_6 =\text{{18, 21,24,45,48,51,72,75,78}} \rightarrow 6$
$S_7 =\text{{19, 22,25,46,49,52,73,76,79}} \rightarrow 7$
$S_8 =\text{{20, 23,26,47,50,53,74,77,80}} \rightarrow 8$

Basically I'm looking for a function $f$ that satisfies $f(s_{ij}) =i $ for any element $s_{ij}$ in the set $S_i$ that does the minimum number of computations.
I've been trying to connect the patterns for almost $3$ hours now, if anyone can help, I would be most grateful.
 A: It seems like your patterns come in blocks. If x is the number you want to de-index, you can first modulo it to get the block number, and the index within that block
a = x % 9
b = x / 9

$a$ is the block number. You can use a simple function c = 3 * a%3 to map $a = 0,1,2,3,4,5,6,7,8$ to the "initial row" of block a $c = 0,3,6,0,3,6,0,3,6$.
Similarly, map $b = 0,1,2,3,4,5,6,7,8$ to $d = 0,1,2,0,1,2,0,1,2$. $c+d$ then gives you the desired result.
Edit: David K pointed out that $a$ and $b$ should be reversed, so
$$ f(x) = 3(x/9)\% 3 + x\%3
$$
A: Let $f(s)=s \pmod 3 +3[(s \pmod {27})/9]$ where the division is integer division-ignore any remainder.
A: I find it easiest to describe the problem in terms of a function.
You want a function $f(n)$ such that if $n$ is in the set
$S_k$, then $f(n) = k$.
In other words, $f$ maps each element of $S_0$ to $0$,
each element of $S_1$ to $1$, etc.
The pattern repeats in blocks of $27$ numbers.
That is, the sequence of values $f(27), f(28), f(29), f(30), \ldots, f(53)$
is an exact repetition of
the sequence of values $f(0), f(1), f(2), f(3), \ldots, f(26)$.
So, first step, let $p = n \bmod 27$, where $a \bmod b$ is defined as the
remainder when integer $a$ is divided by non-zero integer $b$
using only an integer quotient, no fractions.
(This is p = n % 27 in C and other programming languages that have that
kind of "modulo" or "remainder" operator).
For non-negative values of $n$ (which is all we have to deal with here),
this implies that $0 \leq p < 27$.
Now let $q = \left\lfloor \frac p9 \right\rfloor$
(q = p / 9 if p and q are declared "integer" and your programming
language rounds integer division toward zero).
Then $q$ is one of the three values $0,1,2$, identifying whether
$p$ is in the first three sets $S_0,S_1,S_2$ ($0 \leq p \leq 8$),
the middle three sets $S_3,S_4,S_5$ ($9 \leq p \leq 17$), 
or the last three sets $S_6,S_7,S_8$ ($18 \leq p \leq 26$).
(Keep in mind that $n$ will be in the same set as $p$.)
Now let $r = p \bmod 3$ or $r = n \bmod 3$ (it will be the same result
either way).
Then $r$ is one of the three values $0,1,2$, identifying which of the three
sets $p$ lies in within the group of three sets identified by $q$.
Finally, putting it all together:
$$
f(n) = 3q + r 
= 3 \left\lfloor \frac{n \bmod 27}{9} \right\rfloor + n \bmod 3.
$$
