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:
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.
You may adjust indentation appropriately.
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.
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.You must supply an explicit list of any variable/method/function/class renamings performed.
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.
-
\$\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\$izzyg– izzyg2015年07月26日 13:28:50 +00:00Commented Jul 26, 2015 at 13:28
-
\$\begingroup\$ Hi @isaacg, thank you. I'll have a look and delete/edit this one. \$\endgroup\$baao– baao2015年07月26日 13:30:24 +00:00Commented 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\$baao– baao2015年07月26日 13:48:24 +00:00Commented 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\$izzyg– izzyg2015年07月26日 13:49:11 +00:00Commented 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\$Fatalize– Fatalize2015年07月26日 16:04:42 +00:00Commented Jul 26, 2015 at 16:04
3 Answers 3
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=
and:
This community wiki question2147483647
This answerfoo
This answer
I renamed variable foo
as a
and b
in the code.
-
\$\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\$JohnE– JohnE2015年07月26日 16:23:58 +00:00Commented Jul 26, 2015 at 16:23
-
1\$\begingroup\$ @JohnE That's the advantage of being ASCII noise! \$\endgroup\$Fatalize– Fatalize2015年07月26日 16:26:55 +00:00Commented 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\$unclemeat– unclemeat2015年07月27日 00:06:30 +00:00Commented 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\$Fatalize– Fatalize2015年07月27日 06:45:30 +00:00Commented Jul 27, 2015 at 6:45
-
\$\begingroup\$ All of these come from complete code blocks - take a look at the sources. \$\endgroup\$Sean Latham– Sean Latham2015年07月27日 08:09:45 +00:00Commented Jul 27, 2015 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 This could actually be used now, with TIO.getrandbits
, which calls os.urandom
, giving me a NotImplementedError
, so I went this way instead. (削除ここまで)
#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)
Decimal, 2 references
82D82D00D30001D30041D301212010D301200D30001D30042D301212010D301200D30001D30043D301212010D301200D30001D30044D30122
Commands used:
0
SET (default stack index)1
PUSH2
CHAR
2
POP3
I/O00
duplicate stack01
from stack to STDOUT
4
MATH1
ADD2
SUBTRACT3
MULTIPLY4
DIVIDE
8
BUILTIN2
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:
- https://stackoverflow.com/a/473309/6850771 -
d
(capitalized) - https://stackoverflow.com/q/15405966/6850771 -
0
through9
Try it online! You'll need to disable the output cache if it's not automatically disabled.
Explore related questions
See similar questions with these tags.