GolfScript - The missing function

7

1

The GolfScript language has one serious lack, no float or fixed point handling. Remedy this by creating a function F for converting an integer number, given as a string, to a string representing 1 / 10 000 000 000 of it's value. The function must handle both positive and negative values as well as leading 0s.

The following pretty-print rules apply:

No trailing 0s.
No . if the result is integer.
No leading 0s, except for numbers with no integer part, they should have 1 leading 0.
The result must be output as a single string.

Examples:

123 -> 0.0000000123
-00 -> 0
012345678901234567890 -> 1234567890.123456789
-123 -> -0.0000000123
10000000000001 -> 1000.0000000001
123000000000000 -> 12300

The competition is only between GolfScript submissions using no Ruby functionality. Solutions in other languages may be posted, but they are not eligible for winning, these may not use any fixed or float point functionality that the language offers, and must accept arbitrarily large inputs.

aaaaaaaaaaaa

Posted 2011-03-09T18:15:54.663

Reputation: 4 365

Answers

7

55 chars

{~.0<"-"*\abs 10.?..3$@%+`-1%~`)[3-]1$,!!*+-1%@@/\++}:F

Negative numbers are a bit of a pain - responsible for about 13 chars of the 55. Trailing 0s are handled by using a 1 to stand for the decimal point, reversing as a string, converting to an int, and back again, and re-reversing.

Peter Taylor

Posted 2011-03-09T18:15:54.663

Reputation: 41 901

I wrote the requirements pretty strictly, you better give them an extra read and correct your code as needed, I'll postpone a full validation until you make an edit. But it's great to see the question finally being answered. – aaaaaaaaaaaa – 2011-04-06T23:28:38.803

@eBusiness, sorry, got so focussed on the pretty print rules that I forgot the bit about a function. – Peter Taylor – 2011-04-07T05:54:00.980

Okay, that makes one correct entry of 66 characters, and one incorrect of 57 that prints dots for integers. – aaaaaaaaaaaa – 2011-04-07T10:36:57.837

@eBusiness, I'll fix the second one when I got home. Wrote it on a scrap of paper on the bus. Edit: actually, I think I spotted the problem. – Peter Taylor – 2011-04-07T11:49:56.493

It pass all my test cases now, time to dig in and see what delicious details are in your code. – aaaaaaaaaaaa – 2011-04-07T12:15:11.497

Your 55 solution is also good. And just for the rabbit effect, I have got a 50 solution :-P – aaaaaaaaaaaa – 2011-04-07T15:46:11.047

V - Seems like you are going to be the only contestant. – aaaaaaaaaaaa – 2011-04-12T14:03:44.540

4

50 characters

{..~0<<\~abs`.,11\-,,0`*\++-10/(~.!!\"."+*\+-1%}:F

The most noteworthy trick in this solution is probably -10/, detach the last 10 characters and inverse, so that the trailing 0s can be culled by conversion by conversion to integer.

aaaaaaaaaaaa

Posted 2011-03-09T18:15:54.663

Reputation: 4 365

I think the nicest trick is the << to get the "-". I spent ages looking for a better way to do that and didn't find yours. – Peter Taylor – 2011-04-12T15:14:23.030

-10/ was what gave me the big advantage, though I probably was a bit lucky to find that "Hmmm, it also reverses the string, ohhh, that might be an advantage.". << surely ain't bad either. A shame that we didn't get some more participants, I'm sure this puzzle is hiding more tricks waiting to be uncovered. – aaaaaaaaaaaa – 2011-04-12T20:22:21.640

@Nabb Are we missing anything obvious? (And do the summon even work when you haven't participated in the thread?) – aaaaaaaaaaaa – 2011-04-12T20:24:54.913