Python Assesment 4
Python Assesment 4
WORKSHEET :4
Student Nam Jahir Ahmed ollah UID W23BCS12708
Branch: BE-CSE Section/Group: 717/B
Date of Performance: 07/11/2024 Semester: III
Subject Name: Programming in Python Lab Subject Code: 23CSP-201
1.Aim
Set B: Word Reverser for Language Learning App
A language learning app requires a function to reverse the middle characters of words, helping
students practice reading and spelling. Create function reverse_word_middles(s: str) -> str that
takes a string of words and returns it with each word's middle character(s) reversed. If a word
has an odd number of characters, reverse the middle three.
2.Source Code:
def reverse_word_middles(s: str) -> str:
words = s.split()
reversed_words = []
for word in words:
if len(word) < 3:
reversed_words.append(word)
else:
if len(word) % 2 == 1:
middle_index = len(word) // 2
reversed_word = word[:middle_index - 1] + word[middle_index - 1:middle_index +
2][::-1] + word[middle_index + 2:]
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
else:
middle_index = len(word) // 2
reversed_word = word[:middle_index - 1] + word[middle_index - 1:middle_index +
1][::-1] + word[middle_index + 1:]
reversed_words.append(reversed_word)
return " ".join(reversed_words)
3. Screenshots of Outputs:
4. Learning Outcomes:
a) Learnt about string manipulation techniques.
b) Learnt about implementing built-in functions of python.
c) Learnt solving problems using conditional logic.