2.1 Python Ass
2.1 Python Ass
Experiment: 2.1
Aim: Program to demonstrate the various kind of operations that can be applied to a string
Source code:
def is_symmetrical(string):
length = len(string)
for i in range(length // 2):
if string[i] != string[length - i - 1]:
return False
return True
def is_palindrome(string):
string = string.lower().replace(" ", "")
return is_symmetrical(string)
input_string = input("Enter a string: ")
if is_palindrome(input_string):
print(f"{input_string} is a palindrome.")
else:
print(f"{input_string} is not a palindrome.")
if is_symmetrical(input_string):
print(f"{input_string} is symmetrical.")
else:
print(f"{input_string} is not symmetrical.")
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Q2) Write a python program to find uncommon words from two strings.
Source code:
def funcommon_words(string1, string2):
w1 = s1.split()
w2 = s2.split()
set1 = set(w1)
set2 = set(w2)
uncommon_words = (set1.symmetric_difference(set2))
return uncommon_words
s1 = "ashutosh is great at coding"
s2 = "rakshash is great at coding"
uncommon_words = funcommon_words(s1, s2)
print("Uncommon words:", uncommon_words)
Q3)Write a python program to add ‘-ing’ at the end of a given string where string length should be at least
3. If the string already ends with -ing and -ly. If the string length is less than 3 then leave it unchanged.
Source code:
def add_ing_or_ly(string):
if len(string) < 3:
return string
elif string[-3:] == 'ing':
return string[:-3] + 'ly'
else:
return string + 'ing'
string1 = 'read'
string2 = 'writing'
string3 = 'swim'
print(add_ing_or_ly(string1))
print(add_ing_or_ly(string2))
print(add_ing_or_ly(string3))
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Q4) Not everyone probably knows that Chef has younger brother Jeff. Currently Jeff learns to read.
He knows some subset of the letter of Latin alphabet. In order to help Jeff to study, Chef gave him a book
with the text consisting of N words. Jeff can read a word iff it consists only of the letters he knows.
Now Chef is curious about which words his brother will be able to read, and which are not. Please help
him!
Source code:
NO_OF_ALPHABETS = 26
alphabet_present = [False] * NO_OF_ALPHABETS
word = input()
for char in word:
alphabet_present[ord(char) - ord('a')] = True
no_of_words = int(input())
for i in range(no_of_words):
word = input()
can_read = True
for char in word:
if not alphabet_present[ord(char) - ord('a')]:
can_read = False
break
print("Yes" if can_read else "No")