Let's Make Robots!

Python challenge

"There will be some code attached, your job is to change the code to make it more efficient.
However, make sure that in the end, the person running the code should get the same results as before.
Some solutions should be obvious, others less so.
Also, annotating the finished code will get you bonus points... or something.
Have fun!" 
I know how I would change it to produce the same result but no idea how to improve efficiency.
Also I would like to see variations on this so loads of replies welcome.
Thanks for help. 
AttachmentSize
Challenge2.py_.txt926 bytes

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Well I was thinking of something even simpler than that. Again refering to the snippet of code I've posted above, there is no visible need to have words as different variables for this current script, and hence that many lines:

So you could easily replace all of that with:

print "You were born on the "+DOB+" and you like "+CCat+"-"+TCat+" cats!"

which goes in hand with the previous statements style in the script, like so:

print "Hello", Name, ", how are you doing?"

Haha..I was overcomplicating it! Thanks for the help. But is there anyway to get what I was trying to do work?

 "Simple is better than complex." - Useful! Do you have any cool 'complex' scripts I can play with.

If you're willing to do my job for me, then that can be arranged... :-P

Leisure complicated scripts? I don't believe I have any of those at hand :-)

But you can check this out:

http://www.diveintopython.net/

and/or

http://getpython3.com/diveintopython3/

P.S. - I haven't followed the evolution from python 2.X to python 3, but since you're just starting now you might want to be aware of the new/different stuff in python 3

There more than one way of doing the same thing, in Python there's a saying that goes something like: "there's many ways to do a thing, and then there's the (obvious) python way"

As I said before, I THINK the point here was only the reduction in the number of lines, hence doing it through string addition in a single print statement I believe to the the simplest answer. You were trying to do string formatting, which in this case would be that complicated, but still it's a bit more advanced than plain adding strings.

 

#1: Print single string, through simple string addition

print "You were born on the "+DOB+" and you like "+CCat+"-"+TCat+" cats!"

#2: Print sequential strings (each comma add whitespace between strings, hence string addition is still needed) [almost the same as before]

print "You were born on the", DOB, "and you like", CCat+"-"+TCat,"cats!"

#3: Print through string formating (what you were trying to achieve)

print "You were born on the %s and you like %s-%s cats!"%(DOB, CCat, TCat)

#4:

...other ways

 

Well, the key to understanding what is required here is what is meant by "more efficient".

When someone speaks of <b>more efficient</b> code I think of improving code in terms of complexity, runtime and/or memory usage. But taking in mind that the code presented here is so very plain and very very simple, I don't <b>believe</b> that it can be optimized in that sense, at least not something very noticeable.

However, one of the main features of python is enforcing code legibility, and still having in mind this code the only thing I change (without knowing further parameters) would be the final part.

Take a look at:

end1 = "You "
end2 = "were "
end3 = "born "
end4 = "on "
end5 = " and "
end6 = "you "
end7 = "like "
end8 = " cats!"

print end1 + end2 + end3 + end4 +"the " + DOB + end5 + end6 + end7 + CCat + "-" + TCat + end8

 

Now look at the code that precedes it, and try to make it coherent, style-wise. HINT: Focus in reducing the number of code lines used.

 

Thaks a lot for the help. Learning new stuff everyday!

formatter = "%r DOB %r cCat %r TCat %r"

print formatter % (You were born on, and you like, -, cats)