Monday, February 21, 2011

Self-Reproducing Python 3 Program

In response to Ken Thompson's challenge in the ACM Classic paper: Reflections on Trusting Trust, I've written a self-reproducing program using Python 3. The program saves a copy of its source sr.py in a directory d. I didn't take the time to shorten it, which is seemingly the popular thing to do with quines, but feel free to offer suggestions:



1 comment:

  1. Java, using FileIO tricks:

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;

    public class Test {
    private static final String NAME = "Test.java";
    private static final String OUTDIR = "caleb";
    public static void main(String[] args){
    try {
    BufferedReader read = new BufferedReader(new FileReader(NAME));

    File outDir = new File(OUTDIR);
    outDir.mkdirs();

    File newFile = new File(OUTDIR + File.separatorChar + NAME);
    newFile.createNewFile();

    PrintWriter out = new PrintWriter(new FileWriter(newFile));

    String line;

    while ((line = read.readLine()) != null){
    out.write(line + "\n");
    }
    out.close();
    read.close();


    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }

    ReplyDelete