Integer-Digits of the Arithmetic-Tables

17

Challenge:

Output the 'integer-digits' of one of the following six arithmetic-tables based on the input:
- addition (+);
- subtraction (-);
- multiplication (*);
- division (/);
- exponentiation (^);
- modulo operation (%).

Rules:

  • What do I define as 'integer-digits': Every result of the arithmetic operand which is exactly one of the following: 0,1,2,3,4,5,6,7,8,9. This means you exclude every result of 10 or higher, every result of -1 or lower, and every non-integer result.
  • How do we calculate the arithmetic results: By using the top digit first, and then use the operand with the left digit. You are allowed to do this vice-versa (i.e. y/x instead of x/y), as long as you're consistent for all six of the outputs! (So you aren't allowed to use y-x and x/y in the same answer.)
  • We won't output anything for divide by 0 test-cases (for the division and modulo operation tables)
  • We won't output anything for the edge-case 0^0.

Output:

So output the following (table format is somewhat flexible (see below): so the lines are optional and mainly added for readability of the test cases):

Addition:

+ | 0 1 2 3 4 5 6 7 8 9
-----------------------
0 | 0 1 2 3 4 5 6 7 8 9
1 | 1 2 3 4 5 6 7 8 9
2 | 2 3 4 5 6 7 8 9
3 | 3 4 5 6 7 8 9
4 | 4 5 6 7 8 9
5 | 5 6 7 8 9
6 | 6 7 8 9
7 | 7 8 9
8 | 8 9
9 | 9

Subtraction:

- | 0 1 2 3 4 5 6 7 8 9
-----------------------
0 | 0 1 2 3 4 5 6 7 8 9
1 |   0 1 2 3 4 5 6 7 8
2 |     0 1 2 3 4 5 6 7
3 |       0 1 2 3 4 5 6
4 |         0 1 2 3 4 5
5 |           0 1 2 3 4
6 |             0 1 2 3
7 |               0 1 2
8 |                 0 1
9 |                   0

Multiplication:

* | 0 1 2 3 4 5 6 7 8 9
-----------------------
0 | 0 0 0 0 0 0 0 0 0 0
1 | 0 1 2 3 4 5 6 7 8 9
2 | 0 2 4 6 8
3 | 0 3 6 9
4 | 0 4 8
5 | 0 5
6 | 0 6
7 | 0 7
8 | 0 8
9 | 0 9

Division:

/ | 0 1 2 3 4 5 6 7 8 9
-----------------------
0 | 
1 | 0 1 2 3 4 5 6 7 8 9
2 | 0   1   2   3   4
3 | 0     1     2     3
4 | 0       1       2
5 | 0         1
6 | 0           1
7 | 0             1
8 | 0               1
9 | 0                 1

Exponentiation:

^ | 0 1 2 3 4 5 6 7 8 9
-----------------------
0 |   1 1 1 1 1 1 1 1 1
1 | 0 1 2 3 4 5 6 7 8 9
2 | 0 1 4 9
3 | 0 1 8
4 | 0 1
5 | 0 1
6 | 0 1
7 | 0 1
8 | 0 1
9 | 0 1

Modulo:

% | 0 1 2 3 4 5 6 7 8 9
-----------------------
0 | 
1 | 0 0 0 0 0 0 0 0 0 0
2 | 0 1 0 1 0 1 0 1 0 1
3 | 0 1 2 0 1 2 0 1 2 0
4 | 0 1 2 3 0 1 2 3 0 1
5 | 0 1 2 3 4 0 1 2 3 4
6 | 0 1 2 3 4 5 0 1 2 3
7 | 0 1 2 3 4 5 6 0 1 2
8 | 0 1 2 3 4 5 6 7 0 1
9 | 0 1 2 3 4 5 6 7 8 0

Challenge rules:

  • Trailing new-lines and trailing spaces are optional
  • The horizontal and vertical lines in the test cases are optional. I only added them for better readability.
  • The spaces between each result are NOT optional.
  • The symbol for the arithmetic may be different, as long as it's clear which one it is. I.e. × or · instead of * for multiplication; ÷ instead of / for division; etc.
    And as long as it's a single character, so sorry Python's **.
  • The input format is flexible. You can choose an index from 0-5 or 1-6 for the corresponding six tables; you could input the operand-symbol; etc. (Unlike what you display in the result, you are allowed to input complete strings, or ** in Python's case.)
    Just make sure to state which input-format you use in your answer!

General rules:

  • This is , so shortest answer in bytes wins.
    Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.
  • Standard rules apply for your answer, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters, full programs. Your call.
  • Default Loopholes are forbidden.
  • If possible, please add a link with a test for your code.
  • Also, please add an explanation if necessary.

† Example of valid output without horizontal and vertical lines, ÷ as symbol, and using y/x instead of x/y:

÷ 0 1 2 3 4 5 6 7 8 9
0   0 0 0 0 0 0 0 0 0
1   1
2   2 1
3   3   1
4   4 2   1
5   5       1
6   6 3 2     1
7   7           1
8   8 4   2       1
9   9   3           1

Kevin Cruijssen

Posted 2017-04-03T06:53:11.540

Reputation: 67 575

Is giving a result ok for division by zero if that is the result in our language? E.g. in APL, 0÷0 is 1 by default, and N mod-0 is N? One can also chose a different scheme where division by zero is always zero. – Adám – 2017-04-03T12:56:23.947

Are we allowed more than one space between columns? – Adám – 2017-04-03T21:13:50.297

@Adám Sorry for the last response. As for your first question: no sorry. I know some languages print 1, other 0, others something else for 0^0 or divide/mod 0, but you'll have to work around that. As for your second question: Yes sure, as long as the digits are still in the same columns/rows you can use as many spaces as you want. – Kevin Cruijssen – 2017-04-04T07:10:00.947

Is the order of arguments important for non-commutative operations? – Adám – 2017-04-05T05:51:38.510

@Adám So you mean you'll output the grid for y-x, y/x, y^x and y%x instead of x-y, x/y, x^y and x%y? Hmm, I guess that could be fine. I'll edit it in the answer; as long as you'll be consistent for all six of them (so not y-x and x/y in the same answer). – Kevin Cruijssen – 2017-04-05T06:46:17.780

Firstly, I think your tables are backwards compared to most tables. Secondly, for mod(), which is not part of basic arithmetics, who decides the order of arguments?

– Adám – 2017-04-05T07:16:36.160

Answers

7

Japt, 45 bytes

Collaborated with @ETHproductions

AÆAÇU¥'p«Z«XªOvZ+U+X)+P r"..+"SÃuXÃuAo uU)m¸·

Run it online!

Takes input as:

"+" for addition

"-" for subtraction

"*" for multiplication

"/" for division

"p" for exponentiation

"%" for modulo

Explanation (With expanded shortcuts):

AÆ  AÇ  U¥ 'p«  Z«  Xª OvZ+U+X)+P r"..+"SÃ uXÃ uAo uU)m¸  ·
AoX{AoZ{U=='p&&!Z&&!X||OvZ+U+X)+P r"..+"S} uX} uAo uU)mqS qR

A                                                             // By default, 10 is assigned to A
 o                                                            // Create a range from [0...9]
  X{                                         }                // Iterate through the range, X becomes the iterative item
    Ao                                                        //   Create another range [0...9]
      Z{                                 }                    //   Iterate through the range, Z becomes the iterative item
                                                              //     Take:
        U=='p                                                 //       U (input) =="p"
             &&!Z                                             //       && Z != 0
                 &&!X                                         //       && X != 0
                     ||                                       //     If any of these turned out false, instead take
                       Ov                                     //       Japt Eval:
                         Z+U+X                                //         Z{Input}X
                              )+P                             //     Whichever it was, convert to a string
                                  r"..+"S                     //     Replace all strings of length 2 or more with " "
                                                              //     (this makes sure the result !== "false" and has length 1)
                                           uX                 //   Insert X (the row number) into the front of the row
                                               u              // Insert at the beginning the first row:
                                                Ao            //   [0...9]
                                                   uU)        //   with the input inserted at the beginning
                                                      mqS     // Join each item in the final array with " "
                                                          qR  // Join the final array with "\n"

Oliver

Posted 2017-04-03T06:53:11.540

Reputation: 7 160

8

JavaScript (ES7), 128 bytes

f=
c=>[...c+`0123456789`].map((r,_,a)=>a.map(l=>l==c?r:r==c?l:/^\d$/.test(l=c<`^`?eval(l+c+r):l|c?l**r:l/r)?l:` `).join` `).join`
`
<select onchange=o.textContent=f(this.value)><option>><option>+<option>-<option>*<option>/<option>%<option>^<option>&<option>,<option>.</select><pre id=o>

Special-casing 0^0 cost me 8 bytes.

Neil

Posted 2017-04-03T06:53:11.540

Reputation: 95 035

Very cool how you've added additional operands like OR and AND to your test-snippet. +1 – Kevin Cruijssen – 2017-04-03T10:00:31.290

@KevinCruijssen I had to remove OR to save a byte (all the other operators sort before ^), but thanks! – Neil – 2017-04-03T10:01:07.293

5

Operation Flashpoint scripting language, 343 333 303 301 bytes

f={o=_this;s=o+" 0 1 2 3 4 5 6 7 8 9\n";i=0;while{i<10}do{j=0;s=s+format["%1",i];if(i<1&&(o=="/"||o=="%"||o=="^"))then{if(o=="^")then{if(j<1)then{s=s+"  ";j=1}}else{s=s+"\n1";i=1}};while{j<10}do{r=call format["%1%2%3",j,o,i];if(r>9||r<0||r%1>0)then{r=" "};s=s+format[" %1",r];j=j+1};s=s+"\n";i=i+1};s}

Call with:

hint ("+" call f)

Ungolfed:

f=
{
    o=_this;
    s=o+" 0 1 2 3 4 5 6 7 8 9\n";
    i=0;
    while{i<10}do
    {
        j=0;
        s=s+format["%1",i];
        if(i<1&&(o=="/"||o=="%"||o=="^"))then
        {
            if(o=="^")then{if(j<1)then{s=s+"  ";j=1}}
            else{s=s+"\n1";i=1}
        };
        while{j<10}do
        {
            r=call format["%1%2%3",j,o,i];
            if(r>9||r<0||r%1>0)then{r=" "};
            s=s+format[" %1",r];
            j=j+1
        };
        s=s+"\n";
        i=i+1
    };
    s
}

Output:

operator +

operator -

operator *

operator /

operator ^

operator %

Steadybox

Posted 2017-04-03T06:53:11.540

Reputation: 15 798

5

Python 2, 240 231 226 224 203 202 200 197 bytes

a=i=input()
R=range(10)
for z in R:a+=' '+`z`
print a
for x in R:
 try:
	d=`x`
	for b in R:c=eval("b%s(x*1.)"%('**',i)[i<'^']);d+=' '+(' ',`int(c)`)[(i<'^'or x+b>0)and c in R]
 except:pass
 print d

Try it online!

Takes input as one of "+", "-", "*", "/", "^" or "%".

Edits

-9 -16 with thanks to @FelipeNardiBatista for some great hints

Down to 221 with more help from @FelipeNardiBatista and then down to 203 by losing and E(c)==int(E(c)). If we are checking if E(c) is in range(10) it will always be an integer if it is there. No need for the duplicate check.

This has to go below 200 without switching to Python 3 and declaring P=print. Any ideas? I am always happy to learn.

Yesss! I knew it could be done. 197. Time for bed now. I have spent enough time on this one. Thanks for the interesting challenge @KevinCruijssen.

ElPedro

Posted 2017-04-03T06:53:11.540

Reputation: 5 301

1('**',i)[i<'^'] and (i<'^'or x>0 or b>0) saves 4 bytes – Felipe Nardi Batista – 2017-04-03T12:30:02.343

1a=i=input() with for z in R:a+=' '+\z`` for some extra bytes saved vs a=i+' 0 1 2 3 4 5 6 7 8 9' – Felipe Nardi Batista – 2017-04-03T12:52:57.763

11. vs 1.0 and E(c)in R vs -1<E(c)<10 for 2 bytes – Felipe Nardi Batista – 2017-04-03T13:06:44.993

Hey now this is getting down to some really cool tricks! Thanks. Will update with your suggestions when I get time. – ElPedro – 2017-04-03T13:10:02.070

1x+b>0 vs x>0 or b>0 and "b%s(x*1.)"%('**',i)[i<'^'] vs "b"+('**',i)[i<'^']+"(x*1.)" – Felipe Nardi Batista – 2017-04-03T13:44:17.677

1there is one set of extra paren (,) in your join for 223 bytes – Felipe Nardi Batista – 2017-04-03T13:55:17.647

Not sure how I missed that one. Thanks again! – ElPedro – 2017-04-03T14:00:30.927

4

Java 7, 312 305 bytes

String c(int o){String r=("+-*/^%".charAt(o))+" 0 1 2 3 4 5 6 7 8 9\n";for(int i=0,j,p;i<10;i++){r+=i+" ";for(j=0;j<10;r+=p<0|p>9?"  ":p+" ")p=p(o,i,j++);r+="\n";}return r;}int p(int o,int a,double b){b=o<1?b+a:o<2?b-a:o<3?b*a:o<4&a>0?b/a:o<5&(a!=0|b!=0)?Math.pow(b,a):a>0?b%a:-1;return b%1==0?(int)b:-1;}

Uses no horizontal/vertical lines, and the characters are as displayed in the challenge-examples (+-*/^%).
Uses an index of 0-5 for the six mathematical operands as input.

-7 bytes thanks to @Frozn.

Explanation:

String c(int o){                   // Method with integer parameter and String return-type
  String r = ("+-*/^%".charAt(o))  //  Get the current mathematical operand character based on the input index
    + " 0 1 2 3 4 5 6 7 8 9\n";    //  Append the column header and a new-line
  for(int i=0,j,p; i<10; i++){     //  Loop over the rows
    r += i+" ";                    //   Append the left-side row-nr
    for(j=0; j<10;                 //   Inner-loop over the columns of the current row
        r += p<0|p>9?"  ":p+" ")   //     And after every iteration, append the result with either a space or an integer
      p = p(o,i,j++);              //    Calculate the current sum-result
                                   //   End of inner-loop (implicit / single-line body)
    r+="\n";                       //   Append result with new-line
  }                                //  End of loop
  return r;                        //  Return result String
}                                  // End of method

int p(int o,int a,double b){       // Separate method with two integer and a double parameters and integer return-type
  b = o<1 ?                        //  If the given operand is 0:
       b+a                         //   Use addition
      : o<2 ?                      //  Els-if the given operand is 1:
       b-a                         //   Use subtraction
      : o<3 ?                      //  Else-if the given operand is 2:
       b*a                         //   Use multiplication
      : o<4 & a>0 ?                //  Else-if the given operand is 3 and `a` is above 0:
       b/a                         //   Use division
      : o<5 & (a!=0|b!=0) ?        //  Else-if the given operand is 4 and not both `a` and `b` are 0:
       Math.pow(b,a)               //   Use exponentiation
      : a>0 ?                      //  Else-if the given operand is 5:
       b%a                         //   Use modulo
      :                            //  Else:
       -1;                         //   Use -1 as result
  return b%1 == 0 ?                //  If the result is not a decimal number:
     (int)b                        //   Return the result
    :                              //  Else:
     -1;                           //   Return -1 as result
}                                  // End of separate method

Test code:

Try it here.

class M{
  String c(int o){String r=("+-*/^%".charAt(o))+" 0 1 2 3 4 5 6 7 8 9\n";for(int i=0,j,p;i<10;i++){r+=i+" ";for(j=0;j<10;r+=p<0|p>9?"  ":p+" ")p=p(o,i,j++);r+="\n";}return r;}int p(int o,int a,double b){b=o<1?b+a:o<2?b-a:o<3?b*a:o<4&a>0?b/a:o<5&(a!=0|b!=0)?Math.pow(b,a):a>0?b%a:-1;return b%1==0?(int)b:-1;}

  public static void main(String[]a){
    M m = new M();
    System.out.println(m.c(0)); // +
    System.out.println(m.c(1)); // -
    System.out.println(m.c(2)); // *
    System.out.println(m.c(3)); // /
    System.out.println(m.c(4)); // ^
    System.out.println(m.c(5)); // %
  }
}

Kevin Cruijssen

Posted 2017-04-03T06:53:11.540

Reputation: 67 575

1Maybe you could pass b as a double to p and get rid of r by assigning the value of the chained ternary to b. – Frozn – 2017-04-03T13:16:54.733

4

Python 3, 343 335 363 362 bytes

The saddest part about this is that a Java answer is beating me... I'll golf this more in the morning.

o=input()
r=range(10)
g=[['']*10 for i in r]
for x in r:
 for y in r:exec('if"/"!=o and(o!="%"or x)and(o!="**"or x or y):k=str(y'+o+'x);g[x][y]=k')
if'/'==o:
 for x in r:
  for y in r:
   if x and y%x<1:g[x][y]=str(round(y/x))
if'**'==o:o='^'
print('\n'.join([' '.join([o]+list(map(str,r)))]+[' '.join([str(q)]+[' 'if len(x)!=1else x for x in g[q]])for q in r]))

ReplIT

-8 bytes by switching to list comprehension rather than a double loop
+28 bytes to avoid edge case 0 ^ 0. -.-
-1 byte by changing ==0 to <1 thanks to @StewieGriffin

HyperNeutrino

Posted 2017-04-03T06:53:11.540

Reputation: 26 575

"The saddest part about this is that a Java answer is beating me..." This part made me giggle.. xD I like how you're checking the length of the number to determine if it's in the range of 0-9. Btw, umm.. I noticed one error in your Repl. It currently outputs ** instead of ^ for exponentiation. (Also, you are allowed to input **, but not to output it in the result-table. Currently you have it the other way around.) – Kevin Cruijssen – 2017-04-03T08:10:00.477

1@KevinCruijssen Whoops. Fixed successfully, no byte count change. Thanks for pointing that out! – HyperNeutrino – 2017-04-03T12:14:29.313

@StewieGriffin Yes. Thank you. – HyperNeutrino – 2017-04-03T12:53:16.357

4

Mathematica, 150 bytes

r=0~Range~9;p=Prepend;±i_:=Grid@p[p[If[0<=#<=9,#]/._@__->""&/@<|Thread[Characters@"+-*/^%"->{Plus,#-#2&,1##&,#/#2&,Power,Mod}]|>[i][r,#],#]&/@r,r~p~i]

Defines a unary function ± taking one of the characters +-*/^% as its input i (so for example, ±"^"), and returning a Grid object that looks exactly like the last output in the OP.

<|Thread[Characters@"+-*/^%"->{Plus,#-#2&,1##&,#/#2&,Power,Mod}]|> associates, to each possible input character, the corresponding (listable) binary function (where #-#2&,1##&,#/#2& are golfed versions of Subtract,Times,Divide); therefore <|...|>[i][r,#] calculates the binary operation with all possible first arguments and # as the second argument. If[0<=#<=9,#]/._@__->""& converts each result to a Null or "" if it's not a single-digit result (/._@__->"" is necessary because some results like 1/0 can't be processed by the inequalities 0<=#<=9). Finally, we prepend the various headers and footers and display the answer.

Greg Martin

Posted 2017-04-03T06:53:11.540

Reputation: 13 940

Nice answer and explanation. Do you perhaps have a Try-it-online link? And one question/note, I don't see any mention of the edge-case rule: "We won't output anything for the edge-case 0^0." Does Mathematica output nothing on default for this edge-case? – Kevin Cruijssen – 2017-04-03T08:13:12.863

1I added a link above. Mathematica evaluates 0^0 to Indeterminate, which gives us an unhappy unevaluated result If[0<=Indeterminate<=9, Indeterminate] mid-computation; but /._@__->"" is a rule that takes any unevaluated function and its arguments and changes it to an invisible string. – Greg Martin – 2017-04-03T08:29:29.850

Ah ok, so Mathematica correctly returns that two conflicting rules #^0=1 and 0^#=0are occurring for0^0`. Good to now, and convenient for this challenge. :) Btw, your link doesn't seem to work. I get an error that I have no permission to access it.

– Kevin Cruijssen – 2017-04-03T08:40:49.377

Ok, you'll have to go to http://sandbox.open.wolframcloud.com and paste the code in yourself, then call it with a command like ±"^".

– Greg Martin – 2017-04-03T08:47:01.647

3

Haskell, 230 199 182 + 53 47 46 + 1 byte of separator = 284 247 232 229 bytes

f=head.show
g=[0..9]
h=(:" ")
y(%)s=unlines$(s:map f g>>=h):[f y:[last$' ':[f(x%y)|x%y`elem`g]|x<-g]>>=h|y<-g]
0?0=10;a?b=a^b
a!0=10;a!b|(e,0)<-a`divMod`b=e|1>0=10
a&0=10;a&b=mod a b

Function is (zipWith y[(+),(-),(*),(!),(?),(&)]"+-*/^%"!!), which alone takes up 53 bytes, where 0 is addition, 1 is subtraction, 2 is multiplication, 3 is division, 4 is exponentiation, and 5 is modulo.

Try it online!

Explanation

Coming later (possibly) . . . . For now some little tidbits: ? is the exponentiation operator, ! is the division operator, and & is the mod operator.

EDIT: Part of the bulk might be because most (?) of the other answers use eval, which Haskell doesn't have without a lengthy import.

EDIT2: Thanks Ørjan Johansen for -31 bytes (Wow!) off the code and -6 bytes off the function! Also changed some of the 11s to 10s for consistency purposes. Try the updated version online!

EDIT3: Same person, seventeen more bytes! Try the updated, updated version online!

Generic Display Name

Posted 2017-04-03T06:53:11.540

Reputation: 365

1Shorter ! tests: e<-a`div`b,e*b==a=e or (e,0)<-a`divMod`b=e. – Ørjan Johansen – 2017-04-04T01:21:05.587

1Shorter function: (zipWith(#)"+-*/^%"[(+),(-),(*),(!),(?),(&)]!!) – Ørjan Johansen – 2017-04-04T01:32:14.893

Reversing tests + a "standard" golfing trick gives last$f k:[' '|k<0||k>9]. Finally (maybe), [0..9] is just long enough that it pays to define it as a name when using it twice. – Ørjan Johansen – 2017-04-04T01:41:25.317

Nah, one more: k<-[o x y] is shorter than a let. – Ørjan Johansen – 2017-04-04T01:46:41.170

One word: Wow! :) – Generic Display Name – 2017-04-04T01:54:26.240

If you make o an operator instead, then it no longer pays to define k at all: s#(%)=unlines$(s:['0'..'9']>>=h):[f y:[last$f(x%y):[' '|x%y<0||x%y>9]|x<-l]>>=h|y<-l];l=[0..9] – Ørjan Johansen – 2017-04-04T01:54:26.883

Now ['0'..'9'] is shorter as map f l. – Ørjan Johansen – 2017-04-04T01:59:52.490

Sorry if I'm being overwhelming here. – Ørjan Johansen – 2017-04-04T02:00:18.300

nah, it's absolutely fine – Generic Display Name – 2017-04-04T02:17:09.557

Oh and now it's a byte shorter to use an alphanumeric name instead of #, if you swap its arguments. – Ørjan Johansen – 2017-04-04T02:34:35.433

Still missed something: last$' ':[f(x%y)|x%y`elem`g] – Ørjan Johansen – 2017-04-04T02:41:58.553

2

Python 2, 197 bytes

p=input()
r=range(10)
s=' '
print p+s+s.join(map(str,r))
for i in r:print str(i)+s+s.join(eval(("s","str(j"+p+"i)")[i and(j%i==0 and'/'==p or'%'==p)or p in'**+-'and eval("j"+p+"i")in r])for j in r)

Try it online!

Input: Python 2

'+' Addition

'-' Sbtraction

'*' Multiplication

'/' Division

'**' Exponentiation

'%' Modulo

Python 3, 200 bytes

p=input()
r=range(10)
s=' '
print(p+s+s.join(map(str,r)))
for i in r:print(str(i)+s+s.join(eval(("s","str(j"+p+"i)")[i and(j%i==0 and'/'in p or'%'==p)or p in'**+-'and eval("j"+p+"i")in r])for j in r))

Try it online!

Input: Python 3

+ Addition

- Sbtraction

* Multiplication

// Division

** Exponentiation

% Modulo

Explanation

storing range(10) to a variable r, we can get the first line of output of the format

operator 0 1 2 3 4 5 6 7 8 9

by mapping every int in r to string and joining the string list['0','1','2','3','4','5','6','7','8','9'] with space s with p operator

p+s+s.join(map(str,r)

With that, for every i in r(range), for every j evaluate i and j with your operator

eval("j"+p+"i")

here, an exception might be thrown if unhandled - division or modulus by 0. To handle this case(i and(j%i==0 and'/'==p or'%'==p)) and the output format by described in the problem statement(the result for each evaluation shouldn't be a negative number nor a number greater than 10 - eval("j"+p+"i")in r),

i and(j%i==0 and'/'==p or'%'==p)or p in'**+-'and eval("j"+p+"i")in r

Thus printing the arithmetic-table!

Happy Coding!

Keerthana Prabhakaran

Posted 2017-04-03T06:53:11.540

Reputation: 759

Nice Python 2 answer. Guess at 197 that makes us equal. It took me a few more attempts though. Well done. Slight problem. For / the table shows .0 and not integers. Sure that is easily corrected :) – ElPedro – 2017-04-03T20:34:17.100

Sorry but have retracted my upvote once I saw that, Will add it again once you have had the chance to correct it :) – ElPedro – 2017-04-03T20:37:17.530

Thank you! But Python 2 prints int and python 3 prints float if you use '/' as input. Fir python 3 you have to use '//'. I have clearly mentioned that. – Keerthana Prabhakaran – 2017-04-04T01:04:31.013

And it seems that someone is editing my link and te python 2 link have been changed to python 3. I've reverted it now. – Keerthana Prabhakaran – 2017-04-04T01:11:11.017

I've had this problem before. It is because you have 2 TOI links on the same page. When you click either link it will open the first link definition that it finds. Get round it by renaming the second link to "try it online 3" or something and also rename the link definition. Should work fine then. – ElPedro – 2017-04-04T05:15:46.267

2

APL (Dyalog), 68 76 bytes

Requires ⎕IO←0 which is default on many systems. Prompts for input and expects a single character representing the operand.

t←'|'=w←⎕
(w,n),n⍪⍉⍣t∘.{(⍺w⍵≡0'*'0)∨(t∧⍵≡0)∨⍺w≡0'÷':⍬
n∊⍨r←⍵(⍎w)⍺:r
⍬}⍨n←⍳10

Try it online!

Much of the code is to circumvent that APL's results for ÷0 and 0*0 and to counteract that APL's modulo (|) has its arguments reversed compared to most other languages. Would have been only 41 bytes otherwise:

w←⎕
(w,n),n⍪∘.{0::⍬
÷n∊⍨r←⍵(⍎w)⍺:r}⍨n←⍳10

Try it online!

Adám

Posted 2017-04-03T06:53:11.540

Reputation: 37 779

Wow! and after all my hard work on my Python answer. Fair play. – ElPedro – 2017-04-03T20:58:14.870

1

R, 194 177 bytes

-17 bytes switching to manipulating matrix output

function(o){s=0:9
y=sapply(s,function(x)Reduce(o,x,init=s))
dimnames(y)=list(s,rep('',10))
y[!y%in%s|!is.finite(y)]=' '
if(o=='^')y[1]=' '
cat(substr(o,1,1),s)
print(y,quote=F)}

Try it online!

Changing to this approach has some downsides, namely it can't be optimised by using pryr and is a little clunky to set up the original matrix, but it can be output perfectly with some trailing spaces on the first line.

I still have to use the substr trick because of the %% mod operator. Will continue to trim this when I get a chance, it still feels very clunky dealing with the special cases.

CriminallyVulgar

Posted 2017-04-03T06:53:11.540

Reputation: 501

0

PHP, 191 Bytes

** instead of ^ as input + - / % * **

echo$k=$argn,$k=="**"?"":" ",join(" ",$d=range(0,9));foreach($d as$r){echo"\n$r";foreach($d as$c){echo" ";($k=="/"|($m=$k=="%"))&$r<1?print" ":eval("echo in_array($c$k$r,\$d)?$c$k$r:' ';");}}

Online Version of both Versions

PHP, 245 Bytes without eval

input + - / % * ^

use bcpowmod($c,1,$r) instead of bcmod($c,$r) because I need a third parameter in the division input. $c**1%$r==$c%$r

BC Math Functions

echo$k=$argn," ".join(" ",$d=range(0,9));foreach($d as$r){echo"\n$r";foreach($d as$c)echo" ",in_array($s=($k=="/"|($m=$k=="%"))&$r<1?-1:("bc".["+"=>add,"-"=>sub,"/"=>div,"*"=>mul,"^"=>pow,"%"=>powmod][$k])($c,$m?1:$r,$m?$r:9),$d)?round($s):" ";}

Jörg Hülsermann

Posted 2017-04-03T06:53:11.540

Reputation: 13 026

0

05AB1E, 56 55 bytes

9ÝDãεðýì„/%Iåyθ_*I'mQyO_*~iðë.VD9ÝQàiïëð]«TôεN<š}®I:ðý»

Outputs without lines and with reversed x and y. Input/output are using +, -, *, /, m, %.

Try it online or verify all tables.

21 bytes are used to fix edge cases /0, %0 and 0^0, which result in 0, 0, and 1 respectively in 05AB1E.. Here without that part (34 bytes):

9ÝDãεðýì.VD9ÝQàiïëð]«TôεN<š}®I:ðý»

Try it online or try all tables.

Explanation:

9Ý                     # Push list [0,1,2,3,4,5,6,7,8,9]
  D                    # Duplicate it (for the header later on)
   ã                   # Take the cartesian product with itself (creating all pairs):
                       #  [[0,0],[0,1],[0,2],...,[9,7],[9,8],[9,9]]
ε                      # Map each pair `y` to:
 ðý                    #  Join the pairs with a space
   ì                   #  Prepend it before the (implicit) input-char 
 „/%Iå                 #   If the input is "/" or "%"
         *             #   and
      yθ_              #   The last value of the pair is exactly 0
                  ~    #  OR
          I'mQ        '#   If the input is "m"
                 *     #   and
              yO_      #   The sum of the pair is exactly 0 (thus [0,0])
 i                     #  If that is truthy:
  ð                    #   Push a space character " "
 ë                     #  Else:
  .V                   #   Execute the string as 05AB1E code
    D                  #   Duplicate the result
     9ÝQài             #   If it's in the list [0,1,2,3,4,5,6,7,8,9]:
          ï            #    Cast it to an integer to remove any trailing ".0"
                       #    (since dividing always results in a float)
         ë             #   Else:
          ð            #    Push a space character " "
]                      # Close both the if-else clauses and the map
 «                     # Merge the resulting list with the duplicated [0,1,2,3,4,5,6,7,8,9]
  Tô                   # Split the list in parts of size 10
    ε   }              # Map each list to:
     N<                #  Get the map-index + 1
       š               #  And prepend it at the front of the list
         ®I:           # Then replace the "-1" with the input-character
ðý                     # And finally join every inner list by spaces
  »                    # And join the entire string by newlines (which is output implicitly)

Kevin Cruijssen

Posted 2017-04-03T06:53:11.540

Reputation: 67 575