# Lists and arrays, part by part

In this challenge, you'll get four different but somewhat related tasks that must be solved in a specific manner. First, I'll explain the tasks, then follows an explanation of how you must solve it.

Your code should for all four tasks take two positive integers as input: n,m, where n<m. All tasks must be solved in the same language. The orientation of matrices are optional (n-by-m may be interpreted as "n rows, m columns", or "n columns, m rows").

Create (and output/print) a vector / list consisting of the elements: n, n+1 ... m-1, m. So, for n=4, m=9, you should output: 4,5,6,7,8,9.

Create (and output/print) a matrix / array / list of lists (or equivalent) looking like this:

n,   n+1, ... m-1, m
n+1, n+2, ... m-1, m+1
...
n+m, n+m+1, ... 2*m-1, 2*m


For n=4, m=9 you should output:

4, 5, 6, 7, 8, 9
5, 6, 7, 8, 9, 10
...
13, 14, 15, 16, 17, 18


Create (and output/print) an n-by-m multiplication table (on any suitable format). Example for n=4, m=9:

1   2   3  4
2   4   6  8
3   6   9  12
4   8  12  16
5  10  15  20
6  12  18  24
7  14  21  28
8  16  24  32
9  18  27  36


Output / print a vector / list consisting of the elements in the multiplication table from task 3, sorted in ascending order, keeping duplicate values. For n=4, m=9, you should output: 1, 2, 2, 3, 3, 4, 4, 4, 5, 6, 6, 6, 7, 8, 8, 8, 9, 9, 10, 12, 12, 12, 14, 15, 16, 16, 18, 18, 20, 21, 24, 24, 27, 28, 32, 36.

# The challenge:

To make it more clear:

Suppose the code for Task 1 is (works in Octave):

@(n,m)(n:m)


@(n,m)(n:m)+(0:m)'


The code for task Task 3 must be (doesn't work in Octave):

@(n,m)(n:m)+(0:m)'"Code_for_task_3"


And finally, the code for Task 4 must be (doesn't work in Octave):

@(n,m)(n:m)+(0:m)'"Code_for_task_3""Code_for_task_4"


This is , so the submission with the shortest code for task 4 in each language wins. As always: Explanations are highly encouraged.

• Just to be clear, I'm going to guess that this is against the spirit of the challenge, but is it permissible to start the next task's code with a redirect to STDERR >2; so that the previous task's code is essentially rendered a no-op? – AdmBorkBork Mar 31 '17 at 18:49
• @AdmBorkBork, there's no such thing as "the spirit of the challenge" on PPCG :P Yes, that's OK. – Stewie Griffin Mar 31 '17 at 18:59
• Does the multiplication table need to be padded nicely? – HyperNeutrino Mar 31 '17 at 20:31
• @HyperNeutrino, no. – Stewie Griffin Mar 31 '17 at 20:31
• When you say "positive integer", do you mean 0<n<m or 0<=n<m? – Value Ink Apr 3 '17 at 21:05

# Jelly, 12 bytes

r


Try it online!

r+þ0r$}  Try it online! ### Task 3 r+þ0r$}
×þ


Try it online!

r+þ0r$} ×þFṢ  Try it online! ## How it works ### Task 1 r is the dyadic range atom and does exactly what the task asks for. ### Task 2 A dyadic chain that begins with three dyadic links is a fork; the outmost links get evaluated first, then the middle link is called with the results to both sides as arguments. • r behaves as before, yielding [n, …, m]. • 0r$} is a quicklink (or quickquicklink, if you will).

The quick $ (monadich chain) consumes the links 0 (yield 0) and r (dyadic range) and turns them into a monadic chain. When called with argument k, this will yield [0, …, k]. The quick } (right argument) takes the quicklink created by $ and turns it into a dyadic link that calls 0r$ with 0r$}'s right argument.

0r$} will be called with left argument n and right argument m, so 0r$ is alled with argument m and yields [0, …, m].

• +þ is another quicklink. þ (table) will call + (addition) for every element in its left argument and any element in its right argument, grouping the results for each right argument into a single row.

+þ will be called with left argument [n, …, m] and right argument [0, …, m], yielding the desired table.

Every line in a Jelly program defines a different link. The last one is the main link and, like C's main function, is the only link that is executed by default. The remaining links can be called from the main link, but we won't do this here.

As before, þ (table) will call × (addition) for every element in its left argument and any element in its right argument, grouping the results for each right argument into a single row.

Since both arguments to ×þ are integers, þ casts them to ranges, transforming the arguments n and m into [1, …, n] and [1, …, m].

×þ works as before. The following links are monadic, making them atops, i.e., they are applied on top of the previous ones.

After executing ×þ, F flattens the resulting 2D array, and Ṣ sorts the resulting 1D array.

# 05AB1E, 18 17 bytes

Ÿ


Try it online

Ÿ²FD>})


Try it online

Ÿ²FD>})v¹LN*})¦


Try it online

Ÿ²FD>})v¹LN*})¦˜{


Try it online

## Explanations

Ÿ     # range[n ... m]


Ÿ        # range[n ... m]
²F      # m times do:
D     # duplicate
>    # increment
}   # end loop
)  # wrap in list


v          # for each list in result of Task 2 do
¹L        # push range[1 ... n]
N*      # multiply by index
}     # end loop
)    # wrap in list


˜          # flatten the result from Task 3
{         # sort


# MATL, 18 17 bytes

&:


Try it online!

&:O2G:h!+


Try it online!

&:O2G:h!+:7M!*


Try it online!

&:O2G:h!+:7M!*1eS


Try it online!

## Explanation

&:    % Binary range [n n+1 ... m] from implicit inputs n, m


      % ... Stack contains [n n+1 ... m]
O     % Push 0
2G    % Push second input, m
:     % Unary range: gives [1 2 ... m]
h     % Concatenate horizontally: gives [0 1 2 ... m]
!     % Transpose into a column vector


      % ... Stack contains matrix from task 2
:     % Unary range. For matrix input it uses its (1,1) entry. So this gives [1 2 ... n]
7M    % Push [1 2 ... m] again
!     % Transpose into a column vector


      % ... Stack contains matrix from task 3
1e    % Linearize into a row vector
S     % Sort


## Mathematica, 84 77 bytes

Edit: Thanks to Martin Ender for saving 7 bytes.

{n,m}n~Range~m


Pure Function with arguments n and m which outputs n~Range~m, the infix form of Range[n,m].

{n,m}n~Range~m~Table~(m+1)//0~Range~m+#&


n~Range~m~Table~(m+1) creates a 2D array with m+1 rows, where each row is the output of the previous task. Then //0~Range~m+#& is postfix application of the function 0~Range~m+#& which effectively adds 0 to the first row, 1 to the second row, and so on up to m for the m+1-th row.

{n,m}n~Range~m~Table~(m+1)//0~Range~m+#&//1##&~Array~{n,m}&


This just applies the constant function 1##&~Array~{n,m}& to the output of the previous task.

{n,m}n~Range~m~Table~(m+1)//0~Range~m+#&//1##&~Array~{n,m}&//Flatten//Sort


Flattens and Sorts the multiplication table.

# Python, 183 bytes

r=range
a=lambda n,m:r(n,m+1)


Try it online!

r=range
a=lambda n,m:r(n,m+1)
s=lambda n,m:[[w+i for w in r(n,m)] for i in a(0,m+1)]


Try it online!

r=range
a=lambda n,m:r(n,m+1)
s=lambda n,m:[[w+i for w in r(n,m)] for i in a(0,m+1)]
d=lambda n,m:[[w*i for w in a(1,n)] for i in a(1,m)]


Try it online!

r=range
a=lambda n,m:r(n,m+1)
s=lambda n,m:[[w+i for w in r(n,m)] for i in a(0,m+1)]
d=lambda n,m:[[w*i for w in a(1,n)] for i in a(1,m)]
f=lambda(z):sorted(sum(z,[]))


Try it online!

## Explanation:

Simple enough, it generates a n to m list using Python's built-in range function.

For every number 0 to m+1, it adds that number to each item of a list from n to m.

For each number from 1 to m, it multiplies that number by every number in a list from 1 to n.

This uses Python's built in sorted function which sorts a list from least to greatest. The list comprehension in the function is used to flatten the list. It creates a list of every item in every item of the list given to it by task 3.

• Saved very many bytes thanks to @math_junkie.
• Saved 16 bytes thanks to @ThisGuy

Saved very many bytes thanks to @math_junkie.

• Can you not use anonymous functions or are lambdas not actually shorter? (by my count it seems they are) – cole Mar 31 '17 at 21:54
• Is this generally acceptable for Python asnwers? The code for task 2, for instance, isn't a full program with input and output, and it's also not a function or function definition. – ngenisis Mar 31 '17 at 22:07
• @Cole I had to use non-anonymous functions so that I could use them in the later code. – Comrade SparklePony Mar 31 '17 at 22:16
• @ngenisis I think it is okay, because the OP said the code for task 2 must start with the code for task 1, and the code for task 2, def s(n,m):return [[w+i for w in r(n,m)] for i in a(0,m+1)], is a complete function when the code for task 1 defined. – Comrade SparklePony Mar 31 '17 at 22:18
• You can do something like a=lambda n,m:... for each of your function definitions. Anonymous function are always shorter in python – math junkie Apr 1 '17 at 0:35

# PHP 7, 200 bytes

Uses the output buffer to clear previous output.

";');for(;++$i<=$m;)eval($s);  Prints the remaining lines. ### Task 3 [,$n,$m]=$argv;ob_start();eval($s='echo join(" ",$v?:range($n+$i,$m+$i)),"
";');for(;++$i<=$m;)eval($s);for(($c=ob_clean)();++$j<=$m;print"
")for(;++$$j<=n;sort(v))echov[]=j*$$j,' ';


Clears the output buffer and prints the multiplication table, saving the numbers to $v. ### Task 4 [,$n,$m]=$argv;ob_start();eval($s='echo join(" ",$v?:range($n+$i,$m+$i)),"
";');for(;++$i<=$m;)eval($s);for(($c=ob_clean)();++$j<=$m;print"
")for(;++$$j<=n;sort(v))echov[]=j*$$j,' ';$c();eval($s);


Clears the output buffer again and prints $v. # PowerShell, 126 bytes ### Task 1 param($n,$m)$n..$m  Try it online! Uses the .. built-in range operator. The default behavior for the implied Write-Output inserts a newline between elements, so that's why the output shows as newline separated. ### Task 2 param($n,$m)$n..$m>2;0..$m|%{$i=$_;($n..$m|%{$_+$i})-join','}


Try it online!

Dumps the first task to STDERR with >2;, then loops from 0 to $m, each iteration setting helper $i before looping again from $n to $m and incrementing each number by $i. Those are -joined together with commas, else this would be an ambiguous gigantic long one-element-per-line output. ### Task 3 param($n,$m)$n..$m>2;0..$m|%{$i=$_;($n..$m|%{$_+$i})-join','}>2;(1..$m|%{$i=$_;(1..$n|%{$_*$i})-join' '})


Try it online!

Same sort of thing >2; to dump the previous to STDERR. Then we simply double-loop from 1 to $m then 1 to $n, setting helper $i along the way, multiply the values, and -join with a space to make it tabular. Note the encapsulating parens -- they'll come into play on the next task -- but here they just ensure that the output is put onto the pipeline (which it already would be, so they're redundant). ### Task 4 param($n,$m)$n..$m>2;0..$m|%{$i=$_;($n..$m|%{$_+$i})-join','}>2;(1..$m|%{$i=$_;(1..$n|%{$_*$i})-join' '})-split' '|%{+$_}|sort  Try it online! Aha! Finally some redundancy. Since the previous task has parens, we can tack on the -split on whitespace without worry, cast each one to an integer |%{+$_}, and then |sort. The output is again newline separated.

I think there are some ways to better leverage the redundancy between tasks -- still golfing it some.

# ES2016-ish, 401 384 chars

Here's a first try. I'm sure it could be condensed a bit, but it's pretty short. Arrow functions FTW! (Love those implicit return statements.) New and improved with template strings!

var a=Array,l=console.log,f=(n,m)=>a.from(a(1+m-n),(w,i)=>n+i),z=(n,m)=>l(f(n,m).join, )


Call z(n,m) for the log output. (I'm aliasing console.log for later golfing.)

Second verse... expands on the first.

var a=Array,l=console.log,f=(n,m)=>a.from(a(1+m-n),(w,i)=>n+i),z=(n,m)=>l(f(n,m).join, ),p,o,g=(n,m)=>{p=n,o=m;return [...a(m+1).keys()].map((d)=>f(d+p,d+o))},y=(n,m)=>l(g(n,m).join\n


Now call y(n,m). Pretty, no?

Have to bypass most of the existing functionality <sadface />.

var a=Array,l=console.log,f=(n,m)=>a.from(a(1+m-n),(w,i)=>n+i),z=(n,m)=>l(f(n,m).join, ),p,o,g=(n,m)=>{p=n,o=m;return [...a(m+1).keys()].map((d)=>f(d+p,d+o))},y=(n,m)=>l(g(n,m).join\n,h=(n,m)=>[...a(m).keys()].map((d)=>(d+1)*n).join\t,i=(n,m)=>[...a(n).keys()].map((d)=>h((d+1),m)),v=(n,m)=>i(n,m).join\n


Now the method name is v. Call it the same way.

And now we can reuse again.

var a=Array,l=console.log,f=(n,m)=>a.from(a(1+m-n),(w,i)=>n+i),z=(n,m)=>l(f(n,m).join, ),p,o,g=(n,m)=>{p=n,o=m;return [...a(m+1).keys()].map((d)=>f(d+p,d+o))},y=(n,m)=>l(g(n,m).join\n,h=(n,m)=>[...a(m).keys()].map((d)=>(d+1)*n).join\t,i=(n,m)=>[...a(n).keys()].map((d)=>h((d+1),m)),v=(n,m)=>i(n,m).join\n,t=(n,m)=>l(v(n,m).match(/\d+/g).sort((a,b)=>+a>+b||+(a===b)*2-1).join(, )


Had to skip u for my method, so it's t. Bummed that I had to put in that sort function, because the String.match returns values as... strings.

# Ruby, 121 103 bytes

Everything in Ruby is truthy except for nil and false, which means that the tasks can be set up to ignore previous input with naught but a well-placed and/&&.

Try it online!

n,m=$*.map &:to_i p [*n..m]  Task 2 Try it online! n,m=$*.map &:to_i
p [*n..m]&&(0..m).map{|i|[*n+i..m+i]}


n,m=$*.map &:to_i p [*n..m]&&(0..m).map{|i|[*n+i..m+i]}&&(1..m).map{|i|(1..n).map{|j|i*j}}  Task 4 Try it online! n,m=$*.map &:to_i