Inspired by the ill-fated sorting-a-numbers-digits-without-using-an-array, but I thought it made a better code golf than SO question.
Given a positive integer, sort the digits in that integer.
Lowest score wins!
- Start with 0 points.
- Add one point per character.
- Add 20 points for each array you use.
- Add 10 points for each multi-character string in your code. (Except the initial input as long as it is converted to an integer without any other operations done on it.)
- Add 32 points if the maximum number of digits your program can handle is limited by your program (as opposed to the machine).
- Subtract 10 points if your code can change the direction of the sort given another argument (whatever you want, but for example 0 for descending sort and 1 for ascending.)
Every language is different, but the idea is to avoid any kind of iterable-of-digits hackery.
Output: 97654221 or 12245679
- Use any built-in sorting capabilities your programming language provides, but if that sort feature involves strings or arrays, take the penalty!
- You can write the solution as a function that takes an integer directly, or as a program that takes an argument from argv, a file or stream and converts it to an integer. As long as you convert it to an integer immediately and discard the original char* input without doing any further operations on it, no penalty applies.
String.prototype.splithas at least one string as input (
this) and an Array as output, so +30 for using that.
- I've tried to make these rules guide the principle of the algorithm design, not the initial/final I/O (hence note #2). I don't think the penalty ought to apply to
input's signature says it returns a string, as long as that expression is the initial entry point of the program. Likewise, if the final output of the program is
xmust be a string, the penalty doesn't apply to the last-ditch string casting operation. All that said, I explicitly never said that this had to be a program or where the I/O had to come from or go. A function that takes an
intand returns an
intwould serve, and wouldn't suffer from these ambiguities.