Skip to content

Commit fe528f6

Browse files
committed
fixes
1 parent 94875ae commit fe528f6

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

14_rhymer/solution1_regex.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,25 @@ def main():
4040
def stemmer(word):
4141
"""Return leading consonants (if any), and 'stem' of word"""
4242

43+
word = word.lower()
4344
vowels = 'aeiou'
4445
consonants = ''.join(
4546
[c for c in string.ascii_lowercase if c not in vowels])
4647
pattern = (
47-
'([' + consonants + ']+)?' # capture one or more, optional
48-
'(' # start capture
49-
'[' + vowels + ']' # at least one vowel
50-
'.*' # zero or more of anything else
51-
')?') # end capture, optional group
52-
match = re.match(pattern, word.lower())
53-
return (match.group(1) or '', match.group(2) or '') if match else ('', '')
48+
'([' + consonants + ']+)?' # capture one or more, optional
49+
'([' + vowels + '])' # capture at least one vowel
50+
'(.*)' # capture zero or more of anything
51+
)
52+
pattern = f'([{consonants}]+)?([{vowels}])(.*)'
53+
54+
match = re.match(pattern, word)
55+
if match:
56+
p1 = match.group(1) or ''
57+
p2 = match.group(2) or ''
58+
p3 = match.group(3) or ''
59+
return (p1, p2 + p3)
60+
else:
61+
return (word, '')
5462

5563

5664
# --------------------------------------------------
@@ -62,6 +70,7 @@ def test_stemmer():
6270
assert stemmer('chair') == ('ch', 'air')
6371
assert stemmer('APPLE') == ('', 'apple')
6472
assert stemmer('RDNZL') == ('rdnzl', '')
73+
assert stemmer('123') == ('123', '')
6574

6675

6776
# --------------------------------------------------

14_rhymer/solution2_no_regex.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,11 @@ def stemmer(word):
3939
"""Return leading consonants (if any), and 'stem' of word"""
4040

4141
word = word.lower()
42-
vowel_map = map(lambda c: word.index(c) if c in word else -1, 'aeiou')
43-
pos = list(filter(lambda v: v >= 0, vowel_map))
42+
vowel_pos = list(map(word.index, filter(lambda v: v in word, 'aeiou')))
4443

45-
if pos:
46-
first = min(pos)
47-
return (word[:first], word[first:])
44+
if vowel_pos:
45+
first_vowel = min(vowel_pos)
46+
return (word[:first_vowel], word[first_vowel:])
4847
else:
4948
return (word, '')
5049

@@ -58,6 +57,7 @@ def test_stemmer():
5857
assert stemmer('chair') == ('ch', 'air')
5958
assert stemmer('APPLE') == ('', 'apple')
6059
assert stemmer('RDNZL') == ('rdnzl', '')
60+
assert stemmer('123') == ('123', '')
6161

6262

6363
# --------------------------------------------------

0 commit comments

Comments
 (0)