577
\$\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\$
2
  • \$\begingroup\$ What if I posted on the sandbox a long time ago and get no response? \$\endgroup\$
    – None1
    Commented May 15 at 14:05
  • \$\begingroup\$ @None1 If you don't get feedback for a while you can ask in the nineteenth byte \$\endgroup\$ Commented May 29 at 13:27

4830 Answers 4830

1
26 27
28
29 30
161
3
\$\begingroup\$

Befunge Comment Outline Creator

\$\endgroup\$
6
  • \$\begingroup\$ Is it equivalent to a 0-1 matrix with an even sum in each row/column? Additionally, I'm assuming an all-space answer is invalid, but I don't see any rule disallowing it. Could you add one? \$\endgroup\$ Commented Oct 20, 2023 at 6:26
  • \$\begingroup\$ @CommandMaster Almost, however having no semicolons at all would result in the IP reading in the comment as code. I could probably clarify that. \$\endgroup\$ Commented Oct 20, 2023 at 6:36
  • \$\begingroup\$ Can I take as input w+2, h+2 instead of w, h? \$\endgroup\$ Commented Oct 21, 2023 at 6:35
  • \$\begingroup\$ @CommandMaster Is it too expensive to add 2 to the inputs? \$\endgroup\$ Commented Oct 21, 2023 at 7:04
  • \$\begingroup\$ I haven't tried, and w, h might be shorter, but it might be easier to use the size of the output instead of the size of the comment itself \$\endgroup\$ Commented Oct 21, 2023 at 7:06
  • \$\begingroup\$ @CommandMaster Hm, I may consider it, it doesn't seem like it would be unfair \$\endgroup\$ Commented Oct 21, 2023 at 7:10
3
\$\begingroup\$

Minecraft XP Orb Amounts

\$\endgroup\$
3
\$\begingroup\$

Intersections in a Circular Scrambled Word

Given a word W and a scrambled version of it S as input, you are required to return the number of intersections S will have when organized in a circular arrangement.

Example

Let W be VICTORY and S be VICYTRO. After transforming the scrambled word in its circular form and forming the original word by adding segments between the characters, we have the following diagram:

enter image description here

As we can see, for this particular example, the expected output is 2 interceptions.

Specs

  • Input will consist only of [A-Z] characters.
  • It is irrelevant how you set up the circular word (clockwise, counterclockwise, if the characters are evenly spaced from one another or not), just be consistent.
  • Input is flexible, read it however you see fit for you.
  • Standard loopholes are not allowed.

Test Cases

Format: 
W , S  --> output

VICTORY, VICYTRO --> 2
# To be added more 

This is , so shortest answers in bytes wins!

Meta

Is the challenge interesting? Is there anything unclear? I am not a native English speaker, so I welcome any corrections on the wording of the challenge.

I will later add more test cases. Also, what is the general consensus here where there are multiple correct answers? Because when I add words with repeated letters, depending on the "path" you choose when forming the circular word, it will affect the number of intersections. (I'll update the specs section as well when I introduce these special cases).

Lastly, I didn't give much thought on my second point in the specs section, maybe it's wrong... I'll observe more cases later.

Pretty much any feedback is appreciated!

\$\endgroup\$
3
  • 2
    \$\begingroup\$ Could take input as a permutation of 1..n, to get rid of worrying about repeated letter ambiguity as well as some potential for a "chameleon challenge" (i.e. determining the permutation between the two words could be highly nontrivial in some languages and overshadow the actual "core" of intersections between circle chords). If you do want solutions to have to handle repeats, then I'd recommend just allowing them to give any of the possible outputs. \$\endgroup\$ Commented Oct 29, 2023 at 19:52
  • 1
    \$\begingroup\$ re. multiple possible outputs: challenges often specify that submissions may choose to either output all possible outputs or just one of them. \$\endgroup\$ Commented Oct 29, 2023 at 20:10
  • \$\begingroup\$ I suggest to write W and S instead of W and S to avoid confusion, because those are just names, no data. \$\endgroup\$
    – Philippos
    Commented Oct 31, 2023 at 12:44
3
\$\begingroup\$

Seat gangs as far as possible

\$\endgroup\$
2
  • \$\begingroup\$ @noodleman No, the gangs can be any (non-zero) size -- \$k\$ is the number of gangs. \$\endgroup\$ Commented Oct 31, 2023 at 17:45
  • 1
    \$\begingroup\$ Suggest test case 1,2,1,2,1 -> 1,1,1,2,2 \$\endgroup\$ Commented Oct 31, 2023 at 17:46
3
\$\begingroup\$

Calculate a struct size

In C and C++, the size of a structure depends not only on the sizes of its members but also their alignment requirements.

This problem will assume a simple case in which each structure is made up of primitive types that have a size that is a power of two and an alignment that equals their size.

Given a list of the types in a structure, please output the resulting size of the structure.

Here is an example structure:

struct {
  char c[5];
  int i, j;
  short s[3];
}

Input in any of the following formats is acceptable:

  • A list of the sizes of the individual types that make up the structure; for the above that would be [1, 1, 1, 1, 1, 4, 4, 2, 2]
  • A list giving the lengths of each array and the sizes of its elements, which for the above would be [[5, 1], [1, 4], [1, 4], [3, 2]]
  • A list giving the size and alignment of each variable, which for the above would be [[5, 1], [4, 4], [4, 4], [6, 2]]

(The third format indirectly supports nested structures but this can be emulated for the other formats by replacing them with an array of the same size and alignment.)

The size of the above structure is 24. Here is how it is calculated:

  1. Each of the 5 chars consumes 1 byte each, making 5.
  2. Each int requires an alignment of 4, so there are three bytes of padding before the first of them. The total size is now 16.
  3. Each of the 3 shorts consume 2 bytes each. The total size is now 22.
  4. The entire structure has an alignment of the largest alignment of any member, which in this case is 4, so the size must be rounded up to the next multiple of 4, which is 24.

Visually:

c[0] c[1] c[2] c[3]
c[4] ..............
i__________________
j__________________
s[0]_____ s[1]_____
s[2]_____ .........

This is , so the shortest program or function that breaks no standard loopholes wins!

\$\endgroup\$
3
\$\begingroup\$

Just another traffic jam!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Can you specify the zip-lock system? \$\endgroup\$
    – Seggan
    Commented Nov 27, 2023 at 19:53
  • \$\begingroup\$ @Seggan I added that, thank you. I somehow thought this would be internationally known. \$\endgroup\$
    – Philippos
    Commented Nov 28, 2023 at 6:49
3
\$\begingroup\$

Output 5-line calendar

Given year, month and optionally weekday of 1st, output the calendar of the month.

For empty cell, fill it with the date where it's supposed to be, in last or next month, and add # to indicate gray. If last few days can't fit in 5 lines, then they share last line with 5th week, use / to separate two days.

Sample Input: Nov 2023

Sample Output:

 #28   #29   #30     1     2     3     4
   5     6     7     8     9    10    11
  12    13    14    15    16    17    18
  19    20    21    22    23    24    25
  26    27    28    29    30    #1    #2

Sample Input: Dec 2023

Sample Output:

 #26   #27   #28   #29   #30     1     2
   3     4     5     6     7     8     9
  10    11    12    13    14    15    16
  17    18    19    20    21    22    23
24/31   25    26    27    28    29    30

Outputting a 2D array of string is fine

\$\endgroup\$
3
\$\begingroup\$

Longest irreducible quine

Posted here

\$\endgroup\$
15
  • \$\begingroup\$ Would a submission be valid if it can be reduced to an improper quine, but not to a proper one? \$\endgroup\$ Commented Dec 4, 2023 at 17:17
  • \$\begingroup\$ @noodleman ty, clarified \$\endgroup\$
    – math scat
    Commented Dec 4, 2023 at 20:33
  • \$\begingroup\$ In most languages it will be possible to make it longer by somehow splitting it, but you can't reduce it by only removing characters, but you also need to change some. I never saw a good »longest« challenge, and I doubt this will be the first. \$\endgroup\$
    – Philippos
    Commented Dec 5, 2023 at 6:40
  • \$\begingroup\$ @Philippos not quite sure how this would work, care to give an example? \$\endgroup\$
    – math scat
    Commented Dec 5, 2023 at 12:33
  • 2
    \$\begingroup\$ @Philippos This is wrong - it easily follows from Higman's lemma that in all languages (they don't even have to be computable!) there is only a finite number of irreducible programs which perform a given task, so in particular the length must be bounded. \$\endgroup\$ Commented Dec 5, 2023 at 16:28
  • \$\begingroup\$ @CommandMaster thanks for clarifying! \$\endgroup\$
    – math scat
    Commented Dec 5, 2023 at 16:32
  • \$\begingroup\$ An example: You can split your example like v='%r;print("v="+v%%v)';print("v="+v%v) Easy to reduce, but not only by only removing bytes. \$\endgroup\$
    – Philippos
    Commented Dec 6, 2023 at 8:08
  • \$\begingroup\$ @Philippos Sure, but I don't see how this can be extended further indefinitely. (Granted, I'm not very experienced with quines/irreducible code) \$\endgroup\$
    – math scat
    Commented Dec 6, 2023 at 15:19
  • \$\begingroup\$ You could continue like v='%r;pront("v="+v.replace(chr(111),"i")%%v)';print("v="+v.replace(chr(111),"i")%v), which of course could be repeated similarly many times without possible reduction. \$\endgroup\$
    – Philippos
    Commented Dec 7, 2023 at 7:22
  • \$\begingroup\$ @CommandMaster I don't know whether Higman's lemma can be appllied here. Even if it can, the length border could be somewhere in the thousands, millions, billions ... \$\endgroup\$
    – Philippos
    Commented Dec 7, 2023 at 7:25
  • \$\begingroup\$ @Philippos I know it can be large, but that's the point of the challenge. Why couldn't it be applied? If you look at the set of all irreducible programs performing a particular task, no element of that set is a subsequence of another element, so it must be finite. \$\endgroup\$ Commented Dec 7, 2023 at 8:42
  • \$\begingroup\$ @CommandMaster Well, math scat may give it a try, but still I believe it won't be fun, because the people will lose interest before reaching any border. Or I'm wrong (rumours claim this did already happen before ;-) \$\endgroup\$
    – Philippos
    Commented Dec 7, 2023 at 8:48
  • \$\begingroup\$ @Philippos This irreducible code bowling challenge was very well received, I don't see why this will be worse \$\endgroup\$ Commented Dec 7, 2023 at 9:37
  • \$\begingroup\$ @CommandMaster One look at the exponents is enough for me to walk away. \$\endgroup\$
    – Philippos
    Commented Dec 7, 2023 at 10:20
  • \$\begingroup\$ (at Phillipos and CommandMaster) thanks for your feedback, I do think I'll give this challenge a try though. \$\endgroup\$
    – math scat
    Commented Dec 12, 2023 at 12:06
3
\$\begingroup\$

How long is this string, really?

\$\endgroup\$
3
\$\begingroup\$

In this challenge answers will provide three elements:

  • A programming language: \$L\$
  • An output string: \$S\$
  • And a non-empty string: \$B_n\$

You will also write, but not reveal, a program in \$L\$ which outputs \$S\$, built by concatenating copies of the \$B_n\$ strings from your answer and previous answers in any order.

For example here are two answers given in pseudocode:

Answer 1

\$S\$ = H
\$B_0\$ = print "H"

The program is print "H"

Answer 2

\$S\$ = Hiiii
\$B_1\$ = ++"i"

The program is print "H"++"i"++"i"++"i"++"i"

Robbers will try to discover a valid solution, that is a program made from the \$B_n\$ strings in your answer and previous answers which outputs your \$S\$. Your goal will be to make this as hard as possible.

States

As with any challenge, cops' posts here can have 4 different states:

  • Vulnerable
  • Cracked
  • Safe
  • Revealed

All cops' posts start as vulnerable. If a robber finds and posts a solution to a vulnerable post they will receive 1 point and the post will become cracked. If a post lasts 10 days in vulnerable without being solved it automatically becomes safe. A cop with a safe post may choose to reveal their program and their safe post will become revealed. A robber can still solve a safe post, if they do they receive a point and the post becomes revealed.

Only revealed posts are eligible for scoring. Robbers are only permitted to solve vulnerable and safe posts.

Scoring

For cops' an answer's score is the number of cops' posts before it if it is revealed and 0 if it is any other state. The goal is to get as high a score as possible.

Languages

In the interest of fairness, we are going to require that languages are free and reasonably cross platform. Both languages you choose must be freely available on Linux and FreeBSD (the two largest foss operating systems). This includes languages which are free and open source.

Your selected languages must predate this challenge.

Programs here should be complete programs not functions, expressions or snippets.

\$\endgroup\$
5
  • \$\begingroup\$ I wonder, would cops want to make writing challenges for future cops easier or harder? Also, your example challenges don't state the language, although the text above them says the cop ought to state it. \$\endgroup\$ Commented Feb 17 at 5:27
  • \$\begingroup\$ @CommandMaster I state both are in pseudocode. \$\endgroup\$
    – Wheat Wizard Mod
    Commented Feb 17 at 7:00
  • \$\begingroup\$ Oh, I see. It's outside the answer quote, so this might be a bit confusing \$\endgroup\$ Commented Feb 17 at 10:17
  • \$\begingroup\$ Does the cop/robber need to use all of previous \$B_n\$? I suggest stating either way explicitly. \$\endgroup\$
    – pajonk
    Commented Feb 18 at 10:01
  • \$\begingroup\$ I didn't understand the question. Can i simply repeat the Bn by 1 time, 2 times, 3 times, ... and try the connected result one by one? \$\endgroup\$
    – tsh
    Commented Mar 6 at 11:13
3
\$\begingroup\$

How many cacti can I plant here?

\$\endgroup\$
2
  • \$\begingroup\$ Hi, I like the question a lot. I would consider either sticking to edge-wrapping or not. As it is, you should specify which behavior the test cases are for, and ideally add both sets of test cases. Also, don't forget to specify that this is a code golf question. \$\endgroup\$
    – chunes
    Commented Mar 29 at 18:04
  • \$\begingroup\$ @chunes Thank you, I'll stick to the Minecraft template. The test cases actually differ with edge-wrapping, the third one can only contain 16 instead of 17 cacti and the fourth one 76 instead of 79. \$\endgroup\$
    – Cactusroot
    Commented Mar 29 at 18:17
3
\$\begingroup\$

Draw a Fibonacci Swoosh

\$\endgroup\$
3
\$\begingroup\$

Scroll That Text Faster!

Lots of television news stations will show a ticker at the bottom, which can be thought of as a window into a larger string that shifts to the right by one space each cycle:

"Lorem ipsum dolor sit amet, consectetur adipiscin"
"orem ipsum dolor sit amet, consectetur adipiscing"
"rem ipsum dolor sit amet, consectetur adipiscing "
"em ipsum dolor sit amet, consectetur adipiscing e"
"m ipsum dolor sit amet, consectetur adipiscing el"

This works great if the cycle time is short and the text can scroll by quickly. But not every updating text application is so lucky. Radio Data System is used by FM radio stations to send small amounts of data, about 1.2 kilobits per second, which contains information about the station or song or what-have-you. But with such a low bitrate, updates can be quite slow, and stations I see update the text about once a second.

With small enough windows, this can be annoyingly slow if we only advance it one character at a time. But some stations use a trick: separate the message by spaces and fit as many as we can into one window. Any words longer than the window must be scrolled one character at a time.

The Challenge

Write a function f(s, w) where s is the full string to scroll through and w is the size of the window in characters. w must be an integer and greater than 0. The output is a sequence of strings (array or output to console separated by newlines or whatnot), where each line is one step of the ticker.

To build the output, we can divide the words into those that fit into the window and those that don't. Of the first kind, fit as many as you can into one window, ignoring trailing spaces. If a word is exactly 1 window wide, it is considered of the first kind. If we get to a word that doesn't fit in the window but we still have empty space in the current window, just leave the rest of the window blank.

For words that don't fit in the window, scroll through them one character at a time until the end of the word is visible as the last character of the window. Each scroll step becomes its own output line. Words that don't fit in the window never share windows with those that do - the output shouldn't be "NEVER GON", "NA ", it should be "NEVER ", "GONNA ".

Examples

s = "RICK ASTLEY - NEVER GONNA GIVE YOU UP", w = 10

 0123456789
"RICK      "
"ASTLEY -  "
"NEVER     "
"GONNA GIVE"
"YOU UP    "

s = "LUDWIG VAN BEETHOVEN - SYMPHONY NO. 7 IN A MAJOR, OP. 92", w = 6

 012345
"LUDWIG"
"VAN   "
"BEETHO"
"EETHOV"
"ETHOVE"
"THOVEN"
"-     "
"SYMPHO"
"YMPHON"
"MPHONY"
"NO. 7 "
"IN A  "
"MAJOR,"
"OP. 92"

s = "SHERMAN BROTHERS - SUPERCALIFRAGILISTICEXPIALIDOCIOUS", w = 15

 012345678901234
"SHERMAN        "
"BROTHERS -     "
"SUPERCALIFRAGIL"
"UPERCALIFRAGILI"
"PERCALIFRAGILIS"
"ERCALIFRAGILIST"
"RCALIFRAGILISTI"
"CALIFRAGILISTIC"
"ALIFRAGILISTICE"
"LIFRAGILISTICEX"
"IFRAGILISTICEXP"
"FRAGILISTICEXPI"
"RAGILISTICEXPIA"
"AGILISTICEXPIAL"
"GILISTICEXPIALI"
"ILISTICEXPIALID"
"LISTICEXPIALIDO"
"ISTICEXPIALIDOC"
"STICEXPIALIDOCI"
"TICEXPIALIDOCIO"
"ICEXPIALIDOCIOU"
"CEXPIALIDOCIOUS"

Notes

  • Words are defined as any sequence of non-space characters between space characters, at least U+0020. Word delimiters aren't dashes, commas, or punctuation.
  • ASCII support only is fine, though you can get bonus style points if it supports a broader selection of Unicode, including fancy Unicode whitespace.
  • Supporting uppercase-only, lowercase-only, or mixed case is fine.
  • Trailing whitespace in output strings to pad to the window width is optional.
  • This is code golf, so smallest program (bytes, codels, etc.) wins!
\$\endgroup\$
3
\$\begingroup\$

Count trapped self-avoiding walks

Imagine an infinite grid of square tiles. You are standing on one of the tiles. You can move to any of the four cardinally adjacent tiles in a move, but you cannot step on the same tile twice. Calculate the number of ways you can get yourself trapped, i.e. the states where all four adjacent tiles have been already stepped on, after exactly \$n\$ moves. To take symmetry into account, only count the paths where your first move is to the right, and your first vertical move is upwards.

This is sequence A077482:

1, 2, 11, 25, 95, 228, 752, 1860, 5741, 14477, 42939, 109758,
317147, 818229, 2322512, 6030293, 16900541, 44079555, 122379267,
320227677, 882687730, 2315257359, 6346076015, 16675422679,
45502168379, 119728011251, 325510252108, 857400725204, ...

This is a challenge. Submit a program that outputs the answers to the question for \$n = 7, 8, 9, 10, \cdots\$, indefinitely. The score of a submission will be the largest \$n\$ whose answer is printed in a minute on my local machine. Highest score wins; tie is broken by the time taken to output up to that term.

My local machine is a Windows 10 machine with the following specs:

  • CPU: 12th Gen Intel Core i7-12700 (2.1GHz, 12 cores)
  • RAM: 32GB

If a submission has no way to run on Windows, you can provide your own measurement, or online code execution services such as TIO/ATO/Godbolt may be used. I know this is not ideal, but I don't have other good options at the moment.

\$\endgroup\$
3
  • \$\begingroup\$ can you clarify what you mean with the tie-breaker? to me it sounds like it's the same as the score. \$\endgroup\$ Commented Jun 11 at 8:25
  • 1
    \$\begingroup\$ @Themoonisacheese A submission that computes up to n=20 in 30 seconds wins against another that computes up to n=20 in 50 seconds (if both time out before computing n=21). \$\endgroup\$
    – Bubbler
    Commented Jun 11 at 23:29
  • \$\begingroup\$ i see. worth noting in your final submission that the Core i7-12700 has 8 P-cores and 4 E-cores, which will likely affect multithreading. i don't think it's relevant to say, limit answers to only use 8 cores, but I think you should still mention it. \$\endgroup\$ Commented Jun 12 at 8:30
3
\$\begingroup\$

Sorting with a deque

\$\endgroup\$
3
\$\begingroup\$

Given an integer \$n\$. Connect all pairs of points on \$\mathbb Z^2\$ whose distance is \$\sqrt n\$. Count how many connected sets remain.

In case that infinite sets exist, return something not a positive integer. (0 used below)

Test cases

1 => 1
2 => 2
3 => 0
4 => 4
5 => 1
25 => 1
\$\endgroup\$
3
\$\begingroup\$

Shift right by half a trit

\$\endgroup\$
6
  • \$\begingroup\$ I believe there is exactly one f that satisfies the properties. \$\endgroup\$
    – Bubbler
    Commented Aug 8 at 3:15
  • \$\begingroup\$ Some approaches to this challenge might benefit from using [sequence] format I/O, outputting [f(0), f(1), ...] or some prefix of it. Not sure if that's consistent with your vision for this challenge, though. \$\endgroup\$ Commented Aug 8 at 5:25
  • \$\begingroup\$ @Bubbler That is correct. I decided to add an explicit description of f to make the actual task clearer. \$\endgroup\$ Commented Aug 8 at 15:01
  • \$\begingroup\$ @shapewarriort I'm not opposed to this. Would the sequence need to be up to a given length, or the input n, or what? \$\endgroup\$ Commented Aug 8 at 22:58
  • \$\begingroup\$ The most common format for sequence-output challenges based on the first page of recent challenges appears to be: take an index n (0 or 1 indexed) and output the nth term; take a length n and output the first n terms; or take no input and output the entire sequence (as an infinite list, generator, etc.). This is more or less the same as what's under the tag wiki for [sequence] (codegolf.stackexchange.com/tags/sequence/info), though that phrases the "first n terms" format as the more ambiguous "Given some index n it can return all entries up to the nth one in the sequence." \$\endgroup\$ Commented Aug 8 at 23:09
  • \$\begingroup\$ Taking 0-indexed n and outputting the nth term is the current I/O; 1-indexed n is probably not very useful, and I don't think most people would care about it. But being able to output the first n terms (or infinitely yield terms) seems like it could streamline approaches that build the entire sequence (prefix) anyway. Both brute force methods as well as constructions that build the list piece by piece. \$\endgroup\$ Commented Aug 8 at 23:20
3
\$\begingroup\$

Completely introduce your friends

\$\endgroup\$
7
  • 1
    \$\begingroup\$ Welcome to CG&CC, and nice first challenge! You might be happy to learn that we have fairly mature community standards for reasonable I/O flexibility, so you don’t have to go out of your way to provide specific ways to “simplify” it. One nitpick I have for spec readability is the mention of “all ordered pairs” in 3 without re-stating the “different” constraint in 1–if you get rid of the formatting in 2, you could condense it to “output a permutation of all ordered pairs of unique names” then add a note that the pairs can be flattened out as desired. Also, are names guaranteed to be unique? \$\endgroup\$ Commented Aug 22 at 20:20
  • 2
    \$\begingroup\$ @UnrelatedString thanks for the welcome and helpful comments! I've edited the challenge to address your notes. Let me know what you think! \$\endgroup\$ Commented Aug 23 at 2:01
  • \$\begingroup\$ Interesting challenge, seems to boil down to finding an Eulerian path for a complete digraph if I understand Wikipedia correctly. Doesn't seem to be a duplicate, based on searching the site for "euler graph". ||| What does it mean to output as a string? Would it be okay to just return a list of ordered pairs instead? ||| Output format suggestion if it's consistent with your vision for this challenge: since the second element of each pair is always the same as the first element of the next pair (wrapping around for the last), just the first element of each pair is enough to describe a solution. \$\endgroup\$ Commented Aug 23 at 18:27
  • 1
    \$\begingroup\$ @shapewarriort Yep ultimately I think it's a graph traversal. Listing the nodes traversed in order would describe an answer but I think in the theme of the question I'd ask for all the pairs/edges instead. And a list of pairs would do too, I'll add that! \$\endgroup\$ Commented Aug 23 at 19:54
  • \$\begingroup\$ Fair enough with the challenge vision. ||| Some things on the I/O format: The test cases are in a format that, presumably, would be invalid for the actual challenge (assuming that names are not restricted to single characters). Printing a string representation of a data structure (here, a list of tuples) to STDOUT is allowed by default, so if that's what you mean by outputting "as a string", it might not be fully necessary to specify that. Both of these points are quite minor, and I don't think anything needs to be changed, but just wanted to bring them up nonetheless. \$\endgroup\$ Commented Aug 23 at 21:42
  • 1
    \$\begingroup\$ @shapewarriort thanks for the pointers! I'm understanding the I/O standards better I think. As long as these changes look OK, when am I good to post the challenge? \$\endgroup\$ Commented Aug 23 at 22:41
  • \$\begingroup\$ Happy to help! The Sandbox FAQ recommends a wait time of 72 hours and a net score of +3 as thresholds of challenge readiness. I personally don't see any more issues at this point, and doubt that immediately posting the challenge as-is would cause any significant problems, but I can't speak for other people. Looking at the newest questions on the main site that were originally posted to the Sandbox, all of them seem to have a gap of at least 3 days between initial sandboxing and main-site posting. I don't have enough experience to give any advice beyond that. \$\endgroup\$ Commented Aug 23 at 23:06
3
\$\begingroup\$

Visualize "Destroying Democracy"

This MathPickle post poses an interesting problem about gerrymandering.

An n-by-n square grid is given, and all cells are initially painted green. You can paint some cells red and partition the grid into several rectangles. The color of a rectangle is red if there are strictly more red cells than green; otherwise it is green (including when the numbers are equal). Your objective is to "win the election" by having strictly more red rectangles than green, with as few red cells as possible.

  • The rectangles must cover the grid, and may not overlap.
  • The largest rectangle's area must be strictly less than twice the smallest.

This problem has been solved for n <= 12 in this Puzzling post, and for more sizes in this OEIS entry.

Some examples: (same letter = one rectangle, uppercase = red)

n = 3: 4 red cells
AAa
BBb
ccc

n = 4: 6 red cells
AAad
BBbd
CCcd
eeee

n = 5: 8 red cells
AAafg
BBbfg
CCcfg
DDdfg
eeefg

n = 6: 11 red cells
aaaaad
bEFGHd
bEFGHd
befghd
bIIIid
bccccc

In this challenge, you don't need to minimize the number of red cells. Instead, find any one way to win the election by painting \$\frac{n^2}{2}\$ or fewer cells red and partitioning the grid following the rules.

I/O

The input is one integer n which is at least 3.

The output must be a 2D structure with n rows and n columns, where each item identifies the color and the rectangle the cell belongs to. The following are some valid outputs for n = 3:

2D array of nonzero integers, where sign = color and absolute value = rectangle ID
[[-1, -1, 1],
 [-2, -2, 2],
 [3, 3, 3]]

2D array of tuples of (bool, string)
[[(false, "A"), (false, "A"), (true, "A")],
 [(false, "B"), (false, "B"), (true, "B")],
 [(true, "C"), (true, "C"), (true, "C")]]

Strings printed over n lines
R1 R1 G1
R2 R2 G2
G3 G3 G3

Note that the "letter representation" used above is for illustration purposes and not allowed, since the number of rectangles can go over 26.

Standard rules apply. The shortest code in bytes wins.

\$\endgroup\$
2
  • \$\begingroup\$ If a rectangle has an equal amout of red and green cells, is it neither red nor green? In that case, does it contribute to neither the red total or green total? \$\endgroup\$ Commented Aug 27 at 23:13
  • \$\begingroup\$ @Lucenaposition "The color of a rectangle is red if there are strictly more red cells than green; otherwise it is green." So when the numbers are equal, the rectangle is green. Edited a bit to clarify it. \$\endgroup\$
    – Bubbler
    Commented Aug 28 at 0:45
3
\$\begingroup\$

Stuck on Sokoban

\$\endgroup\$
3
\$\begingroup\$

Is it a date format of YYMMDD, MMDDYY, and/or DDMMYY?

\$\endgroup\$
1
3
\$\begingroup\$

Print 4 billion if statements

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Does even / odd need to end in a newline? Is casing important? What about leading/trailing whitespace? \$\endgroup\$
    – ATaco
    Commented Sep 16 at 4:02
  • \$\begingroup\$ @ATaco Leading/trailing whitespace/newlines are covered by standard I/O (yes), but I'll add those explicitly. For the sake of simplicity I'll keep casing as strictly lowercase \$\endgroup\$
    – emanresu A
    Commented Sep 16 at 5:52
3
\$\begingroup\$

Draw a string as a binary tree

\$\endgroup\$
3
\$\begingroup\$

How many dots are there?

\$\endgroup\$
3
\$\begingroup\$

Implement a very tiny APL dialect

In this challenge, you implement a small variant of APL. The core feature of APL is multidimensional arrays, which are a generalization over scalars, vectors, matrices, 3d arrays, .... This challenge has intentionally lax requirements in some places and more strict ones in others to make the challenge more interesting. In this APL, all arrays only contain integers of a size suitable for your language. If you want to limit the amount of axes (i.e. the rank) of an array, this limit should at least be 10.

Concepts

The shape of an array is the amount of things on each axis: for example the scalar 1 has shape [], the vector [1, 2, 3] has shape [3], a 2x3 matrix has shape [2, 3].

Array literals

There should be a way to write an array literal, no matter its dimension. This can be done in many ways: a binary function (see below) like 3 2 makeArray 1 2 3 4 5 6, syntax specifying shape and contents like [3 2; 1 2 3 4 5 6], by providing a syntax for scalars like 1, a syntax for vectors like (1 2 3) and a syntax for combining arrays creating a new axis like [(1 2 3) (4 5 6)]. The rest of the challenge uses the latter syntax. You can choose to provide a syntax for negative numbers or just use negation (see below) to construct them.

Functions

You should provide the following functions: addition, negation, subtraction, shape. You can choose any name for these functions, but the ones for negation and subtraction must be the same and only distinguishable by context (see below). Negation negates each integer contained in the array. Addition and subtraction respectively add and subtract each item in the array, when they have the same shape; if one of the two arrays is a scalar all the values of the other arrays are operated with that scalar; otherwise the result is undefined. Shape always returns a vector containing the shape of the argument.

Examples

1 + 1                                    → 2
(1 2 3) + (4 5 6)                        → (5 7 9)
[(1 2 3) (4 5 6)] + [(1 1 1) (-1 -1 -1)] → [(2 3 4) (3 4 5)]
(1 2 3) + 5                              → (6 7 8)
(1 2) + (3 4 5)                          → undefined
(1 2 3) + [(4 5 6)]                      → undefined
(1 2 3) - (9 3 1)                        → (-8 -1 2)
-[(1 2 3) (4 5 6)]                       → [(-1 -2 -3) (-4 -5 -6)]
shape 2                                  → ()
shape (1 2 3)                            → (3)
shape [(1 2 3) (4 5 6)]                  → (2 3)

Reduce

You should also support reduction. Reduction is written after a binary function (so addition or subtraction) and reduces the first axis of an array right to left. This means that an array of shape [2, 4, 7] becomes [4, 7]. Addition reduce sums all subarrays, subtraction reduce does an alternate sum of the subarrays. Note that you may not provide a function for "add reduce" and another for "minus reduce"; in other words the syntax for add reduce and subtract reduce should respectively have a prefix of the syntax for add and subtract and share a common suffix, where the prefixes and the suffix together constitute the whole function name, ignoring whitespace. Reducing over an empty axis is undefined; reducing over a scalar returns that scalar.

Examples

+ reduce (1 2 3)           → 6
+ reduce [(1 2 3) (4 5 6)] → (5 7 9)
+ reduce 5                 → 5
+ reduce ()                → undefined
- reduce (1 2 3)           → 2

Syntax

The only thing missing is a way to combine expressions. Unary functions consume all the expression to the right as the argument; binary functions do the same for the right argument but only consume the value directly to the left of the function name. Grouping syntax using an opening and closing sequence must also be supported, this can go to the left of a binary function to give it a more complex left argument or around other expressions with no meaning. Grouping around functions or the reduction symbol is not required. Using an unary function as a binary function or vice versa is undefined.

Examples

In these examples, {/} is the grouping syntax.

1 + 2 - 3       → 1 + {2 - 3}         → 0
1--3            → 1-{-3}              → 4
shape +/(1 2 3) → shape {+/{(1 2 3)}} → shape 6 → ()
+5              → undefined
3 shape 2       → undefined
\$\endgroup\$
1
  • \$\begingroup\$ this looks cool \$\endgroup\$
    – Gleb
    Commented Oct 28 at 21:22
3
\$\begingroup\$

Make an almost-square

\$\endgroup\$
1
  • \$\begingroup\$ Instead of 'any character' I would like something more challenging, I feel the challenge as proposed here might be a bit too simple? Nothing wrong with that perse, but I'd like to spark a bit more creativity \$\endgroup\$
    – Martijn
    Commented Nov 7 at 13:05
2
\$\begingroup\$

Huffman Decoding

Write a programm which takes two strings as input and prints a text.


The first argument is a Huffman Tree, serialized in the following format:

  • every ascii character except ~ is always a leaf, if ~ is the first characater it is also a leaf.
  • <tree0><tree1>~ is a tree where <tree0> is the left subtree and <tree1> is the right subtree.

Example: ab~cde~~~ generates this tree:

 ┌─┴─┐
┌┴┐ ┌┴─┐
a b c ┌┴┐
      d e

where a would have the key 00, b 01, c 10, d 110 and e the key 111.


The second argument is a text that has been compressed with with the Huffman code that is defined by the first parameter. This bit-string can contain any bit sequence (also null-bytes and non-printable characters) and is not byte aligned, therefore it has been encoded with a variation of the standard Base64 encoding:

  • the characters used for the encoding are the standard base64 characters: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
  • the bitstring is broken up into 6-bit chunks and mapped to this characters
  • if the last chunk is smaller than 6 bits, a character with this prefix is used, and padding characters are added to the string:
  • - : the last chunk was five bits long
  • = : the last chunk was four bits long
  • =- : the last chunk was three bits long
  • == : the last chunk was two bits long
  • ==- : the last chunk was one bit long

Example:

bits:       1 1 1 1 0 1 1 0 1 0 0 1 1 1 0 1 0 1 0 0 0 1 1 0 1
chunks:    |1 1 1 1 0 1|1 0 1 0 0 1|1 1 0 1 0 1|0 0 0 1 1 0|1[0 0 0 0 0]|
characters:       9           p           1           G           g
base64:     9p1Gg==-

Your programm has to decode the text encoded in the second parameter and print it to stdout.

You have to provide your source code encoded in the way described above. The length of your encoded source code + the length of your serialized huffman tree will be the winning criterion.

TODO: example input

\$\endgroup\$
2
  • \$\begingroup\$ It would be helpful to explicitly state the 64 characters used in the encoding. I presume they're A-Za-z0-9+/ but (especially if you're expecting people to implement that part explicitly) it's best to make the problem self-contained. \$\endgroup\$ Commented Oct 8, 2012 at 16:23
  • \$\begingroup\$ Hello! This looks like a good but abandoned meta post, would you be willing to offer it for adoption? (If you want to, you can still post to main.) \$\endgroup\$
    – user58826
    Commented Jun 9, 2017 at 15:30
2
\$\begingroup\$

Polygon prefixes

Polygons are named after the number of sides that they have. A pentagon has 5 sides, an octagon has 8 sides. But how are they named? What's the name for a 248-sided polygon?

All polygons are suffixed with -gon. There are specific prefixes for each polygon depending on the number of sides. Here are the prefixes for the lower numbers:

3 - tri
4 - tetra
5 - penta
6 - hexa
7 - hepta
8 - octa
9 - nona
10 - deca
11 - undeca
12 - dodeca
13 - triskaideca
14 - tetradeca
15 - pentadeca
16 - hexadeca
17 - heptadeca
18 - octadeca
19 - nonadeca
20 - icosa

Polygons with 21 to 99 sides have a different system. Take the prefix for the tens digit (found on the left column), the ones digit (right column below), and then stick a "kai" between them to get (tens)(ones)gon.

20 - icosi       | 1 - hena
30 - triaconta   | 2 - di
40 - tetraconta  | 3 - tri
50 - pentaconta  | 4 - tetra
60 - hexaconta   | 5 - penta
70 - heptaconta  | 6 - hexa
80 - octaconta   | 7 - hepta
90 - nonaconta   | 8 - octa
                 | 9 - nona

The 3-digit sided polygons are named in a similar fashion. A 100-sided polygon is called a hectogon. Take the hundreds digit, find it on the column for ones digits, then stick a "hecta" to its right. Now number off the tens and ones like above: (hundreds)hecta(tens)(ones)gon. If the hundreds place digit is a 1, don't put the prefix behind "hecta".

So, given an integer (3 <= n <= 999), return the name of an n-sided polygon. n-gon is not a valid answer :P

As with all code golf, shortest code wins.


Is the description good? Would it be harder if I instead asked for the number of sides, given a name?

\$\endgroup\$
3
  • 1
    \$\begingroup\$ What is a 101-sided figure called? "hectahenagon"? Is "hena" from the column for ones digits you mention? If so, then what is a 111-sided figure called? I'd say "hectaundecagon", but then that comes from a column where "hena" is not present. \$\endgroup\$
    – Gaffi
    Commented Feb 11, 2013 at 11:15
  • \$\begingroup\$ @Gaffi: Yep, it's hectahenagon, from what Google says. \$\endgroup\$
    – beary605
    Commented Feb 11, 2013 at 16:03
  • \$\begingroup\$ I am going to take this if you allow me or if you don't respond \$\endgroup\$
    – user63187
    Commented May 30, 2017 at 1:13
2
\$\begingroup\$

DeCSS

It is known that the DVD Content Scrambling System can be deciphered with a rather short program (434 bytes of C, 472 bytes of Perl). Can you do better?

<< Test cases go here >>


I don't plan to include a more detailed spec, because it will just wind up duplicating some of the code. The test cases would be in the form of (key, link to data file, md5sum of the deciphered stream).

\$\endgroup\$
4
  • 3
    \$\begingroup\$ And the winning criterion is who is the first to get post from the courts? \$\endgroup\$
    – celtschk
    Commented Oct 3, 2015 at 20:18
  • 1
    \$\begingroup\$ @celtschk, I think that would be unfair. Winning criteria shouldn't really depend on where people live... \$\endgroup\$ Commented Oct 10, 2015 at 20:56
  • 3
    \$\begingroup\$ I think you should at least explain the general concept of the spec. \$\endgroup\$ Commented Aug 2, 2016 at 22:53
  • 1
    \$\begingroup\$ This actually sounds interesting. @PeterTaylor Perhaps you could use (and link to) Charles Hannum's explanation of the algorithm and post this. (It would be fun to have it as a popularity contest for a program that looks like it's nothing DeCSS related, or a program that furthers the gallery's point about the text vs source code arbitrary distinction - but I don't know if popularity contests are popular any more!) \$\endgroup\$
    – Sundar R
    Commented Jun 25, 2018 at 8:25
2
\$\begingroup\$

Code golfing problem: Surface classification

The task: Given a surface-word reply with the classification of what surface it is.

Example 1: Input: aba'b' ----> Output: 1T

Example 2: Input: aabcb'c' ----> Output: 3P

Bounds on the problem: Since there are only 26 letters, there will never be more than that many labels. Additionally output should be in the form S,nT,mP for n,m positive integers.

Background: In the study of algebraic topology students are often presented with diagrams such as the one below. The represent instructions for how to assemble a surface. The assembly is prescribed as: if there are two edges labeled with the letter x then glue them together so that the arrows point the same direction. To make our job easy, topologists have discovered an algorithmic way to classify surfaces using 'words' assembled from these 'plane gluing-diagrams'.

enter image description here Choosing a corner arbitrarily (top right) and orientation (ccw) we read off the labels on the edges where an inverse appears wherever the arrow points against the orientation. In this case the 'word' that represents this plane model is given as abab.

A surface word is a string that contains the letters a,b,...,@ up to some letter @ and each letter is contained in it exactly twice. In the two occurrences of each letter: 0, 1, or 2 of them may be postfixed by a ' which I am considering using to represent 'inverse' (opposite orientation).

If in a surface word all letters appear twice: once without the ' and once with it (f.ex. ba'b'a) then we say that the surface the word represents is orientable. If a surface is orientable then it is necessarily the direct sum of n Tori for some non-negative integer n. If this condition doesn't hold (like in aab'b) then the surface represented is non-orientable: in this case it is the direct sum of m Projective Planes for some positive integer m.

Once you have found out if the reduced word is orientable or not, the final answer is given as follows. If orientable and number of unique letters in the reduced word is 1 then output should be S. Otherwise if the number of unique letters in an orientable word is n (it will be even) then the output should be sT where s = n/2. If the word is non-orientable then the output should be mP where m is the number of distinct letters in the reduced word.

The goal is to take as input some surface word, reduce it via reduction rules 1-6 and then classify it as a sphere, some number of connected tori, or some number of connected projective planes. Here are the 6 reduction rules where ~ represents 'reduces to':

Let M,A,B,C,D be surface words, x be a single letter, and juxtaposition represents concatenation:

  1. Cycle Rule: If M = AB then M ~ BA
  2. Flip Rule: M ~ M'
  3. Sphere Rule: Axx'B ~ AB
  4. Block Rule: ABC ~ ADC if B is a surface word and B ~ D by 1 or 2
  5. Cylinder Rule: If M = AxBCx'D, then M ~ AxCBx'D
  6. Möbius Rule: If M = AxBxC then M ~ AxxB'C ~ AB'xxC

I am looking for input on:

  • should this be code-golf or programming-challenge?
  • how would scoring work?
  • ???

If I feel satisfied with the question in a few days I'll post it to the site.

\$\endgroup\$
5
  • \$\begingroup\$ If, for each input, there is only one correct output, then it should probably be code-golf. The scoring criteria would then be source code length. \$\endgroup\$
    – PhiNotPi
    Commented Jun 8, 2013 at 14:33
  • \$\begingroup\$ Yes, this is the case. In general however there is not a unique series of applications of the reduction rules for any given instance. \$\endgroup\$
    – Kaya
    Commented Jun 8, 2013 at 16:21
  • \$\begingroup\$ I don't think the order of explanation is correct. You should explain reduction before talking about "the reduced word". And "reduce it via reduction rules" doesn't entirely make sense, because the rules are presented as equivalences rather than reductions, and most of them don't have a "natural" direction. \$\endgroup\$ Commented Jun 10, 2013 at 8:49
  • \$\begingroup\$ It's also occurred to me that you haven't defined the notation M'. Does it just consist of toggling the orientation of each token, or does it also reverse the entire string? And do you have test cases which between them force implementation of all of the reduction rules? \$\endgroup\$ Commented Jun 11, 2013 at 8:32
  • \$\begingroup\$ Good call on the string inverse, yes you have the right idea and I will make it clear. I have a lot of test cases from when I did a number of these computations by hand in a university course and (anecdotal experience) I am pretty sure that it is possible to force the use of all the reduction rules (except maybe 4 which is really just a meta-rule for convenience when doing proofs). Additionally you have alerted me to some concerns regarding the form of the proper output: it's definitely underspecified. I'll put some work into this today. \$\endgroup\$
    – Kaya
    Commented Jun 11, 2013 at 14:04
1
26 27
28
29 30
161

You must log in to answer this question.

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