# 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 to n
• Each integer from 1 to n should be repeated x times where x 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 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]


# Perl, 57 * 0.5 = 28.5

This entry is a subroutine named "l" (for "list"?)

sub l{$d=$n=shift;while($d){for($d--..$n){@a=(@a,$_)}}@a}


You can test it like this:

$"=", "; @a=l(3); print "[@a]\n"; # prints "[3, 2, 3, 1, 2, 3]" @a=(); # needed because the function does not re-initialize @a @a=l(5); print "[@a]\n"; # prints "[5, 4, 5, 3, 4, 5, 2, 3, 4, 5, 1, 2, 3, 4, 5]"  A prior solution is shorter (45 characters), but doesn't print the output prettily so it was disqualified $d=$n=<>;while($d){for($d--..$n){print"$_ "}}  For an input of 3, this one prints (with a trailing space): 3 2 3 1 2 3  • On second thought, only the [] are required. You can get rid of the , – Optimizer Dec 19 '14 at 21:57 # Perl 48 * 0.5 = 24 $i=<>;$"=", ";@a=map{-$_..$i}-$i..-1;print"[@a]"


Test run:

$./repeated.pl <<< 5 [5, 4, 5, 3, 4, 5, 2, 3, 4, 5, 1, 2, 3, 4, 5]  • Are you sure about <!-- language=lang-perl -->? The result doesn't seem right. – John Dvorak Dec 20 '14 at 10:31 • I know. $" makes the syntax highlight crazy :) – core1024 Dec 20 '14 at 10:32
• Can you rename the variable? Switch to lang-none? – John Dvorak Dec 20 '14 at 10:33

# Erlang, 56 Bytes * 0.5 = 28

f(I)->L=lists,L:flatten([L:seq(X,I)||X<-L:seq(I,1,-1)]).


Using a list comprehension, reverses the list using lists:seq(UpperBound, 1, -1) and for each element, creates a sequence using lists:seq(CurrentValue, UpperBound). The eventual list is then flattened.

Expanded:

f(I) ->
lists:flatten([lists:seq(X, I) || X <- lists:seq(I, 1, -1)]).


Input: module_name:f(9).

Output: [9,8,9,7,8,9,6,7,8,9,5,6,7,8,9,4,5,6,7,8,9,3,4,5,6,7,8,9,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9]

# Perl 5: 30 bytes / 2 = 15

Added one byte for the -p flag.

#!perl -p
$_=join$",map-$_..$',-$_..-//  Takes parameter on the input: $ perl int.pl <<<5
5 4 5 3 4 5 2 3 4 5 1 2 3 4 5


## Java (adjacent equals ok): 276 bytes

public class a{public static int[]b(final int c){final int d=(c+1)*c/2;final int[]e=new int[d];int f=1;int g=0;for(int i=0;i<d;i++){if(g==f){f++;g=0;;}
e[i]=f;g++;}
return e;}
public static void main(String[]args){final int q=new java.util.Scanner(System.in).nextInt();b(q);}}


## Java (adjacent equals not ok): 442 * 0.5 = 221 bytes

public class a{private static int h=1;public static int[]j(final int k){final int[]l=new int[k+1];for(int i=1;i<=k;i++){l[i]=i;}
final int m=(k+1)*k/2;final int[]n=new int[m];for(int i=1;i<n.length;i++){n[i]=o(l,k);}
n[0]=k;return n;}
private static int o(final int[]p,final int q){if(h>q){h=1;}
while(p[h]==0){h++;}
p[h]=p[h]-1;return h++;}
public static void main(String[]args){final int q=new java.util.Scanner(System.in).nextInt();j(q);}}

• OK then I removed the aim statement, submission now aligns with the criteria. – PoweredByRice Dec 23 '14 at 15:31
• minified to comply with rules. – PoweredByRice Dec 23 '14 at 15:40
• Most of the modifiers on your variables aren't needed. – James Webster Dec 23 '14 at 16:16

## CoffeeScript, 57

f=(n)->i=0;a=[];(j=0;a.push i while(j++<i))while(i++<n);a


# JAGL Alpha 1.1 - 15 bytes

1Ti[r{d()+S*}/E


Explanation:

1Ti               Push 1, take input, and convert to integer
[r             Increment and make a range
{d()+S*}     Push a block that makes an array with x occurrences of x
/E   Map over list and flatten

• I can see this is your own language, mere two hours older than the question. I guess this is just a test of the language? Note you won't be able to use v 1.2 for this challenge, and it wouldn't be a good test of your language anyways. Oh, and the documentation needs to be vastly improved before a public release ;-) – John Dvorak Dec 20 '14 at 9:15
• I'll work on that. I haven't had much time to make the full documentation yet, but that will be the next step. The language itself is a few days old, but the most recent update was released just a couple hours before the question. – globby Dec 20 '14 at 16:48

# Matlab, 44 * 0.5 = 22

function r=f(n);r=[];for i=1:n;r=[i:n,r];end


f(4) == [4 3 4 2 3 4 1 2 3 4]

# Javascript, 63 * 0.5 = 31.5

F=n=>{r=[m=n];while(--n){r=r.slice(0,m-n).concat(n,r)}return r}


F(4) == [ 4, 3, 2, 1, 4, 3, 2, 4, 3, 4 ]

Nothing fancy, just concatenating arrays.

# Tcl, 36 bytes

time {time {puts $i} [incr i]}$argv


Try it online!

# Stax, 4 bytes * 0.5 = 2

R|]r


Run and debug it online! Note: To verify that is does indeed work, you'll need to step through the execution, as the values are printed as characters rather than numbers.

### Explanation:

R|]r
R       Range [1..n].     Example: [1, 2, 3, 4]
|]     List of suffixes: Example: [[1, 2, 3, 4], [2, 3, 4], [3, 4], [4]]
r    Reverse.          Example: [[4], [3, 4], [2, 3, 4], [1, 2, 3, 4]]
Implicit flatten and print (treating numbers as characters and concatenating).


### Without the bonus (3 bytes):

R|]


or

R|[     (This does what you expect)


# Jelly, 3 × 0.5 = 1.5 bytes

rRF


Try it online!

## Explanation

rRF
r      Range from {input} to
R       each number from 1 to {input}
F    Flatten


For example, with input 3, the rR gives us a range from 3 to 1 (i.e. [3, 2, 1], then a range from 3 to 2 (i.e. [3, 2]), then a range from 3 to 3 (i.e. [3, 3]). Flattening that gives [3, 2, 1, 3, 2, 3]), which meets the problem specifications and has no adjacent numbers.

## Pyth, 6 bytes

smrQdU


Try it online!

# K (oK), 4 bytes

Solution:

&!1+


Try it online!

Examples:

&!1+5
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
&!1+6
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6


Explanation:

&!1+ / the solution
1+ / add one to the input, 1+5 => 6
!   / til, !6 => 0 1 2 3 4 5
&    / where, &0 1 2 3 4 5 => 1 2 2 3 3 3...


# Java 8, 109 bytes (score: 54.5 bytes)

import java.util.*;


n->{List l=new Stack();l.add(n);for(int i=0,j;i<n-1;i++)for(j=i;j++<n;)l.add(j);return l;}


That's a lambda from int to List<Object>. The members are, of course, Integers.

Try It Online

# Golang, 80 bytes

func n(a int)(r[]int){for i:=0;i<=a;i++{for j:=0;j<i;j++{r=append(r,i)}};return}


func n(a int) (r []int){
for i := 0; i <= a; i++ {
for j := 0; j < i; j++ {
r = append(r, i)
}
}
return
}


Try it online!

Pretty straightforward solution and probably pretty bad.

# Pyth, 17 bytes x 0.5 = 8.5

FbQ=+Y-SQSb)+eYPY


Possibly golfable. Explanation:

FbQ=+Y-SQSb)+eYPY
FbQ                For b in range(int(input()))
Y             A variable containing an empty list
=+              +=
-            The set difference of
SQ          [1, 2, ..., input()]
Sb        [1, 2, ..., b]
)       End function
+eY    Append to the last element of Y
PY  All but the last element of Y


So for example, putting 3 through the loop would yield [1,2,3,2,3,3], then the last five characters will transpose that final 3 to the beginning so no two numbers touch each other.

# GolfScript, 14 bytes

~),1>{.[()]*}%


Try it online!