Write a function called exactly weird() which takes three strings as arguments and prints the longest one backwards. (In the case of a tie, the string which is the earlier argument should be selected.
The function call:
weird("I", "Love", "Python")
Should result in the following terminal output:
nohtyP
This is what I have done so far.. am not getting the scratch part right ...
running = True
while running:
word = raw_input("Enter word:")
if word[0] in "aeiou":
print word + "yay"
else:
print word[1:] + word[0] + "ay"
-
4You seem to have two different things..one being actual pig latin, and one being backwards printing.PrettyPrincessKitty FS– PrettyPrincessKitty FS2011年04月07日 20:47:33 +00:00Commented Apr 7, 2011 at 20:47
-
2Your title, question, and sample code seem to be about different things.oosterwal– oosterwal2011年04月07日 20:49:10 +00:00Commented Apr 7, 2011 at 20:49
-
oh yea am sorry.. i was doing that piglatin problem too.. sorry for thatSarah– Sarah2011年04月07日 20:57:17 +00:00Commented Apr 7, 2011 at 20:57
-
Please label your homework with the [homework] tag. Please try to actually make sense of your question before posting it.S.Lott– S.Lott2011年04月07日 21:04:40 +00:00Commented Apr 7, 2011 at 21:04
-
1@zawa: Not really. "am not getting the scratch part right" is vague and confusing. What specifically is your problem? Error? Wrong results? Please be as detailed and specific as you can.S.Lott– S.Lott2011年04月07日 21:10:38 +00:00Commented Apr 7, 2011 at 21:10
3 Answers 3
A faster approach (and it works for an arbitrary number of strings) is:
def weird(*s):
return sorted(s,key=len,reverse=True)[0][::-1]
2 Comments
Unfortunately I'm somewhat of a novice myself with python, but the simplest way I see to do this is refer to this Reverse a string in Python to see a simple way to reverse a string. For the logic of picking which string to reverse, it would be easiest to create a list and store a max string based on length.
Here's a possible solution, using the reverse method on the other thread. Ideally, this method would just take one list as an argument, since then it would work for all sizes of input.
def weird(strOne, strTwo, strThree):
strings = [strOne, strTwo, strThree]
max = ""
for x in strings:
if len(max) < len(x):
max = x
print max[::-1]
weird("I", "Love", "Python")
Comments
def weird(str1, str2, str3):
# sort them according to length
# guaranteed stable sort
strings= sorted( (str1, str2, str3), key=len)
# maximum_length is the length of the last one
maximum_length= len(strings[-1])
# now return the earliest (str1 < str2 < str3)
# having len() == maximum_length
for a_string in reversed(strings):
if len(a_string) < maximum_length:
break
result= a_string
return result[::-1]