Basic Python Programs – 3

#----------------------------- 1 - Rotaing an array using function & while loop -------------

def reverse(start, stop, array):  # reversing funtion

	# No of iterations needed for reversing the list
	rev_Count = stop-start+1

	# By incrementing count value swapping of first and last elements is done.
	count = 0
	while((rev_Count)//2 != count):
		array[start+count], array[stop-count] = array[stop-count], array[start+count]
		count += 1
	return array


def left_Rot(array, size, div):

	# Reverse the Entire List
	start = 0
	stop = size-1
	array = reverse(start, stop, array)

	# Divide array into twosub-array
	
	# Divide First sub-array
	
    # Reverse the First sub-array
	start = 0
	stop = size-div-1
	array = reverse(start, stop, array)

	# Divide Second sub-array
	
	start = size-div
	stop = size-1
	array = reverse(start, stop, array)
	return array


array = []
ele = int(input("Enter the no. of elements : "))  # total no. of elements in the list length of list
# getting inputs from user
for i in range(ele):
	print(i+1,end=".")
	x = int(input("Enter the number : "))
	array.append(x)


size = len(array)
div = int(input("No. of elements to rotate : ")) # no. of elements to be rotated from start point, if div == size then array is same
print("\nRotating....\n\n")
print('Original array:', array)

if(div <= size):
	print('Rotated array: ', left_Rot(array, size, div))
else:
	div = div % size
	print('Rotated array: ', left_Rot(array, size, div))
print()

#----------------------------- 2 - Rotaing an array using function ---------------------------

array = [] # nitializing an empty array
ele = int(input("Enter the no. of elements : "))  # total no. of elements in the list length of list
# getting inputs from user
for i in range(ele):
	print(i+1,end=".")
	x = int(input("Enter the number : "))
	array.append(x)
# slicing approach to rotate the array
def arr_rot(array,div,num):
    array[:]=array[div:num]+array[0:div]
    return array
div = int(input("No. of elements to rotate : ")) # no. of elements to be rotated from start point, if div == size then array is same

print(array) 
print("\nRotating....\n")
print("Rotated list is",end="") 
print(arr_rot(array,div,len(array))) 
#----------------------------- 3 - Checking a list is Monotonic -----------------------------

def isMonotonic(list):  # Monotonic -- either all the values are in increasing or in decreasing order
	first, second = [], []
	first.extend(list)
	second.extend(list)
	first.sort()
	second.sort(reverse=True)
	if(first == list or second == list):
		print("Yes!. The list is Monotonic")

	else:
		print("No! The list is not Monotonic")
	return ""

values = []
ele = int(input("Enter the no. of elements : "))  # total no. of elements in the list length of list
# getting inputs from user
for i in range(ele):
	print(i+1,end=".")
	nums = int(input("Enter the number : "))
	values.append(nums)

print(isMonotonic(values))
#------------------------------ 3 - Finding the remainder for list --------------------------

# multiplies all element in the list and divides with divisor

import math   # pip install math

def Remainder(arr, num):
	# Calculating the sum of logarithms 
	# of array elements and storing them
	# in the variable below
	LogSum = 0
	for i in arr:
		LogSum += math.log10(i)
	
	# Take exponential of the sum and return remainder
	remainder = int(pow(10, LogSum)) % num
	return remainder

values = []
ele = int(input("Enter the no. of elements : "))  # total no. of elements in the list length of list
# getting inputs from user
divisor = int(input("\nEnter the divisor : "))
print()
for i in range(ele):
	print(i+1,end=".")
	nums = int(input("Enter the number : "))
	values.append(nums)
remains = Remainder(values, divisor)
print("The remainder of the list is ",remains)

Join our team, where career growth knows no limits.

For More Details

Our internship program guarantees personalized attention for optimal career development in the tech industry.

+91 97879 68375

-> You should bring your own laptop during the internship

Who can apply?

Internships are open for students who: B.E / B.Tech / M.E / M.Tech / B.Sc / M.Sc / M.C.A / B.C.A / Diploma.

  • Ongoing Students Only
  • days, 5days/ 10days or Based on Student Preference
  • Limited 1 to 10 students per slot
  • Registration fees : 1000 INR
  • Slots are filling quickly only fewer slots are available