571
\$\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

4629 Answers 4629

1
82 83
84
85 86
155
1
\$\begingroup\$

A decimal-based unit of time

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

Is this position a pure mirror mate?

\$\endgroup\$
2
  • \$\begingroup\$ I would add a test case where there is a white piece adjacent to the black king. I'd also add a test case just testing the guarding criteria. Otherwise I like it! \$\endgroup\$
    – jezza_99
    Dec 21, 2022 at 1:41
  • \$\begingroup\$ Suggested test case 8/R7/4k3/2P3P1/3K4/7B/8/8. This passes all test criteria, apart from one square (C6) being attacked by the rook and guarded by the bishop, whereas your test case 2 would return False just from a test to see if there are more than two attackers per square \$\endgroup\$
    – jezza_99
    Dec 21, 2022 at 21:00
1
\$\begingroup\$

Flip the order of operations

\$\endgroup\$
2
  • \$\begingroup\$ Your last test case (with the 2) contradicts the specs (only operators, parens and letters). \$\endgroup\$
    – pajonk
    Dec 23, 2022 at 7:28
  • \$\begingroup\$ @pajonk thanks, fixed \$\endgroup\$
    – mousetail
    Dec 23, 2022 at 7:31
1
\$\begingroup\$

Friend or Foe?

\$\endgroup\$
3
  • \$\begingroup\$ Since "Foe" is short they'd rather include in both program \$\endgroup\$
    – l4m2
    Dec 18, 2022 at 17:27
  • \$\begingroup\$ @l4m2 Good point. Maybe something like "this program is, in fact, a foe"? \$\endgroup\$ Dec 18, 2022 at 17:27
  • \$\begingroup\$ Still don't think it a good idea, slicing is easy in lots of languages \$\endgroup\$
    – l4m2
    Dec 18, 2022 at 17:32
1
\$\begingroup\$

No, thanks!

In the game “No Thanks!” playing cards numbered 3 to 35 are shuffled in a deck and 11 chips distributed to each player. The first player flips over a card and can then decide either to take the card, or to place a chip on top of it and to pass it on.

The card continues to be passed around until a player decides to take the card along with its chips. The same player flips over the next card and continues the procedure.

At the end of the game, the value of their cards get added up. Each chip they own is worth a negative point. The player with the lowest number of points wins.

Should a player have cards in succession (e.g. 18, 19, and 20), then the card with the lowest value (18) will be considered.

Rules

  • 9 cards will be removed from the deck at the start of the game

  • You will be participating with 2 other players

  • Everyone’s cards will always be visible

  • You do not have access to the amount of chips that others possess. You can, however, keep track of them.

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

Find the smallest turing machine that solves the self-counting problem

\$\endgroup\$
11
  • 1
    \$\begingroup\$ Welcome to Code Golf Stack Exchange, and interesting first challenge! I've got some comments on your submission: 1) When you say "smallest TM", how are you measuring it? fewest number of states? 2) I think the last ${0,1}$ should be ${L,R}$. 3) How are you defining input and output? 4) I would also suggest giving more background information, not everyone understands the notation used. I'm curious, does this "self-counting problem" exist in literature? (I'd be interested in articles/links.) \$\endgroup\$
    – user41805
    Dec 26, 2022 at 19:00
  • \$\begingroup\$ @user41805 thanks for the feedback, I came up with the problem myself, I don't think there is any literature on this problem. I'll edit my question to include your suggestions \$\endgroup\$
    – raoof
    Dec 27, 2022 at 2:35
  • \$\begingroup\$ Besides, try to keep your question a little more... aligned with common punctuation rules. It'll look better that way too. And besides, what's your scoring criterion? \$\endgroup\$ Dec 27, 2022 at 13:01
  • \$\begingroup\$ @UndoneStudios it's seems like inline latex is not supported is there a link for common punctuation rules. the scoring is based on number of states of the turing machine \$\endgroup\$
    – raoof
    Dec 27, 2022 at 15:30
  • \$\begingroup\$ Alternatively output 0 and 1, claiming an encoding that fit it \$\endgroup\$
    – l4m2
    Dec 28, 2022 at 7:49
  • \$\begingroup\$ 1) I meant using normal uppercase as well 2) So we have to write the code in a language that simulates a Turing Machine? Suggest using the code-challenge tag for this criterion. \$\endgroup\$ Dec 28, 2022 at 11:16
  • \$\begingroup\$ @l4m2 I didn't understand what you mean can you clarify? \$\endgroup\$
    – raoof
    Dec 28, 2022 at 15:34
  • \$\begingroup\$ @UndoneStudios no you just have to find a transition function and an encoding for the transition function \$\endgroup\$
    – raoof
    Dec 28, 2022 at 15:37
  • \$\begingroup\$ So no programming languages required? \$\endgroup\$ Dec 29, 2022 at 12:25
  • \$\begingroup\$ @raoof Also note that to fix your inline LaTeX use backslash to escape the $, like this: \$Q\$ -> \$Q\$ \$\endgroup\$ Dec 29, 2022 at 12:27
  • \$\begingroup\$ @UndoneStudios yes, no programming language is required. and thanks for the tip \$\endgroup\$
    – raoof
    Dec 29, 2022 at 19:14
1
\$\begingroup\$

RADD decomposition of an integer

\$\endgroup\$
2
  • \$\begingroup\$ What are the winning criteria? \$\endgroup\$
    – pajonk
    Dec 18, 2022 at 14:57
  • \$\begingroup\$ @pajonk I might add fastest-code, and then use time to decompose some big problems as a criteria. n in the range 400000 - 1000000. However, I don't know how to provide a comparison environment for the many languages used here. But I suspect that only non-naive methods with a better than linear time complexity have a chance. \$\endgroup\$ Dec 18, 2022 at 18:53
1
\$\begingroup\$

Maximum of outer product of integer vectors (in linear time)

\$\endgroup\$
2
  • \$\begingroup\$ I think the dyadic product as better known as the outer product. Still, it is really needed to define the problem? I think it's easier understood as, "What's the largest product you can make by selecting one number from each list?" A linear time seems OK to be me, but if you want a looser bound then polynomial time would be fine to eliminate the exponential brute-force solution. For input, the site's convention is to leave it flexible, and allow, say, taking in a list of lists however those are formatting in the language. I'd also suggest limiting to integer-valued vectors. \$\endgroup\$
    – xnor
    Dec 24, 2022 at 22:46
  • \$\begingroup\$ @xnor thx for your suggestions. I have addressed all points and added your problem formulation as an alternative. \$\endgroup\$
    – Sebastian
    Dec 30, 2022 at 12:42
1
\$\begingroup\$

Calculate the McCombination

In 2002, McDonald's advertised a McChoice menu of 8 items, labelled with "40,312 combinations." However, this number is far greater than the actual number of combinations (255; 2^8=256, minus 1 for empty meal). What the McDonald's marketing team calculated was how many different ways you could arrange a meal containing all 8 items (8! = 40,320), but subtracted 8 from the total to remove 1-item combinations. Since their original calculation was incorrect, the final result is meaningless.

The McCombination of a number n is the factorial of n, minus n.

Task:

Given an integer greater than 0 as input, output the McCombination of that number.

Test cases:

8 -> 40312
4 -> 20
9 -> 362871
3 -> 3
1 -> 0

This is , so shortest answer in bytes wins.

\$\endgroup\$
8
  • \$\begingroup\$ Welcome to Code Golf, and nice first challenge! \$\endgroup\$
    – Ginger
    Jan 5, 2023 at 16:49
  • \$\begingroup\$ Seems a bit boring, most languages have built-ins for both parts of this \$\endgroup\$
    – mousetail
    Jan 5, 2023 at 17:09
  • 1
    \$\begingroup\$ @mousetail do you think it would be better if it were something like “generate the first n McCombinayion numbers” or something \$\endgroup\$
    – noodle man
    Jan 5, 2023 at 19:36
  • \$\begingroup\$ @Jacob that doesn't make it more interesting. Stacking trivial problems on top of each-other doesn't make a challenge interesting. Plus if you did that you'd no longer allow the standard sequence rules \$\endgroup\$
    – mousetail
    Jan 7, 2023 at 16:05
  • \$\begingroup\$ Alright then. So if this doesn't seem like a good problem, should I delete this post? Not very familiar with how the sandbox works @mousetail \$\endgroup\$
    – noodle man
    Jan 7, 2023 at 16:13
  • 1
    \$\begingroup\$ You are free to delete or not delete, some people delete their posts others just abandon them. There's no convention one way or the other \$\endgroup\$
    – mousetail
    Jan 7, 2023 at 16:14
  • \$\begingroup\$ Also don't be discouraged, my first few questions where terrible too \$\endgroup\$
    – mousetail
    Jan 7, 2023 at 16:22
  • \$\begingroup\$ Okay thanks. I think I'll just leave this here then \$\endgroup\$
    – noodle man
    Jan 7, 2023 at 16:33
1
\$\begingroup\$

Guess the song title

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

Division between two strings

Inverse function of this challenge

To multiply two strings, you take two strings and compare each character. The character with the highest code point is then added to the output. If they are equal, simply add the character to the output.1

Given an input and the output, provide a random2 input such that multiplying the two inputs returns the output. Each possible input should be possibly generated. If there's no possible input, output nothing or something that isn't a string(crashing is fine, but not infinite loop)2.

1 I know it's not a multiply but that's copyed as-is
2 Otherwise a cat solves the question

\$\endgroup\$
3
  • \$\begingroup\$ How exactly should we generate with "random"? \$\endgroup\$ Jan 10, 2023 at 5:00
  • \$\begingroup\$ @UndoneStudios Each possible input should be possibly generated. \$\endgroup\$
    – l4m2
    Jan 10, 2023 at 5:03
  • \$\begingroup\$ Can you clarify a bit further? Perhaps an example testcase? \$\endgroup\$ Jan 10, 2023 at 5:05
1
\$\begingroup\$

WIP: clarify

Connect WEST and EAST using n pipe.

This is an example output, where n=9.

n=9

You can make sub-component, which each pin behave an extra pipe. Sub-component cost 2 pipe. Following is n=19. (The sub-component at the center is image above) You can also make sub-component in sub-component, and et infinitum.

n=19

It has same cost and behavior as

n=19

Thing change when n=35, where using sub-component only cost 33 pipe:

enter image description here

Now given odd n>8, output the minimum cost to connect WEST and EAST using n pipe.

9 => 9
19 => 19
35 => 33

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

Decimal to String with Mandatory Length

Tags:

Introduction:

Inspired by this SO question, which asks for the most accurate precision of decimal values using either rounding or scientific notation as string, with at most 15 characters long. This would include the -, ., and E in the output-string.

Challenge:

Inputs:

  • A decimal value \$s\$
  • An integer output-length \$n\$

Output:

  • A string of the most accurate representation of the given decimal value, with a length exactly equal to the output-length \$n\$. NOTE: the linked SO question ask for at most 15 characters long, but this challenge asks for exactly \$n\$ characters long instead.

Challenge rules:

  • The output-length input is guaranteed to be \$n\geq7\$
  • The decimal input is guaranteed to be valid and non-empty
  • You are allowed to take the input-decimal \$s\$ as string
  • The input-decimal is guaranteed to only contain the characters 0123456789-., and will not start with an . (but 0. instead), nor start with unnecessary leading 0s like 001.23 instead of 1.23 (except for 0.).
  • The output-string is guaranteed to only contain the characters 0123456789-.E (or e instead of E if you choose so). The input-format restrictions mentioned one bullet-point above, do NOT apply for the output-format however!
  • If the length of the integer part of a number (including - for negative values) is larger than the given output-length: use a scientific notation (with either e or E) and rounded precision. I.e. with inputs s = "-987654321987654321.987654321"; n = 15, the output is supposed to be one of these: ["-987654321.99E9", "-9876543219.9E8", "-98765432199.E7"]
  • It is allowed to add leading 0s to get to length \$n\$ without changing its decimal (base-10) value. I.e. with inputs s = "-123.00"; n = 15, the output "-00000000000123" would be valid.
  • It is allowed to add trailing 0s to the decimal parts to get to length \$n\$ without changing its decimal (base-10) value. I.e. with inputs s = "-1.23"; n = 15, the output "-1.230000000000" or "-1.2300000000E0" are both valid. (With inputs s = "-0.123456789123456789"; n = 15, the only possible output is "-.1234567891235".)
  • Please specify which rounding type your language is using! (E.g. HALF_UP, HALF_DOWN, HALF_EVEN, BANKERS, etc.) My test cases where this is relevant use HALF_UP.
  • If multiple outputs are possible, just output one, multiple, or all of them.

General rules:

  • This is , so shortest answer in bytes wins.
    Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.
  • Standard rules apply for your answer with default I/O rules, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
  • Default Loopholes are forbidden.
  • If possible, please add a link with a test for your code (i.e. TIO).
  • Also, adding an explanation for your answer is highly recommended.

Test cases:

All these test cases will use the output-length as 15 (and rounding mode HALF_UP):

Input:                           Possible Outputs:
"987654321987654321.987654321"   "987654321.988E9","9876543219.88E8","98765432198.8E7","987654321988.E6"
"-987654321987654321.987654321"  "-987654321.99E9","-9876543219.9E8","-98765432199.E7"
"1234567891234567.123456789"     "1234567.89123E9","12345678.9123E8","123456789.123E7","1234567891.23E6","12345678912.3E5","123456789123.E4"
"-1234567891234567.123456789"    "-1234567.8912E9","-12345678.912E8","-123456789.12E7","-1234567891.2E6","-12345678912.E5"
"0.123456789123456789"           ".12345678912346"
"-0.123456789123456789"          "-.1234567891235"
"5.5555555555555555555555555"    "5.5555555555556"
"-5.5555555555555555555555555"   "-5.555555555556"
"123456789123456"                "123456789123456"
"-123456789123456"               "-123456.78912E9","-1234567.8912E8","-12345678.912E7","-123456789.12E6","-1234567891.2E5","-12345678912.E4"
"123.00"                         "123.00000000000","0123.0000000000","00123.000000000","000123.00000000","0000123.0000000","00000123.000000","000000123.00000","0000000123.0000","00000000123.000","000000000123.00","0000000000123.0","00000000000123.","000000000000123","123.000000000E0","0123.00000000E0","00123.0000000E0","000123.000000E0","0000123.00000E0","00000123.0000E0","000000123.000E0","0000000123.00E0","00000000123.0E0","000000000123.E0","12.3000000000E1","012.300000000E1","0012.30000000E1","00012.3000000E1","000012.300000E1","0000012.30000E1","00000012.3000E1","000000012.300E1","0000000012.30E1","00000000012.3E1","1.23000000000E2","01.2300000000E2","001.230000000E2","0001.23000000E2","00001.2300000E2","000001.230000E2","0000001.23000E2","00000001.2300E2","000000001.230E2","0000000001.23E2",".123000000000E3","0.12300000000E3","00.1230000000E3","000.123000000E3","0000.12300000E3","00000.1230000E3","000000.123000E3","0000000.12300E3","00000000.1230E3","000000000.123E3","1230.0000000E-1","01230.000000E-1","001230.00000E-1","0001230.0000E-1","00001230.000E-1","000001230.00E-1","0000001230.0E-1","00000001230.E-1","000000001230E-1","12300.000000E-2","012300.00000E-2","0012300.0000E-2","00012300.000E-2","000012300.00E-2","0000012300.0E-2","00000012300.E-2","000000012300E-2","123000.00000E-3","0123000.0000E-3","00123000.000E-3","000123000.00E-3","0000123000.0E-3","00000123000.E-3","000000123000E-3","1230000.0000E-4","01230000.000E-4","001230000.00E-4","0001230000.0E-4","00001230000.E-4","000001230000E-4","12300000.000E-5","123000000.00E-5","1230000000.0E-5","12300000000.E-5","123000000000E-5","123000000.00E-6","1230000000.0E-6","12300000000.E-6","123000000000E-6","1230000000.0E-7","12300000000.E-7","123000000000E-7","12300000000.E-8","123000000000E-8","123000000000E-9"
"-123.00"                        "-123.0000000000","-0123.000000000","-00123.00000000","-000123.0000000","-0000123.000000","-00000123.00000","-000000123.0000","-0000000123.000","-00000000123.00","-000000000123.0","-0000000000123.","-00000000000123","-123.00000000E0","-0123.0000000E0","-00123.000000E0","-000123.00000E0","-0000123.0000E0","-00000123.000E0","-000000123.00E0","-0000000123.0E0","-00000000123.E0","-000000000123E0","-12.300000000E1","-012.30000000E1","-0012.3000000E1","-00012.300000E1","-000012.30000E1","-0000012.3000E1","-00000012.300E1","-000000012.30E1","-0000000012.3E1","-1.2300000000E2","-01.230000000E2","-001.23000000E2","-0001.2300000E2","-00001.230000E2","-000001.23000E2","-0000001.2300E2","-00000001.230E2","-000000001.23E2","-.12300000000E3","-0.1230000000E3","-00.123000000E3","-000.12300000E3","-0000.1230000E3","-00000.123000E3","-000000.12300E3","-0000000.1230E3","-00000000.123E3","-1230.000000E-1","-01230.00000E-1","-001230.0000E-1","-0001230.000E-1","-00001230.00E-1","-000001230.0E-1","-0000001230.E-1","-00000001230E-1","-12300.00000E-2","-012300.0000E-2","-0012300.000E-2","-00012300.00E-2","-000012300.0E-2","-0000012300.E-2","-00000012300E-2","-123000.0000E-3","-0123000.000E-3","-00123000.00E-3","-000123000.0E-3","-0000123000.E-3","-00000123000E-3","-1230000.000E-4","-01230000.00E-4","-001230000.0E-4","-0001230000.E-4","-00001230000E-4","-12300000.00E-5","-123000000.0E-5","-1230000000.E-5","-12300000000E-5","-123000000.0E-6","-1230000000.E-6","-12300000000E-6","-1230000000.E-7","-12300000000E-7","-12300000000E-8"

TODO: Fix the possible outputs of the other test cases as well:

"0.123"                          ".12300000000000"
"-0.123"                         "-.1230000000000"
"1.23"                           "1.2300000000000"
"-1.23"                          "-1.230000000000"
"0.000000000000001"              "10000000000E-25"
"0"                              "000000000000000"

All these test cases will use the output-length as 7 (and rounding mode HALF_UP):

Input:                           Output:
"987654321987654321.987654321"   "9.88E17"
"-987654321987654321.987654321"  "-9.9E17"
"1234567891234567.123456789"     "1.23E15"
"-1234567891234567.123456789"    "-1.2E15"
"0.123456789123456789"           ".123457"
"-0.123456789123456789"          "-.12346"
"5.5555555555555555555555555"    "5.55556"
"-5.5555555555555555555555555"   "-5.5556"
"123456789123456"                "1.23E14"
"-123456789123456"               "-1.2E14"
"123.00"                         "0000123" or "01.23E2" or "1.230E2"
"-123.00"                        "-000123" or "-12.3E1"
"0.123"                          ".123000"
"-0.123"                         "-.12300"
"1.23"                           "1.23000"
"-1.23"                          "-1.2300"
"0.000000000000001"              "100E-17" or "1.0E-15"
"0"                              "0000000",".000000","0.00000","00.0000","000.000","0000.00","00000.0","000000.","0E12345","000E-98"",etc.,etc. :/
\$\endgroup\$
12
  • 1
    \$\begingroup\$ Suggest testcase: "0.0000000001", 10 -> ".000000000", "0", 10, "0000000000" \$\endgroup\$
    – tsh
    Jan 13, 2020 at 1:50
  • \$\begingroup\$ @tsh Added (although I've used n=15 instead of 10 so I could add it to the other list instead of creating two separated test cases for the n=10. Principle remains the same for your test cases, so thanks for the suggestion! \$\endgroup\$ Jan 13, 2020 at 7:44
  • \$\begingroup\$ @tsh Your test case actually made me realize that s="0.0000000001", n=10 should be "100000E-15" instead for the most accurate result. Will have to do some fixes to my reference implementation. \$\endgroup\$ Jan 13, 2020 at 7:54
  • \$\begingroup\$ Why should it be 100E-17, not 1.0E-15? The rule makes me confusing. \$\endgroup\$
    – tsh
    Jan 13, 2020 at 9:14
  • \$\begingroup\$ @tsh 1.0E-15 would be allowed as well. I still have to change the rules after realizing that 0.00000 would be an incorrect result for s="0.0000000001", n=7, but both 100E-17 and 1.0E-15 are allowed, since they are of length 7 and retain the same exact value as 0.0000000001. I currently don't have the time to revise the rules, test cases, and reference implementation unfortunately (and if I delete the Sandbox post temporarily I can't search back for it). \$\endgroup\$ Jan 13, 2020 at 9:17
  • \$\begingroup\$ You say this challenge asks for exactly 15 characters long but you then have outputs of varying length. I assume you want the latter. I'm being pedantic, I know, but it's the only thing I can find to fix at the moment ;) \$\endgroup\$
    – lyxal
    Jan 13, 2020 at 9:28
  • \$\begingroup\$ @Jono2906 I've changed the part at the output-section. I hope it's a bit clearer now? \$\endgroup\$ Jan 13, 2020 at 9:33
  • \$\begingroup\$ @KevinCruijssen it is clearer now. I mean, as I said, I was just being pedantic about things. \$\endgroup\$
    – lyxal
    Jan 13, 2020 at 9:34
  • \$\begingroup\$ @Jono2906 Well, it was still a valid remark that I agree with, so thanks. :) \$\endgroup\$ Jan 13, 2020 at 9:36
  • \$\begingroup\$ Just another thing: Perhaps The decimal string input is guaranteed to be valid and non-empty should be The decimal string input is guaranteed to be a valid float and non-empty \$\endgroup\$
    – lyxal
    Jan 13, 2020 at 10:41
  • \$\begingroup\$ @Jono2906 Textual there isn't a difference between decimal/double/float, though. I had the sentence in my head as "the decimal string input is guaranteed to be (a) valid (decimal) and non-empty". I could change it to that if it makes it clearer, but talking about decimal first and float after that is more confusing than clarifying imho. But if you indeed meant "the decimal string input is guaranteed to be a valid decimal and non-empty" I'll change it. \$\endgroup\$ Jan 13, 2020 at 11:49
  • \$\begingroup\$ @KevinCruijssen Yep, I indeed meant decimal. I've no clue why I said float. \$\endgroup\$
    – lyxal
    Jan 13, 2020 at 20:10
1
\$\begingroup\$

Form a subset that is a continuous range

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

Implement a bag without replacement

Tags:

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

Yet another Colatz challenge

Background

We all know and love the colatz sequence. It goes like this:

  • If a number is divisible by 2, divide by 2
  • Otherwise, multiply by 3 and add 1.

The Colatz conjecture theorizes that if you repeat this process for any number, you'll eventually reach 1.

Binary

If you write colatz numbers in binary, any numbers divisible by 2 will be followed by a 0. This means we can ignore any trailing 0s at the end of the number in binary notation, as the 0s will be removed first anyways. Thus we only need to consider the odd numbers. For the purpose of this challenge, we'll write it like this:

1[0]

This means "1 followed by any number of 0s". Every colatz number must pass through this number in this form at some point (since no odd number * 3 + 1 = 1). If we then subtract 1, we get this:

[1]

Then divide by 3 we get this:

[01]

Note that only even numbers of 1 are divisible by 3, so we only consider them. Here, we can prove that the last odd number in any numbers colatz sequence must be of this form. Of course, the ancestors in the same family will look like that:

[01][0]

Continuing the pattern

We can continue the pattern to find general forms for the nth ancestor of 1 in the colatz sequence. We start like this:

-1 [01]00[1]
/3 [000111]00[01][0]
-1 [000111]00[01]00[1] (or if [01] is repeated 0 times [000111]00011011111[1])
/3 [000010010111101101000010010111101101]00[000111]00[01][0]

Note: Now, when subtracting 1 the pattern branches. Numbers inside the brackets can be repeated 0 or more times. If they are repeated at least once the 1 can "absorb" the -1 from the right and prevent it from effecting the pattern to it's left. However, if repeated 0 times subtracting one would also flip the 0s to it's left until the first 1 is encountered. For the purpose of this challenge you only need to consider the branch of the path where each pattern is repeated at least once.

The challenge

Your task is to output the general form of the nth-element in the odd only colatz sequence, assuming the branch where each pattern appears at least once.

  • Output must be in binary
  • You may use any reasonable method to mark which sequences can be repeated
  • You may choose to output only the odd or only the even members of the sequence if you want
  • You can loop forever, or output the nth element given a n, rules

This is code golf, shortest code wins

\$\endgroup\$
1
  • \$\begingroup\$ Actually, I don't know the colatz sequence. I do know the Collatz sequence... ;P \$\endgroup\$
    – DLosc
    Feb 15, 2023 at 18:48
1
\$\begingroup\$

Capture the Flag... with a twist

\$\endgroup\$
2
  • \$\begingroup\$ I'm having trouble understanding the challenge. Could you add an example with a bit more explanation? \$\endgroup\$
    – noodle man
    Jan 18, 2023 at 23:51
  • \$\begingroup\$ @Jacob - added. Let me know if I need to add another one. \$\endgroup\$
    – The Thonnu
    Jan 19, 2023 at 7:24
1
\$\begingroup\$

Implement a 2Fuck Interpreter

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

Output code to sort a list without looping

Given a integer n, output the source code for a program or function that can sort a list of n elements without looping. For any n there are a finite number of possible orderings so this has to be possible.

Looping includes any way to run code a variable number of times. It also includes recursion or using any built-ins that internally loop. (including your sort builtin) Branching is ok.

The code you submit may have loops, just the code outputted may not.

The lists you sort will be positive integers in the range \$ 1 \leq n \leq 2^{16} - 1\$. Note 0 is excluded. The list may contain duplicate elements.

For example:

N Possible Output
1 |x:&mut[u16;1]|()
2 |x:&mut[u16;2]|*x=if x[1]>x[0]{[x[0],x[1]]}else{[x[1],x[0]]}

Note the program you output can be as long as you like, your score is only the length of the program that generates the sorting function.

The program you output may be in a different language then the generating program, in this case list both languages in your header. Both may use any IO format from our standard input/output methods list.

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

Given an array of array of array of ..., convert it into a string. Brackets should be used to show depth.

However, the input may include loops (e.g. an array containing itself), which would lead to an infinite output without extra rule. Therefore, if an array has been expanded twice (and is trying to expand a third time), it should be expanded into something else.

Since some languages don't support recursive arrays, it's also allowed to take a list of list of indices \$[A_1,A_2,\ldots]=[[a_{11}, a_{12},\ldots],[a_{21},a_{22},\ldots],\ldots]\$, where each integer \$a_{ij}\$ should be replaced with the expanded array \$A_j\$.

Shortest code wins.

Test cases:

raw indices result
[] [] []
[[],[]] [1,1],[] [[],[]]
A=[A,A] [0,0] [[-,-],[-,-]]
[A=[B],B=[A]] [1,2],[2],[1] [[[[[-]]]],[[[[-]]]]]

Function in javascript to convert indices into array:

function indices2array(arr) {
    var ret = arr.map(_=>[]);
    for (var i in arr) {
        for (var j of arr[i]) ret[i].push(ret[j]);
        ret[i].id = +i;
    }
    return ret[0];
}

Idea from recursive iframe. On Firefox, it expands using this rule.

enter image description here

A<br><iframe src="B.html"></iframe>
B<br><iframe src="A.html"></iframe>
X<br><iframe height="400" src="A.html"></iframe><iframe height="400" src="B.html"></iframe>

Given function that expands once, is it easy to expand twice?

\$\endgroup\$
8
  • \$\begingroup\$ What if my language does not support arrays that have themselves as sub-array? E.g. it is impossible to construct if you can't get a reference to (only the value of) an array. \$\endgroup\$
    – Adám
    Dec 28, 2022 at 7:51
  • \$\begingroup\$ @Adám Add explicit allowance \$\endgroup\$
    – l4m2
    Dec 28, 2022 at 7:55
  • \$\begingroup\$ "Since some language don't support recursive array, it's also allowed to take a list of list of indices, each index meaning the responding list." feels like this sentence change the whole challenge into another one... \$\endgroup\$
    – tsh
    Dec 29, 2022 at 7:57
  • \$\begingroup\$ @tsh How? Just each X[i] => C[X[i]] \$\endgroup\$
    – l4m2
    Dec 29, 2022 at 8:41
  • \$\begingroup\$ Is a=[[[]]]; a[0][0][0]=[[[a[0][0]]]]; a valid testcase? \$\endgroup\$
    – tsh
    Dec 29, 2022 at 8:58
  • \$\begingroup\$ @tsh Should be covered by [A=[B],B=[A]] \$\endgroup\$
    – l4m2
    Dec 29, 2022 at 9:03
  • \$\begingroup\$ I cannot understand your [A=[B],B=[A]], is this some valid syntax in some language? \$\endgroup\$
    – tsh
    Dec 29, 2022 at 10:06
  • \$\begingroup\$ @tsh x=[A=[],B=[]],A[0]=B,B[0]=A input or [ <ref *1> [ [ [Circular *1] ] ], <ref *2> [ <ref *1> [ [Circular *2] ] ] ] output \$\endgroup\$
    – l4m2
    Dec 29, 2022 at 14:00
1
\$\begingroup\$

Knook to Mate... With Portals!

\$\endgroup\$
3
  • \$\begingroup\$ In this image you may want to mark someway that B3 can't be reached, right now you use the arrow both to mean inclusive range and just the endpoint \$\endgroup\$
    – mousetail
    Jan 20, 2023 at 6:42
  • \$\begingroup\$ cdn.discordapp.com/attachments/968416999808057344/… is d6 also valid? \$\endgroup\$
    – l4m2
    Jan 20, 2023 at 7:14
  • \$\begingroup\$ @l4m2 d6 is indeed also valid. Positions may have multiple solutions \$\endgroup\$
    – lyxal
    Jan 20, 2023 at 8:03
1
\$\begingroup\$

Longest N-Sum Sub-Array

Write a program or function which when given an array of non-negative integers and a number \$N\$, output the longest contiguous sub-array whose sum of elements adds up to \$N\$:

$$S = \{ (x, y) : \sum_{n=x}^{y} a_n = N \}$$

$$\text{Output: } argmax_S (y - x)$$

Constraints

  • If there are multiple solutions, output any of them or all of them (choose one).
  • You may output the solution in any reasonable format, which must allow for the following point.
  • There will always be a solution, but it can be the empty list.
  • \$N \ge 0\$.

Test cases

[input array], N -> [output array]

[1, 0, 0, 1, 0], 1 -> [0, 0, 1, 0]
[1, 0, 0, 1, 0], 0 -> [0, 0]
[1, 2, 3, 4, 5], 0 -> []
[1, 2, 3, 4, 5], 5 -> [2, 3]
[4, 3, 1, 1, 0, 5, 2], 8 -> [1, 0, 5, 2]
[0], 0 -> [0]
[], 0 -> []

This is , so shortest code in bytes wins.

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

Toggle my Comments

Write a program which takes a string as input. If the string is a syntactically valid comment in the program's language, the program must output an uncommented version of the string. Otherwise, it must output a syntactically valid commented version. Programs may use either multiline or single-line comments. Languages that do not support comments may not compete.

Python example

Input: spaghetti
Possible output: #spaghetti
Possible output: # spaghetti

Input: # spaghetti
Output: spaghetti

Input: #spaghetti
Output: spaghetti
```
\$\endgroup\$
1
\$\begingroup\$

Toggle Hangul Vowel Harmony

(This post is partly self-plagiarized.)

Objective

Given a Hangul syllable, toggle its vowel harmony.

Introduction to Hangul syllables

Hangul(한글) is the Korean writing system invented by Sejong the Great. Hangul syllables are allocated in Unicode point U+AC00 – U+D7A3. A Hangul syllable consists of an initial consonant, a vowel, and an optional final consonant.

The initial consonants are:

ㄱ ㄲ ㄴ ㄷ ㄸ ㄹ ㅁ ㅂ ㅃ ㅅ ㅆ ㅇ ㅈ ㅉ ㅊ ㅋ ㅌ ㅍ ㅎ

The vowels are:

ㅏ ㅐ ㅑ ㅒ ㅓ ㅔ ㅕ ㅖ ㅗ ㅘ ㅙ ㅚ ㅛ ㅜ ㅝ ㅞ ㅟ ㅠ ㅡ ㅢ ㅣ

The final consonants are:

(none) ㄱ ㄲ ㄳ ㄴ ㄵ ㄶ ㄷ ㄹ ㄺ ㄻ ㄼ ㄽ ㄾ ㄿ ㅀ ㅁ ㅂ ㅄ ㅅ ㅆ ㅇ ㅈ ㅊ ㅋ ㅌ ㅍ ㅎ

For example, has initial consonant , vowel , and final consonant .

South Korean dictionary order

The consonants and vowels above are sorted in South Korean dictionary order. The syllables are firstly sorted by initial consonants, secondly by vowels, and finally by (optional) final consonants.

The Unicode block for Hangul syllables contains every consonant/vowel combinations, and is entirely sorted in South Korean dictionary order.

The Unicode block can be seen here, and the first 256 characters are shown for illustrative purpose:

가각갂갃간갅갆갇갈갉갊갋갌갍갎갏감갑값갓갔강갖갗갘같갚갛개객갞갟갠갡갢갣갤갥갦갧갨갩갪갫갬갭갮갯갰갱갲갳갴갵갶갷갸갹갺갻갼갽갾갿걀걁걂걃걄걅걆걇걈걉걊걋걌걍걎걏걐걑걒걓걔걕걖걗걘걙걚걛걜걝걞걟걠걡걢걣걤걥걦걧걨걩걪걫걬걭걮걯거걱걲걳건걵걶걷걸걹걺걻걼걽걾걿검겁겂것겄겅겆겇겈겉겊겋게겍겎겏겐겑겒겓겔겕겖겗겘겙겚겛겜겝겞겟겠겡겢겣겤겥겦겧겨격겪겫견겭겮겯결겱겲겳겴겵겶겷겸겹겺겻겼경겾겿곀곁곂곃계곅곆곇곈곉곊곋곌곍곎곏곐곑곒곓곔곕곖곗곘곙곚곛곜곝곞곟고곡곢곣곤곥곦곧골곩곪곫곬곭곮곯곰곱곲곳곴공곶곷곸곹곺곻과곽곾곿

Vowel Harmony

Korean vowels express vowel harmony as positive-negative pairs. They're paired like the followings:

(Positive) - (Negative)
ㅏ - ㅓ
ㅐ - ㅔ
ㅑ - ㅕ
ㅒ - ㅖ
ㅗ - ㅜ
ㅘ - ㅝ
ㅙ - ㅞ
ㅚ - ㅟ
ㅛ - ㅠ
(ㅡ, ㅢ, and ㅣ lack counterparts)

Examples

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

It's a dog!

\$\endgroup\$
2
  • \$\begingroup\$ Picture show tail something like leg but don't touch floor; I usually see tail in real life touch floor with lot laid, though \$\endgroup\$
    – l4m2
    Jan 22, 2023 at 5:37
  • 1
    \$\begingroup\$ You may want to specify that the legs need to be separate (i.e. cannot share a common "border"). As per the tail one option is to simplify the task only to check a simple tail (or a "tail ending"), e.g. (space)_ \\ [_ and the leftmost bracket touches the left border (and perhaps is the only non-space character there). \$\endgroup\$
    – pajonk
    Jan 24, 2023 at 13:53
1
\$\begingroup\$

Determine the Octet(/Duplet/18-electron) Rule

Tags:

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

An address is decoded as follow:

   reg eax ecx edx ebx esp ebp esi edi  0  sib
    id  0   1   2   3   4   5   6   7   -   -
+0      00  01  02  03  /   /   06  07  /   04
+imm8   40  41  42  43  /   45  46  47  /   44
+imm32  80  81  82  83  /   85  86  87  05  84

where sib means the following byte parsed as follow:

    +  eax ecx edx ebx esp ebp/imm32*esi edi
   eax  00  01  02  03  04     05     06  07
   ecx  08  09  0a  0b  0c     0d     0e  0f
   edx  10  11  12  13  14     15     16  17
   ebx  18  19  1a  1b  1c     1d     1e  1f
     0  20  21  22  23  24     25     26  27
   ebp  28  29  2a  2b  2c     2d     2e  2f
   esi  30  31  32  33  34     35     36  37
   edi  38  39  3a  3b  3c     3d     3e  3f
 2*eax  40  41  42  43  44     45     46  47
 2*ecx  48  49  4a  4b  4c     4d     4e  4f
 2*edx  50  51  52  53  54     55     56  57
 2*ebx  58  59  5a  5b  5c     5d     5e  5f
 2*esp  60  61  62  63  64     65     66  67
 2*ebp  68  69  6a  6b  6c     6d     6e  6f
 2*esi  70  71  72  73  74     75     76  77
 2*edi  78  79  7a  7b  7c     7d     7e  7f
 4*eax  80  81  82  83  84     85     86  87
 4*ecx  88  89  8a  8b  8c     8d     8e  8f
 4*edx  90  91  92  93  94     95     96  97
 4*ebx  98  99  9a  9b  9c     9d     9e  9f
 4*esp  a0  a1  a2  a3  a4     a5     a6  a7
 4*ebp  a8  a9  aa  ab  ac     ad     ae  af
 4*esi  b0  b1  b2  b3  b4     b5     b6  b7
 4*edi  b8  b9  ba  bb  bc     bd     be  bf
 8*eax  c0  c1  c2  c3  c4     c5     c6  c7
 8*ecx  c8  c9  ca  cb  cc     cd     ce  cf
 8*edx  d0  d1  d2  d3  d4     d5     d6  d7
 8*ebx  d8  d9  da  db  dc     dd     de  df
 8*esp  e0  e1  e2  e3  e4     e5     e6  e7
 8*ebp  e8  e9  ea  eb  ec     ed     ee  ef
 8*esi  f0  f1  f2  f3  f4     f5     f6  f7
 8*edi  f8  f9  fa  fb  fc     fd     fe  ff
* imm32 if no another imm

If an imm8 or imm32 is used, the value is followed after, little-endian.

Given a value of {0,1,2,4,8}*reg+{reg,0}+imm32, output a shortest expression of the address.

Test cases

eax => 00
eax+8 => 40 08
eax+128 => 80 80 00 00 00
esp => 04 24
ebp => 45 00
4*eax => 04 85 00 00 00 00
2*eax => 04 00
2*esp => 04 60 00 00 00 00 

Sandbox Notes

  • Since it's modified(enabled to allow esp*4), and I'd allow use of id, should I remove the eax expression?
\$\endgroup\$
1
\$\begingroup\$

Translate to plorcly borglar

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

How to find the counterfeit coin?

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

How spherical is my ellipsoid?

\$\endgroup\$
1
82 83
84
85 86
155

You must log in to answer this question.

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