3
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
3
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
5
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?
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 – 2018-12-12T15:00:04.380
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 – 2018-12-12T18:06:27.997
@KevinCruijssen Good point, updated the post! – maxb – 2018-12-13T09:00:20.503
5
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 – 2018-12-12T15:34:29.073
@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 – 2018-12-12T15:38:49.403
31 bytes. Sadly, requires !!
, as otherwise: argument to 'which' is not logical
... – Kirill L. – 2018-12-12T15:56:39.930
@KirillL. that's neat! It also inspired me to shave off another 2 bytes. EDIT: another 6 bytes! – Giuseppe – 2018-12-12T15:58:50.810
3
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]]
How comes that your 2-byter is 1-indexed? – maxb – 2018-12-12T15:00:29.813
@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 – 2018-12-12T18:04:12.260
3
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,$_"}}
– mazzy – 2018-12-12T17:12:54.410
@mazzy Of course, removing the -join
. Thanks! – AdmBorkBork – 2018-12-12T17:20:26.490
3
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.
1Welcome 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. – James – 2018-12-12T17:57:32.833
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.
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 – 2018-12-12T18:01:37.913Thanks guys! Sorry about the function error. Appreciate the for increment golf! – Destroigo – 2018-12-12T18:18:25.523
Can you provide a demo? – sergiol – 2018-12-12T19:32:46.833
@sergiol Kevin provided a TIO link. I put it on the post. – Destroigo – 2018-12-13T15:59:18.930
2
– sergiol – 2018-12-12T15:48:30.900
1
I think the question requires variable N
... – Felix Palmen – 2018-12-12T15:04:26.830
@FelixPalmen Doh.. Fixed :) – TFeld – 2018-12-12T15:05:39.903
1
^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
1
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.
1
(⍳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
1
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.
1
1
param($n)$i--..--$n*++$n|%{$i+=!$_;"$i,$_"}
Explanation:
One row 0..$n-1
repeated $n
times.
1
0
0
for$i(0..--$n){for(0..$n){say"$i,$_"}}
(The inner loop saves a few bytes by using the implicit variable "$_" as an index.)
3Welcome 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 – 2018-12-12T14:49:19.9304Are 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 – 2018-12-12T14:54:00.837Also, see https://codegolf.meta.stackexchange.com/a/8066/71420 -- suggest not to ask about doing it in C if any language is acceptable.
– Felix Palmen – 2018-12-12T14:54:24.2331Can we start at (1,1) instead of (0,0) ? – sergiol – 2018-12-12T15:01:51.010
@sergiol must be started at 0,0 – Chris Puglia – 2018-12-12T15:02:45.450
@KevinCruijssen list is acceptable – Chris Puglia – 2018-12-12T15:03:18.287
Is the input the dimensions of the grid? – ouflak – 2018-12-12T15:12:51.380
4Is there a particular order they have to be in or is any order fine? – Post Rock Garf Hunter – 2018-12-12T15:18:10.460
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.
– ბიმო – 2018-12-12T17:04:03.577Nice find on the dupe. Would have taken me forever to track that down. – ouflak – 2018-12-13T07:52:41.107