Python Array Solution Q6

 Create an array of 8 integers. Shift all elements of the array to the right by two positions. Print the modified array.


from array import array


# Create an array of 8 integers

original_array = array('i', [1, 2, 3, 4, 5, 6, 7, 8])


# Shift all elements to the right by two positions

shifted_array = array('i', [0] * 2 + original_array[:-2])


# Print the original and shifted arrays

print("Original Array:", original_array)

print("Shifted Array (Right by Two Positions):", shifted_array)



In this program:


An array of integers ('i' typecode) named original_array is created with initial values.

The shifted_array is created by concatenating two zero elements ([0] * 2) with all elements of the original_array except the last two elements (original_array[:-2]).

The original and shifted arrays are printed.

This program demonstrates how to shift elements of an array to the right by a specified number of positions. You can replace the values in the original_array with your own set of values if needed.


Comments

Popular posts from this blog

Inter Board Karachi Guess/Model Papers 2023 (1st year & 2nd year)

Class X Test / Model Paper Chapter No 1 Computer

Dulla Bhatti Real Hero Of Muslims Rajput