"Write" a simple calculator without writing a single line of code
J, 7 questions/answers, none about J
echo a%b[echo a*b[echo a-b[echo a+b[b=:?2147483647 [a=:?2147483647
It's a pretty damn cheap way to do it, I'm not gonna lie. Here are the SO answers I used:
echo
This answer+
,-
,*
and%
This question?
This answer[
This answer=
and:
This community wiki question2147483647
This answerfoo
This answer
I renamed variable foo
as a
and b
in the code.
Python 2, 7 6 references
Creating this solution wasn't as easy as it looked. Searching Stack Overflow for specific code is difficult, since symbols are not included in the search.
I had found a way to do this with 2000-bit random numbers, using a different answer in place of Ref #1, but I couldn't test it on the online environments I use since it involves This could actually be used now, with TIO.getrandbits
, which calls os.urandom
, giving me a NotImplementedError
, so I went this way instead.
Try it online
#assumed to be loaded
import random
n1 = []
n1.append(random.randint(1, 100))
n2 = []
n2.append(random.randint(1, 100))
r1 = map(sum, zip(n1, n2))
r2 = map(lambda t: t[0] - t[1] ,zip(n1, n2))
ab = [n1[i]*n2[i] for i in range(len(n1))]
r1, last = r1[0], r1[-1]
r2, last = r2[0], r2[-1]
ab, last = ab[0], ab[-1]
n2, last = n2[0], n2[-1]
print r1
print r2
print ab
ab = float(ab) / n2
ab = float(ab) / n2
print ab
References
import random
is assumed to be loaded, since the question says that's allowed.
lst = []
andlst.append(random.randint(1, 100))
- Heremap(sum, zip(r1, r2))
,map(lambda t: t[0] - t[1] ,zip(r1, r2))
,r1
, andr2
- Hereresult = float(a) / b
- Hereab = [a[i]*b[i] for i in range(len(a))]
- Herefirst, last = some_list[0], some_list[-1]
- Hereprint x
- Here
Renamed
lst
renamed ton1
andn2
(Ref #1: I used the entire code twice)r1
andr2
renamed ton1
andn2
(Ref #2: I used the separate variables later though, to assign the maps and to divide in the last print, since the answer included them. )result
anda
renamed toab
, andb
renamed ton2
(Ref #3)a
andb
renamed ton1
andn2
(Ref #4)first
andsome_list
both renamed tor1
,r2
,ab
, orn2
, depending on which line. (Ref #5: I used this four times. Note that only the first assignment is used, so I don't renamelast
)x
is renamed tor1
,r2
, orab
, depending on which line. (Ref #6)