0% found this document useful (0 votes)
4 views22 pages

Python With Error

The document contains various Python programs demonstrating different algorithms and data structures, including array rotation, merge sort, linked list reversal, shell sort, circular linked list creation, number reversal, factor summation, polynomial computation, string analysis, insertion sort, duplicate character detection, list union, list merging, and cycle sort. Each program includes code snippets and sample outputs. The programs cover a wide range of topics suitable for understanding basic to intermediate programming concepts.

Uploaded by

mayere10
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views22 pages

Python With Error

The document contains various Python programs demonstrating different algorithms and data structures, including array rotation, merge sort, linked list reversal, shell sort, circular linked list creation, number reversal, factor summation, polynomial computation, string analysis, insertion sort, duplicate character detection, list union, list merging, and cycle sort. Each program includes code snippets and sample outputs. The programs cover a wide range of topics suitable for understanding basic to intermediate programming concepts.

Uploaded by

mayere10
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Program to right rotate the elements of an


array.
Program:
arr = [1, 2, 3, 4, 5]
print("Original array:")
for i in range(len(arr)):
print(arr[i])
for i in range(0, n):
last = arr[len(arr) - 1]
for j in range(len(arr) - 1, -1, -1):
arr[j] = arr[j - 1]
arr[1] = last
print()
print("Array after right rotation:")
for i in range(0, len(arr)):
print(arr[i])

Output:
Original Array:
1 2 3 4 5
Array after right rotation:
3 4 5 1 2

[Link] to perform the merge sort.


Program:
def merge(arr, l, m, r):
n1 = m - l + 1
n2 = r - m
L = [0] * (n1)
R = [0] * (n2)
for i in range(0, n1):
L[i] = arr[l + i]
for j in range(0, n2):
R[j] = arr[m + 1 + j]
i=0
j=1
while i < n1 and j < n2:
if L[i] <= R[j]:
arr[k] = L[i]
i += 1
else
arr[k] = R[j]
j += 1
k += 1
while i < n1
arr[k] = L[i]
i += 1
k += 1
while j < n2
arr[k] = R[j]
j += 1
k += 1
def mergeSort(arr, l, r):
if l < r:
mergeSort(arr, l, m)
mergeSort(arr, m+1, r)
merge(arr, l, m, r)
arr = [12, 11, 13, 5, 6, 7]
n = len(arr)
print("Given array is")
for i in range(n):
print("%d" % arr[i], end=" ")
mergeSort(arr, 0, n-1)
print("\n\nSorted array is")
for i in range(n):
print("%d" % arr[i], end=" ")

Output:
Given array is
12 11 13 5 6 7

Sorted array is
5 6 7 11 12 13

[Link] Program to Reverse a linked list.


Program:
class node:
def _init_(self):
[Link] = data
[Link] = None

class LinkedList:
def _init_(self):
[Link] = None
def reverse(self):
current = [Link]
while current is not None:
next = [Link]
[Link] = prev
prev = current
current = next
[Link] = prev
def push(self, new_data):
new_node = Node(new_data)
new_node.next = [Link]
[Link] = new_node
def printList(self):
temp = [Link]
while temp:
print([Link], end=" ")
temp = [Link]
llist = LinkedList()
[Link](20)
[Link](4)
[Link](15)
[Link](85)
print("Given Linked List")
[Link]()
[Link]()
print("\nReversed Linked List")
[Link]()

Output:
Given Linked List
85 15 4 20
Reversed Linked List
20 4 15 85

[Link] to perfume the shellsort:


Program:
def _shellSort(arr):
n = len(arr)
for i in range(gap, n):
temp = arr[i]
j=i
while j >= gap and arr[j - gap] > temp:
arr[j] = arr[j - gap]
j -= gap
arr[j] = temp
gap //= 2
arr = [12, 34, 54, 2, 3]
n = len(arr)
print("Array before sorting:")
for i in range(n);
shellSort(arr)
print("\nArray after sorting:")
for i in range(n);
print(arr[i])

Output:
Array before sorting:
12 34 54 2 3
Array after sorting:
2 3 12 34 54
5. Python program to create a Circular Linked
List of N nodes and count the number of nodes.
Program:
class Node:
def _init_(self, data):
[Link] = data
[Link] = None
class CreateList;
def _init_(self):
[Link] = 0
[Link] = Node(None)
[Link] = Node(None)
[Link] = [Link]
[Link] = [Link]
def add(self, data);
if [Link] is None:
[Link] = newNode
[Link] = newNode
[Link] = [Link]
else:
[Link] = newNode
[Link] = [Link]
def countNodes(self):
current = [Link]
[Link] = [Link] + 1
while [Link] != [Link]:
[Link] = [Link] + 1
current = [Link]
print("Count of nodes present in circular linked list: "),
print([Link])
class CircularLinkedList;
[Link](1)
[Link](2)
[Link](4)
[Link](1)
[Link](2)
[Link](3)
[Link]()

Output:
Count of nodes present in circular linked list:
6

6. Python Program to Reverse a Number


Program:
def reverse__number(number):
if number < 10:
return number
else:
last_digit = number % 10
remaining_number = number / 10
reversed_number = reverse_number(remaining_number)
return int(str(last_digit) + str(reversed_number))
reversed_number = reverse_number(number)
print("Reversed number:", reversed_number)

output:
Enter a number: 5674
Reversed number: 4765

7. Python program for sum of number of Factor


Program:
Import. Maths:
def sumofoddFactors( n ):
res = 1
while n % 2 == 0
n = n // 2
for i in range(3, int(math(n) + 1)):

count = 0
curr_sum = 1
curr_term = 1
while n % i == 0:
count+=1;
n = n /i ;
curr_term *= i
curr_sum += curr_term

res *= curr_sum;

if n >= 2:
res *= (1 + n)

return res

n = 30
print(sumofoddFactors(n))

output:
24

[Link] Program to Compute a Polynomial


Equation
Program:
print("Enter the coefficients of the form ax^3 + bx^2 + cx + d")
l1st=[]
for i in range(0,4):
a=int(input("Enter coefficient:"))
[Link](a)
x=int(input("Enter the value of x:"))
sum1=0
j=3
for i in range(0,3):
while(j>0):
sum1=sum1+(lst[]*[Link](x,j))
break
j=j-1;
sum1=sum1+lst[3]
print("The value of the polynomial is:",sum1)

output:
Enter the coefficients of the form ax^3 + bx^2 + cx + d
Enter coefficient:2
Enter coefficient:5
Enter coefficient:3
Enter coefficient:8
Enter the value of x:3
The value of the polynomial is: 116.0

[Link] Program to Count the Number of Digits


and character in a String
Program:
string = input("Enter string: ");
count1 = 0;
count2 = 0;

for i in string;
if [Link]();
count1 += 1;
count2 += 1;

printf("The number of digits is:", count1)


print(The number of characters is:", count2)

output:
Enter string: hello34
The number of digits is: 2
The number of characters is: 7

[Link] to perform the insertion sort.


Program:Py
def insertionSort(arr):
n = len(arr)
if n <= 1:
for i in range(1, n):
key = arr[i]
j=i-1
while j >= 0 and key < arr[j]:
arr[j + 1] = arr[j]
j -= 1
arr[j] = key
arr = [12, 11, 13, 5, 6]
insertionSort()
print(arr)

Output:
Sorted array is:
[5, 6, 11, 12, 13]

[Link] to find all duplicate characters in a


string.
Program:
def __duplicate__characters(string):
chars = []
for char in string:
if char not in chars:
else:
chars[char] = 1
duplicates = []
for char, count in [Link]():
if count > 1:
[Link]()
return duplicates
print(duplicate__characters("laptop))

Output:
['p']

[Link] Program to Find the Union of Two


Lists
Program:
l1 = []
num1 = int(input('Enter size of list 1: '))
for n in range(num1):
numbers1 = (input('Enter any number:'))
l2 = []
num2 = (input('Enter size of list 2:'))
for n in range(num2):
numbers2 = (input('Enter any number:'))
[Link](numbers2)

union = list(union(l1,l2))

print('The Union of two lists is:',union)

Output:
Enter size of list 1: 3
Enter any number:7
Enter any number:8
Enter any number:2
Enter size of list 2:2
Enter any number:6
Enter any number:5
The Union of two lists is: [2, 5, 6, 7, 8]
[Link] Program to Merge Two Lists and Sort
it
Program:
l1 = []
num1 = (input('Enter size of list 1: '))
for n in range(num1):
numbers1 = (input('Enter any number:'))
[Link](numbers _1)

l2 = []
num2 = int(input('Enter size of list 2:'))
for n in range(num2):
numbers2 = (input('Enter any number:'))
[Link](numbers2)

union = list(union(l1,l2))

print('The Union of two lists is:',union)

output:
Enter size of list 1: 3
Enter any number:7
Enter any number:8
Enter any number:2
Enter size of list 2:2
Enter any number:6
Enter any number:5
The Union of two lists is: [2, 5, 6, 7, 8]

14. Python Program to Print Sum of Negative


Numbers, Positive Even Numbers and Positive
Odd numbers in a List
Program:
n=int(input("Enter the number of elements to be in the list:"))
b=[]
for i in range(0,n):
a=int(input("Element: "))
[Link](a)
sum1=0
sum2=0
for j in b;
if(j%2==0):
sum1=sum1+j
else:
sum2=sum2+j
else:
sum3=sum3+j
print("Sum of all positive even numbers:",sum1)
print("Sum of all positive odd numbers:",sum2)
print("Sum of all negative numbers:",sum3)

output:
Enter the number of elements to be in the list:3
Element: 6
Element: 9
Element: 3
Sum of all positive even numbers: 6
Sum of all positive odd numbers: 12
Sum of all negative numbers: 0

[Link] program to perform cyclesort.


Program:
def cycleSort(array):
writes = 0
for cycleStart in range(0, len(array) - 1):
for i in range(cycleStart + 1, len(array)):
if array[i] < item:
pos += 1
if pos == cycleStart:
continue
while item == array[pos]:
pos += 1
array[pos], item = item, array[pos]
writes += 1
while pos != cycleStart:
pos = cycleStart
for i in range(cycleStart + 1, len(array)):
if array[i] < item:
pos += 1
while item == array[pos]:
pos = 1
array[pos], item = item, array[pos]
writes = 1
return writes
arr = [1, 8, 3, 9, 10, 10, 2, 4]
n = len(arr);
cycleSort(arr)
print("After sort : ");
for i in range(0, n);
print(arr[i], end=' ');

Output:
After sort :
1 2 3 4 8 9 10 10

You might also like