Pip, 62 59 56 bytes
55 bytes of code, +1 for -S
flag.
[sXbRA_AEv-_aM,b(Y^aWR_Xb/2s)MyRVsX,b.aYaXbJsxyRL2x]@Aa
Note that this solution works with input of ÷
for division and *
for multiplication, though it uses those characters to draw ASCII-art /
and x
, respectively. OP has clarified that this is okay. Try it online!
How?
Here's a partially spaced and commented version. Note that a
and b
are the command-line args, i.e. the operator and the size respectively. The -S
flag controls how lists are output: it joins on newlines first, but joins any nested lists on spaces.
[ Build a list of all possible figures:
sXbRA_AEv-_aM,b 0 - Multiplication
(Y^aWR_Xb/2s)My 1 - Addition
RVsX,b.a 2 - Division
YaXbJs 3 - Subtraction
x 4 - Empty
yRL2 5 - Equals
x 6 - Empty
] @ Aa Index, mod 7, with ASCII value of a
And here are the individual sections:
Multiplication
For each row in the figure, we generate a string of spaces and then replace two of them with the input character. For row i
, we want to replace the spaces at index i
and b-1-i
; but note that the latter can use the negative index -1-i
. (It's even shorter because the variable v
is preinitialized to negative 1.)
sXbRA_AEv-_aM,b
M,b Map this function to range(b):
sXb Space, repeated b times
_ Function arg
AE (Implicitly) convert to list and append element...
v-_ -1 minus function arg
RA Replace the characters at those indices...
a ... with input character
The resulting list is output with one item per line
Addition
Using the strategy from my Draw a hollow square answer, we define a function that wraps a +
in b/2
copies of its argument and returns the result as a list of characters. (Integer division //
isn't necessary because the string repetition operator X
automatically truncates to integer.) First, we call this function on a space, giving [" ";" ";"+";" ";" "]
; then, we map the same function to that result, giving [" ";" ";"+";" ";" "]
on most lines but ["+";"+";"+";"+";"+"]
on the center line. This nested list is then output, because of the -S
flag, with spaces between all characters on a row and newlines between rows.
(Y^aWR_Xb/2s)My
Anonymous function:
_Xb/2 Function arg, repeated b/2 times (implicitly rounded down)
aWR Wrap a in two copies of the above
^ Split resulting string into list of chars
Y Yank that function into y
( s) Apply function y to space
My Map y to that result
Division
Each row has between b-1
and 0
spaces, followed by the input character a
.
RVsX,b.a
,b Range(b)
sX Space, repeated that many times (works itemwise)
.a Concatenate a (itemwise)
RV Reverse (puts the larger number of spaces at the beginning)
Outputs newline-separated
Subtraction
Make a space-separated row of the input character. Save a copy for later.
YaXbJs
aXb b copies of a
Js Join on space
Y Yank into y (overwrites the function from earlier, but we're done using it anyway)
This is a string, so it just gets output
Equals
The subtraction step stored half of what we need in y
--just double it.
yRL2 (Implicitly) convert y to list and repeat it twice
Outputs newline-separated
The dummy x
values are there to pad the main list so that the modular indexing gives a unique index for each input character in +-*÷=
.
= 10
? \$\endgroup\$