2
\$\begingroup\$

Objective

Your challenge is to write a program that, given a paragraph from a book, will output another program that prints out that paragraph. (See my example program in an answer)

Scoring

The link below is to a .txt file on GitHub containing 100 paragraphs to use your code on. Your score will be the product of (the size of your main program in bytes) and ((the mean size of the output programs produced by your main program for the 100 paragraphs) raised to the power of 1.5). The power of 1.5 is added into the formula to encourage the prioritization of the brevity of the programs produced more than the brevity of the main program.

https://github.com/ACertainArchangel/Program-Generator-Print-a-Paragraph.git

Information/Hints

  • Make it easy for yourself to unit test and please include your testing code. Feel free to modify my python testing script from the sample answer.
  • Do not attempt to reconstruct the paragraphs by scraping; the challenge is to generate them programmatically.
  • You may use the knowledge that all paragraphs are from "Alice in Wonderland" and "The Strange Case of Dr Jekyll and Mr Hyde" by taking advantage of recurring words in these books.
  • The main and produced programs do not have to be in the same language.
New contributor
ACertainArchangel is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
\$\endgroup\$
7
  • \$\begingroup\$ So a 1-byte program would score about 19000? \$\endgroup\$ Commented Sep 2 at 8:25
  • \$\begingroup\$ Is it fβ‚˜β‚α΅’β‚™Γ—(βˆ‘fβ‚’α΅€β‚œΓ·100)¹·⁡ or fβ‚˜β‚α΅’β‚™Γ—βˆ‘(fβ‚’α΅€β‚œΒΉΒ·β΅)Γ·100? \$\endgroup\$ Commented Sep 2 at 9:03
  • 1
    \$\begingroup\$ "The power of 1.5 is added into the formula to encourage the prioritization of the brevity of the programs produced more than the brevity of the main program." β€” Won't work. \$\endgroup\$ Commented Sep 2 at 9:11
  • \$\begingroup\$ I just added more parentheses to clarify the scoring mechanism. Thanks for asking. \$\endgroup\$ Commented Sep 2 at 14:52
  • 1
    \$\begingroup\$ Are we allowed to convert the smart quotes to ASCII? \$\endgroup\$ Commented Sep 2 at 17:07

4 Answers 4

6
\$\begingroup\$

APL producing HTML, score 10896.1

(Score is \$\text{len}(f_\text{main})Γ—(\sum_{i=1}^{100}{\text{len}(f_\text{main}(p_i))\over100})^{1.5}\$.)

⍞

This prompts for a line of input and outputs it verbatim (example). Since none of the paragraphs contain < or & or multiple consecutive spaces, then the output is valid HTML that will produce itself (a trivial quine).

\$\endgroup\$
2
\$\begingroup\$

05AB1E producing HTML, score 0

In 05AB1E, the empty program takes input and outputs it verbatim (example). Since none of the paragraphs contain < or & or multiple consecutive spaces, then the output is valid HTML that will produce itself (a trivial quine).

\$\endgroup\$
1
  • \$\begingroup\$ Can't get better than that. Well played. \$\endgroup\$ Commented Sep 3 at 21:31
2
\$\begingroup\$

A naive example in Python that produces Python β€” (168 bytes; score is 1667263.6473830533)

import zlib,base64;print(f"import zlib,base64;print(zlib.decompress(base64.b85decode('{base64.b85encode(zlib.compress(input().encode())).decode()}')).decode(),end='')")

Testing script (not part of challenge)

import subprocess
import io
import sys

with open("golf.py", "r") as f:
    size_of_program = len(f.read())

def run_program(cmd, input_str=""):
    result = subprocess.run(
        cmd,
        input=input_str.encode(),
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        shell=True
    )
    return result.stdout.decode()

def execute_python_with_stdout_capture(python_code, input_str=""):
    output_buffer = io.StringIO()
    sys.stdout = output_buffer
    exec(python_code, {'input': lambda: input_str})
    sys.stdout = sys.__stdout__
    captured_output = output_buffer.getvalue()
    output_buffer.close()
    return captured_output


if __name__ == "__main__":
    with open("para.txt", "r") as f:
        file_content = f.readlines()

    total = 0
    for line in file_content:
        line = line.strip()
        output = run_program("python3 golf.py", line)
        code_output = execute_python_with_stdout_capture(output)
        if code_output!=line:
            print(f"{code_output}\n{line}")
            raise ValueError("Mismatch! Your program did not output the expected result and does not qualify!")
        total+=len(output)
    score = size_of_program*((total/100)**1.5)

    output = run_program("python3 golf.py")
    print("Your program works! Score:", score)
```
New contributor
ACertainArchangel is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
\$\endgroup\$
1
\$\begingroup\$

Python 3.8 (pre-release), score 465379.4785138254 (39 bytes, Python producing Python)

If it ain't broke, don't fix it.

print(f"print('''{input()}''',end='')")

Testing script (not part of challenge, only tiny changes from OP's)

import subprocess
import io
import sys

with open("golf.py", "r") as f:
    size_of_program = len(f.read())

def run_program(cmd, input_str=""):
    result = subprocess.run(
        cmd,
        input=input_str.encode(),
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        shell=True
    )
    return result.stdout.decode()

def execute_python_with_stdout_capture(python_code, input_str=""):
    output_buffer = io.StringIO()
    sys.stdout = output_buffer
    exec(python_code, {'input': lambda: input_str})
    sys.stdout = sys.__stdout__
    captured_output = output_buffer.getvalue()
    output_buffer.close()
    return captured_output


if __name__ == "__main__":
    with open("para.txt", "r", encoding="utf-8") as f:
        file_content = f.readlines()

    total = 0
    for line in file_content:
        line = line.strip()
        output = run_program("python golf.py", line)
        code_output = execute_python_with_stdout_capture(output)
        if code_output!=line:
            print(f"{code_output}\n{line}")
            raise ValueError("Mismatch! Your program did not output the expected result and does not qualify!")
        total+=len(output)
    score = size_of_program*((total/100)**1.5)

    output = run_program("python golf.py")
    print("Your program works! Score:", score)
```
\$\endgroup\$

Your Answer

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.