PSPP Assignment 1
PSPP Assignment 1
Factorial of a Number
Date:11/10/24 Using Recursion
Aim:
Write a recursive function that calculates the factorial of a number.
Program:
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
print("KRISHNA.J T24D124")
print(factorial(int(input("Enter a number to find its factorial: "))))
Output:
Exp No: 2 Sum of All Elements in
Date:11/10/24 Nested Lists
Aim:
Write a function that takes a nested list of integers and returns the sum of all
elements
Program:
print("KRISHNA.J T24D124")
def nested_list(lst):
sum=0
for i in lst:
if type(i)==list:
sum+=nested_list(i)
else:
sum+=i
return sum
print(nested_list([1, [2, 3], [4, [5]]]))
Output:
Exp No: 3 Reverse Each Word in
Date:11/10/24 a Sentence
Aim:
Write a function that takes a string sentence and reverses each word while
keeping the order of words intact.
Program:
print("KRISHNA.J T24D124")
def reverse_each_word(sentence):
words = sentence.split()
reversed_words = [word[::-1] for word in words]
reversed_sentence = ' '.join(reversed_words)
return reversed_sentence
print(reverse_each_word(input("Enter a sentence: ")))
Output:
Exp No: 4 Find the Maximum of Three Numbers Using Nested
Date:11/10/24 Functions
Aim:
Write a function that takes three numbers and returns the maximum using nested
function calls.
Program:
print("KRISHNA.J T24D124")
def max_of_three(lst):
max=0
for i in lst:
if type(i)==list:
max_of_three(i)
else:
if i>max:
max=i
return max
print(max_of_three((12, 5, 21)))
Output:
Exp No: 5 Check Prime for a Range
Date:11/10/24 of Numbers
Aim:
Write a function that returns all prime numbers in a given range.
Program:
print("KRISHNA.J T24D124")
prime_no=[ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73,
79, 83, 89, 97]
def prime(a,b):
x=[]
for i in range(a,b):
if i in prime_no:
x.append(i)
return x
print(prime(10,20))
Output:
Exp No: 6 Find Nth Fibonacci Number
Date:11/10/24 Using Memoization
Aim:
Write a function that calculates the nth Fibonacci number using memoization to
optimize performance.
Program:
print("KRISHNA.J T24D124")
def fibonacci_memoized(no):
if no==0:
return 0
elif no==1:
return 1
else:
return fibonacci_memoized(no-1)+fibonacci_memoized(no-2)
print(fibonacci_memoized(int(input("Enter a number: "))))
Output:
Exp No: 7 Extract Even Numbers From
Date:11/10/24 Nested Lists
Aim:
Write a function that extracts all even numbers from a nested list structure.
Program:
print("KRISHNA.J T24D124")
x=[]
def even_numbers_nested(lst):
for i in lst:
if type(i)==list:
even_numbers_nested(i)
else:
if i%2==0:
x.append(i)
return x
print(even_numbers_nested(([1, [2, 3], [4, [6]]])))
Output:
Exp No: 8 Convert Nested Dictionary to
Date:11/10/24 Flat Dictionary
Aim:
Write a function to flatten a nested dictionary by concatenating keys.
Program:
print("KRISHNA.J T24D124")
new={}
def flatten_dict(a):
for i in a:
if type(a[i])==dict:
for j in a[i]:
new[i+"_"+j]=a[i][j]
else:
new[i]=a[i]
return new
print(flatten_dict({'a': 1, 'b': {'c': 2, 'd': 3}}))
Output:
Aim:
Count Vowels in a Nested List of Strings.
Program:
print("KRISHNA.J T24D124")
vowels=['a','e','i','o','u','A','E','I','O','U']
a_b=[]
def count_vowels(x):
for i in x:
if type(i)==list:
count_vowels(i)
else:
for j in vowels:
a_b.append(i.count(j))
count_vowels([["apple"], ["orange", "grape"]])
print(sum(a_b))
Output:
Exp No: 10 Find GCD of a List of Numbers
Date:11/10/24
Aim:
Write a function that takes a list of numbers and returns the greatest common
divisor (GCD) of all the numbers.
Program:
print("KRISHNA.J T24D124")
x=[]
def gcd_of_list(a,):
for i in range(1,min(a)):
if a[0]%i==0 and a[1]%i==0 and a[2]%i==0:
x.append(i)
gcd_of_list([24, 36, 48])
print(max(x))
Output:
Exp No: 11 Find the Longest
Date:11/10/24 Palindromic Substring
Aim:
Write a function to find the longest palindromic substring in a given string.
Program:
print("KRISHNA.J T24D124")
palindrom=[]
def longest_palindrome(a):
for i in [a[j:k] for j in range(len(a)) for k in range(j,len(a))]:
if i==i[::-1]:
palindrom.append(i)
return max(palindrom,key=len)
print(longest_palindrome(input("Enter a string: ")))
Output:
Exp No: 12 Generate All Permutations
Date:11/10/24 of a String
Aim:
Write a function to generate all permutations of a given string.
Program:
Output:
Exp No: 13 Count Frequency of Each
Date:11/10/24 Character in a String
Aim:
Write a function to count the frequency of each character in a string.
Program:
print("KRISHNA.J T24D124")
y={}
def char_frequency(x):
for i in x:
y[i]=x.count(i)
char_frequency(input("Enter the string : "))
print(y)
Output:
Exp No: 14 Find the First Non-Repeating
Date:11/10/24 Character in a String
Aim:
Write a function to find the first non-repeating character in a string.
Program:
print("KRISHNA.J T24D124")
def first_non_repeating(x):
for i in x:
if x.count(i)==1:
return i
print(first_non_repeating(input("Enter a string: ")) )
Output:
Exp No: 15 Convert a String to CamelCase
Date:11/10/24
Aim:
Write a function that converts a sentence to camelCase format.
Program:
print("KRISHNA.J T24D124")
def to_camel_case(x):
x=x.split(" ")
for i in range(1,len(x)):
x[i]=x[i].title()
x="".join(x)
return x
print(to_camel_case(input("Enter a string: ")))
Output:
Exp No: 16 Find All Subsequences
Date:11/10/24 of a String
Aim:
Write a function to find all subsequences (not just substrings) of a given string.
Program:
print("KRISHNA.J T24D124")
def subsequences(x):
sub=[]
for i in range(len(x)):
for j in range(i+1,len(x)+1):
sub.append(x[i:j])
return sub
print(subsequences(input("Enter string : ")))
Output:
Exp No: 17 Find All Palindromic
Date:11/10/24 Substrings
Aim:
Write a function to find all palindromic substrings in a given string.
Program:
print("KRISHNA.J T24D124")
palindrom=[]
def find_palindromes(a):
for i in [a[j:k+1] for j in range(len(a)) for k in range(j,len(a))]:
if i==i[::-1]:
palindrom.append(i)
find_palindromes(input("Enter a string: "))
print(palindrom)
Output:
Exp No: 18 Count the Number of Words in a String Ignoring
Date:11/10/24 Multiple Spaces
Aim:
Write a function that counts the number of words in a string, ignoring multiple
spaces.
Program:
print("KRISHNA.J T24D124")
def count_words(a):
a=a.split()
while " " in a:
a.remove(" ")
return len(a)
print(count_words(input("Enter string : ")))
Output:
Exp No: 19 Replace All Occurrences of a Substring Without Using
Date:11/10/24 Replace()
Aim:
Write a function to replace all occurrences of a substring in a string without using
the built-in replace() method.
Program:
print("KRISHNA.J T24D124")
def replace_substring(org,w1,w2):
x=org.split()
for i in range(len(x)):
if x[i]==w1:
x[i]=w2
return " ".join(x)
print(replace_substring(input("Enter the string : "),input("Enter the word to be replaced :
"),input("Enter the new word: ")))
Output:
Exp No: 20 Check if Two Strings are Rotations
Date:11/10/24 of Each Other
Aim:
Write a function to check if two strings are rotations of each other.
Program:
print("KRISHNA.J T24D124")
def is_rotation(w1,w2):
if len(w1)!=len(w2):
return False
w1=w1+w1
if w2 in w1:
return True
else:
return False
print(is_rotation(input("Enter the first string : "),input("Enter the second string : ")))
Output:
Exp No: 21 Find the Longest Increasing
Date:11/10/24 Subsequence in a List
Aim:
Write a function to find the longest increasing subsequence in a list.
Program:
Output:
Exp No: 22 Rotate a List Left
Date:11/10/24 by N Places
Aim:
Write a function that rotates a list left by n places.
Program:
print("KRISHNA.J T24D124")
def rotate_left(lis,n):
for i in range(n):
lis.append(lis.pop(0))
return lis
print(rotate_left([1, 2, 3, 4, 5], 2))
Output:
Exp No: 23 Find All Unique Pairs in a List
Date:11/10/24 that Sum to a Target
Aim:
Write a function to find all unique pairs of numbers in a list that sum up to a
target value.
Program:
print("KRISHNA.J T24D124")
def find_pairs(lis,N):
pairs=[]
for i in range(len(lis)):
for j in range(i+1,len(lis)):
if lis[i]+lis[j]==N:
pairs.append((lis[i],lis[j]))
return pairs
print(find_pairs([1, 2, 3, 4, 5], 5))
Output:
Exp No: 24 Flatten a Nested List
Date:11/10/24
Aim:
Write a function to flatten a deeply nested list into a single list.
Program:
print("KRISHNA.J T24D124")
x=[]
def flatten_list(lis):
for i in lis:
if type(i)==list:
flatten_list(i)
else:
x.append(i)
flatten_list([1, [2, [3, 4], 5], [6, 7]])
print(x)
Output:
Exp No: 25 Find the Kth Smallest Element
Date:11/10/24 in a List
Aim:
Write a function to find the kth smallest element in an unsorted list.
Program:
print("KRISHNA.J T24D124")
def kth_smallest(lis,N):
lis.sort()
return lis[N-1]
print(kth_smallest([7, 10, 4, 3, 20, 15], 3))
Output:
Exp No: 26 Find Common Elements in
Date:11/10/24 Two Sorted Lists
Aim:
Write a function to find the common elements in two sorted lists.
Program:
print("KRISHNA.J T24D124")
def common_elements(lis1,lis2):
common=[]
for i in lis1:
if i in lis2:
common.append(i)
return common
print(common_elements([1, 3, 4, 6], [3, 6, 7, 9]))
Output:
Exp No: 27 Sort a List of Tuples Based on the
Date:11/10/24 Second Element
Aim:
Write a function to sort a list of tuples based on the second element of each
tuple.
Program:
print("KRISHNA.J T24D124")
def sort_by_second(lis):
return sorted(lis,key=lambda x:x[1])
print(sort_by_second([(1, 3), (4, 2), (5, 1)]))
Output:
Exp No: 28 Find Missing Numbers
Date:11/10/24 in a List
Aim:
Write a function that finds the missing numbers in a sequence.
Program:
print("KRISHNA.J T24D124")
def find_missing(lis):
miss=[]
for i in range(1,max(lis)):
if i not in lis:
miss.append(i)
return miss
print(find_missing([1, 2, 4, 6]))
Output:
Exp No: 29 Find the Most Frequent Element
Date:11/10/24 in a List
Aim:
Find the Most Frequent Element in a List.
Program:
print("KRISHNA.J T24D124")
def most_frequent(lis):
freq={}
for i in lis:
freq[i]=lis.count(i)
return max(freq,key=lambda x : freq[x])
print(most_frequent([1, 3, 2, 3, 3, 4, 1]))
Output:
Exp No: 30 Find All Subsequences
Date:11/10/24 of a List
Aim:
Write a function to find all subsequences of a given list.
Program:
print("KRISHNA.J T24D124")
def list_subsequences(lis):
sub=[[]]
for i in range(len(lis)):
for j in range(i,len(lis)):
sub.append(lis[i:j+1])
return sub
print(list_subsequences([1, 2, 3]))
Output:
Result:
The above programs were executed successfully and the output is verified.