Solve the CodeSprint4 Leibniz code golf challenge in Python in 66 characters

4

1

Check out this challenge:

https://www.hackerrank.com/codesprint4/challenges/leibniz

(The competition has already ended, so I'm just curious about the Python solution, which the organizers refused to reveal, so this is not cheating in any way.)

I only managed to solve the challenge in 77 characters. (Talking only about Python here.) However, one adr2370 managed to solve the challenge in 66 characters!

Please help me find the 66-character solution! I'm very very curious because I had to work really hard to bring mine down to 77, and I'm amazed as to how someone came up with a 66-character solution.

For reference, my solution:

for j in'x'*input():print'%.15f'%sum((-1)**i/(2.*i+1)for i in range(input()))

Ram Rachum

Posted 2013-02-18T18:25:38.223

Reputation: 101

For what it's worth, neither your solution nor any of the ones currently present in answers meet the stated specification. E.g. none of them give the correctly rounded answer of 0.785395663397448 for 100000 terms. – Peter Taylor – 2015-04-22T18:42:41.627

Very close to being a duplicate of Calculate pi to 5 decimals (which has a Python solution, FWIW).

– Peter Taylor – 2013-02-18T23:09:28.557

Sorry, I'm curious about a solution for this exact puzzle, not a related puzzle. – Ram Rachum – 2013-02-19T00:08:10.187

Suggest you read through tips-for-golfing-in-python. You can easily drop a few more chars

– gnibbler – 2013-02-19T00:30:09.620

You should open it up to other languages and see how short people can program it :) – beary605 – 2013-02-19T01:03:10.490

adr2370's solution is longer: https://github.com/adr2370/HackerRankBackToSchool/blob/master/Leibniz.py

– Ari – 2013-02-19T15:42:27.093

1Actually, it seems the top python solution wasn't really python, it was just a hack. So no one got a 66-character solution. – Ari – 2013-02-19T18:07:17.813

@Ari I'm very curious to know why you think the 66 characters solution was a hack. – Ram Rachum – 2013-02-19T18:19:47.663

Can you include a description of what the code should do? As it is, it's not clear what you're asking, as the requirements are hidden beyond a link, rather than in the body of the question. (I tried following the link, but it's just a blank page, so not much help). – Toby Speight – 2018-07-20T08:52:04.247

Answers

5

Python 71 bytes

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

Not quite 66 bytes, but with a score of 22.90, it would have ranked second overall.

Edit: regarding Ari's comment about the 66 byte Python solution being a 'hack', I don't think that's exactly what happened. I've noticed several times that the submitted language is displayed incorrectly. For example here: Baconian Cipher. This challenge may only be submitted in Brainf_ck, yet the second ranked solution was submitted in Python 2 (interestingly enough, spot 23 is also Python 2. If I were to speculate, I would say that if the language information is missing from a submission for whatever reason, it seems to default to Python).

I suspect that a similar error may have occurred here, and that the shortest Python solution submitted during the challenge was actually 72 bytes.

Update: After speaking with a moderator, it seems that Ari was right:

enter image description here

primo

Posted 2013-02-18T18:25:38.223

Reputation: 30 891

Delightful hacks here! – Ram Rachum – 2013-02-21T00:28:15.663

5

Python, 65 characters

exec'print`sum((-1.)**x/(x-~x)for x in range(input()))`;'*input()

shane

Posted 2013-02-18T18:25:38.223

Reputation: 541

Interesting. The result of input 10, for example, is not correctly rounded, but is still accepted as correct. Nice find. – primo – 2013-09-30T18:34:39.880

1

Python 2 (50 chars, with input loop grows)

a=n=1.;s=0;exec's+=a/n;a=-a;n+=2;'*input();print s

also prints only 12 digits of result, with loop and formatting

exec input()*"a=n=1.;s=0;exec's+=a/n;a=-a;n+=2;'*input();print '%.15f'%s;"

74 chars

Python 3 (57 chars, with input loop grows to 79 chars)

without loop

a=n=1;s=0;exec('s+=a/n;a=-a;n+=2;'*int(input()));print(s)

with loop

exec(int(input())*"a=n=1;s=0;exec('s+=a/n;a=-a;n+=2;'*int(input()));print(s);")

Python 3 prints 16 digits by default

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

76 chars

AMK

Posted 2013-02-18T18:25:38.223

Reputation: 506

A few problems: these don't iterate through a list of inputs (but rather only process a single input), and don't output the answer rounded to 15 decimal places (by default, Python only prints 12 digits of a float). Under these conditions, 49 bytes is possible, simply by removing the formatting and iteration from the accepted answer print sum((-1.)**i/(i-~i)for i in range(input())) – primo – 2013-10-01T05:41:05.300