Ayushi
Ayushi
1 Write a program that reads an integer value and prints —leap year 2 04-7-22
or —not a leap year.
2 Write a program that takes a positive integer and then produces n 3 07-7-22
lines of output shown as follows.
4 Write a function that takes an integer n as input and calculates the 5 15-7-22
value of 1 + 1/1! + 1/2! + 1/n!
5 Write a function that takes an integer input and calculates the 6 19-7-22
factorial of that number.
7 Write a list function to convert a string into a list, as in list (-abc) 8 29-7-22
gives [a, b, c].
9 Write a program to check whether the input number is even or odd. 10 10-8-22
10 Write a program to compare three numbers and print the largest 11 17-8-22
one.
13 Write a program to create Stack Class and implement all its methods, 14 13-9-22
(Use Lists).
14 Write a program to create Queue Class and implement all its 16 22-9-22
methods, (Use Lists)
16 Write a program to sort a list using insertion sort and bubble sort 22 14-10-
and selection sort.
22
Project -1
Q[1] Write a program that reads an integer value and prints —leap year or —not a leap
year.
else:
Output:-
Project -2
Q[2] Write a program that takes a positive integer and then produces n lines of output
shown as follows.
Output:-
Project -3
Q[3] Write a program to create the following Pattern for example enter a size: 5
*
* *
* * *
* * * *
* * * * *
print()
Output:-
* *
* * *
* * * *
* * * * *
Project -4
Q[4] Write a function that takes an integer n as input and calculates the value of
1 + 1/1! + 1/2! + 1/n!
def calculate_sum():
# Initialize sum to 0
sum = 0
# Iterate from 1 to n
factorial = 1
factorial *= j
sum += 1/factorial
return sum + 1
result = calculate_sum()
print(result)
Output :-
Enter an integer n: 5
2.716666666666667
Project - 5
Q[5] Write a function that takes an integer input and calculate the factorial of that number.
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
Output :-
Project - 6
Q[6] Write a function that takes a string input and checks if it is a palindrome or not.
if (string==string[::-1]) :
print("The letter is a palindrome")
else:
print("The letter is not a palindrome")
Output :-
Project – 7
Q[7] Write a list function to convert a string into a list , as in list (-abc) gives[a,b,c].
def string_to_list():
char_list = []
char_list.append(char)
return char_list
char_list = string_to_list()
print(char_list)
Output :-
Project - 8
n1, n2 = 0, 1
count = 0
if nterms <= 0:
elif nterms == 1:
print(n1)
else:
print("Fibonacci sequence")
while count < nterms:
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1
Output :-
How many terms? 5
Fibonacci sequence
Project – 9
Q[9] Write a program to check whether the input number is even or odd.
if num % 2 == 0:
else:
Output:-
Enter a number: 3
The number is odd.
Enter a number: 2
The number is even.
Project – 10
Q[10] Write a program to compare three numbers and print the largest one.
else:
Output :
Project – 11
if num % i == 0:
print(i)
Output :
Enter a number: 15
1
3
5
15
Project – 12
while b != 0:
a, b = b, a % b
return a
Output :
Project – 13
Q[13] Write a program to create Stack Class and implement all its methods, (Use Lists).
class Stack:
def __init__(self):
self.items = []
def is_empty(self):
return not self.items
def pop(self):
return self.items.pop()
def peek(self):
return self.items[-1]
def size(self):
return len(self.items)
stack = Stack()
while True:
print("Enter 1 to push an element to the stack.")
print("Enter 2 to pop an element from the stack.")
print("Enter 3 to peek at the top element of the stack.")
print("Enter 4 to check the size of the stack.")
print("Enter 5 to quit.")
choice = int(input())
if choice == 1:
stack.push(element)
elif choice == 2:
if stack.is_empty():
else:
elif choice == 3:
if stack.is_empty():
else:
elif choice == 4:
else:
break
Output :-
2
Popped element: 2
3
Cannot peek at an empty stack.
4
Size of stack: 0
Project – 14
Q[14] Write a program to create Queue Class and implement all its methods, (Use Lists)
class Queue:
def __init__(self):
self.queue = []
self.queue.append(item)
def dequeue(self):
return self.queue.pop(0)
def peek(self):
return self.queue[0]
def is_empty(self):
return len(self.queue) == 0
def size(self):
return len(self.queue)
queue = Queue()
while True:
print("Enter 0 to exit")
choice = int(input())
if choice == 1:
queue.enqueue(value)
elif choice == 2:
if queue.is_empty():
else:
elif choice == 3:
if queue.is_empty():
else:
elif choice == 4:
print(queue.is_empty())
elif choice == 5:
elif choice == 0:
break
else:
print("Invalid choice")
Output :-
1
Enter an integer to enqueue into the queue: 3
2
Dequeued 3 from the queue
3
The queue is empty
Enter 1 to enqueue an element into the queue
Enter 2 to dequeue an element from the queue
Enter 3 to peek at the front element of the queue
Enter 4 to check if the queue is empty
Enter 5 to check the size of the queue
Enter 0 to exit
4
True
Project – 15
for i in range(len(arr)):
if arr[i] == target:
return i
return -1
left = 0
right = len(arr) - 1
if arr[mid] == target:
return mid
left = mid + 1
else:
right = mid - 1
return -1
while True:
print("Enter 0 to exit")
choice = int(input())
if choice == 1:
if result == -1:
else:
elif choice == 2:
if result == -1:
else:
elif choice == 0:
break
else:
print("Invalid choice")
Output :-
1
Enter an integer to search for (linear search): 4
Element found at index 3
2
Enter an integer to search for (binary search): 8
Element found at index 7
Project – 16
Ayushi Singh Page 21
Programming In Python BCA-III (2022-23)
# Write a program to sort a list using insertion sort and bubble sort and selection sort.
def insertion_sort(arr):
key = arr[i]
j=i-1
arr[j+1] = arr[j]
j -= 1
arr[j+1] = key
return arr
def bubble_sort(arr):
for i in range(len(arr)):
return arr
def selection_sort(arr):
for i in range(len(arr)):
min_index = i
min_index = j
return arr
arr = [5, 2, 4, 1, 3]
while True:
print("Enter 0 to exit")
choice = int(input())
if choice == 1:
result = insertion_sort(arr)
print(result)
elif choice == 2:
result = bubble_sort(arr)
print(result)
elif choice == 3:
result = selection_sort(arr)
print(result)
elif choice == 0:
break
else:
print("Invalid choice")
Output :-
Ayushi Singh Page 23
Programming In Python BCA-III (2022-23)
1
[1, 2, 3, 4, 5]
2
[1, 2, 3, 4, 5]
3
[1, 2, 3, 4, 5]
Enter 0 to exit