7
\$\begingroup\$

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.

\$\endgroup\$
7
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – aaaaaaaaaaaa Apr 6 '11 at 23:28
  • \$\begingroup\$ @eBusiness, sorry, got so focussed on the pretty print rules that I forgot the bit about a function. \$\endgroup\$ – Peter Taylor Apr 7 '11 at 5:54
  • \$\begingroup\$ Okay, that makes one correct entry of 66 characters, and one incorrect of 57 that prints dots for integers. \$\endgroup\$ – aaaaaaaaaaaa Apr 7 '11 at 10:36
  • \$\begingroup\$ @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. \$\endgroup\$ – Peter Taylor Apr 7 '11 at 11:49
  • \$\begingroup\$ It pass all my test cases now, time to dig in and see what delicious details are in your code. \$\endgroup\$ – aaaaaaaaaaaa Apr 7 '11 at 12:15
4
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Peter Taylor Apr 12 '11 at 15:14
  • \$\begingroup\$ -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. \$\endgroup\$ – aaaaaaaaaaaa Apr 12 '11 at 20:22
  • \$\begingroup\$ @Nabb Are we missing anything obvious? (And do the summon even work when you haven't participated in the thread?) \$\endgroup\$ – aaaaaaaaaaaa Apr 12 '11 at 20:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.