31
3
Write a program that goes through a string of non-whitespace characters (you may assume that they are digits 0
to 9
, but nothing in the way they are to be processed depends on this) and adds spaces according to the following rules.
- Let the current token be the empty string, and the previously emitted tokens be an empty set.
- Iterate through the characters of the string. For each character, first append the character to the current token. Then if the current token is not already in the set of previously emitted tokens, add the current token to that set and let the new current token be the empty string.
- If when you reach the end of the string the current token is empty, output the previously emitted tokens in order of emission, separated by a space character. Otherwise output the original string verbatim.
Input
The input to the STDIN should be a sequence of digits.
Output
The program should print the result as specified in step 3.
Samples
Sample inputs
2015
10101010
4815162342
101010101010
3455121372425
123456789101112131415
314159265358979323846264338327950288419716939937
Sample outputs
2 0 1 5
10101010
4 8 1 5 16 2 3 42
1 0 10 101 01 010
3 4 5 51 2 1 37 24 25
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
3 1 4 15 9 2 6 5 35 8 97 93 23 84 62 64 33 83 27 95 0 28 841 971 69 39 937
This is code golf, so standard CG rules apply. Shortest program in bytes wins.
(Please request any clarifications in the comments. I'm still new to this. Thanks!)
10
4815162342
I see what you did there, brotha. – Fatalize – 2015-09-15T12:36:40.447I suggest to get rid of the “repeat at the end of the number” part of rule 7. (It confused me too.) Maybe you could say, there are not enough digits to form a distinct number. – manatwork – 2015-09-15T13:18:19.623
Probable error in the spec: step 6 asks us to repeat steps 3 and 4, so the only number in the output which can be more than one digit long is the second one. If that were to be corrected simply by changing it to "Repeat steps 4 and 5 ...", the movement in step 5 would mean that each number in the output should have an odd number of digits. And numbers which have leading zeroes are equal to the number without the leading zeroes, so if you want to distinguish
01
from1
you should stop talking about numbers altogether and talk about strings instead. – Peter Taylor – 2015-09-15T13:37:59.513A test case which my code originally failed despite passing all the ones in the challenge:
11312123133
– Martin Ender – 2015-09-15T13:41:20.61316Proposed OEIS entry: numbers which are split into at least two segments by this process. – Martin Ender – 2015-09-15T14:05:20.313
@Sok Notice that
10101010
can result in1 0 10 1010
, which fits the rules. – Ismael Miguel – 2015-09-15T14:50:29.8833@IsmaelMiguel Step 5 (as any other step) can only advance one digit at a time. Once you’ve got
1 0 10
, the next iteration will find1
(already used), then advance one to find10
(already used), then advance one to find101
, which is new and would be ‘added’. It would then add a space and you'd get to a new0
, which has already been used, but is here at the end of the string. Therefore, the output would be1 0 10 101 0
, which is invalid (0
is repeated), and the script must then just output the input string. It could only make1010
if101
had already been used. – Janus Bahs Jacquet – 2015-09-15T15:32:21.700You list
10101010
as an output. That looks like a mistake. – kasperd – 2015-09-15T16:40:20.5033@kasperd No
If a unique number cannot be formed at the end of the string, then the input should be printed verbatim
10101010 cannot be split so it is printed as is. – edc65 – 2015-09-15T17:05:02.787@edc65 I see. I had misunderstood that rule. – kasperd – 2015-09-15T17:07:52.197
@JanusBahsJacquet, no. Given input
10101010
steps 1 to 3 give1 ^0101010
(with^
to indicate the position of the cursor); then step 4 gives1 0^101010
, step 5 gives1 0 ^101010
; step 4 gives1 0 1^01010
; step 5 gives1 0 10^1010
; step 4 gives1 0 101^010
; step 5 gives1 0 101 ^010
; step 4 gives1 0 101 0^10
; step 5 gives1 0 101 01^0
; step 4 gives1 0 101 010^
; step 5 gives1 0 101 010
; and the loop breaks. So the specified output for that input is1 0 101 010
– Peter Taylor – 2015-09-15T20:34:17.243@PeterTaylor No.
10
has not been used yet at1 0 10^1010
, so it wouldn't continue to1 0 101^010
, but add a space, making1 0 10 ^1010
. Moving on to101
doesn't happen until the next ‘group of 10’ when it reaches1 0 10 101^0
. – Janus Bahs Jacquet – 2015-09-15T20:38:03.713@JanusBahsJacquet, step 5 doesn't have a self-loop. It either inserts a space or moves one right. Then you go back to step 4. That's why I pointed out in my first comment that the spec requires each generated part to have an odd number of digits. – Peter Taylor – 2015-09-15T20:39:18.350
@PeterTaylor Exactly. It adds a space if doing so creates a number which has not already been used. At the relevant position,
10
has not yet been used, so step 5 adds a space and does not move to the right. I don't see how that would imply that the output must be an odd number of digits. – Janus Bahs Jacquet – 2015-09-15T20:42:17.0771But when you enter step 5, the space would be after the
1
, which would be a repeat. So instead you move right one in space 5, and then you move right one again in step 4, and you enter step 5 again and create101
. – Peter Taylor – 2015-09-15T20:43:50.450@PeterTaylor Aaah, yes. I see what you mean now. Really, step 4 should be done away with entirely, and step 5 should be the only repeated one. Well spotted! (Really, steps 2 through 4 can all be scrapped, since the first digit will always be unused the first time it's reached.) – Janus Bahs Jacquet – 2015-09-15T20:48:53.853
@PeterTaylor Looks like you beat me to the editing. Thanks! – Arcturus – 2015-09-15T22:40:19.600