r/dailyprogrammer • • Sep 15 '12
[9/15/2012] Challenge #98 [easy] (Arithmetic tables)
Write a program that reads two arguments from the command line:
- a symbol,
+,-,*, or/ - a natural number n (≥ 0)
And uses them to output a nice table for the operation from 0 to n, like this (for "+ 4"):
+ | 0 1 2 3 4
-------------------
0 | 0 1 2 3 4
1 | 1 2 3 4 5
2 | 2 3 4 5 6
3 | 3 4 5 6 7
4 | 4 5 6 7 8If you want, you can format your output using the reddit table syntax:
|+|0|1
|:|:|:
|**0**|0|1
|**1**|1|2Becomes this:
|+|0|1 |:|:|: |0|0|1 |1|1|2
24
Upvotes
4
u/Say_What1 Sep 16 '12
In Python:
Here's the output:
If it gets bigger the formatting goes all wonky. This is my first submission so any feedback would be awesome.
5
u/skeeto -9 8 Sep 15 '12
In Emacs Lisp. There's no command line invocation so I made it as a function.
It can do any arbitrary function that can accept two integer arguments.
|atan|0|1|2|3|4|5 |:|:|:|:|:|:|: |0|0.0|1.5707963267948966|1.5707963267948966|1.5707963267948966|1.5707963267948966|1.5707963267948966 |1|0.0|0.7853981633974483|1.1071487177940904|1.2490457723982544|1.3258176636680326|1.373400766945016 |2|0.0|0.4636476090008061|0.7853981633974483|0.982793723247329|1.1071487177940904|1.1902899496825317 |3|0.0|0.3217505543966422|0.5880026035475675|0.7853981633974483|0.9272952180016122|1.0303768265243125 |4|0.0|0.24497866312686414|0.4636476090008061|0.6435011087932844|0.7853981633974483|0.8960553845713439 |5|0.0|0.19739555984988075|0.3805063771123649|0.5404195002705842|0.6747409422235526|0.7853981633974483
|*|0|1|2|3|4|5 |:|:|:|:|:|:|: |0|0|0|0|0|0|0 |1|0|1|2|3|4|5 |2|0|2|4|6|8|10 |3|0|3|6|9|12|15 |4|0|4|8|12|16|20 |5|0|5|10|15|20|25
2
u/SwimmingPastaDevil 0 0 Sep 15 '12 edited Sep 15 '12
Output:
2
u/prondose 0 0 Sep 15 '12 edited Sep 15 '12
Perl, works with most operators
dividing: table('/', 5)
|/|0|1|2|3|4|5 |:|:|:|:|:|:|: |0|||||| |1|0|1|2|3|4|5 |2|0|0.5|1|1.5|2|2.5 |3|0|0.33333|0.66667|1|1.3333|1.6667 |4|0|0.25|0.5|0.75|1|1.25 |5|0|0.2|0.4|0.6|0.8|1
bit shifting: table('<<', 3);
|<<|0|1|2|3 |:|:|:|:|: |0|0|1|2|3 |1|0|2|4|6 |2|0|4|8|12 |3|0|8|16|24
modulo: table('%', 3);
|%|0|1|2|3 |:|:|:|:|: |0|||| |1|0|0|0|0 |2|0|1|0|1 |3|0|1|2|0
2
u/ittybittykittyloaf Sep 17 '12
Bulky C++ solution:
Usage: 098e.exe --tableinfo *9
Output:
3
u/overra Sep 16 '12
Javascript:
Results:
1
u/shmaw Sep 15 '12
Ruby, practicing with closures and lambda functions
1
u/Ledrug 0 2 Sep 15 '12 edited Sep 15 '12
Haskell.
|+|-2|-1|0|1|2|3|4 |:|:|:|:|:|:|:|: |**-2|-4|-3|-2|-1|0|1|2 |-1|-3|-2|-1|0|1|2|3 |0|-2|-1|0|1|2|3|4 |1|-1|0|1|2|3|4|5 |2|0|1|2|3|4|5|6 |3|1|2|3|4|5|6|7 |4**|2|3|4|5|6|7|8
1
u/thenullbyte 0 0 Sep 15 '12 edited Sep 15 '12
In Ruby:
i.e. square("+", 3) gives you
1
u/oskar_stephens Sep 16 '12 edited Sep 16 '12
Ruby :
Initially i had a case statement to parse the operator argument, but then discovered .to_sym, which is pretty awesome.
Original case statement:
1
u/[deleted] Sep 16 '12
Trying out C++, no error checking.
/ 4
|/|0|1|2|3|4 |:|:|:|:|:|: |0| |0|0|0|0 |1| |1|0.5|0.333333|0.25 |2| |2|1|0.666667|0.5 |3| |3|1.5|1|0.75 |4| |4|2|1.33333|1
+4
|+|0|1|2|3|4 |:|:|:|:|:|: |0|0|1|2|3|4 |1|1|2|3|4|5 |2|2|3|4|5|6 |3|3|4|5|6|7 |4|4|5|6|7|8
1
u/villiger2 Sep 17 '12
Simple implementation in C++, no error checking, uses command line arguments.
1
u/zip_000 Sep 17 '12
Here's my PHP answer:
I didn't put in any testing to make sure it gets valid operators or variables, but that would be easy to throw in there. I'm not entirely happy with how the table is printed out... I'm not that familiar with PHP on the command line yet.
1
u/ChaosPhoenix7 0 0 Sep 18 '12
Python:
Output:
|*|0|1|2|3|4|5|6|7|8|9|10|11|12 |:|:|:|:|:|:|:|:|:|:|:|:|:|: |0|0|0|0|0|0|0|0|0|0|0|0|0|0 |1|0|1|2|3|4|5|6|7|8|9|10|11|12 |2|0|2|4|6|8|10|12|14|16|18|20|22|24 |3|0|3|6|9|12|15|18|21|24|27|30|33|36 |4|0|4|8|12|16|20|24|28|32|36|40|44|48 |5|0|5|10|15|20|25|30|35|40|45|50|55|60 |6|0|6|12|18|24|30|36|42|48|54|60|66|72 |7|0|7|14|21|28|35|42|49|56|63|70|77|84 |8|0|8|16|24|32|40|48|56|64|72|80|88|96 |9|0|9|18|27|36|45|54|63|72|81|90|99|108 |10|0|10|20|30|40|50|60|70|80|90|100|110|120 |11|0|11|22|33|44|55|66|77|88|99|110|121|132 |12|0|12|24|36|48|60|72|84|96|108|120|132|144
Any tips or suggestions are appreciated!
1
u/PoppySeedPlehzr 1 0 Sep 18 '12
Python, it's been a while, be gentle plox :)
Output:
1
u/meowfreeze Sep 28 '12 edited Sep 28 '12
Python. Accepts any valid python operator. Pads output to correct width.
Output:
In the last example you can see the start of a pattern created by the 'bitwise and' operator. Here is a screenshot of the output for &300.
1
u/[deleted] Oct 02 '12
Python 2.7:
1
u/robin-gvx 0 2 Oct 13 '12
A bit late, but oh well: (I replaced
/by<because division by zero raises an exception, and I was to lazy to rewrite it)Example:
|*|0|1|2|3|4|5|6|7|8|9|10 |:|:|:|:|:|:|:|:|:|:|:|: |0|0|0|0|0|0|0|0|0|0|0|0 |1|0|1|2|3|4|5|6|7|8|9|10 |2|0|2|4|6|8|10|12|14|16|18|20 |3|0|3|6|9|12|15|18|21|24|27|30 |4|0|4|8|12|16|20|24|28|32|36|40 |5|0|5|10|15|20|25|30|35|40|45|50 |6|0|6|12|18|24|30|36|42|48|54|60 |7|0|7|14|21|28|35|42|49|56|63|70 |8|0|8|16|24|32|40|48|56|64|72|80 |9|0|9|18|27|36|45|54|63|72|81|90 |10|0|10|20|30|40|50|60|70|80|90|100
1
u/marekkpie Jan 22 '13
Lua. ASCII and reddit implementations:
1
u/Sturmi12 Sep 15 '12
java solution, quick and ugly http://pastebin.com/RrkRJrVU
Result for "+ 4"
|+|0|1|2|3|4 |:|:|:|:|:|: |0|0|1|2|3|4 |1|1|2|3|4|5 |2|2|3|4|5|6 |3|3|4|5|6|7 |4|4|5|6|7|8
Result for "- 4"
|-|0|1|2|3|4 |:|:|:|:|:|: |0|0|-1|-2|-3|-4 |1|1|0|-1|-2|-3 |2|2|1|0|-1|-2 |3|3|2|1|0|-1 |4|4|3|2|1|0
1
u/lawlrng 0 1 Sep 15 '12 edited Sep 17 '12
Python 3.
And I just realized it only works for addition. Woops.Now fixed with horrible eval evil. =)Output:
|+|0|1|2|3|4 |:|:|:|:|:|: |0|0|1|2|3|4 |1|1|2|3|4|5 |2|2|3|4|5|6 |3|3|4|5|6|7 |4|4|5|6|7|8
1
u/5outh 1 0 Sep 15 '12 edited Sep 15 '12
Haskell:
output:
South:Dailies macbook$ ./dp98Easy "+" 4
|+|0|1|2|3|4 |:|:|:|:|:|: |0|0|1|2|3|4 |1|1|2|3|4|5 |2|2|3|4|5|6 |3|3|4|5|6|7 |4|4|5|6|7|8
South:Dailies macbook$ ./dp98Easy "*" 4
|*|0|1|2|3|4 |:|:|:|:|:|: |0|0|0|0|0|0 |1|0|1|2|3|4 |2|0|2|4|6|8 |3|0|3|6|9|12 |4|0|4|8|12|16
Doesn't work for division because of division by zero...but other than that it works fine.
1
u/tgkokk 0 0 Sep 15 '12
Python (output in reddit table format):
Output for + 8:
|+|0|1|2|3|4|5|6|7|8 |:|:|:|:|:|:|:|:|:|: |0|0|1|2|3|4|5|6|7|8 |1|1|2|3|4|5|6|7|8|9 |2|2|3|4|5|6|7|8|9|10 |3|3|4|5|6|7|8|9|10|11 |4|4|5|6|7|8|9|10|11|12 |5|5|6|7|8|9|10|11|12|13 |6|6|7|8|9|10|11|12|13|14 |7|7|8|9|10|11|12|13|14|15 |8|8|9|10|11|12|13|14|15|16
1
u/[deleted] Sep 17 '12
C#: (also my first daily challenge!)
0
u/_Daimon_ 1 1 Sep 15 '12
Anscci C
I used a and b in place of / and *, because my program just wouldn't recognize those two chars. I don't know why.
0
u/swarage 0 0 Sep 15 '12
ruby solution (http://pastebin.com/5PJ4q8iH) although the spacing bugs out a bit
0
u/zelf0gale Sep 17 '12
In Python
0
u/[deleted] Sep 17 '12 edited Sep 17 '12
C
Output:
0 | 0 0 0 0 0 0
1 | 0 1 2 3 4 5
2 | 0 2 4 6 8 10
3 | 0 3 6 9 12 15
4 | 0 4 8 12 16 20
5 | 0 5 10 15 20 25
0
u/seagullcanfly Sep 18 '12
Python: Mine works but would be wonky with formatting above 20 digits
0
u/[deleted] Sep 18 '12
0
u/Eddonarth Sep 18 '12
In Java:
Input: + 4
Output:
|+|0|1|2|3|4| |:|:|:|:|:|: |0|0|1|2|3|4 |1|1|2|3|4|5 |2|2|3|4|5|6 |3|3|4|5|6|7 |4|4|5|6|7|8
I don't know why doesn't work with '*', so it uses 'x' instead.
0
u/AsdfUser Sep 19 '12
C#, formatting works with numbers up to four digits, in the case of division it rounds to one decimal place:
-1
u/SquishyWizard Sep 16 '12 edited Sep 17 '12
C#
The output is quite redditable.
0
u/yentup 0 0 Sep 15 '12 edited Sep 15 '12
in Python:
Usage: $ python c98.py "+" 4