A Silly Task
October 22, 2019
Today’s task is silly:
Your task is to write a program that, when executed, writes to the output in words the number of characters in the program. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
This time – no cheating and no real golfing…. OK – so works up to about 100K as I haven’t got the and/, rule right above that …. The critical bits are “-s $0” gives the size of the current file!, and stuff after END is ignored – added to test code for different lengths…
Here is a very naive solution in R7RS Scheme that I feel is more in
keeping with the spirit of the question, which I interpreted in the
vein of Quines.
With that interpretation (which I realize may not be the author’s
intention), the solutions so far by @programmingpraxis and James Smith
are typically considered ‘”Cheating” quines”:
https://en.wikipedia.org/wiki/Quine_(computing)#%22Cheating%22_quines
Let’s use the excellent Python num2words library to make things easier. Since num2words is multilingual, and in the interests of international solidarity, we’ll generate solutions of the form “print(…)” in a variety of languages:
Output (note that we check the encoded length, not the number of characters):
Here’s a solution in Python.
Output:
Here’s a Python meta solution (ie. run the program to make a program), no libraries, no cheating. Iterate to a fix point to find the right length (proof of termination welcome):
Now we have:
A solution in Common Lisp using the loop syntax and built-in english language number formatting (“~r”).