0% found this document useful (0 votes)
96 views3 pages

2.1 Python Ass

The document contains the details of 4 programming exercises completed by a student. The exercises involve string operations like checking for palindromes and symmetrical strings, finding uncommon words between two strings, adding suffixes to strings based on length, and checking if a word can be read based on known alphabets. Code snippets are provided for each exercise to demonstrate the logic and solutions.

Uploaded by

ashtosh
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)
96 views3 pages

2.1 Python Ass

The document contains the details of 4 programming exercises completed by a student. The exercises involve string operations like checking for palindromes and symmetrical strings, finding uncommon words between two strings, adding suffixes to strings based on length, and checking if a word can be read based on known alphabets. Code snippets are provided for each exercise to demonstrate the logic and solutions.

Uploaded by

ashtosh
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/ 3

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment: 2.1

Student Name: Ashutosh Kumar UID: 21BCS7473


Branch: CSE Section/Group: 815B
Semester: 4TH Date of Performance: 24/03/2023
Subject Name: Python Subject Code: 21CSH214

Aim: Program to demonstrate the various kind of operations that can be applied to a string

Q1) write a python program to check whether string is symmetrical or palindrome .

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")

You might also like