Ask your own question, for FREE!
Computer Science 11 Online
OpenStudy (anonymous):

Something is wrong with my Code, I need my code to get last target string's position. (Homework 6 Unit 2, Udacity) def find_last(string_search, string_target): position = 0 while position != -1: position = string_search.find(string_target) string_search = string_search[ position:] end_pos = position return end_pos print find_last("kaaa","a")

OpenStudy (anonymous):

Example: print find_last("aaaa","a") => 3

OpenStudy (anonymous):

http://ideone.com/kwgyK i edited it and it's much better now but it won't work if the target string is at 0

OpenStudy (anonymous):

that's easy to fix; just define end_pos to be -1 before the while loop begins so that if the target string simply doesn't exist, you won't get an error like: http://ideone.com/JiqgL To get it to work when it is at 0, you can assign position to the result of the find method and assign end_pos the value of position before the loop http://ideone.com/KoLT1

OpenStudy (anonymous):

clever solution, thanks agd... i need to pratice more :-/

OpenStudy (anonymous):

practice*

OpenStudy (anonymous):

your solution is actually better than mine now... I'm going to 'rewrite' my hw :)

OpenStudy (anonymous):

I still didn't start the robot car homework :( off I go.

OpenStudy (anonymous):

oh, good luck with robot car homework and thanks again

OpenStudy (anonymous):

trimmed my solution to 5 lines :-D

OpenStudy (anonymous):

I'll think of a one-liner!

OpenStudy (anonymous):

http://ideone.com/2tL4o

OpenStudy (anonymous):

oops small bug there: http://ideone.com/aZhBM for i in length s, not t :-P

OpenStudy (anonymous):

off I go to rewrite all my solutions to the udacity problems into one-liners.

OpenStudy (anonymous):

Impressive agd, (y)

OpenStudy (anonymous):

can you help me in this http://openstudy.com/study?login#/updates/4f55e90fe4b0862cfd071a91

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!