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!)
4815162342
I see what you did there, brotha. \$\endgroup\$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. \$\endgroup\$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. \$\endgroup\$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
. \$\endgroup\$