"Write" a simple calculator without writing a single line of code

25

It's very easy:

Create a program without writing a single line of code. The code may only consist of existing Stack Overflow questions and answers.

The program should create two random numbers and output the numbers

  • added to,
  • subtracted from,
  • multiplied with and
  • divided by

each other.

Rules

You must include links to the questions/answers you used in your answer. You may not modify the code you find, with the following exceptions:

  1. You may rename variables, functions, and methods. (This doesn't mean you can change a method invocation, by changing, say scanner.nextInt() to scanner.nextLine() and claiming that you're changing a method name. The change must be in the definition or reference to the same entity.). The solution should still work if variables, functions or methods would be renamed again.

  2. You may adjust indentation appropriately.

  3. You can assume that the proper modules are loaded for the code snippets to work. (e.g., import statements for Java and Python, using statements for C# and C++, and their equivalents in all languages) If the code snippet includes the imports for you, you can move them to the top of the code.

  4. If the language requires the code to be in some kind of method body to execute (e.g., public static void main(String[] args) for Java, static int Main(string[] args) for C#, and so on), you can wrap your code in the appropriate method. But the contents of that main method must remain unmodified.

  5. You must supply an explicit list of any variable/method/function/class renamings performed.

  6. You can't take snippets of snippets (meaning if you take a code block from a post, you take the whole thing) Provide a brief description of what the code does for people who aren't intimate with the language you're using. You must use snippets posted before this challenge was started.

Popularity contest, so the most upvotes wins!

Deadline

I will accept the the submission that has the most votes around the end of August, 6.

baao

Posted 2015-07-26T13:18:22.397

Reputation: 369

Hi michael, and welcome to PPCG stack exchange! One of the rules for code challenges on this site is that they must have an objective winning condition, so you should pick such a condition for this one. This is a similar challenge which might give you some ideas.

– isaacg – 2015-07-26T13:28:50.263

Hi @isaacg, thank you. I'll have a look and delete/edit this one. – baao – 2015-07-26T13:30:24.723

@isaacg, hope it's okay now. You are more experienced than me here, is the deadline too close/far? – baao – 2015-07-26T13:48:24.830

It's much better. The deadline is much too close however, standard is about 1-2 weeks. – isaacg – 2015-07-26T13:49:11.510

Ok, thanks again. – baao – 2015-07-26T13:50:00.853

I'm waiting for the one guy that will go through all the ridiculous RegEx questions on SO to get a working Retina answer... – Fatalize – 2015-07-26T16:04:42.440

Haha, me too. @Fatalize – baao – 2015-07-26T16:05:14.477

Are we allowed to use both answers AND questions? Or just answers? – mbomb007 – 2015-07-28T15:57:55.293

You can use both! – baao – 2015-07-28T16:05:30.380

Answers

17

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:

I renamed variable foo as a and b in the code.

Fatalize

Posted 2015-07-26T13:18:22.397

Reputation: 32 976

I imagine taking this approach would be much harder in APL, but there's still a good chance of finding the characters in a code block by themselves. – JohnE – 2015-07-26T16:23:58.800

1@JohnE That's the advantage of being ASCII noise! – Fatalize – 2015-07-26T16:26:55.857

1I like this answer, but the question states "You can't take snippets of snippets" - which this is doing, right? – unclemeat – 2015-07-27T00:06:30.273

3@unclemeat "if you take a code block from a post, you take the whole thing". To me it sounds like if there are multiple code blocks in a post, you can take just one, but you can't take part of a code block. – Fatalize – 2015-07-27T06:45:30.350

All of these come from complete code blocks - take a look at the sources. – Sean Latham – 2015-07-27T08:09:45.223

Using other languages answers to get the keywords is kind of genius... – RedPanda – 2015-07-27T10:09:28.123

@mbomb007 "You must include links to the questions/answers you used in your answer.". I actually didn't see that questions were not included in that first sentence, but they are further down. So I don't know for sure if this fits what OP wants. – Fatalize – 2015-07-28T15:55:44.670

7

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 getrandbits, which calls os.urandom, giving me a NotImplementedError, so I went this way instead. This could actually be used now, with TIO.

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.

  1. lst = [] and lst.append(random.randint(1, 100)) - Here

  2. map(sum, zip(r1, r2)), map(lambda t: t[0] - t[1] ,zip(r1, r2)), r1, and r2 - Here

  3. result = float(a) / b - Here

  4. ab = [a[i]*b[i] for i in range(len(a))] - Here

  5. first, last = some_list[0], some_list[-1] - Here

  6. print x - Here

Renamed

  1. lst renamed to n1 and n2 (Ref #1: I used the entire code twice)

  2. r1 and r2 renamed to n1 and n2 (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. )

  3. result and a renamed to ab, and b renamed to n2 (Ref #3)

  4. a and b renamed to n1 and n2 (Ref #4)

  5. first and some_list both renamed to r1, r2, ab, or n2, depending on which line. (Ref #5: I used this four times. Note that only the first assignment is used, so I don't rename last)

  6. x is renamed to r1, r2, or ab, depending on which line. (Ref #6)

mbomb007

Posted 2015-07-26T13:18:22.397

Reputation: 21 944

1

Decimal, 2 references

82D82D00D30001D30041D301212010D301200D30001D30042D301212010D301200D30001D30043D301212010D301200D30001D30044D30122

Commands used:

  • 0 SET (default stack index)
  • 1 PUSH
    • 2 CHAR
  • 2 POP
  • 3 I/O
    • 00 duplicate stack
    • 01 from stack to STDOUT
  • 4 MATH
    • 1 ADD
    • 2 SUBTRACT
    • 3 MULTIPLY
    • 4 DIVIDE
  • 8 BUILTIN
    • 2 push random integer to stack

Explained version:

82D       ; push random INT    - stack contains {r1}
82D       ; push random INT    - stack contains {r1, r2}

00D300    ; duplicate stack[0] - stack contains {r1, r2, r1}
01D300    ; duplicate stack[1] - stack contains {r1, r2, r1, r2}
41D       ; math +             - stack contains {r1, r2, r1+r2}
301       ; print from stack to output
2         ; pop                - stack contains {r1, r2}

12010D3012; print newline

00D300    ; duplicate stack[0] - stack contains {r1, r2, r1}
01D300    ; duplicate stack[1] - stack contains {r1, r2, r1, r2}
42D       ; math -             - stack contains {r1, r2, r1-r2}
301       ; print from stack to output
2         ; pop                - stack contains {r1, r2}

12010D3012; print newline

00D300    ; duplicate stack[0] - stack contains {r1, r2, r1}
01D300    ; duplicate stack[1] - stack contains {r1, r2, r1, r2}
43D       ; math *             - stack contains {r1, r2, r1*r2}
301       ; print from stack to output
2         ;                    - stack contains {r1, r2}

12010D3012; print newline

00D300    ; duplicate stack[0] - stack contains {r1, r2, r1}
01D300    ; duplicate stack[1] - stack contains {r1, r2, r1, r2}
44D       ; math /             - stack contains {r1, r2, r1/r2}
301       ; print from stack to output

Sources:

Try it online! You'll need to disable the output cache if it's not automatically disabled.

MD XF

Posted 2015-07-26T13:18:22.397

Reputation: 11 605