How to generate cyclic sequence in php ?

Cyclic sequence referes to a sequence of points in a circle in clockwise or anticlockwise direction. We are implementing sequence of number from any point in both direction using php.

For example, we have four points 1,2,3 and 4. If we start from point 2, we will have sequence like 2,3,4,1 in one direction and 2,1,4,3 in another direction.

We will create a php function to achieve this. Lets sort out the parameters.

  1. start of the circle point
  2. end of the circle point
  3. our desired location from where we need cyclic sequence
  4. direction in which we need cyclic sequence.

We will use $start and $end as start and end points in the circle. We will use $point parameter to locate point from where we will start to generate cyclic sequence. And finally we will define an optional parameter $direction whose value can be clockwise or anticlockwise.

function generate_cyclic_sequence($start, $end, $point, $direction='clockwise')
{
     // Our logic
}

Note:

Most important thing to consider here is, we will have start and end point will be positive integers and point will lies anywhere between these two points.

We will generate a normal squence from start to end and then we will use separte logic to generate cyclic sequence in any direction. Our function overview will look like:

function generate_circular_sequence($start, $end, $point, $direction='clockwise')
{
        $circle_points = range($start, $end);

        if($direction == 'clockwise'){
             // do something
        }
        elseif($direction == 'anticlockwise'){
          // do something
        }

        else{
            return null;
        }
}

Now, we will generate in clockwise direction at first. Main logic behind clockwise direction is we will shift the points untill we get our point in clockwise direction. We will use array_shift php function for this. When we find our point we will stop the process, we will get the desired circular sequence.

if($direction == 'clockwise'){
    while($circle_points[0] != $point)
    {
        $shifted_point = array_shift($circle_points);

        $circle_points[] = $shifted_point;
    }

    return $circle_points;
}

In this way we can get the result in clockwise direction.

Sample Input:
generate_cyclic_sequence(1,12,8,'clockwise');
Sample Output:
[
  0 => 8
  1 => 9
  2 => 10
  3 => 11
  4 => 12
  5 => 1
  6 => 2
  7 => 3
  8 => 4
  9 => 5
  10 => 6
  11 => 7
]

Now, we will go towards anticlockwise direction.

Since array shift will not work in opposite direction we will use another approach.

At first, we will reverse the circle points orientation using array_reverse php function and then we will again shift the points using array_shift untill we will get our starting point for generating cyclic sequence.

Our structure for anticlockwise direction will look like:

$circle_points = array_reverse($circle_points);

while($circle_points[0] != $point)
{
    $shifted_point = array_shift($circle_points);

    $circle_points[] = $shifted_point;
}

return $circle_points;
Sample Input:
generate_cyclic_sequence(1,12,8,'anticlockwise');
Sample Output:
[
  0 => 8
  1 => 7
  2 => 6
  3 => 5
  4 => 4
  5 => 3
  6 => 2
  7 => 1
  8 => 12
  9 => 11
  10 => 10
  11 => 9
]

In this way, we can generate cyclic sequence in both clockwise and anticlockwise direction. Our overall function will be like this:

function generate_circular_sequence($start, $end, $point, $direction='clockwise')
{
    $circle_points = range($start, $end);

    if($direction == 'clockwise'){
        while($circle_points[0] != $point)
        {
            $shifted_point = array_shift($circle_points);

            $circle_points[] = $shifted_point;
        }

        return $circle_points;
    }
    elseif($direction == 'anticlockwise'){

        $circle_points = array_reverse($circle_points);

        while($circle_points[0] != $point)
        {
            $shifted_point = array_shift($circle_points);

            $circle_points[] = $shifted_point;
        }

        return $circle_points;
    }

    else{
        return null;
    }
}

Using this function, you can generate cyclic sequence from set of points in any desired direction in simple way.


share :

Sagar Gautam

A Computer Engineer from Nepal.


Leave your Feedback