19
3
Challenge
Your task in this question is to write a program or a named function which takes a positive integer n
(greater than 0) as input via STDIN, ARGV or function arguments and outputs an array via STDOUT or function returned value.
Sounds simple enough ? Now here are the rules
- The array will only contain integers from
1
ton
- Each integer from
1
ton
should be repeatedx
times wherex
is the value of each integer.
For example:
Input:
5
Output:
[1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5]
The array may or may not be sorted.
This is code-golf so winner is shortest code in bytes.
Bonus
Multiply your score by 0.5
if no two adjacent integers in your output array are same.
For example for n = 5
, one such configuration would be
[5, 4, 5, 4, 3, 4, 5, 2, 5, 3, 1, 2, 3, 4, 5]
How about a 4-character expression?
/⍨⍳n
– ngn – 2014-12-20T09:31:43.183As you wish, sir, I've updated the text. But surely your objection must apply to other solutions that are not wrapped in functions? – ngn – 2014-12-20T11:17:48.247
If you write a full program, no need for a function name. But if you write a function, it has to be named. Most of the answers here are full programs (who read from STDIN and output to STDOUT) and all of which are functions, have names. – Optimizer – 2014-12-20T11:21:28.910
An expression is a full program. – ngn – 2014-12-20T11:23:21.767
A expression is not a full program if it requires you to edit the expression before running it. (for instance , to run your initial expression, one would have to add
f←
before and5
after the expression) – Optimizer – 2014-12-20T11:26:54.570I was referring to
/⍨⍳n
without thef←
and5
. And, of course, instead ofn
I can use⎕
which means "ask the user for input". – ngn – 2014-12-20T11:30:48.270Oh, if that was a full program taking input from user/STDIN and outputting/printing to STDOUT, then its fine. Obviously, I do not understand APL that much :) – Optimizer – 2014-12-20T11:55:25.537
Oh, I forgot to mention that APL by default prints the result of every expression, so the STDOUT bit is covered. Quad (
⎕
) asks for user input and evaluates it, so if the user types a number,⎕
will return that number. Unfortunately this can't be tested at tryapl.org as it runs in a restricted environment and⎕
is not available, but I've seen APL solutions to other challenges by other people that use⎕
for input. – ngn – 2014-12-20T12:12:04.953The question said bytes. Assuming you're using UTF-8, this is 10 bytes. – nyuszika7h – 2014-12-20T12:43:03.167
3Dyalog APL comes in two flavours: "Classic" and "Unicode". The Classic version has existed for decades, since before the Unicode standard appeared, and uses a custom byte-per-character encoding for the APL character set. It is still supported, though its use is discouraged. So, I'd like to use this as an excuse. More broadly, I think in golfing we should be counting characters, not bytes. The fact that the lowest code points in Unicode are occupied by the English-centric ASCII is a historical accident that shouldn't matter today. Interestingly, APL was conceived before ASCII came out. – ngn – 2014-12-20T13:01:12.970
3
@ngn counting chars is not a good idea, as answers will generally become alphabet soup decodes. APL chars are counted as bytes because that encoding exists; this is well established on this site. This works with any byte encoding that existed prior to the question's asking.
– FryAmTheEggman – 2014-12-20T17:37:01.9271@ngn: Can you explain your bonus answer? Because it can be done via: 5 4 3 2 1 5 4 3 2 5 4 3 5 4 5 or 6 minus each of 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1, which feels like it is not far from your initial answer. – Phil H – 2014-12-22T11:20:18.177
@phil-h Excellent idea, thank you! So, it can be done in 8 characters (
– ngn – 2014-12-22T16:58:57.8336-∊⌽⍳¨⍳⎕
) instead of my complicated 11... I'll update the answer and add an explanation later today.