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!