GCSE · 10 exercises
Python text processing
Go through text a character at a time, and build the answer as you go: a new string, or a list of pieces joined at the end with "".join().
ord() gives a character's number and chr() the character for a number: useful for shifting letters.
text = input()
out = []
for ch in text:
out.append(ch.upper() if ch.islower() else ch.lower())
print("".join(out))Watch for: Shifting letters past "z" must wrap round to "a": use % 26 on the letter's position in the alphabet.
Exercises
- Caesar cipherShift every letter forward in the alphabet by a number of places, wrapping from zIntermediate
- Run-length encodingCompress a word by writing each run of the same letter as the letter and how manyIntermediate
- Run-length decodingUndo run-length encoding: each letter is followed by how many times it repeats (theIntermediate
- camelCase to snake_caseConvert a camelCase name to snake_case: put an underscore before each capital letterIntermediate
- Roman numeralsConvert a Roman numeral to a number.Intermediate
- To binaryWrite a number in binary by repeatedly dividing by 2 and collecting the remainders.Intermediate
- No repeated lettersAn isogram is a word with no letter repeated.Intermediate
- Reverse each wordReverse the letters of every word but keep the words in their order.Intermediate
- Capitalise sentencesMake the first letter of every sentence a capital.Intermediate
- Simple email checkCheck an email address: exactly one @, something before it, and after it a dot withIntermediate