568
\$\begingroup\$

This "sandbox" is a place where Code Golf users can get feedback on prospective challenges they wish to post to main. This is useful because writing a clear and fully specified challenge on your first try can be difficult, and there is a much better chance of your challenge being well received if you post it in the sandbox first.

Sandbox FAQ

Posting

To post to the sandbox, scroll to the bottom of this page and click "Answer This Question". Click "OK" when it asks if you really want to add another answer.

Write your challenge just as you would when actually posting it, though you can optionally add a title at the top. You may also add some notes about specific things you would like to clarify before posting it. Other users will help you improve your challenge by rating and discussing it.

When you think your challenge is ready for the public, go ahead and post it, and replace the post here with a link to the challenge and delete the sandbox post.

Discussion

The purpose of the sandbox is to give and receive feedback on posts. If you want to, feel free to give feedback to any posts you see here. Important things to comment about can include:

  • Parts of the challenge you found unclear
  • Comments addressing specific points mentioned in the proposal
  • Problems that could make the challenge uninteresting or unfit for the site

You don't need any qualifications to review sandbox posts. The target audience of most of these challenges is code golfers like you, so anything you find unclear will probably be unclear to others.

If you think one of your posts requires more feedback, but it's been ignored, you can ask for feedback in The Nineteenth Byte. It's not only allowed, but highly recommended! Be patient and try not to nag people though, you might have to ask multiple times.

It is recommended to leave your posts in the sandbox for at least several days, and until it receives upvotes and any feedback has been addressed.

Other

Search the sandbox / Browse your pending proposals

The sandbox works best if you sort posts by active.

To add an inline tag to a proposal, use shortcut link syntax with a prefix: [tag:king-of-the-hill]. To search for posts with a certain tag, include the name in quotes: "king-of-the-hill".

\$\endgroup\$
0

4564 Answers 4564

1
146 147
148
149 150
153
-2
\$\begingroup\$

Given a positive even integer \$n\$, generate a random Brainfuck snippet of length \$n\$, containing only +-<>, that do no modify to the tape or tapehead.

To avoid random generation and try again or fallback into a trivial nop for invalid nops, your solution should run in polynomial time, and the ratio of possibility returning any two nops should be below polynomial.

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

Solve any NPC problem. Shortest code win.

Sandbox Notes

  • Will every submission tend to single NPC problem?
  • How many builtins are known to solve this in Mathematica?
  • Do 0-byte solution exist?
\$\endgroup\$
3
  • 3
    \$\begingroup\$ I think this is too broad to be a good challenge \$\endgroup\$
    – pxeger
    May 4, 2021 at 12:44
  • \$\begingroup\$ Dyalog Extended can solve it in two bytes: ⌂X (Knuth's X algorithm which solves the Exact Cover problem). \$\endgroup\$
    – Bubbler
    May 6, 2021 at 8:59
  • \$\begingroup\$ @Bubbler Lots of language will have builtin for this question I guess \$\endgroup\$
    – l4m2
    May 6, 2021 at 17:45
-2
\$\begingroup\$

Compress La Campanella.

Notice that music theory may give you more rules than general compress give, but I don't know music theory that much, so I won't post this

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

Self-Obfuscator Program

According to Wikipedia,

obfuscation is the deliberate act of creating source or machine code that is difficult for humans to understand.

In this challenge, you need to create a program that'll be able to obfuscate itself and produce a program with the same functionality, using the shortest amount of bytes required.

Rules

  • Standard loopholes are prohibited.
  • The output must always be consistent for each input. If you take the original code (iteration 0) and run it through itself 10 times to get a very long obfuscated code (iteration 10), running the initial code through it should give a code identical to iteration 1 code.
  • The output must be at least twice as long from the input for each iteration.

Obfuscated, not verbose

  • The obfuscation process must make the code longer, but it mustn't add any comments, no-op or no-effect statements, or any statements or expressions that don't directly affect the output code.
  • The output should have minimal resemblance to the input. No sequence of 5 bytes should repeat in the output.
  • The code must be able to obfuscate itself, obfuscations of itself from further ahead iterations and code from earlier iterations. It's not required to be able to obfuscate anything else.
\$\endgroup\$
2
  • 4
    \$\begingroup\$ Bonus in code golf is highly discouraged. And "the output should have minimal resemblance to the input" should be more rigorously defined. \$\endgroup\$
    – Bubbler
    Nov 13, 2021 at 23:19
  • \$\begingroup\$ The idea is fine, but you need more detailed and cleaner define about obsfucate and the meaning of statement that don't directly affect the output code \$\endgroup\$
    – okie
    Nov 22, 2021 at 8:43
-2
\$\begingroup\$

The number of alphabets in 3 seconds

In 3 seconds, output as many alphabets as possible. The output may be separated by consistent character.

An alphabet here is this:

abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

Meta

  • Is this clear?
  • I'm not really sure about the tagging.
\$\endgroup\$
2
  • \$\begingroup\$ This should be closed, because machine code can do this with a lot of "A" prints without loops and win. this is off-topic \$\endgroup\$
    – Fmbalbuena
    Jan 10, 2022 at 15:50
  • 1
    \$\begingroup\$ I don't think this is off-topic as it have a clear "objective winning criteria". But I am afraid that this question may be marked as a duplicate to codegolf.stackexchange.com/questions/215216/… \$\endgroup\$
    – tsh
    Jan 11, 2022 at 6:51
-2
\$\begingroup\$

TDG - Test Driven Golf

Our company requires unit testing before code can be deployed to production. Unfortunately, my code is written in esoteric programming languages, none of which have test runners.

Please help me get to production!

Challenge

Write a test runner for a programming language (here after called Language X) that takes Language X source code and Language X test cases then outputs the results of running the test cases.

NOTE: The language used to write the test runner does not necessarily have to be Language X.

Specification

Input

The runner should take in:

  • Code written in Language X
  • Test cases written in Language X

Output

The runner should produce:

  • At least a list of test cases. For each test case, include:
    • The test case name
    • A Truthy value if the code executes without errors and no assertions failed
    • A Falsey value if the code errors or an assertion failed

Test Runner

  • The test runner can be written in any language. The only constraint is that the source and test code language be the same
  • Each test runner must at least provide a helper/utility/function to assert that a value is truthy
    • Assert.True(false) => Fails test
    • assertTrue(1 == 3) => Fails test
    • is_truthy 1 => Doesn't fail
    • [[ "a" = "a" ]] => Doesn't fail

Test Cases

  • Each test case must have a name/be name-able
    • it("does stuff", _ => ...) => it does stuff
    • it_does_stuff = _ => ... => it_does_stuff
    • func ItDoesStuff() {...} => ItDoesStuff
  • A code block of language X code

Submissions

For consistency, here are some recommendations:

  • Title should be: # Language X runner written in Language Y
    • Where Language X is the source and test code language
    • And Language Y is the language the runner is written in
  • Runner code in a ## Runner section
  • Example input source code in a ## Example Source section
  • Example input test code in a ## Example Tests section
  • Example output in a ## Example Output section

Sandbox Questions

  • Is this challenge too board?
  • Is this challenge too big?
  • I've created some basic test runners in a single file before and with some elbow grease, I think you could say small (submit-able on CGCC) and add a few nice things
  • Does fit here?
  • Is there room for creativity?
    • The testing syntax and features for the test cases are left up to the implementation
      • How golf-able the test syntax is
      • How the code and test cases interact
      • What asserts/helpers are provided
      • Syntax for skipping test cases
      • Syntax for beforeEach/afterEach
      • Harder features like parameterized tests and fixtures are possible
    • Similarly, the features for the test runner are left pretty open ended
      • TAP output
      • JUnit output
      • Test isolation and parallelization
      • Harder features like specific failure messages and code coverage aren't impossible
\$\endgroup\$
4
  • \$\begingroup\$ "Input: Source code written in language X, Test cases written in language X" I don't understand. Are we providing the input to our own programs? Or are we meant to detect the language? \$\endgroup\$ Jan 21, 2022 at 20:02
  • \$\begingroup\$ This doesn't make sense as a pop-con, there's very little room for creativity \$\endgroup\$ Jan 21, 2022 at 21:55
  • \$\begingroup\$ @thejonymyster, I've added some clarity (hopefully). The test runner should be for testing a specific language (no detecting) and it would take in some code (not it's own source) and some tests in that language. \$\endgroup\$ Jan 22, 2022 at 2:31
  • \$\begingroup\$ @cairdcoinheringaahing, there is a fair spectrum of test runners and testing syntaxes out there. So I was thinking there was some room for creativity (especially since I don't think any of them are made for golfing). I've added some comments to that end. I couldn't think of a better tag \$\endgroup\$ Jan 22, 2022 at 2:37
-2
\$\begingroup\$

Alternate brackets

Given a matched string containing only ()[]{}, apply the following:

  • If [] is next to () or {}, replace the () or {} with [].
  • If a () follows [] or {}, remove the ().

Test cases

()[]{} => [][][]
{}(){} => (){}
\$\endgroup\$
1
  • 1
    \$\begingroup\$ This doesn't have a scoring criterion, and is also in need of some more explaining - if we have [](), which rule should apply?- The first is second? Your second test case also makes no sense as it doesn't follow either of the rules... \$\endgroup\$
    – Lecdi
    Apr 1, 2022 at 9:05
-2
\$\begingroup\$

Task

Make a program that finds the first IP address that responds to pings after a certain IP address.

Requirements

Your program should take an IP address as a string in the form xx.xx.xx.xx. It should find the first IP greater than the input (incrementing least significant byte first) that responds to pings, and output it in the form xx.xx.xx.xx.

Scoring

This is code-golf, so shortest answer in bytes wins.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ This... feels morally unsafe. Although responding to a ping is generally not-malicious, IP scrapping doesn't feel right to me. \$\endgroup\$
    – ATaco
    May 19, 2022 at 2:24
-2
\$\begingroup\$

Write a program on any language that accepts some input and give some different output. The challenge is that the text of the program must form a square: all rows must be the same length and the number of rows must equal the number of columns (ie length of rows). Adding extra spaces, dead code, or comments to pad lines is not allowed.

This is Code Golf so the one with the shortest functioning program (ie smallest square) wins!

\$\endgroup\$
2
  • \$\begingroup\$ First I would recommend adding title using # markdown. Then adding tags code-golf and source-layout using [tag:name] markdown. Also you should probably specify output a bit better. Does it need to be different for every input or can it be always same? Also in some programming languages spaces are actually instructions. \$\endgroup\$
    – Jiří
    Aug 7, 2022 at 20:37
  • \$\begingroup\$ I believe a square code challenge has been done. Also, this is ultimately about the features of the language rather than any particular skill of the programmer. The minimum score is the length of whatever the language's equivalent of "print" is. \$\endgroup\$
    – Xcali
    Aug 8, 2022 at 2:28
-2
\$\begingroup\$

Self-Debugger.

Suddenly, someone comes and deletes a character from the program. From analyzing the output and error messages (if any), you quickly know which character was deleted.

Your job is to create a function that does this. For every character deletion, you will get one point if feeding the output and error message(s) to the undamaged function does not give the correct deleted character. You attempt to minimize the score of your program. Your program must have a function, i.e. if must not be empty.

Default loopholes forbidden, as always.

Example program:

lambda a,aa:'a'

Score: the program is not correct when we take away a character that is not a. Thus, the score is 9. (We intend to minimize this score.)

\$\endgroup\$
14
  • \$\begingroup\$ Meta: we need tags!!! \$\endgroup\$ Aug 26, 2022 at 7:48
  • \$\begingroup\$ Why even take the stdout as input? What could you possibly do with this info that would improve your score? \$\endgroup\$
    – mousetail
    Aug 26, 2022 at 8:34
  • \$\begingroup\$ stdout could be useful \$\endgroup\$ Aug 26, 2022 at 9:57
  • \$\begingroup\$ The entire second paragraph makes no sense to me \$\endgroup\$
    – mousetail
    Aug 26, 2022 at 10:48
  • \$\begingroup\$ Ouch, let me explain it a bit more \$\endgroup\$ Aug 26, 2022 at 10:56
  • \$\begingroup\$ Done, @mousetail \$\endgroup\$ Aug 26, 2022 at 14:00
  • \$\begingroup\$ Ok I see what you are going for, fix the name and remove the first sentence though since they have nothing to do with the challenge \$\endgroup\$
    – mousetail
    Aug 26, 2022 at 14:01
  • \$\begingroup\$ Now?sdkvwoefjowejfojweojfowepjfpowejpofwe \$\endgroup\$ Aug 26, 2022 at 14:06
  • \$\begingroup\$ I'd name it what the challenge actually is "from the output and error determine what character was removed from your code" \$\endgroup\$
    – mousetail
    Aug 26, 2022 at 14:08
  • \$\begingroup\$ But I think it is too long \$\endgroup\$ Aug 26, 2022 at 14:09
  • \$\begingroup\$ Let us continue this discussion in chat. \$\endgroup\$ Aug 26, 2022 at 14:09
  • \$\begingroup\$ Dupe of this? \$\endgroup\$ Aug 26, 2022 at 14:10
  • \$\begingroup\$ No. We give the unradiated program the output and error of the radiated program. \$\endgroup\$ Aug 26, 2022 at 14:18
  • \$\begingroup\$ Ohh so it's more of a narcisist type thing, that makes sense. Interesting \$\endgroup\$ Aug 26, 2022 at 18:40
-2
\$\begingroup\$

🤖 Robotics competition

You're entering a robotics competition! There are 3 types of parts for you to choose for your robot: wheels, legs and sensors. A robot:

  • can have up to 8 parts in total
  • can only have 4 legs
  • must have at least 2 parts
  • must have at least one leg or one wheel

You can't decide what robot to build, so you'll just make all of the possible ones. How many robots will you build?

The challenge

Write a program which calculates the number of possible robots (the answer is 134). Shortest program wins.

The output of your program can be its return code. The program needn't take any input (undefined behaviour if input is provided).

Bonus swag points for a point-free solution in APL 😉

Example

A non-competitive python example:

from itertools import chain, combinations_with_replacement

options = list(filter(
    lambda option: \
        len(option) >= 2 and \
        option.count('leg') <= 4 and \
        (option.count('leg') + option.count('wheel') > 0),
    chain(*(combinations_with_replacement(('leg', 'wheel', 'sensor'), j) for j in range(9)))
))

for option in options:
    print(option)
print("Number of valid options: {}".format(len(options)))

META

  • Is the question original enough, or has there been any challenges like this
  • Is the python example a useful thing to include?
\$\endgroup\$
2
  • \$\begingroup\$ Seems like print(134) would be a valid answer. Challenges that require no input need to be careful to be more difficult than just printing the answer. Limiting answers to just one specific strategy is also discouraged since it reduced the creative freedom and subjective rules are annoying when trying to get the best possible answer. \$\endgroup\$
    – mousetail
    Sep 7, 2022 at 11:45
  • \$\begingroup\$ @mousetail thanks for the input! :)) i'll have a think on how to modify the challenge and see if i can make it work. cheers. \$\endgroup\$ Sep 7, 2022 at 11:52
-2
\$\begingroup\$

ABACABA pattern without recursion

ABACABA pattern on Wikipedia

Taking a number n as input, generate the ABACABA pattern with n symbols as stated in the above page, without using any form of recursion in your code. You can use any output method; characters, a list of numbers, an image, whatever is possible.

If taking input is not possible in your programming language, output the pattern forever. E.g.: ABACABADABACABAE...

Test cases:

input => output

1 => A / 1
2 => ABA / 1, 2, 1
3 => ABACABA / 1, 2, 1, 3, 1, 2, 1
4 => ABACABADABACABA / 1, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3, 1, 2, 1
5 => ABACABADABACABAEABACABADABACABA / 1, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3, 1, 2, 1, 5, 1, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3, 1, 2, 1
```
\$\endgroup\$
3
  • \$\begingroup\$ Please add some information on what exactly the ABACABA pattern is. \$\endgroup\$
    – Ginger
    Jan 7 at 19:25
  • 1
    \$\begingroup\$ Also, "no recursion" is an unobservable requirement. \$\endgroup\$
    – Ginger
    Jan 7 at 19:26
  • \$\begingroup\$ Related \$\endgroup\$
    – l4m2
    Jan 13 at 4:56
-2
\$\begingroup\$

Detect if a variable is declared

Create an expression which detects if a variable (named whatever you wish) has been declared yet. For languages that set undeclared variables to a type that can not be differentiated from a declared variable set to that type (e.g. nil in Lua), just detecting that type is enough. If the variable exists, the expression should return a truthy value, and if it doesn't it should return a falsy value. It should not output anything.

For example, the following expression in D detects if a variable named x has been declared in the current scope or not:

__traits(compiles, x)

PS: this challenge definitely wouldn't work for stack-based or assembly languages, and I'm also not sure if marking this as code-golf or popularity-contest would be better.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ code-golf is nearly always better. popularity-contests are really hard to write properly. Also, note that some stack based languages do have variables \$\endgroup\$
    – Seggan
    Jan 21 at 22:57
  • \$\begingroup\$ For languages that set undeclared variables to a type that can not be differentiated from a declared variable set to that type (e.g. nil in Lua), just detecting that type is enough. What does this mean? Are you saying a variable with a value of nil should return false, and a variable with a value of anything else should return true? If so, there are probably better ways to word it. \$\endgroup\$
    – chunes
    Jan 23 at 7:04
-2
\$\begingroup\$

PHPFuck Golf - Hello World

Like this

PHPFuck is an esoteric language in which any PHP statement can be accurately reproduced into another valid PHP program that uses only the 5 characters (^.9).

The PHPFuck converter, when given an input of echo "Hello World!", produces a block of code that is 74,450 characters long. Because the program used a lot of automatic conversions to create that block, I believe that the code can be made a lot shorter using manual optimizations.

Your task is to build a PHP program that performs the task echo "Hello World!", using only the characters (^.9). The shortest code to do so wins.

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

Polishing off a Sudoku

Given a set of 8 numbers as input, output the missing one.

Examples

12346789 -> 5

94351726 -> 8

\$\endgroup\$
1
  • 1
    \$\begingroup\$ I am fairly certain this is a dupe of a more general question. This was the first I could find, though I feel like there was one with an arbitrary range too. \$\endgroup\$
    – Giuseppe
    Feb 10 at 1:51
-2
\$\begingroup\$

Print the input times infinity

Make a program/function that prints the input of the function infinite times, separated be a line break. Do note that the output must be to stdout.
tags:

\$\endgroup\$
5
  • \$\begingroup\$ Might get marked as duplicate of Implement a truth-machine or Simple cat program \$\endgroup\$
    – noodle man
    Jun 14 at 22:41
  • \$\begingroup\$ Why? It endlessly returns the input with a newline regardless of what it is \$\endgroup\$
    – Dadsdy
    Jun 15 at 7:02
  • 1
    \$\begingroup\$ It’s not the same challenge, but I think it’s likely to be marked as a dupe of one of those since most answers to that can be easily ported to your challenge (this practice of handling duplicates is fairly common on this site) \$\endgroup\$
    – noodle man
    Jun 15 at 12:14
  • \$\begingroup\$ @noodleman Really? My answer (in (,)) was very different. \$\endgroup\$
    – Dadsdy
    Jun 15 at 18:18
  • \$\begingroup\$ Exact dupe restricted-source version fastest-code version \$\endgroup\$ Jul 19 at 3:57
-2
\$\begingroup\$

That is disproportionate!

You want to overwhelm your nasty boss with an error message so large that they quit their job. But your boss will fire you instead if it's too long.


Challenge

In this challenge, you have to write code that is more than 0 bytes long that produces the longest error message possible.

Scoring

Answers will be scored using this formula: $$\frac{\text{Error message length in bytes}}{\text{Programme length in bytes}}$$

Other

All answers must include the answer score, source code and error message.



Meta

  • Is this a duplicate?
  • Any other appropriate tags?
  • Anything else?
\$\endgroup\$
1
-2
\$\begingroup\$

KCMCK

Print this ASCII art:

  +--------------  +-+                   +-+   +--------------  |
 ++                | |                   | |  ++                |
 |                 |  \                  / |  |                 |
 |                 |   \                /  |  |                 |
 |                 |    \              /   |  |                 |
 |                 |     \            /    |  |                 |
 |                 |      \          /     |  |                 |
 |                 |       \        /      |  |                 |
 |                 |        \      /       |  |                 |
 |                 |         \    /        |  |                 |
 ++                |          \  /         |  ++                |
  +--------------  |           \/          |   +--------------  |
________________________________________________________________|
________________________________________________________________|

Shortest code in characters wins!

  • Any leading whitespace must be the same on every line.
  • No restrictions on trailing whitespace.
\$\endgroup\$
-2
\$\begingroup\$

Be a cat

I have not found a single challenge that asks for a plain cat programme. The closest I have found requires you to print cat goes meow when the input is cat.


In this challenge, you must read everything from STDIN, or an acceptable alternative, and print the exact text to STDOUT, or an acceptable alternative, with only the leading and trailing whitespace in the input.


Meta

  • Have I been so silly that I missed a duplicate?
\$\endgroup\$
1
-2
\$\begingroup\$

Type annotation

Given a function definition and a list of types, both valid in your language, output the function annotated with those types. Shortest code wins, since this is .

Meta

  • Is it a duplicate?
  • How should I include languages without type annotation support?
  • Does it need to be explained better?
  • Anything else?
\$\endgroup\$
-2
\$\begingroup\$

Execute... yourself!

Almost a duplicate, but only requires you to compile a subset, and is closed.


You work at a compiler factory, making compilers tailored to your customers.


Challenge

In a programming language of your choice, write a program that executes code in the chosen language. Snippets are allowed, but you may not hardcode the input.

This is , so shortest code wins!


Meta

  • To deal with languages like No, should I change it to a , or disallow languages like that completely?
  • Is it clear enough?
  • Are there any unclosed challenges this duplicates?
  • Anything else?

\$\endgroup\$
4
-2
\$\begingroup\$

Optimal Span for Multiple Byte Ranges [Draft]

I recently ran into the following challenge, you have a list of common crawl URL indexes that have the setup below. Each of the GZ files are ~1.2GB, the challenge would be to design the most efficient solution that can respond with the decrypted byte span. If this if of interest, I'll revise this to flush out more details.

In the real world, some considerations are:

  1. Cache vs HTTP fetch byte range
  2. Minimizing number of requests by making requests spanning multiple offsets
  3. Decrypting entire file for multiple offsets vs decrypting offsets individually
  4. Trade off between # of connections, download time, local storage space, decryption time

Example:

FileName.gz Offset Length CONTENT_DIGEST
crawl-data/CC-MAIN-2023-23/segments/1685224648465.70/warc/CC-MAIN-20230602072202-20230602102202-00011.warc.gz 755688583 1327 123456789
crawl-data/CC-MAIN-2023-23/segments/1685224648465.70/warc/CC-MAIN-20230602072202-20230602102202-00011.warc.gz 86276364 3404 ABCDEFGHI
crawl-data/CC-MAIN-2023-23/segments/1685224648465.70/warc/CC-MAIN-20230602072202-20230602102202-00011.warc.gz 963176585 1294 987654321
\$\endgroup\$
-2
\$\begingroup\$

Output Shakespeare with the highest probability

We all know that a monkey hammering out random bytes will output the works of Shakespeare with some probability. This probability is extremely low.

But what if the monkey would type out a computer program instead, and then we run the computer program and see if that outputs the works of Shakespeare? The program might output random text but with biases that make the works of Shakespeare much more likely to appear. That way, the overall probability of outputting Shakespeare might be much higher. But how much higher? Let's find out.

The essence of this challenge is to maximise the following probability:

p(the monkey outputs your program) * p(your program outputs the works of Shakespeare)

But to make that work as a practical challenge we have to introduce some rules and a little bit more maths, so read on.

This challenge is similar in spirit to my previous Write Moby Dick, approximately, but the scoring system is very different and this should lead to a significantly different challenge.

more details

The following file contains the complete works of Shakespeare in ASCII format. [to do: create the file and upload it somewhere]

In principle your program is meant to output random text, but we need to do that in such a way that we can calculate the probability of a given output. For most programming languages that isn't possible, so instead of having your program behave randomly we will have it output a probability distribution.

It works like this: your program (or function etc.) will be called multiple times (about 3,500,000 times). On each invocation it will be given the first n characters of bill.txt and it will output a probability distribution over ASCII characters, which is its probability of guessing a given next character. This output can be in any reasonable format - for example, it could be a Python array of 128 floats. But it must be a probability distribution, i.e. in this example the floats must sum to 1.

The following pseudocode shows how your score is calculated:

log_p_monkey_outputs_program = -(size of your submission in bytes)*8

log_p_program_outputs_shakespeare = 0
bill = contents of bill.txt
for n = 1 to length(bill)-1
    probabilities = your_program(first n characters of bill)
    correct_prob = probabilities[(n+1)th character of bill]
    log_p_program_outputs_shakespeare += log2(correct_prob)

score = log_p_monkey_outputs_program + log_p_program_outputs_shakespeare

The score that this program calculates is the logarithm of the probability that the monkey outputs your program and the program outputs Shakespeare, assuming that we always feed the program's output back in as input. We calculate the logarithm to avoid floating point errors, as the final probability will be extremely small. Note that the logarithm is to base 2.

If the scoring program is implemented correctly, the score will always be negative. A higher score (closer to 0) is better.

Note that your program outputs a probability distribution but it should not itself behave randomly. Your program may not use a random number generator - it must always return the same probability distribution for a given input.

If you want to store state in between invocations this is allowed. You can do this by writing to an external file, by using static or global variables, by submitting a class rather than a function, using a state monad, or whatever else works for your language.

Submission format

Your submission should include the following, which do count towards the size of your submission. If they are excessively large you can link to github etc.

  • your program
  • any data it needs in order to run

Your submission should also include the following, which don't count towards its size:

  • the code used to calculate its score, implementing the pseudocode above
  • any code that was used to generate your submission (e.g. to create any data files that you included)
  • an explanation of how your submission works.

Rules

As mentioned, your program must run deterministically, so that it always outputs the same probability distribution given the same input (and hence always gets the same score).

If at any time the value of correct_prob in the scoring pseudocode is 0, then your score is -∞, which is the worst possible score.

You may not use any libraries or functions that your language might have that include data or statistics about natural language. This includes pre-trained neural networks, word lists, etc. It also includes any built-in function that outputs any of Shakespeare's works. It's fine to use neural networks and word lists etc., but the data or weights must be included in your submission and count towards its byte count.

You may not use any libraries or functions designed for text compression. It's fine to use algorithms like bzip etc., but you have to implement them yourself (and hence include the implementation in your byte count).

Your submission should include the code used to calculate its score. (This doesn't count towards the byte count.)

If you want to store state between invocations you can do this however you like, as long as your program never has access to 'future' bytes from the bill.txt file. (So, for example, you can't just pass it a string containing all of the input and get back a big list of probability distributions as output.)

You must actually run your test program and calculate/verify your score before submitting your entry. If your submission runs too slowly for you to verify its score then it is not qualified to compete, even if you know what its score would be in principle.

You may import existing libraries other than the exceptions above, but you may not load any other external files unless they're included in your byte count. Your code may not access the bill.txt file in any way other than described above.


sandbox notes:

I'm unsure about the rule banning built-in compression algorithms. It seems more elegant to leave it out, but in Paint Starry Night, objectively, in 1kB of code they spoiled the fun a bit, and I'm worried that with this scoring system the same could happen here. I'm happy to hear any thoughts about that.

I'm also worried about this being closed as a duplicate of Write Moby Dick, approximately. That challenge was popular (it's the 12th highest scoring question on the site), but I wasn't really satisfied with it because the answers ended up being dominated by one method. I've been thinking for years about how to improve the scoring system so that that won't happen. A huge amount of thought has gone into what makes a good scoring system and why this one in particular should encourage more creative answers than the previous one - but that work isn't visible in the question text itself, so I'm worried that people will see two questions about predicting the next character in a text file and vote to close it. I welcome any thoughts about how to avoid this possibility.

I'd also really like feedback on the score calculation pseudocode - is it sufficiently clear how the score is calculated, and can I make it clearer?

Finally, a very specific query: the ban on word tables seems like it would rule out some golfing languages. I'm unsure whether I should make an exception for those, or if that would be seen as giving those languages an unfair advantage.

\$\endgroup\$
8
  • \$\begingroup\$ I'd say you can encode randomness into program, resulting in a pure "output Shakespeare shortest code" \$\endgroup\$
    – l4m2
    yesterday
  • \$\begingroup\$ @l4m2 if I understand correctly that would just make it a kolmogorov-complexty challenge. This is meant to be more of an optimisation challenge like Paint Starry Night or Write Moby Dick. I like Kolmogorov complexity challenges but these optimisation type challenges are quite different in terms of what makes a good answer, and that's what I'm going for with this question. \$\endgroup\$
    – N. Virgo
    yesterday
  • \$\begingroup\$ @l4m2 ah - or maybe you're saying that the optimal solution will be to compress the shakespeare file and just output it deterministically? If so that's a reasonable criticism but having done the maths I'm more or less certain it's not the case. \$\endgroup\$
    – N. Virgo
    yesterday
  • \$\begingroup\$ The point is that compressing the file in this way produces an optimal p_program_outputs_shakespeare but you have to include all the data in compressed form in the program, so you pay for it in p_monkey_outputs_program. If you can do some kind of lossy compression then you'll do worse at p_program_outputs_shakespeare but should be able to save more on p_monkey_outputs_program. So I expect the optimum to be some combination of compression and probabilistic guessing. \$\endgroup\$
    – N. Virgo
    yesterday
  • \$\begingroup\$ Basically, if I'm right, the optimal solution should be to implement a learning algorithm with good inductive biases - something that doesn't contain much specific information about Shakespeare (hence doesn't take up much space and does well on p_monkey_outputs_program) but is good at learning patterns in the text, so that it gets better at predicting it over time and scores well on p_program_outputs_shakespeare. I could be wrong, but I've been thinking about this for a very long time and if I am wrong I don't think it will be for a trivial reason. \$\endgroup\$
    – N. Virgo
    yesterday
  • \$\begingroup\$ But what is fed to learn? \$\endgroup\$
    – l4m2
    yesterday
  • \$\begingroup\$ @l4m2 on each time step it gets fed the first n bytes and has to predict the (n+1) th one. So by the time it's guessing the millionth byte it's had a megabyte of data to learn from. \$\endgroup\$
    – N. Virgo
    yesterday
  • \$\begingroup\$ I will check I'm right - before I post this I will implement the obvious compression-based solution using bzip etc. and see if I can beat it with stochastic guessing. If I can't then your worry is correct and I will have to change the scoring system. \$\endgroup\$
    – N. Virgo
    yesterday
-3
\$\begingroup\$

Code-challenge: Guess my number

The challenge

You have a number from 1 to 10 in mind, and your program should ask questions to find out which number. These questions can be any questions, the program only has to find out the number as fast as possible.

Your program should ask a question, such as "Is the number a prime?", and the user must answer either y or n (yes or no). Ask questions until you know the number.

The scoring

To calculate the score, you need to take the sum of the question count for each number. For example, if you need 1 question to find the number 1, 2 questions to find the number 2, and so on, the score is 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10, so the score is 55.

Important note: the question count for a specific number must always be the same. For example, if you need 4 questions to find out the number 10, then you have to ask always 4 questions to find out the number 10, otherwise it is impossible to calculate the score.

\$\endgroup\$
3
  • 6
    \$\begingroup\$ boooring. The Huffman tree for a uniform set is any perfectly balanced tree. The question asks us to perform a binary search on the usr device. Is the number greater than 5? Is the number greater than 2? Is the number greater than 1? Hey' I think it's 1. \$\endgroup\$ Jan 2, 2014 at 11:49
  • 3
    \$\begingroup\$ Maybe if this were a pop-contest and the goal was to make the most original set of questions while still keeping the score at its theoretical minimum. \$\endgroup\$ Jan 3, 2014 at 5:28
  • \$\begingroup\$ This is off-topic and AI is off-topic for this site \$\endgroup\$
    – Fmbalbuena
    Nov 12, 2021 at 17:39
-3
\$\begingroup\$

Popularity Contest: Implementation of a Hash Table

Create a class in some OOP language for a hash table that supports getting, setting, and removing values. You can't use the built in hash table/dictionary/map implementation. Highest votes in one week wins.

A key is any valid string. A value is any valid string, number, or boolean.

Example functionality:

hash.set("key","value");
hash.get("key"); // returns "value"
hash.set("key", 1234);
hash.get("key"); // returns 1234
hash.set("key2",hash.get("key"));
hash.get("key2"); // returns 1234
hash.delete("key");
hash.get("key"); // returns null/undefined/none/etc. or throws an error
hash.get("key2"); // still returns 1234

Definition of a hash table (from Wikipedia):

In computing, a hash table (also hash map) is a data structure used to implement an associative array, a structure that can map keys to values. A hash table uses a hash function to compute an index into an array of buckets or slots, from which the correct value can be found.

The hash table cannot be simply an array that is searched in linear time. It must be an actual hash table that uses a hash function to map the keys to the value.

\$\endgroup\$
3
  • 2
    \$\begingroup\$ Popularity contest and shortest don't mix. That aside, the spec is too vague. What is a "value"? What assumptions can be made about hashcodes? If the language makes all types nullable, should null be permitted as a key? What should the type be in languages which have co- and contravariance? And for that matter, what qualifies as a "hash table", bearing in mind that people will try to exploit any loophole? \$\endgroup\$ Jan 2, 2014 at 23:16
  • \$\begingroup\$ @PeterTaylor Thank you for the feedback! Please see my edits, and let me know what you think. Could you meant about co/contravaraince? I looked at the wikipedia article about it but I'm not really sure how that has anything to do with this question. \$\endgroup\$
    – hkk
    Jan 2, 2014 at 23:37
  • \$\begingroup\$ I think it's still vulnerable to the loophole of "I have a hashtable with one bucket" (i.e. it's really a list of (key, value) pairs which I traverse in linear time). The thing about variance is to do with static typing of the elements of the map. E.g. in Java Map<String, Integer>'s get method has signature public Integer get(Object); in C#, a Dictionary<string, int>'s Get method has signature public int Get(string). The edited version makes it clear enough that the hashtable isn't expected to be genericised. \$\endgroup\$ Jan 3, 2014 at 0:08
-3
\$\begingroup\$

Bovine Ignorance

I'm curious about code which still works after being mangled by figlet, toilet, cowsay et al, but I'm not sure whether this in any way sane.

What I'm toying with is a challenge in which a participant may submit any program in any language. It should be possible to use this program's source code as input to cowsay or whatever, and the result should be another valid program in any language, which still does a similar thing. For instance, the following bf program prints Hello world! with no newline:

+++++ +++++
[
> +++++ ++
> +++++ +++++
> +++
> +
<<<< -
]
> ++ .
> + .
+++++ ++ .
.
+++ .
> ++ .
<< +++++ +++++ +++++ .
> .
+++ .
----- - .
----- --- .
> + .
+++++++++++++++++++++++++++++++++++++++++

Running cat ./prog.bf | cowsay -e .. -T $'>.' yields the following output:

 _________________________________________
/ +++++ +++++ [ > +++++ ++ > +++++ +++++  \
| > +++ > + <<<< - ] > ++ . > + . +++++   |
| ++ . . +++ . > ++ . << +++++ +++++      |
| +++++ . > . +++ . ----- - . ----- --- . |
| > + .                                   |
| +++++++++++++++++++++++++++++++++++++++ |
\ ++                                      /
 -----------------------------------------
        \   ^__^
         \  (..)\_______
            (__)\       )\/\
             >. ||----w |
                ||     ||

Which is itself a valid bf program which prints Hello world!!!, followed by a newline.

The problem with using bf here is that it ignores most of the cow, making this a bit too easy. The problem with using any other language is that it doesn't ignore most of the cow, making this far too difficult. Is there a sensible middle ground I could pick for this? I don't think it's impossible, I'm fairly sure you can exploit cowsay's behavior on one-liners to produce valid svgs, but I'm not sure how best to pose this challenge. Any ideas?

\$\endgroup\$
6
  • 2
    \$\begingroup\$ I could not think of any language that falls in the middle ground. Even brainfuck is affected by the -----------------------------------------..>.---- inserted by cowsay. Most languages have strong parsing rules that would not cope with being post-processed by cowsay. The few exceptions for this will be either completely unaffected or badly affected, making the challenge uninteresting. \$\endgroup\$ Feb 19, 2014 at 12:32
  • \$\begingroup\$ Actually, you can't transform just any brainfuck program to cowsay-brainfuck. Namely those that can output fewer than three characters cannot be transformed at all. \$\endgroup\$ Feb 19, 2014 at 14:52
  • \$\begingroup\$ @JanDvorak, I was intending to allow competitors to choose the parameters of their calls to cowsay. For the uninitiated, -e controls the string used for eyes and defaults to oo, and -T controls the string used for the tongue, defaulting to ` U`. This is all yak-shaving, though, and having written this up and read the comments, I suspect that this idea has neither legs, horns nor udders. \$\endgroup\$
    – ymbirtt
    Feb 19, 2014 at 23:19
  • 2
    \$\begingroup\$ If I could propose a variant that is more feasible, you could do a challenge like "Write a program in your language of choice that draws ASCII art of a cow saying something (does not have to be identical or even similar to the cowsay art). The entire drawing must itself be valid source code that does something other than no-op. Post results of both programs." That gives people more leeway to work around the specific restrictions of their compiler. \$\endgroup\$ Feb 21, 2014 at 23:22
  • \$\begingroup\$ Ok, I found a language that falls within the middle ground: whitespace. Anyway, this question has a too narrow scope to develop an interesting challenge. \$\endgroup\$ Feb 22, 2014 at 18:31
  • \$\begingroup\$ @JonathanVanMatre That would be a subjective validity criterion, and would probably be closed as too broad. \$\endgroup\$
    – wastl
    Jul 2, 2018 at 13:55
-3
\$\begingroup\$

99 Bottles of Errors

While there are already many versions of "print 99 Bottles of Beer," I thought another one wouldn't hurt.


The challenge is fairly simple: print the lyrics to 99 bottles of Beer to STDERR. I don't care how you do it, so long as the entire lyrics show up. An entire program is required, so the following Java program would be invalid (even if it did do the correct thing):

System.out.println("99 Bottles of Beer on the Wall, take one down and pass it around...");

The scoring:

  • This challenge is , so shortest code by byte count wins.
  • If necessary, assume UTF-8 is the character encoding used.

The rules

  • All the code must be in one file.
  • Any language is allowed.
  • Reading input, whether it is from STDIN, a file, or the web, is not allowed.
\$\endgroup\$
1
  • 6
    \$\begingroup\$ This is trivial in some languages (Java), where it reduces to a simple kolmogorov challenge, and impossible in others (those that have no distinct STDERR) \$\endgroup\$ Mar 27, 2014 at 7:42
-3
\$\begingroup\$

Create an Identicon Generator

The challenge is to create an identicon generator. The identicons must be randomly generated, so we get a new identicon for each key the program receives. You can input a key using std-in or you can use your language's random number generator for the key.

In order to make your identicon look reasonably nice, it must generate a picture, then rotate that picture around the bottom right corner, the way this mockup shows:

enter image description here

The output must be to a PNG file. Shortest code wins.

\$\endgroup\$
3
  • 5
    \$\begingroup\$ Far too broad. As this stands I can create a 1-pixel image whose colour is just the key. I don't think this question will be ready to go until you've found a way to prevent me from making the images differ only in their palette (and to pre-empt, I think that adding a rule "Images may not differ only in their palette" isn't a real fix). \$\endgroup\$ Mar 28, 2014 at 14:50
  • 5
    \$\begingroup\$ If you just ask for "random" images, you'll get images that are either hardly random at all (a solitary pixel in a random location), or completely random (noise). To get something "reasonably nice", you'll have to provide very clear instructions on how to produce these images. I suggest you try creating a few of these yourself, and find a minimal set of rules that produces results that look OK. Include requirements on dimensions (100x100px?), selection of colours (at least 2, not too similar), and drawing method (e.g., "five triangles with random vertices and a minimum area of 20 px²"). \$\endgroup\$
    – r3mainer
    Mar 28, 2014 at 15:25
  • 1
    \$\begingroup\$ How important is the PNG file output? This will be a challenge in itself for many languages. Would you accept an uncompressed non-interlaced format like PPM? \$\endgroup\$ Apr 16, 2014 at 9:45
-3
\$\begingroup\$

Shortest Program that May or May not Terminate:

Write a program such that whether or not it terminates depends on the answer to an unsolved question in Computer Science or Mathematics. For example, your program might test the Goldbach conjecture for every N and quit if a counterexample is found, or hunt for odd perfect numbers. Please include an explanation of why your program may or may not terminate!

Note: assume infinite memory and stack size, because otherwise they all terminate. Your program must be self contained, take no input, and only use standard libraries. This is Codegolf, so shortest code wins!

\$\endgroup\$
6
  • \$\begingroup\$ What about "unsolvable" problems, e.g. halting problem? Can I take another code as input and terminate if that terminates? Because that other program may or may not terminate, and there's no way to tell. \$\endgroup\$ Nov 20, 2014 at 18:03
  • \$\begingroup\$ The intention was that the program isn't allowed to take input. I'll be more specific. \$\endgroup\$ Nov 20, 2014 at 18:50
  • 2
    \$\begingroup\$ Does this differ from this previous question in the sandbox? \$\endgroup\$ Nov 20, 2014 at 19:34
  • \$\begingroup\$ (even if not the comments explaining why that one wouldn't work as a question may help Taylor this one) \$\endgroup\$ Nov 20, 2014 at 19:35
  • \$\begingroup\$ The intent of this doesn't differ significantly from the question you linked, I searched posted questions but forgot to search the sandbox. \$\endgroup\$ Nov 20, 2014 at 19:41
  • \$\begingroup\$ Infinite memory isn't required. \$\endgroup\$
    – feersum
    Nov 20, 2014 at 21:46
-3
\$\begingroup\$

Every number is interesting

We know that every number is interesting but how?

You should write a program or function which:

  • takes a list of N positive integers (>0 and <2^31)
  • outputs N lines each of them showing how the corresponding input number is interesting
  • is not longer than 1024 bytes
  • uses no more than 1 second per number
  • doesn't use external sources

Examples

172: 444 in base6
5776: 76*76
9801: 9 * 1089 (reverse)
68101: no 11 in base2 (10000101000000101)
491033: 317 * 1549 (product of 2 big primes)
467808816: no digit 5 from base6 to base10

Inputs

You should include the output for the following input in your post:

58 92 120 224 358 490 912 1578 7812 222008 1645060 19796411 550453633 

If you care to run your program on a bigger sample and share the result with us use this input data (2500 numbers). (You can upload your output to e.g. pastebin.)

This is a popularity-contest so highest voted answer wins.

Tags: popularity-contest, number

\$\endgroup\$
7
  • 1
    \$\begingroup\$ What sort of criteria are necessary for defining a number as 'interesting'? I see things like square numbers, other bases, etc. But are there any specifics? I'm interested in this challenge (but worried it might be closed as too broad). \$\endgroup\$ Apr 7, 2015 at 13:18
  • \$\begingroup\$ @ASCIIThenANSI There wasn't a clear definition. That's part of the reason why I abandoned the challenge. \$\endgroup\$
    – randomra
    Apr 8, 2015 at 1:55
  • 1
    \$\begingroup\$ Would you mind if I tried taking it up? I would have to post as a new answer, because I can't directly edit. \$\endgroup\$ Apr 8, 2015 at 2:00
  • \$\begingroup\$ @ASCIIThenANSI Not at all. \$\endgroup\$
    – randomra
    Apr 8, 2015 at 2:01
  • \$\begingroup\$ @ASCIIThenANSI Where did you post it? \$\endgroup\$
    – wizzwizz4
    Jul 24, 2019 at 13:33
  • \$\begingroup\$ @ASCIIThenANSI I am also curious \$\endgroup\$
    – MilkyWay90
    Aug 27, 2019 at 23:41
  • \$\begingroup\$ I don't think he posted it or at least with this account. (he only has asked 1 question and it is not this one). \$\endgroup\$
    – Bjop
    Sep 9, 2022 at 10:58
1
146 147
148
149 150
153

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .