17
2
The challenge is really simple: given a number, you split its digits into an array of smaller numbers such that the resulting numbers are non-decreasing. The catch is that you have to split it such that the array length is maximal.
Confused?
- You are given a positive integer via STDIN (or closest alternative), command-line argument or function argument in any convenient, unambiguous input format.
- You have to partition the number's decimal digits into contiguous, disjoint groups.
- The array of numbers represented by these digit groups should be sorted (in the usual, non-decreasing order) without rearranging the groups.
- In cases where more than one such partition exists, you have to partition the input into as many numbers as possible. In the case of ties, return one such result.
- You can output the array to STDOUT (or closest alternative) or as a function return value. In case of STDOUT (or closest alternative), the array should be printed in any convenient, unambiguous list format.
- The split numbers should not have leading zeroes. So for instance
1002003
cannot be printed as either[1, 002, 003]
or[1, 2, 3]
and the only valid answer for it is[100, 2003]
.
Test cases:
123456 -> [1, 2, 3, 4, 5, 6]
345823 -> [3, 4, 5, 8, 23]
12345678901234567890 -> [1, 2, 3, 4, 5, 6, 7, 8, 90, 123, 456, 7890]
102 -> [102]
302 -> [302]
324142 -> [3, 24, 142] OR [32, 41, 42]
324142434445 -> [32, 41, 42, 43, 44, 45]
1356531 -> [1, 3, 5, 6, 531]
11121111111 -> [1, 1, 1, 2, 11, 11, 111]
100202003 -> [100, 202003]
Scoring
This is code-golf so shortest code in bytes wins.
You can use
aY
instead of~Y]
– FryAmTheEggman – 2015-03-18T00:18:06.243@FryAmTheEggman I always forget about
a
. Don't know why. – Jakube – 2015-03-18T00:21:10.857@Jakube Maybe because it's not in the docs? – Sp3000 – 2015-03-18T02:07:28.920
I had a solution for ~45 chars. I was not aware of the fact that
int("01")
being an error in Pyth (this doesn't happen in Python). – orlp – 2015-03-18T03:06:47.830@orlp Me neither. I thought Pyth (and Python) translates it to an Octal number. Stumbled upon it accidentally. – Jakube – 2015-03-18T07:54:49.243
@orlp Got curious about Python:
int("01")
works in Python 2/3, buteval("01")
(which I use to convert it) fails in Python 3. – Jakube – 2015-03-18T08:14:52.530@Jakube This is not O(n). It is O(2^n), because of
yUz
. – isaacg – 2015-03-18T08:19:39.787int("01")
is not an error in Pyth. That would bes"01"
, notv"01"
. – isaacg – 2015-03-18T08:20:40.150@isaacg It is O(n), at least if you interpret the input as number. O(2^len(input)) = O(2^log(number)) = O(number). – Jakube – 2015-03-18T08:21:46.783
3@Jakube haha, although seems logical, but generally
n
is the length of the input. – Optimizer – 2015-03-18T09:06:08.140