Skip to content

Commit 9b632c6

Browse files
committed
final commit for "Array Programs"
1 parent afedc75 commit 9b632c6

4 files changed

Lines changed: 120 additions & 0 deletions

File tree

Array Programs/ArrayRotation.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Python Program for array rotation
2+
3+
4+
def rotate_array(array, rotation_index):
5+
rotation_index -= 1
6+
new_arr = array[rotation_index:]
7+
new_arr.extend(array[:rotation_index])
8+
print(new_arr)
9+
10+
try:
11+
num = int(input('Enter the number of elements in the array: '))
12+
user_array = []
13+
for index in range(num):
14+
while True:
15+
value = input(f'Enter the {index + 1} element : ')
16+
if value:
17+
user_array.append(value)
18+
break
19+
else:
20+
print('Enter valid input')
21+
pivot = int(input('Enter the index on which you want to rotate : '))
22+
23+
if pivot > num:
24+
print('Pivot cannot be greater than length of list')
25+
else:
26+
rotate_array(user_array, pivot)
27+
except ValueError:
28+
print('Invalid Input')
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Find reminder of array multiplication divided by n
2+
# EXAMPLE :
3+
# Input : arr[] = {100, 10, 5, 25, 35, 14},
4+
# n = 11
5+
# Output : 9
6+
# 100 x 10 x 5 x 25 x 35 x 14 = 61250000 % 11 = 9
7+
8+
from functools import reduce
9+
10+
try:
11+
length_of_list = int(input('Enter the number of elements in the list : '))
12+
user_array = []
13+
for i in range(length_of_list):
14+
while True:
15+
value = int(input(f'Enter the {i + 1} element : '))
16+
if value:
17+
user_array.append(value)
18+
break
19+
else:
20+
print('Please enter valid input')
21+
22+
divisor = int(input('Enter the dividing number : '))
23+
if divisor:
24+
# step 1: reduce the list
25+
reduced = reduce(lambda acc, cur: acc * cur, user_array, 1)
26+
# step 2: divide by divisor
27+
result = reduced % divisor
28+
print(f'{reduced} when divided by {divisor} leaves a quotient : {result}')
29+
else:
30+
print('Invalid Divisor. Quitting')
31+
except ValueError:
32+
print('Invalid Input')
33+

Array Programs/ReconstructArray.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Reconstruct the array by replacing arr[i] with (arr[i-1]+1) % M
2+
3+
#get input
4+
try:
5+
num = int(input('Enter the length of the array : '))
6+
user_array = []
7+
for i in range(num):
8+
while True:
9+
value = int(input(f'Enter the {i + 1} element : '))
10+
if value >= 0:
11+
user_array.append(value)
12+
break
13+
else:
14+
print('Please enter valid element')
15+
16+
m_value = int(input('Enter the value of M : '))
17+
if not m_value:
18+
print('Enter valid value for M ')
19+
else:
20+
print(f'The original list : {user_array}')
21+
for i in range(1,num):
22+
user_array[i] = (user_array[i-1] + 1) % m_value
23+
print(f'The reconstructed list : {user_array}')
24+
except ValueError:
25+
print('Invalid Input')
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Python Program to Split the array
2+
# and add the first part to the end
3+
4+
5+
def split_and_switch(array, index):
6+
first_part = array[:index]
7+
last_part = array[index:]
8+
last_part.extend(first_part)
9+
return last_part
10+
11+
12+
try:
13+
num = int(input('Enter the size of the list : '))
14+
user_list = []
15+
for i in range(num):
16+
while True:
17+
value = input(f'Enter the {i + 1} element : ')
18+
if value:
19+
user_list.append(value)
20+
break
21+
else:
22+
print('Please enter valid input')
23+
24+
split_index = int(input('Enter the splitting index : '))
25+
if split_index:
26+
if split_index > num:
27+
print('The splitting index cannot be greater than the size of the lab')
28+
else:
29+
print('-'*50)
30+
print(split_and_switch(user_list, split_index))
31+
else:
32+
print('Enter valid splitting index')
33+
except ValueError:
34+
print('Invalid Input')

0 commit comments

Comments
 (0)