6 Replies - 2355 Views - Last Post: 11 February 2016 - 07:10 AM Rate Topic: -----

#1 benf_1996   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-February 16

Trying to find specific order of vowels in words

Posted 10 February 2016 - 01:36 PM

The overall goal is to find words that contain one of each vowel ('a','e','i','o', 'u'), as well as in the order that the vowels are listed. So far I am able to locate words that contain one of each vowel, but I am completely unsure on how to locate that specific order of vowels. Any type of hint or advice would be appreciated!

for line_str in dict_file:
    line_str = line_str.strip().lower()
    line_count_int = 0
    for i in line_str:
        a_count = line_str.count('a')
        e_count = line_str.count('e')
        i_count = line_str.count('i')
        o_count = line_str.count('o')
        u_count = line_str.count('u')
    if a_count == 1 and e_count == 1\
    and i_count == 1 and o_count == 1 and u_count == 1:
        print(line_str)
        line_count_int += 1
    else:
        line_count_int += 1


Is This A Good Question/Topic? 0
  • +

Replies To: Trying to find specific order of vowels in words

#2 Ryano121   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1461
  • View blog
  • Posts: 3,289
  • Joined: 30-January 11

Re: Trying to find specific order of vowels in words

Posted 10 February 2016 - 01:47 PM

Look into the index() method on strings. It will give you the index (location) of the specified character(s) within a string.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: Trying to find specific order of vowels in words

Posted 10 February 2016 - 01:56 PM

For reference ;)

English words that use all vowels in alphabetical order
Was This Post Helpful? 0
  • +
  • -

#4 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 607
  • View blog
  • Posts: 1,927
  • Joined: 27-December 13

Re: Trying to find specific order of vowels in words

Posted 11 February 2016 - 05:16 AM

Why don't you consider the letter 'y a vowel?
In my language it is!
Was This Post Helpful? 0
  • +
  • -

#5 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 607
  • View blog
  • Posts: 1,927
  • Joined: 27-December 13

Re: Trying to find specific order of vowels in words

Posted 11 February 2016 - 05:23 AM

Here is a suggestion for an algorithm:

Initialize an empty result list.
Go through the word letter by letter.
Upon finding a vowel, add the vowel to the result list.
When the word is fully analysed, compare the result list with a target list (= ['a', 'e', 'i', 'o', 'u', 'y'])

You can optimize by breaking away as soon as the result list deviates from the target list.

This post has been edited by DK3250: 11 February 2016 - 05:24 AM

Was This Post Helpful? 0
  • +
  • -

#6 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: Trying to find specific order of vowels in words

Posted 11 February 2016 - 05:56 AM

With that approach, going through the word letter by letter, I think I would build a string and just compare it to "aeiou", aborting when the length reaches 5.

I was thinking of something a little messier, something like:

if word.index('a') < word.index('e') < word.index('i') etc:

but it needs jiggling if any value is -1. Well, only if the search for 'a' is -1.

Edited: Actually, it is find() rather than index() that returns -1. index() raises a ValueError if not found.
Was This Post Helpful? 0
  • +
  • -

#7 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 607
  • View blog
  • Posts: 1,927
  • Joined: 27-December 13

Re: Trying to find specific order of vowels in words

Posted 11 February 2016 - 07:10 AM

From Wikipedia:

These letters are vowels in English:

A, E, I, O, U, and sometimes Y
The letter Y can be a vowel (as in the words "cry", "sky", "fly" or "why"), or it can be a consonant (as in "yellow", "yacht", "yam" or "yesterday").

So, when 'y' is at the end of a word it will probably be counted as a vowel; and as you search for words where 'y' actually is in the end (if at all), you should include 'y' - at least as a possibility...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1