Kodeclik Blog
How to find the closest vowel to a letter (in Python)
An interesting problem, if you like tinkering with words, is to find the closest vowel to a letter in Python. For instance, the closest vowel to a ‘b’ is ‘a’. Similarly, the closest vowel to ‘f’ is ‘e’, and so on.
It is easy for us to think about the letters arranged on a line and finding the closest vowel by looking in either the forward or reverse direction. But how do we operationalize this in Python?
Fortunately, the way letters are stored internally inside a computer already gives us an idea. There is an ASCII code for each letter. In ASCII encoding, letters have specific decimal values - 'a' is 97, 'e' is 101, 'i' is 105, 'o' is 111, 'u' is 117, and so on. Uppercase letters also have specific decimal values (more on that later). This numerical representation gives us a way to calculate distances between letters mathematically.
For instance, because the ASCII code for ‘a’ is 97 and the ASCII code for ‘b’ is 98, the closest vowel to ‘b’ can be found by identifying the closest vowel in terms of “ASCII distance”. Here the ASCII distance between ‘a’ and ‘b’ is 1. This approach works because ASCII codes preserve the alphabetical ordering of letters, making distance calculations meaningful.
Python program to find the closest vowel
Here therefore is a Python function that finds the closest vowel to a given letter based on ASCII distance:
def closest_vowel(letter):
vowels = 'aeiou'
letter = letter.lower()
# Check for immediate vowel
if letter in vowels:
return letter
# Get ASCII value of the letter
letter_ascii = ord(letter)
min_distance = float('inf')
closest_vowel = ''
# Find the closest vowel
for vowel in vowels:
vowel_ascii = ord(vowel)
distance = abs(letter_ascii - vowel_ascii)
if distance < min_distance:
min_distance = distance
closest_vowel = vowel
return closest_vowel
The closest_vowel function begins by defining a string of vowels 'aeiou' and converting the input letter to lowercase. This normalization ensures consistent processing regardless of the input case. As an optimization, it first checks if the input letter is already a vowel - if so, it immediately returns that letter since no further processing is needed.
The core of the function revolves around finding ASCII distances between letters. It converts the input letter to its ASCII value using the ord() function and initializes two variables: min_distance set to infinity (which ensures the first comparison will always succeed) and an empty string for closest_vowel. These variables will keep track of the smallest distance found and its corresponding vowel throughout the search process.
The function then enters a loop that examines each vowel in turn. For each vowel, it calculates the absolute difference between the ASCII values of the input letter and the current vowel. If this distance is smaller than the current minimum distance, both the minimum distance and closest vowel are updated. After checking all vowels, the function returns the vowel that had the smallest ASCII distance from the input letter. This approach effectively finds the "closest" vowel by treating letters as points on a number line, where distance is measured by the difference in their ASCII values.
Let us now use this function to find the closest vowel to each letter of the alphabet:
for i in range(ord('a'), ord('z') + 1):
letter = chr(i)
closest = closest_vowel(letter)
print(f"Closest vowel to {letter} is {closest}.")
This code creates a loop that iterates through all lowercase letters of the alphabet by using the range() function with ASCII values from 'a' to 'z'. For each iteration, it converts the current ASCII value i back to a character using the chr() function, stores it in the variable letter, then passes this letter to our closest_vowel() function to find its nearest vowel. Finally, it prints a formatted string showing both the original letter and its closest vowel, creating a complete mapping of every letter in the alphabet to its nearest vowel based on ASCII distance.
The output is:
Closest vowel to a is a.
Closest vowel to b is a.
Closest vowel to c is a.
Closest vowel to d is e.
Closest vowel to e is e.
Closest vowel to f is e.
Closest vowel to g is e.
Closest vowel to h is i.
Closest vowel to i is i.
Closest vowel to j is i.
Closest vowel to k is i.
Closest vowel to l is i.
Closest vowel to m is o.
Closest vowel to n is o.
Closest vowel to o is o.
Closest vowel to p is o.
Closest vowel to q is o.
Closest vowel to r is o.
Closest vowel to s is u.
Closest vowel to t is u.
Closest vowel to u is u.
Closest vowel to v is u.
Closest vowel to w is u.
Closest vowel to x is u.
Closest vowel to y is u.
Closest vowel to z is u.
Isn’t that neat?
In the above program, we lowercase all inputs so that we make comparisons only among lower case letters and lower case vowels. Now let us update our program to deal with capital (upper case) letters.
def closest_vowel(letter):
lowercase_vowels = 'aeiou'
uppercase_vowels = 'AEIOU'
# Check for immediate vowel for lowercase
if letter in lowercase_vowels:
return letter
# Check for immediate vowel for uppercase
if letter in uppercase_vowels:
return letter
# Get ASCII value of the letter
letter_ascii = ord(letter)
min_distance = float('inf')
closest_vowel = ''
# Find the closest vowel based on letter case
if letter.isupper():
vowels_to_check = uppercase_vowels
else:
vowels_to_check = lowercase_vowels
for vowel in vowels_to_check:
vowel_ascii = ord(vowel)
distance = abs(letter_ascii - vowel_ascii)
if distance < min_distance:
min_distance = distance
closest_vowel = vowel
return closest_vowel
# Test both lowercase and uppercase letters
for i in range(ord('a'), ord('z') + 1):
letter = chr(i)
closest = closest_vowel(letter)
print(f"Closest vowel to {letter} is {closest}.")
for i in range(ord('A'), ord('Z') + 1):
letter = chr(i)
closest = closest_vowel(letter)
print(f"Closest vowel to {letter} is {closest}.")
The key changes in this updated version are as follows. We removed the letter.lower() conversion to preserve case, added a separate string of uppercase vowels 'AEIOU', added a case check using isupper() to determine which set of vowels to use, and finally added a second loop to test uppercase letters
When run, this program will show that uppercase letters map to uppercase vowels (A-C → A, D-G → E, H-L → I, M-R → O, S-Z → U) and lowercase letters map to lowercase vowels (a-c → a, d-g → e, h-l → i, m-r → o, s-z → u).
The output is:
Closest vowel to a is a.
Closest vowel to b is a.
Closest vowel to c is a.
Closest vowel to d is e.
Closest vowel to e is e.
Closest vowel to f is e.
Closest vowel to g is e.
Closest vowel to h is i.
Closest vowel to i is i.
Closest vowel to j is i.
Closest vowel to k is i.
Closest vowel to l is i.
Closest vowel to m is o.
Closest vowel to n is o.
Closest vowel to o is o.
Closest vowel to p is o.
Closest vowel to q is o.
Closest vowel to r is o.
Closest vowel to s is u.
Closest vowel to t is u.
Closest vowel to u is u.
Closest vowel to v is u.
Closest vowel to w is u.
Closest vowel to x is u.
Closest vowel to y is u.
Closest vowel to z is u.
Closest vowel to A is A.
Closest vowel to B is A.
Closest vowel to C is A.
Closest vowel to D is E.
Closest vowel to E is E.
Closest vowel to F is E.
Closest vowel to G is E.
Closest vowel to H is I.
Closest vowel to I is I.
Closest vowel to J is I.
Closest vowel to K is I.
Closest vowel to L is I.
Closest vowel to M is O.
Closest vowel to N is O.
Closest vowel to O is O.
Closest vowel to P is O.
Closest vowel to Q is O.
Closest vowel to R is O.
Closest vowel to S is U.
Closest vowel to T is U.
Closest vowel to U is U.
Closest vowel to V is U.
Closest vowel to W is U.
Closest vowel to X is U.
Closest vowel to Y is U.
Closest vowel to Z is U.
Enjoy this blogpost? Want to learn Python with us? Sign up for 1:1 or small group classes.