1

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"
Zero Piraeus
59.7k28 gold badges158 silver badges164 bronze badges
asked Apr 7, 2011 at 20:45
6
  • 4
    You seem to have two different things..one being actual pig latin, and one being backwards printing. Commented Apr 7, 2011 at 20:47
  • 2
    Your title, question, and sample code seem to be about different things. Commented Apr 7, 2011 at 20:49
  • oh yea am sorry.. i was doing that piglatin problem too.. sorry for that Commented 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. Commented 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. Commented Apr 7, 2011 at 21:10

3 Answers 3

4

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]
answered Apr 15, 2011 at 7:52
Sign up to request clarification or add additional context in comments.

2 Comments

adding a '*' in front of an argument means you can pass in as many arguments as you want and Python treats it as a list of arguments.
+1 for generalization of input, and I wish I could do +1 for the FP approach :)
1

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")
answered Apr 8, 2011 at 15:36

Comments

0
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]
answered May 1, 2011 at 11:58

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.