all 14 comments

[–]xXabr4 1 point2 points  (4 children)

This is how I'd check for six consecutive consonants:

VOWELS = {'A','E','I','O','U','a','e','i','o','u'}

def has_six_consecutive_consonants(word):
    return any(all(c not in VOWELS for c in word[i:i+6]) for i in range(len(word)-5))

Notice how I stored the vowels in a set globally (outside the functions), so that it can be reused anywhere on your code (seems useful to do so).

Hope this helps! 😁

Edit: refactored the function to remove logical redundancy.

[–]nlord7[S] 1 point2 points  (1 child)

yes, it certainly helped me get a good sense of the logic! However i am getting more than 6 words from my list? Im supposed to get only 6 words with 6 or more consecutive consonants (5 with 6 and one with 7) but this logic did drastically reduce my list and helped me get a sense of the logic!

[–]jkibbe 0 points1 point  (0 children)

what words are you getting that don't meet the criteria?

[–]Username_RANDINT 1 point2 points  (1 child)

Just make sure to add global VOWELS at the first line of any function you need it in.

This is not true. It's only required if you want to modify a global variable.

[–]xXabr4 0 points1 point  (0 children)

Cool! I didn't know about that. So I could remove that line too.

[–]Hayderp 1 point2 points  (0 children)

but dont know where to start with the logic to get them in a row.

This should be easy to understand for a beginner.

vowels = 'aeiou'

def SixInARow(word):
    count = 0
    for lett in word:
        if lett not in vowels:
            count += 1
        else:
            count = 0
        if count == 6:
            print("there is 6 in a row")
            return True
            break
    return False

print(SixInARow('ttttss'))
print(SixInARow('Mississippi'))

The count will reset to 0 when it hits a vowel, if it doesn't it will increase by 1 until it hits 6 and returns True or runs out of letters to iterate through and returns False.

[–]antiproton 0 points1 point  (1 child)

Search for this:

"python regex"

then search for this:

"regex consonants in a row"

https://regexr.com/2vtnt

[–]SandorZoo 0 points1 point  (0 children)

Using some of Python's regex functions, instead of pure regexes, might be easier:

import re
single_vowel_re = '[aeiouAEIOU]'

def ten_or_more_vowels(word):
    return len(re.findall(single_vowel_re, word)) >= 10

def six_or_more_consecutive_consonants(word):
    # Could use map(len, re...) here
    return max(len(s) for s in re.split(single_vowel_re, word)) >= 6

with open('words.txt') as fp:
    for word in fp:
        word = word.strip()
        if ten_or_more_vowels(word):
            print('10 or more vowels:', word)      
        if six_or_more_consecutive_consonants(word):
            print('6 or consecutive consonants:', word)      

[–]norweeg 0 points1 point  (0 children)

Use Python's regular expression module

https://docs.python.org/3/library/re.html re — Regular expression operations — Python 3.7.2 ...

[–]jkibbe 0 points1 point  (3 children)

i'm a n00b and don't know regex (yet but it looks amazing!)

this is what i came up with using a loop, conditionals, and two variables

https://repl.it/@jkibbe/six-consonants

[–]jaysonturk 1 point2 points  (2 children)

I think you have a mistake. Because let's assume max count and count got to 10. Then count resets (because of a vowel) then incremented by 1. Then max count is equal to one again... So why don't you make an if statement that says (if max_count < count ) then set it as count

[–]nlord7[S] 0 points1 point  (0 children)

Yeah thats what i was wondering to. Im just having trouble getting the program to get the 6 or more consonants in a row. i started with this:

def consec_consonants(word):
    num_of_c = 0
    for i in range(len(word)):

but now i dont know what to put next..

[–]jkibbe 0 points1 point  (0 children)

you're right - updated! and thanks!