Duct tape can fix anything

13

1

Overview

This challenge was inspired by this StackOverflow Meta post. In it, @hakre says:

I've seen a project really written by Stackoverflow (a noob copies together from SO here and SO there), attribution normally never is done which results in rights termination under CC. (sic)

Your mission is to make the most interesting program you can -- without ever writing a single line of code. You can only use code which is already written for you, both in questions and answers from StackOverflow. All you can do is duct tape it all together the best you can. And of course, it's very important that we provide appropriate attribution so that we don't get our rights terminated.


Rules

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

    a. 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.)

    b. You may adjust indentation appropriately.

    c. 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.

    d. 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.

  3. You must supply an explicit list of any variable/method/function/class rename-ings performed.

  4. You can't take snippets of snippets (meaning if you take a code block from a post, you take the whole thing)
  5. Provide a brief description of what the code does for people who aren't intimate with the language you're using.
  6. Popularity contest, so the most upvotes wins!

Boring Five Minute Example (Python 3)

Description: Reads a bunch of log files from the current directory and randomizes the lines in them (exciting, right? This would totally win a popularity contest)

Source

import glob
import random

for file in glob.glob('log-*-*.txt'):
    # Etc.
    with open(file) as f:
        content = f.readlines()
        # shuffle the list in place 
        random.shuffle(content)

        # print it
        print(content)

Legal Modifications

  • Renamed fname to file from link 1
  • Renamed b to content from link 2

Attributions

  1. https://stackoverflow.com/a/3277516/1435657
  2. https://stackoverflow.com/a/16737991/1435657
  3. https://stackoverflow.com/a/4918520/1435657

asteri

Posted 2014-05-08T01:28:28.910

Reputation: 824

Question was closed 2014-05-11T05:53:40.167

8I really like the idea, but I think this question would immensely benefit from limiting the scope to a particular task. That would a) inspire more creativity because you can't just copy together anything and b) it would leave open the possibility to reuse this (interesting) type of question later on in a different context. If you just leave it as "do whatever you want", all further questions like this will be duplicates. I'm afraid as it stands, I have to close-vote as "too broad" because it torpedoes possibly interesting future questions. – Martin Ender – 2014-05-08T01:33:38.273

Not to mention that someone could just say, "Print "hello world" and be done – TheDoctor – 2014-05-08T01:34:40.493

@m.buettner Hmm... fair enough. I don't have any good ideas for what to limit the scope to, though, I'm afraid. – asteri – 2014-05-08T01:35:00.393

Couldn't I just take a Brainfuck program, and "reuse" the +-<>,.[] characters to form a new program? – marinus – 2014-05-08T01:35:04.070

@TheDoctor I doubt that that would win a popularity contest. – asteri – 2014-05-08T01:35:48.093

2I should add, no I don't want this to become the next code-trolling, where everyone posts a "Add two numbers", "Split this string", with a "duct-tape-coding" tag. I think we can handle this more creatively than that. I would just like to leave some room for other interpretations of this kind of restricted-source competition. – Martin Ender – 2014-05-08T01:35:56.013

1@m.buettner I'm also not sure that CodeGolf.SE would necessarily benefit from lots of questions like this, for the same reasons you mention: why have "split a string using SO code", "get a random number from 1-100 using SO code", and so forth? But that's just my opinion. I'm far from a CodeGolf.SE pro. :) – asteri – 2014-05-08T01:41:00.177

3I'm going to remove the random custom tag for now because the need for it hasn't been established (and it will very likely be unnecessary and add no value). – asteri – 2014-05-08T02:16:41.783

@marinus You can't reuse characters from a post. You have to use the entire block of code. So I don't think so, no. :) – asteri – 2014-05-08T03:31:10.063

I dunno, the "no snippets of snippets" rule seems like it could be a little too restricting. I know the intention is to stop them from just copying one character from a bunch of places to write anything they want, so why not change "no snippets of snippets", to "you can make snippets of snippets, but only if it consists of complete lines". – Braden Best – 2014-05-08T03:44:56.397

@B1KMusic I think the challenge becomes too easy if you can pull out any lines from any posts. I think it's important that the "piecing together" remain the focal point of the challenge rather than rewarding a person who spends the time to look up 200 SO posts, takes a line from each of them, and makes a comprehensive program from it. The latter makes the challenge both extremely trivial and, more importantly, extremely boring. – asteri – 2014-05-08T03:48:00.883

1I agree that we need to be careful with flooding PPCG with questions like this, just because it's fun and novel. But I think a few genuinely different questions could come of this... like a maths-based, a graphics-based one, one that uses code-golf scoring and a polyglot challenge maybe. Hence, I'd prefer the scope to be limited. In terms of people just copying the challenge without anything substantial, I think it's the duty of the community to be a bit stricter in "closing as duplicate" when it comes to new challenge types. – Martin Ender – 2014-05-08T09:45:02.177

Answers

8

C - one operation calculator

Let's have this for a start.
It's actually quite hard to find pieces of C programs that aren't whole programs. I had most trouble with declarations.

// http://stackoverflow.com/a/2911978 Block #1 (changed i to a)
static int a = 10;
// http://stackoverflow.com/a/2911978 Block #1 (changed i to b)
static int b = 10;
// http://stackoverflow.com/a/442647 Block #1 (changed c to op)
unsigned char op = (unsigned char)-1;
// http://stackoverflow.com/q/3711048 Inline block #1
int main()
// http://stackoverflow.com/a/14902422 Inline block #1
{
// http://stackoverflow.com/q/217074 Inline block #2 (changed i to a)
scanf("%d", &a)
// http://stackoverflow.com/a/23353870 Inline block #2
;
// http://stackoverflow.com/q/217074 Block #2 (changed c to op)
scanf("%c", &op)
// http://stackoverflow.com/a/23353870 Inline block #2
;
// http://stackoverflow.com/q/217074 Inline block #2 (changed i to b)
scanf("%d", &b)
// http://stackoverflow.com/a/23353870 Inline block #2
;
// http://stackoverflow.com/a/4173188 Block #2
int result = 0;
result = (op == '*') ? a * b : result;
result = (op == '/') ? a / b : result;
result = (op == '+') ? a + b : result;
result = (op == '-') ? a - b : result;
printf("%d",result);
// http://stackoverflow.com/a/3245525 Block #2
printf("\n");  /* prints newline */
// http://stackoverflow.com/q/4138649 Inline block #1
return 0;
// http://stackoverflow.com/a/14902422 Inline block #2
}

Takes a simple integral operation like 3+151 or 41/2 on stdin and outputs the result to stdout.

mniip

Posted 2014-05-08T01:28:28.910

Reputation: 9 396

1Interesting re-use of the same code block and renaming the variable twice. I didn't even think of that! +1 – asteri – 2014-05-08T13:04:27.113