My solution to the Coderbyte task 'Vowel Square'. It's a simple task, but I'm sure there's a more Pythonic solution than mine.
The problem:
Vowel Square
Have the function VowelSquare(strArr)
take the strArr
parameter being passed
which will be a 2D matrix of some arbitrary size filled with letters from
the alphabet, and determine if a 2x2 square composed entirely of vowels
exists in the matrix. For example: strArr
is ["abcd", "eikr", "oufj"]
then
this matrix looks like the following:
a b c d
e i k r
o u f j
Within this matrix there is a 2x2 square of vowels starting in the second row and first column, namely, ei, ou. If a 2x2 square of vowels is found your program should return the top-left position (row-column) of the square, so for this example your program should return 1-0. If no 2x2 square of vowels exists, then return the string not found. If there are multiple squares of vowels, return the one that is at the most top-left position in the whole matrix. The input matrix will at least be of size 2x2.
Examples:
- Input:
["aqrst", "ukaei", "ffooo"]
- Output: 1-2,
- Input:
["gg", "ff"]
- Output: not found
My solution:
VOWELS = "aeiouAEIOU"
def find_vowel_square(strs: list):
"""Return the top left grid ref of any 2x2 sq composed of vowels only
If more than one 2x2 sq exists. Return that which is at the most top-left
position.
"""
height, width = len(strs), len(strs[0])
# Ensure all strings within strs are of equal length
assert all(len(s) == width for s in strs)
for string_i in range(height-1):
for i in range(width-1):
if strs[string_i][i] in VOWELS and strs[string_i][i+1] in VOWELS:
if strs[string_i+1][i] in VOWELS and strs[string_i+1][i+1] in VOWELS:
return f"{i}-{string_i}"
return "Not found"
assert find_vowel_square(strs=["aqree", "ukaei", "ffooo"]) == "3-0"
assert find_vowel_square(strs=["aqrst", "ukaei", "ffooo"]) == "2-1"
assert find_vowel_square(strs=["gg", "ff"]) == "Not found"