CS 1101 - Programming Assignment Unit 5
Course:
Programming Fundamentals (CS 1101)
NAME: PRAISEGOD SHUMBA
University: University of the People
Technical Explanation
This Python program performs three key operations on the name "PraiseGod":
1. Substring Extraction
Input Handling: Accepts user input for n and clamps it between 0 and the name's length (9 characters)
using max(0, min(n, len(name)))
String Slicing: Uses name[:n] to extract characters from index 0 to n-1
Validation: Prevents index errors for values outside 0-9 range
2. Vowel Counting
Case Handling: Converts characters to lowercase using char.lower() for uniform comparison
Set Membership: Uses a vowel set {'a','e','i','o','u'} for O(1) lookup efficiency
Generator Expression: Efficiently counts matches with sum() and generator
3. String Reversal
Extended Slicing: Uses [::-1] syntax to create reversed copy
Original Preservation: Maintains original casing while reversing
Screenshot.
Key Observations:
Substring extraction shows "PraiseGod" (9 characters)
Vowel count identifies 4 vowels (a, i, e, o)
Reversal maintains original casing: "PraiseGod" → "doGesiarP"
The program demonstrates Python's string manipulation capabilities with O(n) time complexity for all operations,
making it efficient for typical use cases.
Screenshot.
Reference.
Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tea Press. This book is licensed
under Creative Commons Attribution-NonCommercial 3.0 Unported