| Bytes | Lang | Time | Link |
|---|---|---|---|
| nan | Swift 6 | 250515T125555Z | macOSist |
| 003 | Vyxal 3 | 250515T080540Z | Themooni |
| nan | J | 250515T074004Z | Galen Iv |
| nan | Juby | 250512T010906Z | Jordan |
| nan | Python 2 | 250515T004015Z | Lucenapo |
| nan | Vyxal | 250412T014818Z | lyxal |
| 028 | TIBASIC TI83 | 250408T163737Z | absolute |
| nan | JavaScript | 250409T115858Z | Shaggy |
| nan | Ruby | 221014T223339Z | Jordan |
| 023 | Zsh | 250408T082713Z | roblogic |
| nan | Tcl | 250408T004819Z | sergiol |
| nan | Python 3.8 prerelease | 240624T190212Z | squarero |
| 009 | Uiua | 240621T110914Z | Joonas |
| nan | 240130T052620Z | RARE Kpo | |
| nan | Thunno 2 | 230617T132613Z | The Thon |
| 012 | K ngn/k | 221015T130731Z | oeuf |
| 6053 | Pyth | 180324T230330Z | hakr14 |
| nan | Japt | 221014T215219Z | Kamil Dr |
| nan | Raku | 221014T205411Z | Sean |
| 004 | Vyxal | 221014T070452Z | DialFros |
| 014 | APOL | 220125T134102Z | Ginger |
| 165 | GeoGebra | 220125T074552Z | Aiden Ch |
| 2412 | MathGolf | 220125T083032Z | Kevin Cr |
| 098 | brainf*** | 220105T212021Z | ThatCool |
| 044 | JavaScript V8 | 220102T082442Z | ThatCool |
| 009 | Add++ | 220102T001051Z | lyxal |
| nan | Factor + grouping.extras | 220101T163723Z | chunes |
| 4052 | Husk | 220101T141450Z | Natte |
| nan | Python | 220101T112446Z | loopy wa |
| nan | Wolfram Language Mathematica | 141220T034627Z | alephalp |
| 4052 | 05AB1E | 220101T073304Z | wasif |
| 014 | GolfScript | 200127T095022Z | user8505 |
| nan | Pyth | 180415T194944Z | ericesch |
| 080 | Golang | 180415T181407Z | Dust |
| nan | Java 8 | 180324T222311Z | Jakob |
| 004 | K oK | 180324T163206Z | mkst |
| nan | C# | 180323T220918Z | Romen |
| nan | Jelly | 180323T211904Z | ais523 |
| 4052 | Stax | 180323T202348Z | Khuldrae |
| 036 | Tcl | 180323T184244Z | sergiol |
| nan | Matlab | 150101T122118Z | zabalajk |
| 053 | Haxe | 141220T114924Z | Mark Kno |
| 015 | JAGL Alpha 1.1 | 141220T000859Z | globby |
| 057 | CoffeeScript | 141226T004246Z | rink.att |
| 004 | APL | 141220T054128Z | ngn |
| nan | C# | 141222T211059Z | Ichabod |
| 276 | Java adjacent equals ok | 141223T152551Z | PoweredB |
| nan | Haskell | 141219T190000Z | John Dvo |
| 030 | Perl 5 | 141223T102357Z | nutki |
| nan | TECO | 141222T130922Z | feersum |
| nan | Erlang | 141222T080818Z | c.P.u1 |
| 131 | JavaScriptreadable | 141222T020353Z | SirPytho |
| nan | ECMAScript6 | 141220T194953Z | binormal |
| nan | 141221T115152Z | edc65 | |
| nan | Haskell | 141219T182505Z | Sp3000 |
| 081 | C# 81 161bytes * 0.5 | 141219T180242Z | VisualMe |
| nan | Python 2 | 141219T173941Z | Sp3000 |
| nan | Pyth | 141219T185013Z | Sp3000 |
| nan | Ruby recursive | 141219T182753Z | AlexRath |
| 026 | perl | 141219T184454Z | michael5 |
| 024 | Perl 48 * 0.5 = | 141220T102938Z | core1024 |
| nan | Haskell | 141220T110159Z | proud ha |
| nan | J | 141220T092029Z | randra |
| nan | 141219T180402Z | ProgramF | |
| nan | TSQL | 141219T230356Z | MickyT |
| 014 | GolfScript | 141219T224237Z | Peter Ta |
| nan | CJam | 141219T215050Z | Martin E |
| nan | Perl | 141219T200157Z | KJP |
| nan | JavaScript | 141219T210452Z | apsiller |
| nan | Bash + coreutils | 141219T194626Z | Digital |
| nan | Bash with seq | 141219T193602Z | pgy |
| 005 | Pyth 10 * .5 = | 141219T184950Z | FryAmThe |
| nan | R | 141219T193150Z | MickyT |
| nan | vba | 141219T181208Z | SeanC |
| 041 | JavaScript | 141219T180952Z | Optimize |
| nan | C | 141219T180749Z | feersum |
Vyxal 3, 3 bytes
ƛY⍟
Getting a worthwile bonus would require achieving it in only 2 extra bytes, and I don't see that happening.
ƛY⍟
ƛ # map over implict range 1..implicit input
Y # repeat implicit n implicit n times
⍟ # close map and flatten
💎
Created with the help of Luminespire.
<script type="vyxal3">
ƛY⍟
</script>
<script>
args=[["4"],["5"],["2"]]
</script>
<script src="https://themoonisacheese.github.io/snippeterpreter/snippet.js" type="module"/>
J-uby, 38 bytes × 0.5 = 19 bytes
~:^%(:& &~(:!~|:~)|:+&:flat_map|:|&:+)
J-uby, 21 bytes
No bonus.
:+|:flat_map+~:*%-[I]
Vyxal, 21 bitsv2, 2.625 × 0.5 = 1.3125 bytes
ɾṡf
Bitstring:
001000110010110111010
Given the optimal answer is going to score 1.5 and tie everything, I figured it'd be funny to claim untied 1st using range coding :p
Explained
ɾṡf
ɾ # Range [1, input]
ṡ # Range [input, x] for each x in ^
f # Flatten that, resulting in a list where no adjacent numbers are the same
TI-BASIC (TI-83), 28 29 bytes
-1 byte thanks to MarcMush
Input A
{1
For(I,2,A
augment(Ans,seq(I,X,1,I
End
Ans
Explanation:
Input A ; Request the input and store it in A
{1 ; Store {1} in Ans
For(I,2,A ; Loop from 2 to the input
seq(I,X,1,I ; Generate a list of length I filled with I
augment(Ans, ; ... then append it to Ans
End
Ans ; Print Ans
Demonstration:
prgmCDGF2F
?4
{1 2 2 3 3 3 4 ▶
TI-BASIC has a list length limit of \$999\$ entries, so this program will only work up to inputs of \$44\$ since the length of the generated list is \$length(N)=\frac{N(N+1)}{2}\$ and \$length(45)=1035>999\$.
Note: TI-BASIC is a tokenized language. Character count does not equal byte count.
Program size is equal to \$MEM\: byte\: count - program\: name\: length - 9\: bytes\$.
JavaScript, 48 bytes * .5 = 24
f=n=>[...n?f(n-1):[],...Array(n)].map(i=>i||n--)
43 bytes * .5 = 21.5
With more relaxed output formatting.
f=n=>n?f(n-1)+[,...Array(n)].map(_=>n--):[]
41 bytes
Without the bonus.
f=n=>n?[...f(n-1),...Array(n).fill(n)]:[]
Tcl, 64 bytes * 0.5 = 32 points
time {set j $argv
while \$j>=[incr k] {puts $j
incr j -1}} $argv
Python 3.8 (pre-release), 52 × 0.5 = 26 bytes
lambda n:[j+n for i in range(n)for j in range(-i,1)]
A simple double list comprehension solution.
Uiua, 9 bytes, 5 characters
▽.+1⇡
Code explained:
⇡ # Create an array of integers from 0 to [input]
+1 # Add 1 to each element
. # Duplicate the array
▽ # Create [element value] amount of copies of each element of the array
jot 23 | awk NF=OFS=\$_
1
22
333
4444
55555
666666
7777777
88888888
999999999
10101010101010101010
1111111111111111111111
121212121212121212121212
13131313131313131313131313
1414141414141414141414141414
151515151515151515151515151515
16161616161616161616161616161616
1717171717171717171717171717171717
181818181818181818181818181818181818
19191919191919191919191919191919191919
2020202020202020202020202020202020202020
212121212121212121212121212121212121212121
22222222222222222222222222222222222222222222
2323232323232323232323232323232323232323232323
Thunno 2, 3 bytes * 0.5 = 1.5
RȷI
Explanation
RȷI # Implicit input -> 5
R # Range [1..input] -> [1,2,3,4,5]
ȷ # Map over each:
I # Range [input..n] -> [[5,4,3,2,1],[5,4,3,2],[5,4,3],[5,4],[5]]
# Sum (flatten) the list -> [5,4,3,2,1,5,4,3,2,5,4,3,5,4,5]
# Implicit output
K (ngn/k), 12 bytes
,/{x#x}'1+!:
Short and sweet.
Explanations:
,/{x#x}'1+!: Main function. Takes implicit input x via Right (:)
! Create a range between 0 to x (exclusive)
1+ + 1 to each of them
' For each of the numbers in the range
{ } Execute a function that
x Takes the number as implicit argument x
x# And duplicate by themselves' amount of times
,/ Join together
Japt, 5 bytes * 0.5 = 2.5
õõU c
õõU c #
õ # For all numbers X in the range [1, input]:
õU # Generate the range [input, X]
c # Flatten
A cheatier 3 byte version produces the same visible output, but that's only because Japt's default string representation of 2D arrays isn't very clear. Using the -Q flag reveals the real output.
Raku, 25 bytes * 0.5 = 12.5
{$_,|flat (1..$_-1)X..$_}
This generates the list consisting of the input number n, and the flattened list of ranges 1 .. (n-1), 2 .. (n-1), ..., n-2 .. n-1, n-1 .. n-1.
APOL, 14 bytes
⭳(Ƒ(⧣ Ƒ(∈ ⋒)))
Explanation:
Implicit print
⭳( Flatten list
Ƒ( One-indexed list-builder for loop
⧣ Integer input
Ƒ( One-indexed list-builder for loop
∈ Loop item (of parent loop in this case)
⋒ For iterator (what's being iterated through)
)
)
)
GeoGebra, 44 33 bytes * 0.5 bonus = 22 16.5
n=4
l=Flatten(Zip(1…a,a,n…1))
My first time code golfing with GeoGebra :D and I gotta say, GeoGebra has a ton more built-in functions than Desmos. I think I'll start trying out GeoGebra a little bit more, it definitely has a lot of features. It's also surprisingly golfy, especially when it comes to lists!
But, I couldn't figure out how to get functions working properly in GeoGebra for some reason, so the input has to be changed by editing the value of n, and then pressing Enter. The code currently has a default input of n=4.
MathGolf, score: 2 (4 bytes \$\times \frac{1}{2}\$ bonus)
╒╒x─
Explanation:
╒ # Push a list in the range [1, (implicit) input]
╒ # Map each value to an inner list in the range [1, value]
x # Reverse this list of lists
─ # Flatten
# (after which the entire stack is output implicitly as result)
brainf***, 98 bytes
[[->+>+<<]>>[-<<+>>]<-[[->+>+<<]>>[-<<+>>]<-<[-]<[->+>>+<<<]>>>[-<<<+>>>]<]<[->+>+<<]>>[-<<+>>]<-]
Takes input as first cell on tape and outputs onto the tape. I'm not sure if this is allowed but input and output is really painful in brainf*** so I'm presuming it's allowed. The example on TIO has header and footer code to print the values out because I couldn't find a way to dump the memory into the output.
If you consider that each brainf*** character only contains 3 bits of information (as there are only 8 possible characters), then it's only 98 * (3/8) = 36.75 bytes of information.
I would put an explanation but I wrote the code yesterday and have forgotten how it works since then.
JavaScript (V8), 44 bytes
y=x=>x-1?y(x-1).concat(Array(x).fill(x)):[1]
Could probably be golfed more and probably similar to some other answers but might as well post it.
Add++, 9 bytes
L,Rdz£XbF
In most other languages, it might be worth going for the bonus by using things like prefixes etc. Not in add++.
Explained
L,Rdz£XbF
L, # create a lambda that:
R # pushes the range [1...n] to the stack
dz # ... zips that with itself
£X # ... reduces each item by item repetition
bF # ... and flattens that
Factor + grouping.extras, 35 * 0.5 = 17.5 bytes
[ [1,b] tail-clump reverse concat ]
! 5
[1,b] ! { 1 2 3 4 5 }
tail-clump ! { { 1 2 3 4 5 } { 2 3 4 5 } ... }
reverse ! { { 5 } { 4 5 } { 3 4 5 } ... }
concat ! { 5 4 5 3 4 5 2 3 4 5 1 2 3 4 5 }
Husk, 4 bytes * 0.5 = 2
Σ↔ḣṫ
Explanation
Σ↔ḣṫ
ṫ range from input to 1, [5,4,3,2,1]
ḣ prefixes, [[5],[5,4],[5,4,3]...]
↔ reverse, [[5,4,3,2,1],[5,4,3,2]...]
Σ concat, [5,4,3,2,1,5,4,3,2...]
Python, 53 bytes ÷ 2 = 26.5
lambda n:[n-i%-~n%(i//n-~n)for i in range(n*~n//2,0)]
Python 2 version, 51 bytes ÷ 2 = 25.5
lambda n:[n-i%-~n%(i/n-~n)for i in range(n*~n/2,0)]
This uses ranges like many other answers here but groups them in pairs (longest,shortest);(2nd longest,2nd shortest) etc. That way we can use a closed form expression inside a flat loop.
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.
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}
Ungolfed and readable:
func n(a int) (r []int){
for i := 0; i <= a; i++ {
for j := 0; j < i; j++ {
r = append(r, i)
}
}
return
}
Pretty straightforward solution and probably pretty bad.
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.
K (oK), 4 bytes
Solution:
&!1+
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...
C#, 108 bytes * 0.5 = 54
List<int> f(int n){var r=new List<int>();int m=n-1,i;r.Add(n);for(;m-->0;)for(i=m;i++<n;)r.Add(i);return r;}
Thanks to VisualMelon for doing the hard work! I thought I'd try to squeeze it down as much as possible.
(114 bytes * 0.5 = 57, if you insist on using .ToArray() to return int[])
Jelly, 3 × 0.5 = 1.5 bytes
rRF
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.
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)
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.
Haxe, 53 bytes
function l(v)return[for(i in 0...v)for(j in 0...i)i];
Works with l(6); because of array comprehension.
Test online http://try.haxe.org/#741f9
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
CoffeeScript, 57
f=(n)->i=0;a=[];(j=0;a.push i while(j++<i))while(i++<n);a
APL, 4 characters
How it works:
⎕ reads user input. As for output, APL by default prints the result from every line.
⍳n is the integers from 1 to n. Example: ⍳3←→ 1 2 3
/ means replicate. Each element from the right argument is repeated as many times as specified by its corresponding element from the left argument. Example: 2 0 3/'ABC'←→ 'AACCC'
⍨ is the commute operator. When it occurs to the right of a function, it modifies its behaviour, so it either swaps the arguments (A f⍨ B ←→ B f A, hence "commute") or provides the same argument on both sides (f⍨ A ←→ A f A, a "selfie"). The latter form is used in this solution.
Bonus:
6-∊⌽⍳¨⍳⎕ (8 characters, thanks @phil-h)
⍳5 (iota five) is 1 2 3 4 5.
⍳¨ ⍳5 (iota each iota five) is (,1)(1 2)(1 2 3)(1 2 3 4)(1 2 3 4 5), a vector of vectors. Each (¨) is an operator, it takes a function on the left and applies it to each item from the array on the right.
⌽ reverses the array, so we get (1 2 3 4 5)(1 2 3 4)(1 2 3)(1 2)(,1).
∊ is enlist (a.k.a. flatten). Recursively traverses the argument and returns the simple scalars from it as a vector.
C#, 114 99 * 0.5 = 49.5 bytes
(With a little help from VisualMelon's answer) Edit: and James Webster's comment
int[]A(int n){int m=n,i,c=0;var a=new int[n*(n+1)/2];while(m-->0)for(i=m;i++<n;)a[c++]=i;return a;}
Ungolfed:
int[] FooBar(int n)
{
int altCounter = n, i, arrayCounter = 0;
var returnArray = new int[n * (n + 1) / 2];
while(m-->0)
for(i = altCounter; i++ < n; )
returnArray[arrayCounter++]=i;
return returnArray;
}
There is an unsafe version that I shamelessly took from feersum's C answer, but I'm not 100% sure it fits within the rules since you have to allocate the memory before calling the method.
C# (unsafe), 82 * 0.5 = 41 bytes
unsafe void A(int n,int*p){int*z=p;int m=n,i;while(m-->0)for(i=m;i++<n;)z++[0]=i;}
Called as follows:
int n = 5, length = (int)((n / 2f) * (n + 1));
int* stuff = stackalloc int[length];
int[] stuffArray = new int[length];
A(n, stuff);
System.Runtime.InteropServices.Marshal.Copy(new IntPtr(stuffArray), stuffArray, 0, stuffArray.Length);
//stuffArray == { 5, 4, 5, 3, 4, 5, 2, 3, 4, 5, 1, 2, 3, 4, 5 }
Per VisualMelon's suggestion (thanks!), the unsafe code can be re-made with safe code which reduces the size even further! Still poses the question if the creation of the final result array is allowed to be done outside of the method.
C#, 72 * 0.5 = 36 bytes
void A(int n,int[]p){int z=0,m=n,i;while(m-->0)for(i=m;i++<n;)p[z++]=i;}
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);}}
Haskell, 31 characters = 15.5 score
f n=[y|x<-[n,n-1..1],y<-[x..n]]
27 characters without the bonus
f n=[x|x<-[1..n],_<-[1..x]]
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
TECO, 25 bytes * 0.5 = 12.5
a\+1%a%b<qauc-1%b<-1%c=>>
The above barely beats the non-bonus version at 13 bytes:
a\%a<%b<qb=>>
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]
JavaScript(readable), 131 bytes
I'm new to Code Golf so this isn't the best
function f(n) {
var arr = [];
for(var i = 1; i <= n; i++) {
for(var j = 0; j < i; j++) {
arr.push(i);
}
}
return arr;
}
JavaScript(less readable), 87 bytes
Minified using jscompress.com
function f(e){var t=[];for(var n=1;n<=e;n++){for(var r=0;r<n;r++){t.push(n)}}return t}
ECMAScript6, 67 * 0.5 = 33.5 bytes
f=n=>{a=[],b=0;while(c=n+b,n--){while(c-b)a.push(c--);b++}return a}
Pretty happy with this one...It's about a quarter the size of my original.
f(4) returns:
[ 4, 3, 2, 1, 4, 3, 2, 4, 3, 4 ]
Old answer:
f=i=>{a=b=Array;while(i)a=a.concat(b.apply(null,b(i)).map(e=>i)),i--;return a}
This is my first shot at code golf...I still want to get that 0.5x bonus. Any suggestions are welcomed!
Called with f(n).
JavaScript (ES6) 29 (58 * 0.5)
Edit remove ; thx @Optimizer
Q=o=>(m=>{for(n=o,r=[];n>m||++m<(n=o);)r.push(n--)})(0)||r
Test in FireFox/FireBug console
Q(9)
Output
[9, 8, 7, 6, 5, 4, 3, 2, 1, 9, 8, 7, 6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 9, 8, 7, 6, 5, 4, 9, 8, 7, 6, 5, 9, 8, 7, 6, 9, 8, 7, 9, 8, 9]
Ungolfed
Q=o=>{
for(m=0,r=[];m<o;++m)
for(n=o;n>m;)
r.push(n--);
return r
}
Haskell, 34 bytes * 0.5 = 17
0%n=[]
i%n=[i..n]++(i-1)%n
g n=n%n
That's the first time I've ever used Haskell for golfing. Call with g <number>.
C# - 81 (161bytes * 0.5)
Simple job in C#, hopefully gets the no-neibouring-numbers bonus. Reads an int from stdin, writes out an array like the example to stdout.
class P{static void Main(){int n=int.Parse(System.Console.ReadLine()),m=n-1,i;var R="["+n;for(;m-->0;)for(i=m;i++<n;)R+=", "+i;System.Console.WriteLine(R+"]");}}
More readable:
class P
{
static void Main()
{
int n=int.Parse(System.Console.ReadLine()),m=n-1,i;
var R="["+n;
for(;m-->0;)
for(i=m;i++<n;)
R+=", "+i;
System.Console.WriteLine(R+"]");
}
}
Examples output:
n = 5
[5, 4, 5, 3, 4, 5, 2, 3, 4, 5, 1, 2, 3, 4, 5]
Python 2, 53 bytes * 0.5 = 26.5
i=n=input()
x=[]
while i:x+=range(i,n+1);i-=1
print x
Shamelessly borrowed @VisualMelon's idea
Pyth, 9 bytes * 0.5 = 4.5
smrhQhdUQ
With help from @FryAmTheEggman
Explanation
s reduce + on list
m map
rhQhd lambda d: reversed(range(d+1, Q+1)), over
UQ range(Q)
where Q is the input.
Ruby (recursive), 41 bytes * 0.5 = 20.5
def n(n,i=1);i>n ?[]:n(n,i+1)+[*i..n];end
Or using a lambda (as recommended by histocrat and Ventero): 34 bytes * 0.5 = 17
r=->n,i=n{i>0?[*i..n]+r[n,i-1]:[]}
(call using r[argument])
perl ,26 bytes
for(1..$n){print"$_ "x$_;}
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]
Haskell, 14 = 28 bytes / 2
f n=n:[1..n-1]>>= \r->[r..n]
example output:
>f 5
[5,1,2,3,4,5,2,3,4,5,3,4,5,4,5]
24 bytes without the bonus:
f n=[1..n]>>= \r->[r..n]
J, 23 * 0.5 = 11.5
f=.-;@(<@|.@i."0@>:@i.)
f 5
5 4 5 3 4 5 2 3 4 5 1 2 3 4 5
J, 11
f=.#~@i.@>:
f 5
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
C#, 116 115 + 33 = 148 bytes
Not the shortest code, but... it works anyway :P
int[]l(int m){List<int>i=new List<int>();for(int j=1;j<=m;j++){for(int x=0;x<j;x++){i.Add(j);}}return i.ToArray();}
Requires this at the top of the file (33 bytes):
using System.Collections.Generic;
Un-golfed version:
int[] RepatedNumberList(int m)
{
List<int> intList = new List<int>();
for (int j = 1; j <= m; j++)
{
for (int x = 0; x < j; x++)
{
intList.Add(j);
}
}
return initList.ToArray();
}
T-SQL, 176 * 0.5 = 88
Since you seemed to miss the T-SQL @Optimizer, here it is in all it's verbose glory :).
A couple of function options, a Scalar and a Inline Table Valued function. The Scalar function uses while loops to recurse and returns a string of numbers, where the Inline Table Valued function uses a recursive CTE for a sequence and returns a table. Of course these will never be competitive, so I haven't spent a lot of time golfing.
Inline Table Valued Function, 176 * .5
CREATE FUNCTION F(@ INT)RETURNS TABLE RETURN WITH R AS(SELECT @ N UNION ALL SELECT N-1FROM R WHERE N>0)SELECT B.N FROM R CROSS APPLY(SELECT TOP(R.N)N FROM R A ORDER BY N DESC)B
Called as follows
SELECT * FROM dbo.F(5)
SQLFiddle example
Scalar Function, 220 * .5
CREATE FUNCTION G(@ INT)RETURNS VARCHAR(MAX)AS BEGIN DECLARE @S VARCHAR(MAX),@N INT=1,@I INT,@C INT WHILE @N<=@ BEGIN SELECT @I=@N,@C=@ WHILE @C>=@I BEGIN SELECT @S=CONCAT(@S+',',@C),@C-=1 END SET @N+=1 END RETURN @S END
Called as follows
SELECT dbo.G(5)
SQLFiddle example
GolfScript (14 bytes * 0.5 = score 7)
~:x,{~x),>~}%`
I think this is probably similar to some existing answers in that it builds up the array concat( [n], [n-1, n], [n-2, n-1, n], ..., [1, 2, ..., n] )
Sadly I wasn't able to golf any further the arguably more elegant:
~:x]{{,{x\-}/}%}2*`
which puts the input x into an array and then twice applies {,{x\-}/}%, which maps each element in an array to a count down of that many elements from x.
CJam, 12 15 bytes * 0.5 = 7.5
li_,f{),f-W%~}`
This is full STDIN-to-STDOUT program. It concatenates increasing suffixes of the 1 ... n range, which ensures that no two adjacent numbers are identical.
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
JavaScript, ES6, 66 bytes * 0.5 = 33
f=i=>(g=n=>[...Array(n).fill().map((v,x)=>i-x),...n?g(n-1):[]])(i)
Building on Optimizer's recursive approach, we can build descending runs of decreasing length, like [4,3,2,1, 4,3,2, 4,3, 4].
Instead of making same-value subarrays with Array(i).fill(i), we make undefined-filled subarrays of the appropriate length with Array(n).fill() and then change the values to a descending run using .map((v,x)=>i-x). Also, we define and recurse over an inner function g; the outer function f exists only to store the value of i while g recurses.
Bash + coreutils, 28/2 = 14
Shamelessly stealing @pgy's idea and golfing:
seq -f"seq %g $1" $1 -1 1|sh
Pure bash (no coreutils), 30/2 = 15
Eval, escape and expansion hell:
eval eval echo \\{{$1..1}..$1}
Bash with seq, expr and xargs = 59 / 2 = 29.5
Save it and run with the number as the first argument.
seq $1|xargs -n1 seq|xargs -n1 expr $1 + 1 -|sed 1d;echo $1
Pyth - 15 10 * .5 = 5
smr-QdhQUQ
Expects input on stdin. Independently discovered algorithm. Thanks @Sp3000 for helping me stick the last Q in there :P Also, irony? XD
Explanation:
Q=eval(input()) : implicit
s : The sum of...
m UQ : map(...,range(Q))
r-QdhQ : range(Q-d,Q+1)
R, 44 *.5 = 22
f=function(n){r=0;for(i in 1:n)r=c(r,n:i);r}
A quick test
> f(1)
[1] 1
> f(2)
[1] 2 1 2
> f(3)
[1] 3 2 1 3 2 3
> f(4)
[1] 4 3 2 1 4 3 2 4 3 4
vba, 76*0.5=38
Sub i(q)
For Z=1 To q:For x=q To Z Step -1:Debug.?x;",";:Next:Next
End Sub
JavaScript, ES6, 41 bytes
f=i=>[...Array(i).fill(i),...i?f(--i):[]]
This creates a function f which can be called like f(6) and it returns the required array.
This uses a recursive approach, where each iteration creates an array of i elements all valued i and concatenates an array returned by f(i-1) with stopping condition of i==0.
Works on latest Firefox.
C, 22 = 44 bytes * 0.5
The function h takes two parameters. The first is an int specifying n. The second is an int* which is the output buffer.
h(n,o)int*o;{for(n&&h(~-n,o+=n);*--o=n--;);}
Test program
main(){
int wow[999],*i;
memset(wow,0,sizeof(wow));
h(6, wow);
for(i=wow;*i;i++)printf("%d ", *i);
}