views:

159

answers:

6

Is there a function that takes 1,2,3 and returns 3,1,2 respectively and vice-verse. eg: f(1)=3 f(2)=1 f(3)=2

This is required to get the cyclic order eg 1 when gone left will be 3 , 2 will be 1 and 3 will be 2. 1 when gone right will be 2 , 2 will be 3 and 3 will be 1.

+1  A: 

None but you can create your own function.

hallie
+2  A: 

Just make your own function to do it:

function(x) {
  var lookup = [3, 1, 2];
  return lookup[x - 1];
}
Matt Huggins
+3  A: 

f(x) = floor(3/x) + floor(x/3)

Damien_The_Unbeliever
+3  A: 

f(x) = (x + 1) mod 3 + 1

mrjoltcola
Wouldn't that return values in the range 0-2 in most programming languages?
Damien_The_Unbeliever
Yep, fixed, I think. :)
mrjoltcola
thanks mrjoltcola also the reverse is required to go clockwise ie 3,1,2==> 1,2,3
Thunder
+1  A: 

In Python:

def f(x):
    return ((x+4)%3)+1

In C:

int f(int x)
{
    switch(x)
    {
    case 1: return 3;
    case 2: return 1;
    case 3: return 2;
    default: return x;
    }
    return x;
 }

Although the mod (%) solution would work in C as well.

mtrw
+4  A: 

If the inputs and outputs are always the same ie 1,2,3 should return 3,1,2 then the most effiecent thing to do is to have something like this. Why bother with math functions with such limited ins and outs...

function(x)
{
    if (x == 1) return 3;
    if (x == 2) return 1;
    if (x == 3) return 2;
    !throw some unsupported error!
}
mouters