0% found this document useful (0 votes)
21 views5 pages

Experiment 2.1

This document contains the details of an experiment conducted by a student named Ravi Kumar. It includes 3 questions answered with Python code. Question 1 checks if a string is a palindrome or symmetrical. Question 2 finds uncommon words between two strings. Question 3 adds 'ing' or 'ly' to the end of a given string depending on its length and ending.

Uploaded by

Ravi Kumar
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)
21 views5 pages

Experiment 2.1

This document contains the details of an experiment conducted by a student named Ravi Kumar. It includes 3 questions answered with Python code. Question 1 checks if a string is a palindrome or symmetrical. Question 2 finds uncommon words between two strings. Question 3 adds 'ing' or 'ly' to the end of a given string depending on its length and ending.

Uploaded by

Ravi Kumar
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
You are on page 1/ 5

EXPERIMENT 2.

1
NAME – RAVI KUMAR
UID – 20BET1009
BRANCH – BE-IT
SECTION – 20BET601-A

QUES 1 - Python program to check whether the string is


Symmetrical or Palindrome?
CODE - def palin(string):
st = 0
end = len(string) - 1
f=0
while (st < end):

if (string[st] == string[end]):

st += 1
end -= 1

else:
f=1
break;
if f == 0:
print("The entered string is palindrome")
else:
print("The entered string is not palindrome")

def symm(string):
l = len(string)
flag = 0
if l % 2 == 0:
mid = l // 2
else:
mid = l // 2 + 1

s1 = 0
s2 = mid
while (s1 < mid and s2 < l):

if (string[s1] == string[s2]):
# of given string
s1 = s1 + 1
s2 = s2 + 1
else:
flag = 1
break

if flag == 0:
print("The entered string is symmetrical")
else:
print("The entered string is not symmetrical")

# Main code

string = input("Enter the string: ")


palin(string)
symm(string)

QUES 2- Python program to find uncommon words from two


Strings
CODE - def UncommonWords(A, B):
count = {}
for word in A.split():
count[word] = count.get(word, 0) + 1

for word in B.split():


count[word] = count.get(word, 0) + 1
return [word for word in count if count[word] == 1]
A = " HOW ARE U "
B = "HOW ARE U AMAN "
print(UncommonWords(A, B))
QUES 3 - Write a Python program to add 'ing' at the end of a given
string (length should be at least 3). If the given string already ends
with 'ing' then add 'ly' instead. If the string length of the given string
is less than 3, leave it unchanged. Example:- Sample String : 'abc'
Expected Result : 'abcing' Sample String : 'string' Expected Result :
'stringly'
CODE - def add_string(str1):
length = len(str1)

if length > 2:
if str1[-3:] == 'ing':
str1 += 'ly'
else:
str1 += 'ing'

return str1
print(add_string('ab'))
print(add_string('abc'))
print(add_string('string'))

You might also like