[Tutor] another better way to do this ?

Roelof Wobben rwobben at hotmail.com
Sun Jan 12 09:12:21 CET 2014


Hello, 
 
Here is the whole exercise with examples. 
 
# By Sam the Great from forums
# That freaking superhero has been frequenting Udacity
# as his favorite boss battle fight stage. The 'Udacity'
# banner keeps breaking, and money is being wasted on
# repairs. This time, we need you to proceduralize the
# fixing process by building a machine to automatically
# search through debris and return the 'Udacity' banner
# to the company, and be able to similarly fix other goods.
# Write a Python procedure fix_machine to take 2 string inputs
# and returns the 2nd input string as the output if all of its
# characters can be found in the 1st input string and "Give me
# something that's not useless next time." if it's impossible.
# NOTE: # If you are experiencing difficulties taking
        # this problem seriously, please refer back to
        # "Superhero flyby", the prequel, in Problem Set 11.
# TOOLS: # if statement
         # while loop
         # string operations
         # Unit 1 Basics
# BONUS: # 
# 5***** #  If you've graduated from CS101,
#  Gold  #  try solving this in one line.
# Stars! #
def fix_machine(debris, product):
    ### WRITE YOUR CODE HERE ###
### TEST CASES ###
print "Test case 1: ", fix_machine('UdaciousUdacitee', 'Udacity') == "Give me something that's not useless next time."
print "Test case 2: ", fix_machine('buy me dat Unicorn', 'Udacity') == 'Udacity'
print "Test case 3: ", fix_machine('AEIOU and sometimes y... c', 'Udacity') == 'Udacity'
print "Test case 4: ", fix_machine('wsx0-=mttrhix', 't-shirt') == 't-shirt'
 
Roelof

 
> To: tutor at python.org
> From: alan.gauld at btinternet.com
> Date: Sun, 12 Jan 2014 00:45:11 +0000
> Subject: Re: [Tutor] another better way to do this ?
> 
> On 11/01/14 21:24, Roelof Wobben wrote:
> 
> > I have two strings a and b
> >
> > Now I have to check if the characters of b are all in a.
> > But they do have to be in the same order.
> 
> I'm not sure exactly what you mean? Can you give some examples of
> data that pass and that fail the criteria?
> 
> Your algorithm below might meet one definition of the spec but
> its not valid code since it uses return but is not a function.
> 
> > length = len(b)
> > start = 1
> > while start < length :
> >        check = a.find (b[start])
> >        if check == -1 :
> >              return False
> >        start = start + 1
> > return True
> 
> Problems I see are:
> 1) you start testing at b[1] not b[0]
> 2) you don't check if the letters are in the same sequence in a as in b
> 3) you probably could tidy it up using a for loop over b rather than 
> indexing
> 
> > But according to the site this can be solved in a one-liner.
> 
> That depends on the spec.
> And have you covered regular expressions? That is probably one
> way to do a one-liner...
> 
> But just because you can do it in one line doesn't mean you
> should. It's better for code to be readable than short.
> 
> 
> HTH
> -- 
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.flickr.com/photos/alangauldphotos
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140112/f171e322/attachment.html>


More information about the Tutor mailing list