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.

  • 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 Jul 26 '15 at 13:28
  • Hi @isaacg, thank you. I'll have a look and delete/edit this one. – baao Jul 26 '15 at 13:30
  • @isaacg, hope it's okay now. You are more experienced than me here, is the deadline too close/far? – baao Jul 26 '15 at 13:48
  • It's much better. The deadline is much too close however, standard is about 1-2 weeks. – isaacg Jul 26 '15 at 13:49
  • Ok, thanks again. – baao Jul 26 '15 at 13:50
up vote 15 down vote accepted

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.

  • 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 Jul 26 '15 at 16:23
  • 1
    @JohnE That's the advantage of being ASCII noise! – Fatalize Jul 26 '15 at 16:26
  • 1
    I like this answer, but the question states "You can't take snippets of snippets" - which this is doing, right? – unclemeat Jul 27 '15 at 0:06
  • 2
    @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 Jul 27 '15 at 6:45
  • All of these come from complete code blocks - take a look at the sources. – Sean Latham Jul 27 '15 at 8:09

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)

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.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.