Print coordinates of an NxN grid [duplicate]
This question already has an answer here:
Cartesian product of a list with itself n times
22 answers
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
code-golf grid
marked as duplicate by O.O.Balance, BMO, Xcali, Wît Wisarhd, Draco18s Dec 12 '18 at 17:51
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 5 more comments
This question already has an answer here:
Cartesian product of a list with itself n times
22 answers
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
code-golf grid
marked as duplicate by O.O.Balance, BMO, Xcali, Wît Wisarhd, Draco18s Dec 12 '18 at 17:51
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
3
Welcome to PPCG! This is the start of a good question. I couldn't find a duplicate from a quick search, though I might be mistaken. One thing I'd suggest is to specify the input and output. I assume that the input is an integerN
. Is the output a list of tuples, a list of strings, should it be printed? Does it need to be separated by a comma?
– maxb
Dec 12 '18 at 14:49
4
Are we allowed to return a list instead of printing them? Is any order acceptable (i.e.[[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?
– Kevin Cruijssen
Dec 12 '18 at 14:54
1
Can we start at (1,1) instead of (0,0) ?
– sergiol
Dec 12 '18 at 15:01
4
Is there a particular order they have to be in or is any order fine?
– Wît Wisarhd
Dec 12 '18 at 15:18
2
Can't decide whether this is a dupe of this (just hardcode $n=2$) or this (just duplicate the argument instead of taking two distinct), but probably both.
– BMO
Dec 12 '18 at 17:04
|
show 5 more comments
This question already has an answer here:
Cartesian product of a list with itself n times
22 answers
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
code-golf grid
This question already has an answer here:
Cartesian product of a list with itself n times
22 answers
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
This question already has an answer here:
Cartesian product of a list with itself n times
22 answers
code-golf grid
code-golf grid
edited Dec 12 '18 at 15:12
asked Dec 12 '18 at 14:38
Chris Puglia
244
244
marked as duplicate by O.O.Balance, BMO, Xcali, Wît Wisarhd, Draco18s Dec 12 '18 at 17:51
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by O.O.Balance, BMO, Xcali, Wît Wisarhd, Draco18s Dec 12 '18 at 17:51
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
3
Welcome to PPCG! This is the start of a good question. I couldn't find a duplicate from a quick search, though I might be mistaken. One thing I'd suggest is to specify the input and output. I assume that the input is an integerN
. Is the output a list of tuples, a list of strings, should it be printed? Does it need to be separated by a comma?
– maxb
Dec 12 '18 at 14:49
4
Are we allowed to return a list instead of printing them? Is any order acceptable (i.e.[[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?
– Kevin Cruijssen
Dec 12 '18 at 14:54
1
Can we start at (1,1) instead of (0,0) ?
– sergiol
Dec 12 '18 at 15:01
4
Is there a particular order they have to be in or is any order fine?
– Wît Wisarhd
Dec 12 '18 at 15:18
2
Can't decide whether this is a dupe of this (just hardcode $n=2$) or this (just duplicate the argument instead of taking two distinct), but probably both.
– BMO
Dec 12 '18 at 17:04
|
show 5 more comments
3
Welcome to PPCG! This is the start of a good question. I couldn't find a duplicate from a quick search, though I might be mistaken. One thing I'd suggest is to specify the input and output. I assume that the input is an integerN
. Is the output a list of tuples, a list of strings, should it be printed? Does it need to be separated by a comma?
– maxb
Dec 12 '18 at 14:49
4
Are we allowed to return a list instead of printing them? Is any order acceptable (i.e.[[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?
– Kevin Cruijssen
Dec 12 '18 at 14:54
1
Can we start at (1,1) instead of (0,0) ?
– sergiol
Dec 12 '18 at 15:01
4
Is there a particular order they have to be in or is any order fine?
– Wît Wisarhd
Dec 12 '18 at 15:18
2
Can't decide whether this is a dupe of this (just hardcode $n=2$) or this (just duplicate the argument instead of taking two distinct), but probably both.
– BMO
Dec 12 '18 at 17:04
3
3
Welcome to PPCG! This is the start of a good question. I couldn't find a duplicate from a quick search, though I might be mistaken. One thing I'd suggest is to specify the input and output. I assume that the input is an integer
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?– maxb
Dec 12 '18 at 14:49
Welcome to PPCG! This is the start of a good question. I couldn't find a duplicate from a quick search, though I might be mistaken. One thing I'd suggest is to specify the input and output. I assume that the input is an integer
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?– maxb
Dec 12 '18 at 14:49
4
4
Are we allowed to return a list instead of printing them? Is any order acceptable (i.e.
[[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?– Kevin Cruijssen
Dec 12 '18 at 14:54
Are we allowed to return a list instead of printing them? Is any order acceptable (i.e.
[[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?– Kevin Cruijssen
Dec 12 '18 at 14:54
1
1
Can we start at (1,1) instead of (0,0) ?
– sergiol
Dec 12 '18 at 15:01
Can we start at (1,1) instead of (0,0) ?
– sergiol
Dec 12 '18 at 15:01
4
4
Is there a particular order they have to be in or is any order fine?
– Wît Wisarhd
Dec 12 '18 at 15:18
Is there a particular order they have to be in or is any order fine?
– Wît Wisarhd
Dec 12 '18 at 15:18
2
2
Can't decide whether this is a dupe of this (just hardcode $n=2$) or this (just duplicate the argument instead of taking two distinct), but probably both.
– BMO
Dec 12 '18 at 17:04
Can't decide whether this is a dupe of this (just hardcode $n=2$) or this (just duplicate the argument instead of taking two distinct), but probably both.
– BMO
Dec 12 '18 at 17:04
|
show 5 more comments
17 Answers
17
active
oldest
votes
C (gcc), 83 51 bytes
Saved 32 bytes and fixed output order thanks to Kevin Cruijssen.
i;f(n){for(i=0;i<n*n;)printf("%d,%dn",i++%n,i/n);}
Try it online!
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?
51 bytes :) (And no, main method doesn't have to be included. For Java, C, C#, etc. etc. we allows functions instead of full programs, since full programs are quite verbose and doesn't add anything to the actual solution.)
– Kevin Cruijssen
Dec 12 '18 at 15:00
Btw, to have the exact same order for each coordinate as in the input (ordered by y-then-x instead of x-then-y), thei++/n,i%n
should bei++%n,i/n
. :)
– Kevin Cruijssen
Dec 12 '18 at 18:06
@KevinCruijssen Good point, updated the post!
– maxb
Dec 13 '18 at 9:00
add a comment |
Japt, 3 bytes
o ï
Try it
o :Range [0,input)
ï :Cartesian product with itself
1
I had the same solution with ` ñÌ` at the end to sort it. Hopefully the OP doesn't care about the order.
– Oliver
Dec 12 '18 at 15:38
@Oliver,mw
would also work.
– Shaggy
Dec 13 '18 at 22:45
add a comment |
R, 33 25 bytes
which(diag(scan())|1,T)-1
Try it online!
Thanks to Kirill L. for suggesting a 2-byte golf, which inspired me to look further :-)
Where do you hear about all of these functions in R? I've never even used arrayInd before, but I feel like it could be useful in a lot of things!
– Sumner18
Dec 12 '18 at 15:34
@Sumner18 You can't be as golfy if you don't understand a big chunk of the language, so it pays to read the docs! there are a few workhorse functions likematch
andwhich
.which
in particular has anarr.ind
argument, so looking at the documentation forwhich
, we see a note in the Details under.dimnames
about passing to thearrayInd
function!
– Giuseppe
Dec 12 '18 at 15:38
31 bytes. Sadly, requires!!
, as otherwise:argument to 'which' is not logical
...
– Kirill L.
Dec 12 '18 at 15:56
@KirillL. that's neat! It also inspired me to shave off another 2 bytes. EDIT: another 6 bytes!
– Giuseppe
Dec 12 '18 at 15:58
add a comment |
PowerShell, 48 42 bytes
param($n)0..--$n|%{$i=$_;0..$n|%{"$i,$_"}}
Try it online!
Boring double-for loop.
Saved 6 bytes thanks to mazzy.
?param($n)0..--$n|%{$i=$_;0..$n|%{"$i,$_"}}
– mazzy
Dec 12 '18 at 17:12
@mazzy Of course, removing the-join
. Thanks!
– AdmBorkBork
Dec 12 '18 at 17:20
add a comment |
05AB1E, 8 bytes
L<ãí',ý»
Exactly as the challenge description: prints the 0-indexed coordinates ordered by y-then-x comma- and newline-delimited to STDOUT.
Try it online.
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,0n1,0n2,0n0,1n1,1n2,1n0,2n1,2n2,2"
05AB1E, 2 bytes
Lã
Returns a list of 1-indexed coordinates ordered by x-then-y.
Try it online.
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]]
How comes that your 2-byter is 1-indexed?
– maxb
Dec 12 '18 at 15:00
@maxb I've added an explanation to my post now.L
is a 1-indexed list in the range[1,n]
(wheren
is the implicit input). I've made it 0-indexed in my 8-byte answer with the<
(decrease by 1).
– Kevin Cruijssen
Dec 12 '18 at 18:04
add a comment |
C#, 59, 65
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.
1
Welcome to the site! Unfortunately, assuming input is in a predefined variable is not a standard form of input. You could add aconsole.readline()
or however that's supposed to work (I'm not too familiar with C#) or submit a function instead.
– DJMcMayhem♦
Dec 12 '18 at 17:57
1
Hi, welcome to PPCG! We allow either functions or full programs as answers (yours is currently more like a snippet), and all imports add to the byte-count. Which means your current answer should bel=>{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
toj++/l
and remove thej++
like this:l=>{for(int j=0;j<l*l;)System.Console.Write($"{j%l},{j++/l}n");}
. Try it online.
– Kevin Cruijssen
Dec 12 '18 at 18:00
1
Also, if you haven't seen it yet, tips for golfing in C# and tips for golfing in <all languages> might be interesting to read through. Apart from some ruling, your first answer looks fine, so +1 from me. Again welcome, and enjoy your stay! :)
– Kevin Cruijssen
Dec 12 '18 at 18:01
Thanks guys! Sorry about the function error. Appreciate the for increment golf!
– Destroigo
Dec 12 '18 at 18:18
Can you provide a demo?
– sergiol
Dec 12 '18 at 19:32
|
show 1 more comment
Tcl, 70 bytes
proc C {n i 0} {time {set j 0
time {puts $i,$j
incr j} $n
incr i} $n}
Try it online!
Failed outgolf: tio.run/##K0nO@f@/oCg/…
– sergiol
Dec 12 '18 at 15:48
add a comment |
Python 2, 39 bytes
lambda n:[(i%n,i/n)for i in range(n*n)]
Try it online!
I think the question requires variableN
...
– Felix Palmen
Dec 12 '18 at 15:04
@FelixPalmen Doh.. Fixed :)
– TFeld
Dec 12 '18 at 15:05
add a comment |
Pyth, 4 bytes
^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
add a comment |
MathGolf, 2 bytes
r■
Try it online!
Explanation
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.
add a comment |
APL+WIN, 11 bytes
(⍳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
add a comment |
Pepe, 88 bytes
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
Try it online!
Warning: Do not run it with input below 1 - it will kill your browser.
add a comment |
Perl 5 -na
, 35 bytes
map{//;say"$',$_"for 0..$F[0]}0..$_
Try it online!
add a comment |
Powershell, 43 bytes
param($n)$i--..--$n*++$n|%{$i+=!$_;"$i,$_"}
Explanation:
One row 0..$n-1
repeated $n
times.
add a comment |
Pure Bash (no external utilities), 35
eval echo {0..$[$1-1]},{0..$[$1-1]}
Try it online!
add a comment |
Lua, 63 bytes
s=io.read()-1 for i=0,s do for j=0,s do print(i..','..j)end end
Try it online!
add a comment |
Perl5, 42 38 bytes
for$i(0..--$n){for(0..$n){say"$i,$_"}}
(The inner loop saves a few bytes by using the implicit variable "$_" as an index.)
add a comment |
17 Answers
17
active
oldest
votes
17 Answers
17
active
oldest
votes
active
oldest
votes
active
oldest
votes
C (gcc), 83 51 bytes
Saved 32 bytes and fixed output order thanks to Kevin Cruijssen.
i;f(n){for(i=0;i<n*n;)printf("%d,%dn",i++%n,i/n);}
Try it online!
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?
51 bytes :) (And no, main method doesn't have to be included. For Java, C, C#, etc. etc. we allows functions instead of full programs, since full programs are quite verbose and doesn't add anything to the actual solution.)
– Kevin Cruijssen
Dec 12 '18 at 15:00
Btw, to have the exact same order for each coordinate as in the input (ordered by y-then-x instead of x-then-y), thei++/n,i%n
should bei++%n,i/n
. :)
– Kevin Cruijssen
Dec 12 '18 at 18:06
@KevinCruijssen Good point, updated the post!
– maxb
Dec 13 '18 at 9:00
add a comment |
C (gcc), 83 51 bytes
Saved 32 bytes and fixed output order thanks to Kevin Cruijssen.
i;f(n){for(i=0;i<n*n;)printf("%d,%dn",i++%n,i/n);}
Try it online!
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?
51 bytes :) (And no, main method doesn't have to be included. For Java, C, C#, etc. etc. we allows functions instead of full programs, since full programs are quite verbose and doesn't add anything to the actual solution.)
– Kevin Cruijssen
Dec 12 '18 at 15:00
Btw, to have the exact same order for each coordinate as in the input (ordered by y-then-x instead of x-then-y), thei++/n,i%n
should bei++%n,i/n
. :)
– Kevin Cruijssen
Dec 12 '18 at 18:06
@KevinCruijssen Good point, updated the post!
– maxb
Dec 13 '18 at 9:00
add a comment |
C (gcc), 83 51 bytes
Saved 32 bytes and fixed output order thanks to Kevin Cruijssen.
i;f(n){for(i=0;i<n*n;)printf("%d,%dn",i++%n,i/n);}
Try it online!
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?
C (gcc), 83 51 bytes
Saved 32 bytes and fixed output order thanks to Kevin Cruijssen.
i;f(n){for(i=0;i<n*n;)printf("%d,%dn",i++%n,i/n);}
Try it online!
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?
edited Dec 13 '18 at 9:00
answered Dec 12 '18 at 14:58
maxb
2,92611131
2,92611131
51 bytes :) (And no, main method doesn't have to be included. For Java, C, C#, etc. etc. we allows functions instead of full programs, since full programs are quite verbose and doesn't add anything to the actual solution.)
– Kevin Cruijssen
Dec 12 '18 at 15:00
Btw, to have the exact same order for each coordinate as in the input (ordered by y-then-x instead of x-then-y), thei++/n,i%n
should bei++%n,i/n
. :)
– Kevin Cruijssen
Dec 12 '18 at 18:06
@KevinCruijssen Good point, updated the post!
– maxb
Dec 13 '18 at 9:00
add a comment |
51 bytes :) (And no, main method doesn't have to be included. For Java, C, C#, etc. etc. we allows functions instead of full programs, since full programs are quite verbose and doesn't add anything to the actual solution.)
– Kevin Cruijssen
Dec 12 '18 at 15:00
Btw, to have the exact same order for each coordinate as in the input (ordered by y-then-x instead of x-then-y), thei++/n,i%n
should bei++%n,i/n
. :)
– Kevin Cruijssen
Dec 12 '18 at 18:06
@KevinCruijssen Good point, updated the post!
– maxb
Dec 13 '18 at 9:00
51 bytes :) (And no, main method doesn't have to be included. For Java, C, C#, etc. etc. we allows functions instead of full programs, since full programs are quite verbose and doesn't add anything to the actual solution.)
– Kevin Cruijssen
Dec 12 '18 at 15:00
51 bytes :) (And no, main method doesn't have to be included. For Java, C, C#, etc. etc. we allows functions instead of full programs, since full programs are quite verbose and doesn't add anything to the actual solution.)
– Kevin Cruijssen
Dec 12 '18 at 15:00
Btw, to have the exact same order for each coordinate as in the input (ordered by y-then-x instead of x-then-y), the
i++/n,i%n
should be i++%n,i/n
. :)– Kevin Cruijssen
Dec 12 '18 at 18:06
Btw, to have the exact same order for each coordinate as in the input (ordered by y-then-x instead of x-then-y), the
i++/n,i%n
should be i++%n,i/n
. :)– Kevin Cruijssen
Dec 12 '18 at 18:06
@KevinCruijssen Good point, updated the post!
– maxb
Dec 13 '18 at 9:00
@KevinCruijssen Good point, updated the post!
– maxb
Dec 13 '18 at 9:00
add a comment |
Japt, 3 bytes
o ï
Try it
o :Range [0,input)
ï :Cartesian product with itself
1
I had the same solution with ` ñÌ` at the end to sort it. Hopefully the OP doesn't care about the order.
– Oliver
Dec 12 '18 at 15:38
@Oliver,mw
would also work.
– Shaggy
Dec 13 '18 at 22:45
add a comment |
Japt, 3 bytes
o ï
Try it
o :Range [0,input)
ï :Cartesian product with itself
1
I had the same solution with ` ñÌ` at the end to sort it. Hopefully the OP doesn't care about the order.
– Oliver
Dec 12 '18 at 15:38
@Oliver,mw
would also work.
– Shaggy
Dec 13 '18 at 22:45
add a comment |
Japt, 3 bytes
o ï
Try it
o :Range [0,input)
ï :Cartesian product with itself
Japt, 3 bytes
o ï
Try it
o :Range [0,input)
ï :Cartesian product with itself
edited Dec 13 '18 at 10:27
answered Dec 12 '18 at 15:07
Shaggy
18.9k21666
18.9k21666
1
I had the same solution with ` ñÌ` at the end to sort it. Hopefully the OP doesn't care about the order.
– Oliver
Dec 12 '18 at 15:38
@Oliver,mw
would also work.
– Shaggy
Dec 13 '18 at 22:45
add a comment |
1
I had the same solution with ` ñÌ` at the end to sort it. Hopefully the OP doesn't care about the order.
– Oliver
Dec 12 '18 at 15:38
@Oliver,mw
would also work.
– Shaggy
Dec 13 '18 at 22:45
1
1
I had the same solution with ` ñÌ` at the end to sort it. Hopefully the OP doesn't care about the order.
– Oliver
Dec 12 '18 at 15:38
I had the same solution with ` ñÌ` at the end to sort it. Hopefully the OP doesn't care about the order.
– Oliver
Dec 12 '18 at 15:38
@Oliver,
mw
would also work.– Shaggy
Dec 13 '18 at 22:45
@Oliver,
mw
would also work.– Shaggy
Dec 13 '18 at 22:45
add a comment |
R, 33 25 bytes
which(diag(scan())|1,T)-1
Try it online!
Thanks to Kirill L. for suggesting a 2-byte golf, which inspired me to look further :-)
Where do you hear about all of these functions in R? I've never even used arrayInd before, but I feel like it could be useful in a lot of things!
– Sumner18
Dec 12 '18 at 15:34
@Sumner18 You can't be as golfy if you don't understand a big chunk of the language, so it pays to read the docs! there are a few workhorse functions likematch
andwhich
.which
in particular has anarr.ind
argument, so looking at the documentation forwhich
, we see a note in the Details under.dimnames
about passing to thearrayInd
function!
– Giuseppe
Dec 12 '18 at 15:38
31 bytes. Sadly, requires!!
, as otherwise:argument to 'which' is not logical
...
– Kirill L.
Dec 12 '18 at 15:56
@KirillL. that's neat! It also inspired me to shave off another 2 bytes. EDIT: another 6 bytes!
– Giuseppe
Dec 12 '18 at 15:58
add a comment |
R, 33 25 bytes
which(diag(scan())|1,T)-1
Try it online!
Thanks to Kirill L. for suggesting a 2-byte golf, which inspired me to look further :-)
Where do you hear about all of these functions in R? I've never even used arrayInd before, but I feel like it could be useful in a lot of things!
– Sumner18
Dec 12 '18 at 15:34
@Sumner18 You can't be as golfy if you don't understand a big chunk of the language, so it pays to read the docs! there are a few workhorse functions likematch
andwhich
.which
in particular has anarr.ind
argument, so looking at the documentation forwhich
, we see a note in the Details under.dimnames
about passing to thearrayInd
function!
– Giuseppe
Dec 12 '18 at 15:38
31 bytes. Sadly, requires!!
, as otherwise:argument to 'which' is not logical
...
– Kirill L.
Dec 12 '18 at 15:56
@KirillL. that's neat! It also inspired me to shave off another 2 bytes. EDIT: another 6 bytes!
– Giuseppe
Dec 12 '18 at 15:58
add a comment |
R, 33 25 bytes
which(diag(scan())|1,T)-1
Try it online!
Thanks to Kirill L. for suggesting a 2-byte golf, which inspired me to look further :-)
R, 33 25 bytes
which(diag(scan())|1,T)-1
Try it online!
Thanks to Kirill L. for suggesting a 2-byte golf, which inspired me to look further :-)
edited Dec 12 '18 at 16:00
answered Dec 12 '18 at 15:10
Giuseppe
16.6k31052
16.6k31052
Where do you hear about all of these functions in R? I've never even used arrayInd before, but I feel like it could be useful in a lot of things!
– Sumner18
Dec 12 '18 at 15:34
@Sumner18 You can't be as golfy if you don't understand a big chunk of the language, so it pays to read the docs! there are a few workhorse functions likematch
andwhich
.which
in particular has anarr.ind
argument, so looking at the documentation forwhich
, we see a note in the Details under.dimnames
about passing to thearrayInd
function!
– Giuseppe
Dec 12 '18 at 15:38
31 bytes. Sadly, requires!!
, as otherwise:argument to 'which' is not logical
...
– Kirill L.
Dec 12 '18 at 15:56
@KirillL. that's neat! It also inspired me to shave off another 2 bytes. EDIT: another 6 bytes!
– Giuseppe
Dec 12 '18 at 15:58
add a comment |
Where do you hear about all of these functions in R? I've never even used arrayInd before, but I feel like it could be useful in a lot of things!
– Sumner18
Dec 12 '18 at 15:34
@Sumner18 You can't be as golfy if you don't understand a big chunk of the language, so it pays to read the docs! there are a few workhorse functions likematch
andwhich
.which
in particular has anarr.ind
argument, so looking at the documentation forwhich
, we see a note in the Details under.dimnames
about passing to thearrayInd
function!
– Giuseppe
Dec 12 '18 at 15:38
31 bytes. Sadly, requires!!
, as otherwise:argument to 'which' is not logical
...
– Kirill L.
Dec 12 '18 at 15:56
@KirillL. that's neat! It also inspired me to shave off another 2 bytes. EDIT: another 6 bytes!
– Giuseppe
Dec 12 '18 at 15:58
Where do you hear about all of these functions in R? I've never even used arrayInd before, but I feel like it could be useful in a lot of things!
– Sumner18
Dec 12 '18 at 15:34
Where do you hear about all of these functions in R? I've never even used arrayInd before, but I feel like it could be useful in a lot of things!
– Sumner18
Dec 12 '18 at 15:34
@Sumner18 You can't be as golfy if you don't understand a big chunk of the language, so it pays to read the docs! there are a few workhorse functions like
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!– Giuseppe
Dec 12 '18 at 15:38
@Sumner18 You can't be as golfy if you don't understand a big chunk of the language, so it pays to read the docs! there are a few workhorse functions like
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!– Giuseppe
Dec 12 '18 at 15:38
31 bytes. Sadly, requires
!!
, as otherwise: argument to 'which' is not logical
...– Kirill L.
Dec 12 '18 at 15:56
31 bytes. Sadly, requires
!!
, as otherwise: argument to 'which' is not logical
...– Kirill L.
Dec 12 '18 at 15:56
@KirillL. that's neat! It also inspired me to shave off another 2 bytes. EDIT: another 6 bytes!
– Giuseppe
Dec 12 '18 at 15:58
@KirillL. that's neat! It also inspired me to shave off another 2 bytes. EDIT: another 6 bytes!
– Giuseppe
Dec 12 '18 at 15:58
add a comment |
PowerShell, 48 42 bytes
param($n)0..--$n|%{$i=$_;0..$n|%{"$i,$_"}}
Try it online!
Boring double-for loop.
Saved 6 bytes thanks to mazzy.
?param($n)0..--$n|%{$i=$_;0..$n|%{"$i,$_"}}
– mazzy
Dec 12 '18 at 17:12
@mazzy Of course, removing the-join
. Thanks!
– AdmBorkBork
Dec 12 '18 at 17:20
add a comment |
PowerShell, 48 42 bytes
param($n)0..--$n|%{$i=$_;0..$n|%{"$i,$_"}}
Try it online!
Boring double-for loop.
Saved 6 bytes thanks to mazzy.
?param($n)0..--$n|%{$i=$_;0..$n|%{"$i,$_"}}
– mazzy
Dec 12 '18 at 17:12
@mazzy Of course, removing the-join
. Thanks!
– AdmBorkBork
Dec 12 '18 at 17:20
add a comment |
PowerShell, 48 42 bytes
param($n)0..--$n|%{$i=$_;0..$n|%{"$i,$_"}}
Try it online!
Boring double-for loop.
Saved 6 bytes thanks to mazzy.
PowerShell, 48 42 bytes
param($n)0..--$n|%{$i=$_;0..$n|%{"$i,$_"}}
Try it online!
Boring double-for loop.
Saved 6 bytes thanks to mazzy.
edited Dec 12 '18 at 17:20
answered Dec 12 '18 at 16:15
AdmBorkBork
26.3k364228
26.3k364228
?param($n)0..--$n|%{$i=$_;0..$n|%{"$i,$_"}}
– mazzy
Dec 12 '18 at 17:12
@mazzy Of course, removing the-join
. Thanks!
– AdmBorkBork
Dec 12 '18 at 17:20
add a comment |
?param($n)0..--$n|%{$i=$_;0..$n|%{"$i,$_"}}
– mazzy
Dec 12 '18 at 17:12
@mazzy Of course, removing the-join
. Thanks!
– AdmBorkBork
Dec 12 '18 at 17:20
?
param($n)0..--$n|%{$i=$_;0..$n|%{"$i,$_"}}
– mazzy
Dec 12 '18 at 17:12
?
param($n)0..--$n|%{$i=$_;0..$n|%{"$i,$_"}}
– mazzy
Dec 12 '18 at 17:12
@mazzy Of course, removing the
-join
. Thanks!– AdmBorkBork
Dec 12 '18 at 17:20
@mazzy Of course, removing the
-join
. Thanks!– AdmBorkBork
Dec 12 '18 at 17:20
add a comment |
05AB1E, 8 bytes
L<ãí',ý»
Exactly as the challenge description: prints the 0-indexed coordinates ordered by y-then-x comma- and newline-delimited to STDOUT.
Try it online.
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,0n1,0n2,0n0,1n1,1n2,1n0,2n1,2n2,2"
05AB1E, 2 bytes
Lã
Returns a list of 1-indexed coordinates ordered by x-then-y.
Try it online.
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]]
How comes that your 2-byter is 1-indexed?
– maxb
Dec 12 '18 at 15:00
@maxb I've added an explanation to my post now.L
is a 1-indexed list in the range[1,n]
(wheren
is the implicit input). I've made it 0-indexed in my 8-byte answer with the<
(decrease by 1).
– Kevin Cruijssen
Dec 12 '18 at 18:04
add a comment |
05AB1E, 8 bytes
L<ãí',ý»
Exactly as the challenge description: prints the 0-indexed coordinates ordered by y-then-x comma- and newline-delimited to STDOUT.
Try it online.
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,0n1,0n2,0n0,1n1,1n2,1n0,2n1,2n2,2"
05AB1E, 2 bytes
Lã
Returns a list of 1-indexed coordinates ordered by x-then-y.
Try it online.
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]]
How comes that your 2-byter is 1-indexed?
– maxb
Dec 12 '18 at 15:00
@maxb I've added an explanation to my post now.L
is a 1-indexed list in the range[1,n]
(wheren
is the implicit input). I've made it 0-indexed in my 8-byte answer with the<
(decrease by 1).
– Kevin Cruijssen
Dec 12 '18 at 18:04
add a comment |
05AB1E, 8 bytes
L<ãí',ý»
Exactly as the challenge description: prints the 0-indexed coordinates ordered by y-then-x comma- and newline-delimited to STDOUT.
Try it online.
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,0n1,0n2,0n0,1n1,1n2,1n0,2n1,2n2,2"
05AB1E, 2 bytes
Lã
Returns a list of 1-indexed coordinates ordered by x-then-y.
Try it online.
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]]
05AB1E, 8 bytes
L<ãí',ý»
Exactly as the challenge description: prints the 0-indexed coordinates ordered by y-then-x comma- and newline-delimited to STDOUT.
Try it online.
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,0n1,0n2,0n0,1n1,1n2,1n0,2n1,2n2,2"
05AB1E, 2 bytes
Lã
Returns a list of 1-indexed coordinates ordered by x-then-y.
Try it online.
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]]
edited Dec 12 '18 at 17:45
answered Dec 12 '18 at 14:57
Kevin Cruijssen
35.6k554186
35.6k554186
How comes that your 2-byter is 1-indexed?
– maxb
Dec 12 '18 at 15:00
@maxb I've added an explanation to my post now.L
is a 1-indexed list in the range[1,n]
(wheren
is the implicit input). I've made it 0-indexed in my 8-byte answer with the<
(decrease by 1).
– Kevin Cruijssen
Dec 12 '18 at 18:04
add a comment |
How comes that your 2-byter is 1-indexed?
– maxb
Dec 12 '18 at 15:00
@maxb I've added an explanation to my post now.L
is a 1-indexed list in the range[1,n]
(wheren
is the implicit input). I've made it 0-indexed in my 8-byte answer with the<
(decrease by 1).
– Kevin Cruijssen
Dec 12 '18 at 18:04
How comes that your 2-byter is 1-indexed?
– maxb
Dec 12 '18 at 15:00
How comes that your 2-byter is 1-indexed?
– maxb
Dec 12 '18 at 15:00
@maxb I've added an explanation to my post now.
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).– Kevin Cruijssen
Dec 12 '18 at 18:04
@maxb I've added an explanation to my post now.
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).– Kevin Cruijssen
Dec 12 '18 at 18:04
add a comment |
C#, 59, 65
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.
1
Welcome to the site! Unfortunately, assuming input is in a predefined variable is not a standard form of input. You could add aconsole.readline()
or however that's supposed to work (I'm not too familiar with C#) or submit a function instead.
– DJMcMayhem♦
Dec 12 '18 at 17:57
1
Hi, welcome to PPCG! We allow either functions or full programs as answers (yours is currently more like a snippet), and all imports add to the byte-count. Which means your current answer should bel=>{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
toj++/l
and remove thej++
like this:l=>{for(int j=0;j<l*l;)System.Console.Write($"{j%l},{j++/l}n");}
. Try it online.
– Kevin Cruijssen
Dec 12 '18 at 18:00
1
Also, if you haven't seen it yet, tips for golfing in C# and tips for golfing in <all languages> might be interesting to read through. Apart from some ruling, your first answer looks fine, so +1 from me. Again welcome, and enjoy your stay! :)
– Kevin Cruijssen
Dec 12 '18 at 18:01
Thanks guys! Sorry about the function error. Appreciate the for increment golf!
– Destroigo
Dec 12 '18 at 18:18
Can you provide a demo?
– sergiol
Dec 12 '18 at 19:32
|
show 1 more comment
C#, 59, 65
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.
1
Welcome to the site! Unfortunately, assuming input is in a predefined variable is not a standard form of input. You could add aconsole.readline()
or however that's supposed to work (I'm not too familiar with C#) or submit a function instead.
– DJMcMayhem♦
Dec 12 '18 at 17:57
1
Hi, welcome to PPCG! We allow either functions or full programs as answers (yours is currently more like a snippet), and all imports add to the byte-count. Which means your current answer should bel=>{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
toj++/l
and remove thej++
like this:l=>{for(int j=0;j<l*l;)System.Console.Write($"{j%l},{j++/l}n");}
. Try it online.
– Kevin Cruijssen
Dec 12 '18 at 18:00
1
Also, if you haven't seen it yet, tips for golfing in C# and tips for golfing in <all languages> might be interesting to read through. Apart from some ruling, your first answer looks fine, so +1 from me. Again welcome, and enjoy your stay! :)
– Kevin Cruijssen
Dec 12 '18 at 18:01
Thanks guys! Sorry about the function error. Appreciate the for increment golf!
– Destroigo
Dec 12 '18 at 18:18
Can you provide a demo?
– sergiol
Dec 12 '18 at 19:32
|
show 1 more comment
C#, 59, 65
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.
C#, 59, 65
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.
edited Dec 13 '18 at 15:58
answered Dec 12 '18 at 17:42
Destroigo
312
312
1
Welcome to the site! Unfortunately, assuming input is in a predefined variable is not a standard form of input. You could add aconsole.readline()
or however that's supposed to work (I'm not too familiar with C#) or submit a function instead.
– DJMcMayhem♦
Dec 12 '18 at 17:57
1
Hi, welcome to PPCG! We allow either functions or full programs as answers (yours is currently more like a snippet), and all imports add to the byte-count. Which means your current answer should bel=>{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
toj++/l
and remove thej++
like this:l=>{for(int j=0;j<l*l;)System.Console.Write($"{j%l},{j++/l}n");}
. Try it online.
– Kevin Cruijssen
Dec 12 '18 at 18:00
1
Also, if you haven't seen it yet, tips for golfing in C# and tips for golfing in <all languages> might be interesting to read through. Apart from some ruling, your first answer looks fine, so +1 from me. Again welcome, and enjoy your stay! :)
– Kevin Cruijssen
Dec 12 '18 at 18:01
Thanks guys! Sorry about the function error. Appreciate the for increment golf!
– Destroigo
Dec 12 '18 at 18:18
Can you provide a demo?
– sergiol
Dec 12 '18 at 19:32
|
show 1 more comment
1
Welcome to the site! Unfortunately, assuming input is in a predefined variable is not a standard form of input. You could add aconsole.readline()
or however that's supposed to work (I'm not too familiar with C#) or submit a function instead.
– DJMcMayhem♦
Dec 12 '18 at 17:57
1
Hi, welcome to PPCG! We allow either functions or full programs as answers (yours is currently more like a snippet), and all imports add to the byte-count. Which means your current answer should bel=>{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
toj++/l
and remove thej++
like this:l=>{for(int j=0;j<l*l;)System.Console.Write($"{j%l},{j++/l}n");}
. Try it online.
– Kevin Cruijssen
Dec 12 '18 at 18:00
1
Also, if you haven't seen it yet, tips for golfing in C# and tips for golfing in <all languages> might be interesting to read through. Apart from some ruling, your first answer looks fine, so +1 from me. Again welcome, and enjoy your stay! :)
– Kevin Cruijssen
Dec 12 '18 at 18:01
Thanks guys! Sorry about the function error. Appreciate the for increment golf!
– Destroigo
Dec 12 '18 at 18:18
Can you provide a demo?
– sergiol
Dec 12 '18 at 19:32
1
1
Welcome to the site! Unfortunately, assuming input is in a predefined variable is not a standard form of input. You could add a
console.readline()
or however that's supposed to work (I'm not too familiar with C#) or submit a function instead.– DJMcMayhem♦
Dec 12 '18 at 17:57
Welcome to the site! Unfortunately, assuming input is in a predefined variable is not a standard form of input. You could add a
console.readline()
or however that's supposed to work (I'm not too familiar with C#) or submit a function instead.– DJMcMayhem♦
Dec 12 '18 at 17:57
1
1
Hi, welcome to PPCG! We allow either functions or full programs as answers (yours is currently more like a snippet), and all imports add to the byte-count. Which means your current answer should be
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.– Kevin Cruijssen
Dec 12 '18 at 18:00
Hi, welcome to PPCG! We allow either functions or full programs as answers (yours is currently more like a snippet), and all imports add to the byte-count. Which means your current answer should be
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.– Kevin Cruijssen
Dec 12 '18 at 18:00
1
1
Also, if you haven't seen it yet, tips for golfing in C# and tips for golfing in <all languages> might be interesting to read through. Apart from some ruling, your first answer looks fine, so +1 from me. Again welcome, and enjoy your stay! :)
– Kevin Cruijssen
Dec 12 '18 at 18:01
Also, if you haven't seen it yet, tips for golfing in C# and tips for golfing in <all languages> might be interesting to read through. Apart from some ruling, your first answer looks fine, so +1 from me. Again welcome, and enjoy your stay! :)
– Kevin Cruijssen
Dec 12 '18 at 18:01
Thanks guys! Sorry about the function error. Appreciate the for increment golf!
– Destroigo
Dec 12 '18 at 18:18
Thanks guys! Sorry about the function error. Appreciate the for increment golf!
– Destroigo
Dec 12 '18 at 18:18
Can you provide a demo?
– sergiol
Dec 12 '18 at 19:32
Can you provide a demo?
– sergiol
Dec 12 '18 at 19:32
|
show 1 more comment
Tcl, 70 bytes
proc C {n i 0} {time {set j 0
time {puts $i,$j
incr j} $n
incr i} $n}
Try it online!
Failed outgolf: tio.run/##K0nO@f@/oCg/…
– sergiol
Dec 12 '18 at 15:48
add a comment |
Tcl, 70 bytes
proc C {n i 0} {time {set j 0
time {puts $i,$j
incr j} $n
incr i} $n}
Try it online!
Failed outgolf: tio.run/##K0nO@f@/oCg/…
– sergiol
Dec 12 '18 at 15:48
add a comment |
Tcl, 70 bytes
proc C {n i 0} {time {set j 0
time {puts $i,$j
incr j} $n
incr i} $n}
Try it online!
Tcl, 70 bytes
proc C {n i 0} {time {set j 0
time {puts $i,$j
incr j} $n
incr i} $n}
Try it online!
edited Dec 12 '18 at 15:27
answered Dec 12 '18 at 15:20
sergiol
2,4901925
2,4901925
Failed outgolf: tio.run/##K0nO@f@/oCg/…
– sergiol
Dec 12 '18 at 15:48
add a comment |
Failed outgolf: tio.run/##K0nO@f@/oCg/…
– sergiol
Dec 12 '18 at 15:48
Failed outgolf: tio.run/##K0nO@f@/oCg/…
– sergiol
Dec 12 '18 at 15:48
Failed outgolf: tio.run/##K0nO@f@/oCg/…
– sergiol
Dec 12 '18 at 15:48
add a comment |
Python 2, 39 bytes
lambda n:[(i%n,i/n)for i in range(n*n)]
Try it online!
I think the question requires variableN
...
– Felix Palmen
Dec 12 '18 at 15:04
@FelixPalmen Doh.. Fixed :)
– TFeld
Dec 12 '18 at 15:05
add a comment |
Python 2, 39 bytes
lambda n:[(i%n,i/n)for i in range(n*n)]
Try it online!
I think the question requires variableN
...
– Felix Palmen
Dec 12 '18 at 15:04
@FelixPalmen Doh.. Fixed :)
– TFeld
Dec 12 '18 at 15:05
add a comment |
Python 2, 39 bytes
lambda n:[(i%n,i/n)for i in range(n*n)]
Try it online!
Python 2, 39 bytes
lambda n:[(i%n,i/n)for i in range(n*n)]
Try it online!
edited Dec 12 '18 at 15:05
answered Dec 12 '18 at 15:00
TFeld
14.2k21240
14.2k21240
I think the question requires variableN
...
– Felix Palmen
Dec 12 '18 at 15:04
@FelixPalmen Doh.. Fixed :)
– TFeld
Dec 12 '18 at 15:05
add a comment |
I think the question requires variableN
...
– Felix Palmen
Dec 12 '18 at 15:04
@FelixPalmen Doh.. Fixed :)
– TFeld
Dec 12 '18 at 15:05
I think the question requires variable
N
...– Felix Palmen
Dec 12 '18 at 15:04
I think the question requires variable
N
...– Felix Palmen
Dec 12 '18 at 15:04
@FelixPalmen Doh.. Fixed :)
– TFeld
Dec 12 '18 at 15:05
@FelixPalmen Doh.. Fixed :)
– TFeld
Dec 12 '18 at 15:05
add a comment |
Pyth, 4 bytes
^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
add a comment |
Pyth, 4 bytes
^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
add a comment |
Pyth, 4 bytes
^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
Pyth, 4 bytes
^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
answered Dec 12 '18 at 15:06
Sok
3,537722
3,537722
add a comment |
add a comment |
MathGolf, 2 bytes
r■
Try it online!
Explanation
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.
add a comment |
MathGolf, 2 bytes
r■
Try it online!
Explanation
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.
add a comment |
MathGolf, 2 bytes
r■
Try it online!
Explanation
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.
MathGolf, 2 bytes
r■
Try it online!
Explanation
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.
answered Dec 12 '18 at 15:09
maxb
2,92611131
2,92611131
add a comment |
add a comment |
APL+WIN, 11 bytes
(⍳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
add a comment |
APL+WIN, 11 bytes
(⍳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
add a comment |
APL+WIN, 11 bytes
(⍳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
APL+WIN, 11 bytes
(⍳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
answered Dec 12 '18 at 15:17
Graham
2,25678
2,25678
add a comment |
add a comment |
Pepe, 88 bytes
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
Try it online!
Warning: Do not run it with input below 1 - it will kill your browser.
add a comment |
Pepe, 88 bytes
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
Try it online!
Warning: Do not run it with input below 1 - it will kill your browser.
add a comment |
Pepe, 88 bytes
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
Try it online!
Warning: Do not run it with input below 1 - it will kill your browser.
Pepe, 88 bytes
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
Try it online!
Warning: Do not run it with input below 1 - it will kill your browser.
answered Dec 12 '18 at 17:07
RedClover
572523
572523
add a comment |
add a comment |
Perl 5 -na
, 35 bytes
map{//;say"$',$_"for 0..$F[0]}0..$_
Try it online!
add a comment |
Perl 5 -na
, 35 bytes
map{//;say"$',$_"for 0..$F[0]}0..$_
Try it online!
add a comment |
Perl 5 -na
, 35 bytes
map{//;say"$',$_"for 0..$F[0]}0..$_
Try it online!
Perl 5 -na
, 35 bytes
map{//;say"$',$_"for 0..$F[0]}0..$_
Try it online!
answered Dec 12 '18 at 17:16
Xcali
5,168520
5,168520
add a comment |
add a comment |
Powershell, 43 bytes
param($n)$i--..--$n*++$n|%{$i+=!$_;"$i,$_"}
Explanation:
One row 0..$n-1
repeated $n
times.
add a comment |
Powershell, 43 bytes
param($n)$i--..--$n*++$n|%{$i+=!$_;"$i,$_"}
Explanation:
One row 0..$n-1
repeated $n
times.
add a comment |
Powershell, 43 bytes
param($n)$i--..--$n*++$n|%{$i+=!$_;"$i,$_"}
Explanation:
One row 0..$n-1
repeated $n
times.
Powershell, 43 bytes
param($n)$i--..--$n*++$n|%{$i+=!$_;"$i,$_"}
Explanation:
One row 0..$n-1
repeated $n
times.
answered Dec 12 '18 at 17:20
mazzy
2,1251315
2,1251315
add a comment |
add a comment |
Pure Bash (no external utilities), 35
eval echo {0..$[$1-1]},{0..$[$1-1]}
Try it online!
add a comment |
Pure Bash (no external utilities), 35
eval echo {0..$[$1-1]},{0..$[$1-1]}
Try it online!
add a comment |
Pure Bash (no external utilities), 35
eval echo {0..$[$1-1]},{0..$[$1-1]}
Try it online!
Pure Bash (no external utilities), 35
eval echo {0..$[$1-1]},{0..$[$1-1]}
Try it online!
answered Dec 12 '18 at 17:39
Digital Trauma
58.6k787221
58.6k787221
add a comment |
add a comment |
Lua, 63 bytes
s=io.read()-1 for i=0,s do for j=0,s do print(i..','..j)end end
Try it online!
add a comment |
Lua, 63 bytes
s=io.read()-1 for i=0,s do for j=0,s do print(i..','..j)end end
Try it online!
add a comment |
Lua, 63 bytes
s=io.read()-1 for i=0,s do for j=0,s do print(i..','..j)end end
Try it online!
Lua, 63 bytes
s=io.read()-1 for i=0,s do for j=0,s do print(i..','..j)end end
Try it online!
edited Dec 12 '18 at 16:24
answered Dec 12 '18 at 16:15
ouflak
1931311
1931311
add a comment |
add a comment |
Perl5, 42 38 bytes
for$i(0..--$n){for(0..$n){say"$i,$_"}}
(The inner loop saves a few bytes by using the implicit variable "$_" as an index.)
add a comment |
Perl5, 42 38 bytes
for$i(0..--$n){for(0..$n){say"$i,$_"}}
(The inner loop saves a few bytes by using the implicit variable "$_" as an index.)
add a comment |
Perl5, 42 38 bytes
for$i(0..--$n){for(0..$n){say"$i,$_"}}
(The inner loop saves a few bytes by using the implicit variable "$_" as an index.)
Perl5, 42 38 bytes
for$i(0..--$n){for(0..$n){say"$i,$_"}}
(The inner loop saves a few bytes by using the implicit variable "$_" as an index.)
edited Dec 12 '18 at 17:57
Wît Wisarhd
34k10156365
34k10156365
answered Dec 12 '18 at 17:30
Tom Williams
93
93
add a comment |
add a comment |
3
Welcome to PPCG! This is the start of a good question. I couldn't find a duplicate from a quick search, though I might be mistaken. One thing I'd suggest is to specify the input and output. I assume that the input is an integer
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?– maxb
Dec 12 '18 at 14:49
4
Are we allowed to return a list instead of printing them? Is any order acceptable (i.e.
[[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?– Kevin Cruijssen
Dec 12 '18 at 14:54
1
Can we start at (1,1) instead of (0,0) ?
– sergiol
Dec 12 '18 at 15:01
4
Is there a particular order they have to be in or is any order fine?
– Wît Wisarhd
Dec 12 '18 at 15:18
2
Can't decide whether this is a dupe of this (just hardcode $n=2$) or this (just duplicate the argument instead of taking two distinct), but probably both.
– BMO
Dec 12 '18 at 17:04