Create a range grid
This challenge is based on the i.
verb from J.
A range grid is basically a range of m × n
numbers fit into a rectangular grid with m
rows and n
columns.
A 2×3
range grid would be:
0 1 2
3 4 5
Challenge
Generate a range grid, given m
and n
. You can display it, or return an array.
This is code-golf
. Shortest answer in each language wins.
Ruby, 33 bytes ``` ->n,m{[(0 …
2y ago
BQN (CBQN), 10 bytes Anonymou …
2y ago
C (gcc), 59 bytes ``` C i; …
2y ago
[Python 2], 46 bytes …
2y ago
[Python 3], 49 bytes …
2y ago
Japt `-m`, 4 bytes VÆT° …
2y ago
MATL, 8 bytes ``` tp:qwPe! …
2y ago
Lua 5.4, 76 bytes ``` lua …
2y ago
8 answers
MATL, 8 bytes
tp:qwPe!
tp:qwPe!
t : duplicate with implicit input (function args as array)
p : product [2 3]p -> 6
: : range 1..n
q : decrement
w : swap top of stack
P : flip [2 3]P -> [3 2]
e : reshape [1 2 3 4][2 2]e -> [1 3;2 4]
! : transpose
Reshape fills columns instead of rows, hence the flip and transpose.
0 comment threads
C (gcc), 59 bytes
i;f(m,n){while(i<m*n){putchar(i%n?32:13);putchar(48+i++);}}
-
m
is the number of rows. -
n
is the number of columns.
1 comment thread
BQN (CBQN), 10 bytes
Anonymous function that takes m
on the left and n
on the right.
{𝕨‿𝕩⥊↕𝕨×𝕩}
↕𝕨×𝕩 # list of range [0,m*n)
𝕨‿𝕩⥊ # reshape list to m*n
1 comment thread