11
1
The Challenge
In this challenge you have to do two different (but related) tasks depending of the order of the input.
Your program will recieve a string s
and an integer n
as input and will
- split
s
into pieces of lengthn
ifs
comes first. The last element will be shorter if necessary. - split
s
inton
pieces of equal length ifn
comes first. Iflen(s)
is not a multiple ofn
the firstlen(s) mod n
elements will be one longer.
You may only take those 2 inputs. s
will never contain only digits.
Notes
- You may use the reverse mapping. Note this in your answer if you do that.
s
will only contain printable ASCII characters (no newlines).- You may not use any builtins that solve either those two tasks directly. All other builtins are allowed.
- You have to take both arguments from the same source.
- You may take the arguments in an ordered list or any other format which clearly indicates their order as long as it is unambiguous.
- You may take the input as one string/ stream and use a character which is not a valid input (like a nullbyte) to separate them.
n
will always be equal or less than the length ofs
and greater than zero.- You may output the resulting list in any resonable format as long as it clearly indicates the particular pieces and their order.
Example
Input: programming, 3
The last element contains only 2 characters, because 11 is not divisible by 3.
Output: ["pro", "gra", "mmi", "ng"]
Input: 3, programming
11 is not a multiple of 3, so the first 2 elements will be one longer:
Output: ["prog", "ramm", "ing"]
Rules
- Function or full program allowed.
- Default rules for input/output.
- Standard loopholes apply.
- This is code-golf, so lowest byte-count wins. Tiebreaker is earlier submission.
Test cases
The test cases got generated with this Pyth program (uses builtins, so no valid answer). Thanks to @FryAmTheEggman for providing the base version of that!
3, helloworld -> ['hell', 'owo', 'rld'] helloworld, 3 -> ['hel', 'low', 'orl', 'd'] 1, programming -> ['programming'] programming, 1 -> ['p', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g'] 8, programming -> ['pr', 'og', 'ra', 'm', 'm', 'i', 'n', 'g'] programming, 8 -> ['programm', 'ing'] 9, code golf -> ['c', 'o', 'd', 'e', ' ', 'g', 'o', 'l', 'f'] code golf, 9 -> ['code golf'] 4, 133tspeak -> ['133', 'ts', 'pe', 'ak'] 133tspeak, 4 -> ['133t', 'spea', 'k']
Happy Coding!
You may not use any builtins that solve those two tasks. Does that include other built-ins, such get every n-th character from a string or split at occurrences? – Dennis – 2016-04-07T16:54:13.930
@Dennis This was just meant to rule out builtins that solve this directly. I clarified. – Denker – 2016-04-07T16:55:58.643
If our language does not support arrays, how should we output? Would a newline between each result of the string be acceptable? – Conor O'Brien – 2016-04-08T03:50:22.967
Also, for languages where the input is an array of ambiguous numbers, what should the procedure be? – Conor O'Brien – 2016-04-08T03:54:58.850
@CᴏɴᴏʀO'Bʀɪᴇɴ Can't happen. s will never contain only digits. Also You may output the resulting list in any resonable format as long as it clearly indicates the particular pieces and their order which includes multiline output of course. – Denker – 2016-04-08T04:39:52.293
@DenkerAffe Let me clarify. The input to the program
65, "Hello!"
would be read by the program as65,72,101,108,108,111,33
, so it cannot disambiguate which is the string and which isn't. To compensate, could one take a number that represents the order of the arguments? So the input could be1 <n> <s>
or0 <s> <n>
. Secondly, you say the input can have printable ASCII characters, including newlines, which would make my output ambiguous as well. – Conor O'Brien – 2016-04-08T04:42:50.517@CᴏɴᴏʀO'Bʀɪᴇɴ I don't count newlines as printable ASCII, so thats one problem solved. However, taking a number which indicates the input order would be a huge advantage, so I can't really allow that, sorry.
– Denker – 2016-04-08T04:55:13.747Let us continue this discussion in chat.
– Conor O'Brien – 2016-04-08T04:55:30.883I assume the answer is no, but can builtins that solve one task be used to solve the other? – Unrelated String – 2019-05-19T23:33:40.537