Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 23rd, 2013, 09:38 PM
laffytaffykidd laffytaffykidd is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 2 laffytaffykidd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 32 m 12 sec
Reputation Power: 0
Indexing a string! Help me!

My question is:
Words that start with a vowel or ’y’ and end with a vowel or ’y’.

So far I have,

Code:
def start_end_vowel(s):
	for i in range(0, len(s)-5):
		if (s[0] == 'a' or s[0] == 'e' or s[0] == 'i' or s[0] == 'o' or s[0] == 'u' or s[0] == 'y') and (s[len(s)-5] == 'a' or s[len(s)-5] == 'e' or s[len(s)-5] == 'i' or s[len(s)-5] == 'o' or  s[len(s)-5] == 'u' or s[len(s)-5] == 'y'):
			return True
	return False 



There must be an easier method than this..... please help!!

Reply With Quote
  #2  
Old February 24th, 2013, 02:37 AM
leonnaley2 leonnaley2 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 32 leonnaley2 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 42 m 2 sec
Reputation Power: 1
Could you post some input and output examples?

and instead of doing
if (s[0] == 'a' or s[0] == 'e' or s[0] == 'i' or s[0] == 'o' or s[0] == 'u' or s[0] == 'y')

you could just do
if s[0] in "aeiouy"

and maybe read up on slicing? Slicing is really powerful.

Reply With Quote
  #3  
Old February 24th, 2013, 02:40 AM
leonnaley2 leonnaley2 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 32 leonnaley2 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 42 m 2 sec
Reputation Power: 1
btw,
"hello"[-1] returns an "o" because the index -1 is the last letter in the string.

Reply With Quote
  #4  
Old February 24th, 2013, 03:23 AM
dariyoosh's Avatar
dariyoosh dariyoosh is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Location: Iran / France
Posts: 132 dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 6 h 25 m 17 sec
Reputation Power: 133
Quote:
Originally Posted by laffytaffykidd
... There must be an easier method than this..... please help!!
I use a regular expression for this problem, something like this:
Code:
import re

def checkForLetters(param_Text):
    prog = re.compile("^[aeiouy].*[aeiouy]$(?i)") 
    if prog.match(param_Text):
        return True
    else:
        return False
For more information about regular expressions, please read RE - Regular expression operations


Regards,
Dariyoosh

Reply With Quote
  #5  
Old February 24th, 2013, 06:29 AM
dariyoosh's Avatar
dariyoosh dariyoosh is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Location: Iran / France
Posts: 132 dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 6 h 25 m 17 sec
Reputation Power: 133
And if you don't want to use regular expressions for this problem, another solution could be this
Code:
def checkForLetters(p_text):
    l_lettersList = ["a", "e", "i", "o", "u", "y"]
    l_lowerTxt = p_text.lower()
    return (
                l_lowerTxt[0] in l_lettersList and 
                l_lowerTxt[-1] in l_lettersList
            )


Regards,
Dariyoosh

Reply With Quote
  #6  
Old February 24th, 2013, 12:23 PM
Nyktos Nyktos is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 76 Nyktos User rank is Corporal (100 - 500 Reputation Level)Nyktos User rank is Corporal (100 - 500 Reputation Level)Nyktos User rank is Corporal (100 - 500 Reputation Level)Nyktos User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 3 h 26 m 18 sec
Reputation Power: 2
If you're only using it for "in" checks, you should be using a set, not a list.

But yeah, that's the way to do it.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Indexing a string! Help me!

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap