With help from a friend of mine who knows Python, here's an edit to make TPK a bit more Pythonic:
from math import sqrt
def f(x):
return sqrt(abs(x)) + 5.0*(x**3)
a = [input() for i in xrange(11)]
a.reverse()
for e, res in [(e, f(e)) for e in a]:
if res > 400:
print e, "TOO LARGE"
else:
print e, res
xrange is a nifty tool, returning an object that is somewhere between a lazy array and a function. Well, I guess it is one or the other, but it's a nice conceptual place. Also, is it just me, or is it the case that the name doesn't make any sense, and it's better than any name that does?Also, creating tuples for my loop state is bringing back memories of FP... I think I may prefer the more imperative style for that part of the task. Either that, or I just don't like the look of list comprehension. Introducing a variable name in the middle? What's wrong with just making a function
for that does both filter and map?You know, doing without curlies is perhaps less strange than doing without semicolons. Maybe it's just habit, but it's real strange to leave statements unpunctuated, ready to float off to the right.



1 comments:
xrange returns a generator
not a lazy array because it doesn't store previous values, and not a function because it stores state.
it's called xrange because range exists and returns a list.
in python 3k xrange will replace range.
Post a Comment