19
6
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.
Example:
Input: 52146729
Output: 97654221 or 12245679
Notes:
- 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.
- The penalties apply not only to string literals in your program text, but any part of your program feature that arguably inputs or outputs a string or iterable. For example, JavaScript
String.prototype.split
has 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
int(input())
even ifinput
'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 isprint(x)
andx
must 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 anint
and returns anint
would serve, and wouldn't suffer from these ambiguities.
If I take a number and convert it to a string, and then use
split
but don't define the array directly will you count that as me using an array? – WallyWest – 2014-03-28T23:40:23.523Also, is '' counted as a multi-character string even though there's no characters within it? – WallyWest – 2014-03-28T23:41:15.120
1Does
" "
count as a multi character string? A single character would not be considered as "multi"... – WallyWest – 2014-03-28T23:50:47.237I think to really be in the spirit of the no strings rule the program should not be able to handle an input with a non-numeric character in it. – kojiro – 2014-03-28T23:54:23.147
How are zeros to be handled? Sorting 100 should yield what? 001? That's going to be very hard to do without strings. How 'bout 1? Of course, reverse sorting is easy. – MtnViewMark – 2014-03-29T02:07:03.187
Sorting 100 should yield 1 or 100. – kojiro – 2014-03-29T02:18:44.563
A single character is not multi character, until you combine it with another non empty character. – kojiro – 2014-03-29T02:22:18.183
4Seems to me like an easy implementation of sleepsort. – user12205 – 2014-03-29T04:13:50.173
1You don't forbid the language's sorting funtions. – user80551 – 2014-03-29T05:18:44.457
Is it okay if I don't add the
main()
method in JAVA? (i'm not considering console input.) – Hungry Blue Dev – 2014-03-29T07:04:33.980With all the "it doesn't count if the string isn't explicitly declared" pseudo-solutions, I'm tempted to write a pointfree answer that does all the work in integers without ever explicitly mentioning an integer anywhere. – user2357112 supports Monica – 2014-03-29T13:28:43.193
Yeah, I really meant no strings longer than one character, regardless of how the string comes to be. The only reasonable exception to this is the necessary initial step of converting the input from strong to int. – kojiro – 2014-03-29T15:16:42.540
I don't forbid the language's sorting functions. If you can find a way to use them without strings, arrays or iterables-of-digits, more power to you. – kojiro – 2014-03-29T15:34:24.657
1I'd write better the rules about built-in functions, also penalities are too low: this incentivate the use of an array instead of an alternative (probably longer) method. – Antonio Ragagnin – 2014-03-29T15:51:34.590
1@AntonioRagagnin I didn't want to change the penalties ex post facto, but I clarified the rules around built-in functions. – kojiro – 2014-03-29T19:34:28.933
Please elaborate on Note #2 – Hungry Blue Dev – 2014-03-30T05:35:12.217
Can I use a function that inputs a string? Does the function have to
return
orprint
? – user80551 – 2014-03-30T06:57:08.147Does a regex count as a string? – Hasturkun – 2014-03-30T08:08:59.987
@Hasturkun that's a tough one. I think to be fair they probably should, but even if the expression itself gets a pass it's hard to argue that regular expressions operate on non-strings. Wikipedia says regex are mainly for use in pattern matching with strings.
– kojiro – 2014-03-30T14:33:35.633@ace Here's a sleepsort answer. Not sure if you posted your comment before or after this
– Digital Trauma – 2014-03-30T19:24:26.397String is backed by an array. Does that mean if I use a multi-char string it actually costs me 30 chars? +20 for the array in the string? If not, then shouldn't ArrayList in java be free? – Cruncher – 2014-03-31T14:19:11.470
Can't I just use an empty array to gain a 20-point bonus? Repeat that a few times, profit. ;) – nyuszika7h – 2014-04-26T15:53:26.383