26
\$\begingroup\$

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.

mbomb007
23.6k7 gold badges66 silver badges142 bronze badges
asked Jul 26, 2015 at 13:18
\$\endgroup\$
8
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Jul 26, 2015 at 13:28
  • \$\begingroup\$ Hi @isaacg, thank you. I'll have a look and delete/edit this one. \$\endgroup\$ Commented Jul 26, 2015 at 13:30
  • \$\begingroup\$ @isaacg, hope it's okay now. You are more experienced than me here, is the deadline too close/far? \$\endgroup\$ Commented Jul 26, 2015 at 13:48
  • \$\begingroup\$ It's much better. The deadline is much too close however, standard is about 1-2 weeks. \$\endgroup\$ Commented Jul 26, 2015 at 13:49
  • \$\begingroup\$ I'm waiting for the one guy that will go through all the ridiculous RegEx questions on SO to get a working Retina answer... \$\endgroup\$ Commented Jul 26, 2015 at 16:04

3 Answers 3

18
\$\begingroup\$

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.

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

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)

answered Jul 28, 2015 at 19:14
\$\endgroup\$
1
\$\begingroup\$

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.

answered Jun 5, 2017 at 2:11
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.