How to Shift Values of N Consecutive Variables in Python?


Values of n variables can be easily shifted by making use of an extra temporary variable. However, that is not a good practice. Today we will discuss shift values of N consecutive variables in Python without using a temporary variable. Here, we will try to shift values of 2, 3 & n variables in the left-to-right direction as well as the right-to-left direction.

Shift in case of two variables

If the language is python, then we can use an inbuilt syntax. Suppose two variables A & B with any random values say 5 & 7 respectively.

Here shifting the values from left-to-right or right-to-left makes the same sense. In this case, we can also call it swapping the values of the variables.

Consider the following python code –

A,B = 5,7 #declaring the values of two variables
B,A = A,B #swapping/shifting/rotating the values

If the language isn’t python, we can still implement it using a different approach that doesn’t make use of an extra variable. Consider the following code in python (which doesn’t make use of inbuilt syntax) –

A,B = 5,8
A = A+B
B = A-B
A = A-B

The aforementioned algorithm is used to swap values of 2 variables without making use of another temporary variable. And the good thing is that it can be implemented easily in any language.

Shift in case of three variables

Case I – Shift values Left-to-Right

Consider the following image

3 variables in left-to-right

From the above image, we can clearly see how we must shift the values of the 3 variables from left to right. Now, consider the following pseudo-code

  1. Declare three variables in the order of your choice
  2. Let the rightmost/last variables have a new value that is equal to the sum of values of all the variables
  3. Now start from the leftmost/first variable
  4. The new value of this variable is the difference between the last variable & the sum of the remaining variables.
  5. For example – A = ( C ) – ( A + B )
  6. Now proceed to the next variable & update its value as we did for the previous variable
  7. Traverse through all the variables in a sequential manner & update their values in the same manner.
  8. Remember to update the value of the rightmost/last variable as well

Let us now implement the pseudocode in python

a,b,c = 1,2,3
print("Original Values ↓")
print(a,b,c)
c=a+b+c
a=c-a-b
b=c-a-b
c=c-a-b
print("Shifted values to right ↓")
print(a,b,c)

Try to run this code & experience it yourself.

Case II – Shift values Right-to-Left

Consider the following image

From the image, we can clearly visualize the direction of the shift & how the result should look like. The pseudo-code for this is not very different from the one used while shifting left-to-right. It has some minor changes & let’s see what they are

  1. Declare the variables in the order of your choice.
  2. Select the leftmost/first variable & update it with a new value
  3. which is equal to the sum of the value of all the variables.
  4. Start from the rightmost/last variable
  5. Update its value which is equal to the difference between the values of the first variable & the sum of the remaining variables.
  6. For example, C = ( A ) – ( B + C )
  7. Now move to the 2nd last variable or just move backward.
  8. Update its value as well using the same method as for the last variable.
  9. Traverse all the variables, starting from last, moving backward & reaching all the way to the first variable.
  10. Make sure to again update the value of 1st variable as well

Let us now try to implement it in python

a,b,c=1,2,3
print("Original Values ↓")
print(a,b,c)
a=a+b+c
c=a-c-b
b=a-c-b
a=a-c-b
print("Shifted values to left ↓")
print(a,b,c)

Try to run this code to experience it yourself

Shift in case of N variables

After looking at the algorithms used for 3 variables, we can easily extend it to work for n variables as well.

Consider the above image. We can clearly visualize how to shift values in the case of n variables. The algorithm which was used for three variables can be used again here. You just need to add extra variables. Let’s implement in python –

#Values are being shifted left
a0 , a1 , a2 , ... , an = 1 , 2 , 3 , ... , n
print("Original Values ↓")
print(a0 , a1 , ... , an)
a0 = a0 + a1 + ... + an
an = a0 - ( an + an_1 + ... + a1 )
an_1 = a0 - ( an + an_1 + ... + a1 )
    .
    .
    .
a1 = a0 - ( an + an_1 + ... + a1 )
print("Shifted values to left ↓")
print(a0 , a1 , ... , an)

Note: You must obviously replace the ” … ” with the remaining variables.

Upon inspection, the code looks very lengthy for large values of n. The code can be rewritten to make it a bit more presentable without changing the gist of the problem. Let’s see how we can modify it.

Here consider an array whose element resembles the variables. So we will try to shift the values of the element in the array in either direction w/o using a 3rd variable.

The approach is explained by the following pseudocode. This is only for the left-to-right shift. The steps will be similar for shifting in the right-to-left direction.

  1. First, we need to add the value of all elements except the last one into the last element of the array
  2. Use a for loop to traverse from the start of the array to 2nd last element. At every iteration, add that element to the last element of the array
  3. Now we need to perform the step of taking the difference between the last element & the sum of the remaining element.
  4. Basically array[ith element] = array[last element] – SUM ( all elements except last)
  5. Let the last element value be called z & the current element value be called
  6. Let value of remaining elements be a,b,c,d …
  7. array[ith element] = -i – ( a+b+c+d+…+z ) + 2z = z – (a+b+…+i+…+x+y) = ( last element ) – ( sum of remaining element )
  8. when i = the last element then don’t take the -ve of i & then add 2*z
  9. Only subtract the remaining digits from the last element

Let us implement the code again in python using the new technique discussed above

Case I – Shift values Left-to-Right

    A = [1,2,3,4,5,6,7,8,9,10]
    print("Before \n",A)
    for i in range(-2,-len(A)-1,-1):
        A[-1]+=A[i]
    for i in range(0,len(A)):
        if i!=len(A)-1:
            A[i] = -A[i]
            A[i] = A[i] + 2*(A[len(A)-1])
        for j in range(0,len(A)):
            if j!=i:
                A[i] = A[i]-A[j]
    print("After \n",A)

Case II – Shift values Right-to-Left

    A = [1,2,3,4,5]
    print("Before \n",A)
    for i in range(1,len(A)):
        A[0]+=A[i]

    for i in range(-1,-len(A)-1,-1):
        if i!=-len(A):
            A[i] = -A[i]
            A[i] = A[i] + 2*(A[0])
        for j in range(-1,-len(A)-1,-1):
            if j!=i:
                A[i] = A[i] - A[j]
    print("After \n",A)

Note – To shift the values by n times, you have to loop the array n number of times in that direction.

Few Practice Problems:

  1. The program will shift/rotate the values of variables
  2. The number of the variables will be user-defined or generated randomly
  3. The values of the variable can be user-defined or generated randomly
  4. The values can be rotated in either the direction
  5. The direction of shifting can be user defined or selected randomly
  6. The amount of rotation can be defined
  7. The range of random numbers can be set by the user for every case

Feel free to drop in comments if you have any question or queries.

Spread the word!
0Shares

Leave a comment

Your email address will not be published. Required fields are marked *