Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find every two (non-overlapping) vowels inbetween consonants

Task You are given a string . It consists of alphanumeric characters, spaces and symbols(+,-). Your task is to find all the substrings of the origina string that contain two or more vowels. Also, these substrings must lie in between consonants and should contain vowels only.

Input Format: a single line of input containing string .

Output Format: print the matched substrings in their order of occurrence on separate lines. If no match is found, print -1.

Sample Input: rabcdeefgyYhFjkIoomnpOeorteeeeet

Sample Output:

ee
Ioo
Oeo
eeeee


The challenge above was taken from https://www.hackerrank.com/challenges/re-findall-re-finditer

The following code passes all the test cases:

import re

sol = re.findall(r"[^aiueo]([aiueoAIUEO]{2,})(?=[^aiueo])", input())

if sol:
    for s in sol:
        print(s)
else:
    print(-1)

The following doesn't.

import re

sol = re.findall(r"[^aiueo]([aiueoAIUEO]{2,})[^aiueo]", input())

if sol:
    for s in sol:
        print(s)
else:
    print(-1)

The only difference beteen them is the final bit of the regex. I can't understand why the second code fails. I would argue that ?= is useless because by grouping [aiueoAIUEO]{2,} I'm already excluding it from capture, but obviously I'm wrong and I can't tell why.

Any help?

like image 506
WhyWhyWhy Avatar asked Jul 05 '17 14:07

WhyWhyWhy


1 Answers

The lookahead approach allows the consonant that ends one sequence of vowels to start the next sequence, whereas the non-lookahead approach requires at least two consonants between those sequences (one to end a sequence, another to start the next, as both are matched).

See

import re
print(re.findall(r'[^aiueo]([aiueoAIUEO]{2,})(?=[^aiueo])', 'moomoom'))
print(re.findall(r'[^aiueo]([aiueoAIUEO]{2,})[^aiueo]', 'moomoom'))

Which will output

['oo', 'oo']
['oo']

https://ideone.com/2Wn1TS

To be a bit picky, both attempts aren't correct regarding your problem description. They allow for uppercase vowels, spaces and symbols to be separators. You might want to use [b-df-hj-np-tv-z] instead of [^aeiou] and use flags=re.I

like image 192
Sebastian Proske Avatar answered Nov 10 '22 19:11

Sebastian Proske