1

How can I write this complete code in python in just one line or may be I must say something which uses least space or least no of characters?

t=int(input())
while t>0:
  n=int(input())
  s=sum(1/(2.0*i+1) for i in range(n))
  print "%.15f"%s
  t-=1
5
  • While restructuring this code, you must also consider other aspects rather than just trimming it to a single line. Commented Feb 3, 2013 at 6:19
  • Initially that sum line was also I did in 2 or 3 lines. Till now I just came up with this: t=int(input()) while t>0: n=int(input()) print "%.15f"%(sum(1/(2.0*i+1) for i in range(n))) t-=1 Commented Feb 3, 2013 at 6:19
  • @Lion what kind of ascepts you are trying to tell me to consider? Can you please elaborate as I didn't get it. Commented Feb 3, 2013 at 6:36
  • Why not post it on codegolf? They're really good at doing this kind of stuff Commented Feb 3, 2013 at 7:59
  • This really is not a pythonic, hence recommended, thing to do. Python stresses readability of code and distilling a multi line for loop might look cool on one line, but is a bitch to read Commented Feb 3, 2013 at 8:05

3 Answers 3

6

You're welcome

for t in range(int(input()), 0, -1): print '%.15f' % sum(1/(2.0*i+1) for i in range(int(input())))

EDIT (explanation):

Firstly, instead of a while loop you can use a for loop in a range. The last argument in the for loop is a -1 to subtract 1 every time instead of the default of plus 1 every time.

If there is only one statement in an if statement, or loop, you can keep the one statement in the same line without going to the next line.

Instead of creating the variable of n, you can simply plug it in since it's only being used once. Same goes for s.

Sign up to request clarification or add additional context in comments.

5 Comments

a small explanation would also help if you could. Thanx. :)
int(input()) is redundant in Python 2 since it would already return an integer (if one is typed in). Also, I think it should be range(input(), 0, -1) because the condition is while t>0, not while t>=0.
@Volatility You're right, i changed it to a 0. Don't you mean Python 3? In python 2 you do need int(raw_input())
I mean in Python 2 you don't need to typecast input() to an int. (Although int(raw_input()) is probably the better way)
I'd suggest using sum(map(lambda x: 1/(2.0 * x +1), range(n)) as a replacement for the sum function
3
for _ in range(input()):print"%.15f"%sum(1/(2.0*i+1)for i in range(input()))

Comments

0

exec"print sum((-1.)**i/(i-~i)for i in range(input()));"*input()

I know I am too late for answering this question.but above code gives same result. It will get even more shorter. I am also finding ways to shorten it. #CodeGolf #Python2.4

Comments

Your Answer

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.