Question: Print out all the coordinates of a NxN grid. Preferably in C, however other languages also accepted
Input: N (integer)
Output: for N=3, a 3x3 grid:
0,0
1,0
2,0
0,1
1,1
2,1
0,2
1,2
2,2
Saved 32 bytes and fixed output order thanks to Kevin Cruijssen.
i;f(n){for(i=0;i<n*n;)printf("%d,%d\n",i++%n,i/n);}
I'm by no means a C programmer (or C golfer), but I thought I'd give it a try. Should the main be included in the byte count?
i++/n,i%n
should be i++%n,i/n
. :)
\$\endgroup\$
Commented
Dec 12, 2018 at 18:06
which(diag(scan())|1,T)-1
Thanks to Kirill L. for suggesting a 2-byte golf, which inspired me to look further :-)
match
and which
. which
in particular has an arr.ind
argument, so looking at the documentation for which
, we see a note in the Details under .dimnames
about passing to the arrayInd
function!
\$\endgroup\$
param($n)0..--$n|%{$i=$_;0..$n|%{"$i,$_"}}
Boring double-for loop.
Saved 6 bytes thanks to mazzy.
param($n)0..--$n|%{$i=$_;0..$n|%{"$i,$_"}}
\$\endgroup\$
-join
. Thanks!
\$\endgroup\$
Commented
Dec 12, 2018 at 17:20
L<ãí',ý»
Exactly as the challenge description: prints the 0-indexed coordinates ordered by y-then-x comma- and newline-delimited to STDOUT.
Explanation:
L # Create a list in the range [1, (implicit) input]
# i.e. 3 → [1,2,3]
< # Decrease each by 1 to make the range [0, input)
# i.e. [1,2,3] → [0,1,2]
ã # Create each possible pair with itself
# i.e. [0,1,2] → [[0,0],[0,1],[0,2],[1,0],[1,1],[1,2],[2,0],[2,1],[2,2]]
í # Reverse each pair so they're sorted by y-then-x instead of x-then-y
# i.e. [[0,0],[0,1],[0,2],[1,0],[1,1],[1,2],[2,0],[2,1],[2,2]]
# → [[0,0],[1,0],[2,0],[0,1],[1,1],[2,1],[0,2],[1,2],[2,2]]
',ý '# Join each pair with a space delimiter
# i.e. [[0,0],[1,0],[2,0],[0,1],[1,1],[2,1],[0,2],[1,2],[2,2]]
# → ["0,0","1,0","2,0","0,1","1,1","2,1","0,2","1,2","2,2"]
» # And then join everything with a newline delimiter (and output implicitly)
# i.e. ["0,0","1,0","2,0","0,1","1,1","2,1","0,2","1,2","2,2"]
# → "0,0\n1,0\n2,0\n0,1\n1,1\n2,1\n0,2\n1,2\n2,2"
Lã
Returns a list of 1-indexed coordinates ordered by x-then-y.
Explanation:
L # Create a list in the range [1, (implicit) input]
# i.e. 3 → [1,2,3]
ã # Create each possible pair with itself (and output implicitly)
# i.e. [1,2,3] → [[1,1],[1,2],[1,3],[2,1],[2,2],[2,3],[3,1],[3,2],[3,3]]
L
is a 1-indexed list in the range [1,n]
(where n
is the implicit input). I've made it 0-indexed in my 8-byte answer with the <
(decrease by 1).
\$\endgroup\$
Commented
Dec 12, 2018 at 18:04
First time posting. Apologies if I do something wrong!
l=>{for(int j=0;j<l*l;)System.Console.Write($"{j%l},{j++/l}\n");}
Try it online! -per Kevin Crujissen's TIO Link.
console.readline()
or however that's supposed to work (I'm not too familiar with C#) or submit a function instead.
\$\endgroup\$
Commented
Dec 12, 2018 at 17:57
l=>{for(var j=0;j<l*l;j++)System.Console.Write($"{j%l},{(int)j/l}\n");}
instead (71 bytes). However, you can golf 6 bytes by changing (int)j/l
to j++/l
and remove the j++
like this: l=>{for(int j=0;j<l*l;)System.Console.Write($"{j%l},{j++/l}\n");}
. Try it online.
\$\endgroup\$
Commented
Dec 12, 2018 at 18:00
N
...
\$\endgroup\$
Commented
Dec 12, 2018 at 15:04
^UQ2
Full program. Outputs list of coordinate pairs.
^UQ2 Implicit: Q=eval(input())
UQ [0-Q)
^ 2 Take the cartesian product of the previous result with itself
r■
r Range(0, n)
■ Cartesian product with self for lists
For pretty-printing, you could add n
to have it print one list item per line.
(⍳n)∘.,⍳n←⎕
Index origin = 0. Prompts for input for n and outputs the following for n=4:
0 0 0 1 0 2 0 3
1 0 1 1 1 2 1 3
2 0 2 1 2 2 2 3
3 0 3 1 3 2 3 3
I've never done 2D iterating in Pepe before and it doesn't seem to work pretty well due to labels being dynamic. There's quite a lot of two byte commands to avoid moving the pointer.
REREeErEErerErEReREErEEEErreEEreeeEeEEeerEEeerreEErEEEEEreeEReererEEEEErERRREEEEEeRrEree
Warning: Do not run it with input below 1 - it will kill your browser.
param($n)$i--..--$n*++$n|%{$i+=!$_;"$i,$_"}
Explanation:
One row 0..$n-1
repeated $n
times.
for$i(0..--$n){for(0..$n){say"$i,$_"}}
(The inner loop saves a few bytes by using the implicit variable "$_" as an index.)
N
. Is the output a list of tuples, a list of strings, should it be printed? Does it need to be separated by a comma? \$\endgroup\$[[0,0],[0,1],[0,2],[1,0],[1,1],[1,2],[2,0],[2,1],[2,2]]
instead of what you have above)? Can the output have 1-indexed coordinates instead of 0-indexed? \$\endgroup\$