OneCompiler

arrayoop

1643

from array import *
import traceback

flag = True
my_arr = array('i')
dummy_arr = array('i')

#function to accept array elemets from the user
def acceptElements():
n = int(input("Enter the total number of elements : "))
for i in range(n):
data = int(input("Enter element number %d : "%(i+1)))
my_arr.append(data)

#function to print the array
def printArray():
for i in my_arr:
print(i,end = " ")
print()

#function to accept an element from user and append it
def appendArray():
n = int(input("Enter the element to append: "))
my_arr.append(n)

#function to insert an element in the array
def insertArray():
pos = int(input("Enter the position to insert: "))
data = int(input("Enter the data: "))
my_arr.insert(pos,data)

#function to remove last element from the array
def removeLast():
print("%d is removed from the array"%(my_arr.pop()))

#function to print index of an element
def printIndex():
n = int(input("Enter the data element to print its index : "))
print("The index of %d is %d "%(n,my_arr.index(n)))

#function to print the sorted array in ascending order
def sortArray():
for i in range(0,len(my_arr) - 1):
for j in range(0,len(my_arr) -i - 1):
if(my_arr[j] > my_arr[j+1]):
temp = my_arr[j]
my_arr[j]= my_arr[j+1]
my_arr[j+1] = temp

#function to extend the original array
def extendArray():
size = int(input("How many elements in the new array : "))
for i in range(size):
data = int(input(f'Enter element number {i+1} : '))
dummy_arr.append(data)

print("The extended array is : ")
my_arr.extend(dummy_arr)

printArray()

#function t add items from list into array using fromlist() method
def addListInArray():
my_list = []
size = int(input("Enter the number of the elements in the list : "))
for i in range(size):
data = int(input(f'Enter element number {i+1} : '))
my_list.append(data)
print("Array after adding items from above list is : ")
my_arr.fromlist(my_list)
printArray()

#function to remove an element from the array
def removeElement(n):
if n in my_arr:
print(f'{n} is removed from the array')
else:
print(f'{n} does not exists in the array')

#function to fetch element at given index using fetch method
def fetchIndex(index):
if index < 0 or index >= len(my_arr):
print("Index out of bound")
return
else:
print(f'Element at index {index} is : {my_arr.index(index)}')

#function to reverse the array
def revArray():
my_arr.reverse()
print("Reversed array is : ")
printArray()

#function to get buffer information of the array
def bufferInfo():
print("Buffer information of the array is : ")
print(my_arr.buffer_info())

#function to count the occurence of the number in the array
def countNum(n):
print(f'The occurence of {n} in the array is : {my_arr.count(n)}')

#function to display the menu
def menu():
global flag
print("\nMENU")
print("0.Accept\n1.Print Array\n2.Append\n3.Insert\n4.Remove last element\n5.Prin
ch = input()

if ch == '0':
    acceptElements()
elif ch == '1':
    printArray()
elif ch == '2':
    appendArray()
elif ch == '3':
    insertArray()
elif ch == '4':
    removeLast()
elif ch == '5':
    printIndex()
elif ch == '6':
    print("Sorted array is : ")
    sortArray()
    printArray()
elif ch == '7':
    extendArray()
elif ch == '8':
    addListInArray()
elif ch == '9':
    n = int(input("Enter an element to remove from the array : "))
    removeElement(n)
elif ch == '10':
    index = int(input("Enter the index : "))
    fetchIndex(index)
elif ch == '11':
    revArray()
elif ch == '12':
    bufferInfo()
elif ch == '13':
    n = int(input("Enter an elememt to count its frequency"))
    countNum(n)
elif ch == '14':
    print("Exiting the program!")
    flag = False
    
else:
    print("Invalid choice!")



print("***********************************************************************")

#main function in python
if name == 'main':
try:
#creating an array
while flag == True:
menu()

except:
    print("\nAn error occured\n")
    traceback.print_exc()