Tanisha XII
Tanisha XII
Q5 FIND FACTORIAL
def factorial(n):
if n == 0 or n == 1:
return 1
return n * factorial(n – 1)
Q6 Fibonacci sequence
def fibonacci(n):
sequence = []
a, b = 0, 1
while len(sequence) < n:
sequence.append(a)
a, b = b, a + b
return sequence
Q11Count vowels
def count_vowels(s):
return sum(1 for char in s.lower() if char in “aeiou”)
Q17Sort a list
def sort_list(lst):
return sorted(lst)
2)Result=multiply(3,7)
Print ( result)
Output
21
3)print(find_max(10,20,15))
Output
20
4)print(odd_or_even(8))
Output
Even
5)Print(factorial (5))
Output
120
8)Print(is_prime(17))
Output
True
9)Print (sum_of_list([1,2,3,4]))
Output
10
10) print(is_palindrome(“madam”))
Output
True
12)Print(celsius _to_fahrenheit(0))
Output
32.0
13) print(find_gcd(48,18))
Output
6
14)Print(power(2,3))
Output
8
15) print(is_leap_year(2024))
Output
True
16)print(string_length(“python”))
Output
6
17)Print(sort_list([4,2,9,1]))
Output
[1,2,4,9]
20)Print(is_armstrong(153))
Output
True
CSV
FILE
21 Program to store student details
Import csv
24)WRITE TO A FILE
# Write to a text file
with open(“example.txt”, “w”) as file:
file.write(“Hello, this is the first line.\n”)
file.write(“This is the second line.”)
print(“Data written to example.txt”)
Output
Data written to example.txt
#INSERTION SORT:-
‘’’33.WAF isort()to arrange a list in ascending order using insertion sort.’’’
def isort(l):
for I in range (1,len(l)):
t=l[i]
j=i-1
while t<l[j] and j>=0:
l[j+1]=l[j]
j=j-1
l[j+1]=t
print(l)
L=eval(input(“enter a list :”))
isort(l)
#OUTPUT
enter a list :[3,5,2,1,5,6,8,3,0,4,2,0,7,8,9]
[0, 0, 1, 2, 2, 3, 3, 4, 5, 5, 6, 7, 8, 8, 9]
#SEARCHING
#LINEAR SEARCH:-
‘’’34.WAF TO PASS A LIST AND A NO. AND SEARCH IT IF FOUND RETURN 1 OTHERWISE -1’’’
def linear_search(l,n):
k=-1
for I in l:
if i==n:
k=1
print(k)
l=eval(input(“enter a list :”))
n=int(input(“enter no. :”))
linear_search(l,n)
#OUTPUT
enter a list :[1,4,6,7]
enter no. :6
1
#BINARY SEARCH:-
‘’’35.WAF to pass a list and a no. and search it using binary search.’’’
def binary_search(l,n):
k=0
beg=0
last=len(l)-1
while beg<=last:
mid=int((beg+last)/2)
if l[mid]==n:
k=1
break
elif l[mid]>n:
last=mid-1
elif l[mid]<n:
beg=mid+1
if k==0:
print(“not found”)
else:
print(“it is found”)
l=eval(input(“enter a list”))
n=int(input(“enter a no.”))
binary_search(l,n)
#OUTPUT
enter a list[1,2,3]
enter a no.1
it is found
#2D LIST (MATRIX)
‘’’36WAF that passes a 2d list and find the sum of first and last column’’’
def twod(l,r,c):
for I in range®:
print(l[i][0]+l[i][c-1])
l=[]
r=int(input(“enter no. of rows”))
c=int(input(“enter no. of columns”))
for I in range®:
row=[]
for j in range©:
x=int(input(“enter no.”))
row.append(x)
l.append(row)
twod(l,r,c)
#OUTPUT
enter no. of rows2
enter no. of columns2
enter no.1
enter no.2
enter no.3
enter no.4
3
7
# Stack Implementation
‘’’
Stack: implemented as a list
Top: Integer having position of topmost element in stack
‘’’
def isempty(stk):
if stk == []:
return True
else:
return False
def pop(stk):
if isempty(stk):
return “underflow”
else:
item = stk.pop()
return item
def peek(stk):
if isempty(stk):
return “underflow”
else:
return stk[-1]
Def display(stk):
if isempty(stk):
print(“stack empty”)
else:
print(stk[-1], “<-top”)
for a in range(len(stk)-2, -1, -1):
print(stk[a])
# MAIN
stack = []
while True:
print(“Stack operation”)
print(“1. Push”)
print(“2. Pop”)
print(“3. Peek”)
print(“4. Display stack”)
print(“5. Exit”)
ch = int(input(“Enter your choice (1-5): “))
if ch == 1:
item = int(input(“Enter item: “))
push(stack, item)
elif ch == 2:
item = pop(stack)
if item == “underflow”:
print(“Underflow! Stack is empty!”)
else:
print(“Popped item is”, item)
elif ch == 3:
item = peek(stack)
if item == “underflow”:
print(“Underflow! Stack is empty”)
else:
print(“Topmost item is”, item)
elif ch == 4:
display(stack)
elif ch == 5:
break
else:
print(“Invalid choice”)
#OUTPUT
Stack operation
1.push
2.pop
3.peek
4.display stack
5.exit
Enter your choice(1-5):1
Enter item :1
Stack operation
1.push
2.pop
3.peek
4.display stack
5.exit
Enter your choice(1-5):1
Enter item :2
Stack operation
1.push
2.pop
3.peek
4.display stack
5.exit
Enter your choice(1-5):1
Enter item :4
Stack operation
1.push
2.pop
3.peek
4.display stack
5.exit
Enter your choice(1-5):1
Enter item :5
Stack operation
1.push
2.pop
3.peek
4.display stack
5.exit
Enter your choice(1-5):4
5 <-top
4
2
1
Stack operation
1.push
2.pop
3.peek
4.display stack
5.exit
Enter your choice(1-5):3
Topmost item is 5
Stack operation
1.push
2.pop
3.peek
4.display stack
5.exit
Enter your choice(1-5):2
Popped item is 5
Stack operation
1.push
2.pop
3.peek
4.display stack
5.exit
Enter your choice(1-5):5