Computer
Computer
Program 2
test_str = input("Enter the string ")
print("The original string is : " + str(test_str))
res = ""
for idx in range(len(test_str)):
if not idx % 2 :
res = res + test_str[idx].upper()
else:
res = res + test_str[idx].lower()
print("The alterate case string is : " + str(res))
Program 3
def second_largest(list):
list.sort()
return list[-2]
li=[]
n=int(input("Enter size of list: "))
for i in range(0,n):
e=int(input("Enter element of list: "))
li.append(e)
Program 4
NumList = []
j = 0
n = len( theSeq )
for i in range( n - 1 ) :
flag = 0
for j in range(n - 1) :
if theSeq[j] > theSeq[j + 1] :
tmp = theSeq[j]
theSeq[j] = theSeq[j + 1]
theSeq[j + 1] = tmp
flag = 1
if flag == 0:
break
return theSeq
li=[]
n=int(input("Enter size of list: "))
for i in range(0,n):
e=int(input("Enter element of list: "))
li.append(e)
Program 6
def insertionSort(array):
for step in range(1, len(array)):
key = array[step]
j = step - 1
while j >= 0 and key < array[j]:
array[j + 1] = array[j]
j = j - 1
array[j + 1] = key
data = []
n=int(input("Enter size of list: "))
for i in range(0,n):
e=int(input("Enter element of list: "))
data.append(e)
insertionSort(data)
print("Sorted List in Ascending Order:",data)
Program 7
list1=[22,4,16,38,13]
choice=0
while True:
print("The list has the following elements",list1)
print("\n L I S T O P E R A T I O N S")
print("1.Append an element")
print("2.Inerst an element at the desired position")
print("3.Append a list to the given list")
print("4.Modify an existing element")
print("5.Delete an existing element by its position")
print("6.Delete an existing element by its value")
print("7.Sort the list in ascending order")
print("8.Sort the list in descending order")
print("9.Display the list")
print("10.Exit")
choice=int(input("ENTER YOUR CHOICE (1-10):"))
if choice==1:
element=int(input("Enter the element to be appended: "))
list1.append(element)
print("The element has been appended\n")
elif choice==2:
element=int(input("Enter the element to be inserted: "))
pos=int(input("Enter the position "))
list1.insert(pos,element)
print("The element has been inserted\n")
elif choice==3:
newList=int(input("Enter the list to be appended: "))
list1.extend(newList)
print("The list has been appended\n")
elif choice==4:
i=int(input("Enter the position of the element to be modified: "))
if i < len(list1):
newElement=int(input("Enter the new element: "))
oldElement=list1[i]
list1[i]=newElement
print("The element",oldElement,"has been modified\n")
else:
print("Position of the element is more than the length of list")
elif choice==5:
i=int(input("Enter the position of the element to be deleted: "))
if i<len(list1):
element=list1.pop(i)
print("The element",element,"has been deleted\n")
else:
print("\nPosition of the element is more than the length of list")
elif choice==6:
element=int(input("\nEnter the element to be deleted: "))
if element in list1:
list1.remove(element)
print("\nThe element",element,"has been deleted\n")
else:
print("\nThe element",element,"is not present in the list")
elif choice==7:
list.sort()
print("\nThe list has been sorted")
elif choice==8:
list1.sort(reverse=True)
print("\nThe list has been sorted in the reverse order")
elif choice==9:
print("\n The list is ",list1)
elif choice==10:
break
else:
print("Choice is not valid")
print("\n\nPress any key to continue.............")
ch=input()
Program 8
Program 9
array = [10,51,2,18,4,31,13,5,23,64,29]
x = int(input("Please enter the element: "))
n = len(array)
result = linearSearch(array, n, x)
if(result == -1):
print("Element not found")
else:
print("Element found at index: ", result)
Program 10
li=[]
n=int(input("Enter size of list: "))
for i in range(0,n):
e=int(input("Enter element of list: "))
li.append(e)
Program 11
NumList = []
Number = int(input("How many elements in list :- "))
if( Number%2 != 0 ):
print("This program will not accept odd number.")
exit()
for i in range(1, Number + 1):
value = int(input("Please enter the Value of %d Element :- " %i))
NumList.append(value)
num = int(Number/2)
list1 = NumList[:num]
list2 = NumList[num:]
print("list : ",NumList)
print("list 1: ",list1)
print("list 2: ",list2)
Program 12
def rearrange(arr, n ) :
j = 0
for i in range(0, n) :
if (arr[i] > 0) :
temp = arr[i]
arr[i] = arr[j]
arr[j]= temp
j = j + 1
print(arr)
arr = [-12,11,-13,-5,6,-7,5,-3,-6]
n = len(arr)
rearrange(arr, n)