560
\$\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".

Get the Sandbox Viewer to view the sandbox more easily!

\$\endgroup\$
0

4201 Answers 4201

1
21 22
23
24 25
141
3
\$\begingroup\$

Runs of Ones (What Fun!)

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Not sure if only on my browser: Width of ✓ and number are different. So last ✓ is under ] instead of 9. \$\endgroup\$
    – tsh
    Jul 26, 2022 at 5:55
  • \$\begingroup\$ Apparently, the monospace font doesn't include Unicode characters, oh well \$\endgroup\$ Jul 26, 2022 at 14:47
3
\$\begingroup\$

Remove redundant parenthesis

\$\endgroup\$
2
  • 3
    \$\begingroup\$ Tags: balanced-string, string \$\endgroup\$
    – Steffan
    Jul 25, 2022 at 23:36
  • \$\begingroup\$ If there never will be an empty pair, you probably shouldn't give (abc((123))()) as an example. \$\endgroup\$ Aug 1, 2022 at 14:19
3
\$\begingroup\$

A better Hexagony template

\$\endgroup\$
4
  • \$\begingroup\$ 4000th answer to this question, hooray \$\endgroup\$ Aug 10, 2022 at 12:58
  • \$\begingroup\$ "For convenience, define \$a(0)=0\$ (for zero-sized grid)." \$a(0)=3\times0(0−1)+1=1\$ though. Does the \$n=1\$ output have to be *, or are we allowed to output . instead as well? In terms of output it doesn't really make sense, but for the formula it does (and it would save me 2 bytes in my prepared solution 😅). \$\endgroup\$ Aug 11, 2022 at 11:40
  • \$\begingroup\$ @Kevin I meant size 0 grid has zero spots and size 1 has one, but now that I think of it, I don't know if Hexagony interpreter special-cases empty code or it just treats it as size 1. Something to think about... \$\endgroup\$
    – Bubbler
    Aug 11, 2022 at 14:02
  • 1
    \$\begingroup\$ @KevinCruijssen I decided to allow both :) \$\endgroup\$
    – Bubbler
    Aug 11, 2022 at 22:56
3
\$\begingroup\$

CGCC Rocket Biking

\$\endgroup\$
5
  • \$\begingroup\$ Suggestion: Split it to two challenges (validating and generating instructions). \$\endgroup\$
    – pajonk
    Mar 27, 2022 at 18:00
  • \$\begingroup\$ Good challenge! Just one question: what does may not save the input while reading mean? Does "save" just mean saving to a file, or does it include variables too? In some languages, it would be impossible to do anything with input without saving it somewhere, even if not in an explicitly named variable. \$\endgroup\$ Mar 27, 2022 at 18:02
  • \$\begingroup\$ @pajonk That's a good idea, thanks! \$\endgroup\$ Mar 28, 2022 at 12:22
  • 1
    \$\begingroup\$ @SylvesterKruin I think the idea behind that could also be written as "The program may not work on more than one character of the input at any time.", i.e. reading the entire input bit-by-bit into a var and doing transformations on that isn't allowed. I used the wording from the file directly, which wasn't a good idea as it is rather lacking in other places too. \$\endgroup\$ Mar 28, 2022 at 12:23
  • 1
    \$\begingroup\$ this does seem like an interesting challenge! I do think the while thing about only reading the input once is A) confusing and mostly unnecessary, and B) kinda against the meta consensus against unobservable requirements. The code obviously takes a single text string in somehow, but what it does with that is not controllable. Also seconding the suggestion to split this into two challenges/get rid of one of them, as determining whether it is possible is a different matter from determining how to precisely stop at the end \$\endgroup\$
    – des54321
    Apr 3, 2022 at 1:08
3
\$\begingroup\$

Polyglot Quiz (Cops' thread) Polyglot Quiz (Robber's thread)

\$\endgroup\$
8
  • 3
    \$\begingroup\$ Not a comment on the challenge itself, but I like the four-state CnR system! \$\endgroup\$ Aug 8, 2022 at 11:21
  • \$\begingroup\$ Does the cop reveal which of the 4 programs satisfy which of the criteria 1-4? \$\endgroup\$
    – pajonk
    Aug 8, 2022 at 11:39
  • \$\begingroup\$ @pajonk Yes they should. I'll edit it to make it clear. \$\endgroup\$
    – Wheat Wizard Mod
    Aug 8, 2022 at 11:39
  • \$\begingroup\$ @UnrelatedString I have a feeling this is equivalent to "standard" CnR, where to become safe you need to reveal your language (or other features the challenge is about) - or are there some differences I missed? \$\endgroup\$
    – pajonk
    Aug 8, 2022 at 11:42
  • 2
    \$\begingroup\$ @pajonk In a normal CnR if a robber ever gets your answer you cannot score. However here your answer is cracked after 10 days you still get to score your answer. On a lot of CnR's people will delay the reveal to give robbers an extra chance, this ruleset allows you to do that without having to sacrifice your score. \$\endgroup\$
    – Wheat Wizard Mod
    Aug 8, 2022 at 11:45
  • \$\begingroup\$ @WheatWizard I get it now, thanks! This indeed makes more sense. \$\endgroup\$
    – pajonk
    Aug 8, 2022 at 12:16
  • \$\begingroup\$ Requiring that languages can run on FreeBSD seems unnecessarily restrictive to me, it being a pretty niche OS in the grand scheme of things. \$\endgroup\$
    – pxeger
    Aug 15, 2022 at 16:15
  • \$\begingroup\$ @pxeger The idea is that if it is Free, and runs on both linux and freeBSD it is very likely to run everywhere. And users should be able to sandbox either linux or freeBSD. It also doesn't seem like a very high bar to cross actually. If you can find a language that can't be run on freeBSD but has otherwise wide support that would make a compelling case. \$\endgroup\$
    – Wheat Wizard Mod
    Aug 15, 2022 at 17:52
3
\$\begingroup\$

Quantise a list

In music production, quantisation is the process of "aligning" recorded notes to the beat, to remove the variability caused by imprecise human playing.

In this challenge, we will implement an abstract and approximate version of quantisation, representing notes as runs of positive integers, and using zeroes to represent gaps. The "beat" will be defined as every second note, starting with the first (even indices, 0-based).

Given a list of non-negative integers, insert or remove zeroes such that every run of non-zeroes begins on an even index (using 0-based indexing).

Runs of non-zeroes may not be split or joined together if they weren't already in the input. In other words, a zero cannot be inserted or removed if it only has non-zeroes next to it.

The minimal number of insertion/removal operations must be performed. If a removal is possible, it should be preferred over an insertion.

Test cases

todo

blah blah code golf rules

\$\endgroup\$
6
  • \$\begingroup\$ Removal of double negatives: "No runs of non-zeroes may be split..." -> "Runs of non-zeroes may not be split...", maybe also "...zero cannot be inserted or removed if it only has non-zeroes..." -> "...zero can only be inserted or removed if it has at least one zero..." \$\endgroup\$ Aug 30, 2022 at 17:24
  • \$\begingroup\$ The latter conversion is incorrect, because [0, 3, 4] can become [3, 4] because the zero has an end-of-the-list next to it rather than a zero. I agree this could be clearer though \$\endgroup\$
    – pxeger
    Aug 30, 2022 at 19:28
  • \$\begingroup\$ that's not supported by the current phrasing either: "a zero cannot be inserted or removed if it only has non-zeroes next to it". If that zero can be removed, you're saying it has something other than non-zeros next to it, which means you're saying that either "end-of-the-list"s or 3s are zero. Addendum: my head hurts \$\endgroup\$ Aug 30, 2022 at 19:34
  • 1
    \$\begingroup\$ @thejonymyster My head hurts too and now you see why lol. The way I drafted this in my head is slightly different from how I wrote it down, which doesn't help \$\endgroup\$
    – pxeger
    Aug 30, 2022 at 19:38
  • \$\begingroup\$ Maybe instead of splitting / joining runs, just say the number of runs of nonzeros cant change between input and output. That handles the 0s on the ends situations too and is probably simpler \$\endgroup\$ Aug 30, 2022 at 19:41
  • \$\begingroup\$ I mean, the first sentence (before "in other words") explains it pretty well already. Maybe I should just remove the second rephrasing. \$\endgroup\$
    – pxeger
    Aug 30, 2022 at 19:45
3
\$\begingroup\$

Use "e" or a suffix?

Background

In Māori (the indigenous language of New Zealand), to make a verb a command the verb is either preceded by "e" for example, "oma" (to run) -> "e oma", or, it's given a passive ending to make it a passive verb for example "kōrero" (to speak, talk, say, etc) -> "kōrerotia".

Besides some edge cases with intransitive verbs and different dialects, the rule is that if the verb has exactly two short vowels or one long vowel (which have macrons over the top of them like "ā") then the verb is preceded with "e" otherwise it is given a suffix.

Challenge

Your challenge is to determine whether which case a verb fits in to. If it should be preceded by "e" output a truthy value, otherwise output a falsey value.

Rules

  • You can assume that all the input will be lower case.
  • You may not take input with combining diacritics.
  • Because there are very few, if any verbs in Māori with only one short vowel, and the fact that what happens in that case depends on dialect, you don't need to handle those cases.
  • This is so the shortest answer wins.

Test cases

ako -> true
kai -> true
haere -> false
tū -> true
āwhina -> false
kōrero -> false
huri -> true

Meta

  • Thoughts on the combining diacritics rule?
  • Are there any other rules that should be added?

\$\endgroup\$
7
  • \$\begingroup\$ Maybe add a test case with a single short vowel? (Or if there are no actual mono-moraic verbs, consider including in the spec that such an input doesn't have to be considered.) \$\endgroup\$ Jul 25, 2022 at 7:27
  • 1
    \$\begingroup\$ @UnrelatedString I was thinking about that, I haven't been able to find any verbs with one short vowel, but I just can't be sure that there are none so I'm not really sure what to do about it. \$\endgroup\$ Jul 25, 2022 at 7:40
  • \$\begingroup\$ Fix your link! (It's the second one.) \$\endgroup\$
    – Laurel
    Aug 30, 2022 at 23:26
  • \$\begingroup\$ @Laurel Ah, thanks. I'll replace it with a new one. \$\endgroup\$ Aug 30, 2022 at 23:29
  • \$\begingroup\$ Tag: decision-problem \$\endgroup\$ Aug 31, 2022 at 1:26
  • \$\begingroup\$ You should also probably specify that it has to be exactly two short/one long, and the cases are mutually exclusive -- e.g. say "...the verb has exactly two short vowels or two long vowels, but not both" \$\endgroup\$ Aug 31, 2022 at 15:02
  • \$\begingroup\$ @Adam true, done. \$\endgroup\$ Aug 31, 2022 at 20:51
3
\$\begingroup\$

Compute the Fabius function

  • \$f(x)\$ always takes on rational values at dyadic rationals (i.e. \$\frac m{2^n}\$ with \$m,n\in\mathbb Z\$). It could also be interesting to restrict input to such values, and ask for a rational output.
\$\endgroup\$
6
  • \$\begingroup\$ Forgive my ignorance, but is it even possible to exactly determine the value of the function for any given input \$x\$? \$\endgroup\$ Sep 5, 2022 at 23:03
  • \$\begingroup\$ @97.100.97.109 What do you mean by "exactly"? There do exist algorithms which can approximate the value of \$f\$ to arbitrary precision for any given input. For example, from math se \$\endgroup\$
    – att
    Sep 5, 2022 at 23:22
  • \$\begingroup\$ I suppose the answer to my question is "no" (when the input is not a dyadic rational). In that case, how do solvers decide the level of precision with which to calculate the answer? I think that limiting to dyadic rationals is a good idea because then there is an explicit formula to calculate the values. \$\endgroup\$ Sep 5, 2022 at 23:38
  • \$\begingroup\$ @97.100.97.109 I don't think the absence of an explicit, non-limiting formula should be a factor in that decision. How would you "exactly" calculate \$\sin\$ for arbitrary inputs? \$\endgroup\$
    – att
    Sep 5, 2022 at 23:44
  • \$\begingroup\$ I think it can make two challenges actually: first with f on dyadic rationals asking for rational output (no floating point errors); second with f at any input with stated required precision (maybe limited to interval 0-1). \$\endgroup\$
    – pajonk
    Sep 7, 2022 at 19:51
  • \$\begingroup\$ I'd prefer keeping the function to [0,1] where it is defined by the cumulative distribution. \$\endgroup\$
    – xnor
    Sep 10, 2022 at 22:08
3
\$\begingroup\$

Convert integer to IEEE 754 float

The task is simple, given a 32 bit integer, convert it to its floating point value as defined by the IEEE 754 (32-bit) standard.

IEEE 754

Here is a converter for your reference.

Here is how the format looks:

The standard is similar to scientific notation.

The sign bit determines whether the output is negative or positive. If the bit is set, the number is negative otherwise it is positive.

The exponent bit determines the exponent (base 2), it's value is offset by 127. Therefore the exponent is \$2^{n-127}\$ where n is the integer representation of the exponent bits.

The mantissa defines a floating point number in the range \$[1,2)\$. The way it represents the number is like binary, the most significant bit is \$\frac 1 2\$, the one to the right is \$\frac 1 4\$, the next one is \$\frac 1 8\$ and so on... A one by default is added to the value.

Now the final number is: $$\text{sign}\cdot 2^{\text{exponent}-127}\cdot \text{mantissa}$$

Test cases

1078523331 ->   3.1400001049041748046875
1076719780 ->   2.71000003814697265625
1036831949 ->   0.100000001490116119384765625
3264511895 -> -74.24919891357421875
1056964608 ->   0.5
3205496832 ->  -0.5625

For this challenge assume that cases like NaN and inf are not going to be the inputs, and subnormals need not be handled, and you may output 0 for the case where the number represented is -0.

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

\$\endgroup\$
5
  • \$\begingroup\$ Closely related. Kinda opposite task, but I think there are certainly overlapping techniques around reinterpreting a bit pattern from one type to another. \$\endgroup\$
    – Bubbler
    Sep 22, 2022 at 0:22
  • \$\begingroup\$ I don't quite understand what this challenge is asking for. Are we supposed to reinterpret the integer as a float? \$\endgroup\$
    – pigrammer
    Oct 2, 2022 at 21:51
  • \$\begingroup\$ @pigrammer yes. \$\endgroup\$
    – PyGamer0
    Oct 3, 2022 at 3:42
  • \$\begingroup\$ @solid.py shouldn't all the digits be accurate, because there is no floating point conversion error when you are reinterpreting an integer as a float \$\endgroup\$
    – PyGamer0
    Oct 9, 2022 at 10:33
  • \$\begingroup\$ @solid.py 1/2 + 1/4 + 1/8 + ... should be accurately stored, because, well it can be accurately represented by IEEE754, which is supported by most languages... \$\endgroup\$
    – PyGamer0
    Oct 10, 2022 at 15:51
3
\$\begingroup\$

Hexadecimal -> Binary Art

\$\endgroup\$
8
  • \$\begingroup\$ Is 10000001 actually 81? A simple web search shows 81 is 1010001 \$\endgroup\$ Sep 30, 2022 at 9:38
  • \$\begingroup\$ -1 because of that \$\endgroup\$ Sep 30, 2022 at 9:39
  • \$\begingroup\$ @py3programmer Since hexadecimal is in base sixteen, hexadecimal 81 is 10000001. \$\endgroup\$
    – Aiden4
    Sep 30, 2022 at 14:27
  • \$\begingroup\$ @py3programmer no. Hexadecimal is base 16. I've edited the post so you can remove your downvote. \$\endgroup\$
    – The Thonnu
    Sep 30, 2022 at 14:30
  • \$\begingroup\$ There's a different way to calculate binary numbers in base 16? I didn't know that. \$\endgroup\$ Oct 1, 2022 at 2:46
  • 1
    \$\begingroup\$ @py3programmer 81 in base 16 isn't the same as 81 in base 10, the same way that, e.g. 10 in binary is not equal to 10 in decimal. 81 in base 16 is 8*16+1 = 129 in decimal, which is indeed 10000001 in binary. \$\endgroup\$ Oct 2, 2022 at 18:59
  • \$\begingroup\$ Since 16 is a power of 2, you can also convert each character individually into binary then concatenate -- 8 -> 1000, 1 -> 0001, 81 -> 10000001. \$\endgroup\$ Oct 2, 2022 at 19:18
  • \$\begingroup\$ Yeah, I get you. I was think more in Python terms, bin(81) instead of bin(0x81) \$\endgroup\$ Oct 3, 2022 at 3:58
3
\$\begingroup\$

Find a word in the dictionary of all possible words

\$\endgroup\$
3
  • \$\begingroup\$ Can input use strings? (e.g. 123 and 231 for your example) \$\endgroup\$
    – pigrammer
    Oct 2, 2022 at 21:47
  • \$\begingroup\$ @pigrammer that's a default input method and thus should be fune \$\endgroup\$
    – Aiden4
    Oct 2, 2022 at 22:00
  • \$\begingroup\$ @solid.py that should be fine so long as tuples of arbitrary length can be handled \$\endgroup\$
    – Aiden4
    Oct 4, 2022 at 19:19
3
\$\begingroup\$

Generate QR code from string

I'm surprised to have not seen anyone doing this particular challenge yet.

The challenge is simple: Take input as a string, your task is to turn it into a QR code.

This challenege originally requires outputting to an image (or any graphical output), but to relax the restrictions down, you only need to output the result as a matrix (or a 2D array). A binary string (with splits between) is also acceptable.

And for sake of simplicity, you will only need to output a 25x25 QR code image (which is the most popular QR code size, I believe). For the input Version 2, the image should look like this:

Version 2 QR code on Wikipedia

You may not need to handle empty strings.

Standard loopholes are forbidden.

More resources

\$\endgroup\$
13
  • \$\begingroup\$ Can we output as a binary string \$\endgroup\$ Oct 9, 2022 at 9:28
  • \$\begingroup\$ @py3programmer No, the output string must be full, correct image data. If you are using a binary string to later be used with metadata to output to a, say bitmap file, then yes, you can. \$\endgroup\$
    – oeuf
    Oct 9, 2022 at 10:06
  • \$\begingroup\$ You may consider turning this into a binary-matrix challenge instead. It would be easier to test on TIO and the like. \$\endgroup\$
    – Arnauld
    Oct 9, 2022 at 21:02
  • \$\begingroup\$ Also, it would need to be significantly more specified. Which version of QR code? Which kind of data? With which error correction level? (Challenges are supposed to be self-contained. So, ideally, the full algorithm should be explained.) \$\endgroup\$
    – Arnauld
    Oct 9, 2022 at 21:05
  • \$\begingroup\$ @oeuf I meant a binary string that describes whether each pixel is lighted up or not. The binary string would describe the pixels from left to right. Is that valid? \$\endgroup\$ Oct 10, 2022 at 3:50
  • \$\begingroup\$ @py3programmer Perhaps yes. I will take some time to think about this. \$\endgroup\$
    – oeuf
    Oct 10, 2022 at 5:32
  • 1
    \$\begingroup\$ @Arnauld I have edited to relax the output restrictions down and specify more about the output. Thanks for suggestion! I am currently deciding on the error correction level \$\endgroup\$
    – oeuf
    Oct 10, 2022 at 6:08
  • \$\begingroup\$ Don't we need 2 inputs? One for the version, and one for the string we need to encode the qr code with? \$\endgroup\$ Oct 10, 2022 at 7:01
  • \$\begingroup\$ @py3programmer No, we only need one input for the string. \$\endgroup\$
    – oeuf
    Oct 10, 2022 at 7:14
  • \$\begingroup\$ So you mean that using version 2, you'll encode the input as a QR code? \$\endgroup\$ Oct 10, 2022 at 7:17
  • \$\begingroup\$ @py3programmer Yes. \$\endgroup\$
    – oeuf
    Oct 10, 2022 at 7:18
  • \$\begingroup\$ Can one use built-in libraries that generate QR codes? I think that the real would be to compute the image/matrix \$\endgroup\$
    – matteo_c
    Oct 17, 2022 at 14:50
  • \$\begingroup\$ @matteo_c Builtins are allowed, but builtins that solves the entire problem are generally disencouraged. \$\endgroup\$
    – oeuf
    Oct 18, 2022 at 4:50
3
\$\begingroup\$

Integer Bluffing (Still a draft)

It's an exciting night at the IGS casino, as a brand new table game is being revealed - Integer Bluffing. Bots from all over the network have come to have a chance at playing the inaugral game, and luckily for you, you just happen to have a seat at the table.

The Rules of Integer Bluffing

At the beginning of a game of Integer Bluffing, each player starts with 20 tokens. A round of Integer Bluffing consists of 4 phases: (i) the deal, (ii) declaring, (iii) reacting, (iv) the showdown

The Deal

Each round, 1 player is chosen to pay an ante, which increases during the game, into the pot. A random integer in the range [1, 8] is then "dealt" to each player. Players only know the true value of their own integer, and once an integer is given to a player, it can't be dealt to anyone else.

Declaring

Starting with the player after the player who paid the ante, each player has the choice to either: a) decline to play the round (fold) or b) put 1 token into the pot and declare what their integer is (play).

Here's the twist - if choosing to play, the player doesn't have to tell the truth about the value of their integer - they can lie and declare they have an integer they don't have. The player also sets a flag whether they say they are bluffing or not - this can also be lied about.

Consider the following game with players A, B, C and D:

Player Integer
A 4
B 2
C 7
D 3

Assuming Player A paid the ante for the round, player B goes first in the declaring round. Player B sees that they have a 2, which is highly unlikely to win against other integers, so they fold.

Player C sees that they have a 7, so they choose to play the round. Wanting to trick Players D and A into putting a token into the pot, C declares that they have 2, and states they are not bluffing.

Player D sees that Player C allegedly has 2 and that they aren't bluffing. Player D knows that 3 is higher than 2, but knows that Player A might have a higher integer. Therefore, Player D decides to declare 6, not bluffing, in the hopes that Player A folds.

TODO: Continue writing

\$\endgroup\$
2
  • \$\begingroup\$ "new table game"? Pfff, I've played this before :3 \$\endgroup\$
    – DialFrost
    Oct 14, 2022 at 13:28
  • \$\begingroup\$ You should state the number of players at the table (always exactly 4?), probably in the "The Rules of Integer Bluffing". I also think this sentence: "The player also sets a flag whether they say they are bluffing or not" should be reworded. I think "The player may also set a flag to say they are bluffing" works. \$\endgroup\$ Oct 14, 2022 at 19:58
3
\$\begingroup\$

Triangular polkadot numbers

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

Tic, Tac, stub your Toe

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Welcome to Code Golf, and thanks for using the Sandbox! Currently, this is missing a winning criterion. If it's code-golf, you should probably specify. \$\endgroup\$
    – Steffan
    Oct 26, 2022 at 4:12
  • 1
    \$\begingroup\$ I'd also recommend allowing looser I/O formats - see here. It's your challenge though, so if you really want to keep the strict formats, that's okay. \$\endgroup\$
    – Steffan
    Oct 26, 2022 at 4:17
  • \$\begingroup\$ Don't know how to add tags to specify which type so right now it's just fastest possible solution wins. Also will update to remove number of testcases and commas from input. \$\endgroup\$
    – JohnBGoode
    Oct 26, 2022 at 14:14
3
\$\begingroup\$

Is this series of quotes valid python?

Python string parsing has quite a few edge cases. This is a string:

"a"

Putting 2 strings immediately after each other implicitly concatenates them, so this is also a string:

"a""a"

However, if you put 3 quotes in a row, it will create a "triple quoted string" which can only be ended by another triple quoted string. A triple quoted string can contain other quotes. These quotes will not end the string unless there are 3 of them. Thus this is valid:

"""a"a"""

Of course, you can combine these together, so this is a valid string:

"""a""""a"

And this:

"""""aaa"""""""""

A string is not valid if:

  1. Any a appears outside of a string literal (would get SyntaxError: invalid syntax in python) OR
  2. The end of the sequence is inside a string literal (would get SyntaxError: unterminated string literal (detected at line 1) in python)

Your task

Given a string containing 2 distinct characters, one representing a double quote and another representing any alphanumeric character, determine if it would be a valid python string, or invalid syntax.

You do not need to consider single quotes or how double and single quotes normally interact.

A array of booleans or a array of bytes would also be a valid input method.

This is , shortest answer wins.

Test Cases

Truthy Falsy
"a" (1-1) "a (1)
"a""a" (1-2-1) "a"a" (1-1-1)
"""a"a""" (3-1-3) ""a"a""" (2-1-3)
"""a""""a" (3-4-1) """a"""a" (3-3-1)
"""""aaa""""""""" (5-9) """""aaa"""""""" (5-8)
"""""""""""" (12) """"""aaa""""""""" (6-8)
"a""a""a""a" (1-2-2-2-1) """" (4)
"a""" (1-3)

eval or exec or ast.literal_eval would be valid answers, though I hope to see more creative python answers as well.

\$\endgroup\$
22
  • \$\begingroup\$ Well, well, well... exec can be used, so I'm claiming that. Ha! \$\endgroup\$ Oct 26, 2022 at 12:02
  • 2
    \$\begingroup\$ Ok go for it. Not sure if I would count that as creative though \$\endgroup\$
    – mousetail
    Oct 26, 2022 at 12:03
  • \$\begingroup\$ But the problem with this is, lambdas are not going to work in Python, and neither can normal functions. LHS = SyntaxError = RHS -> hence proved that eval and exec 4 byters are not valid. \$\endgroup\$ Oct 26, 2022 at 12:06
  • \$\begingroup\$ And neither will normal inputs. Therefore, python would not work at all for this program, as input cannot only be provided. \$\endgroup\$ Oct 26, 2022 at 12:07
  • \$\begingroup\$ Of course python works? You can take input as a string containing quotes \$\endgroup\$
    – mousetail
    Oct 26, 2022 at 12:08
  • \$\begingroup\$ Though I assume it could be made to work in online interpreters like TIO and ATO, but not in IDLE style. \$\endgroup\$ Oct 26, 2022 at 12:08
  • \$\begingroup\$ "string containing quotes": won't the string itself contain quotes? Like "a"a" when passed to an argument would be '"a"a"' \$\endgroup\$ Oct 26, 2022 at 12:09
  • \$\begingroup\$ Notice the '? \$\endgroup\$ Oct 26, 2022 at 12:10
  • \$\begingroup\$ The string will contain only double quotes. \$\endgroup\$
    – mousetail
    Oct 26, 2022 at 12:11
  • \$\begingroup\$ Nod nod so when are you posting this? \$\endgroup\$ Oct 26, 2022 at 12:15
  • \$\begingroup\$ When I get 3-4 votes \$\endgroup\$
    – mousetail
    Oct 26, 2022 at 12:17
  • \$\begingroup\$ +1. You're 2 votes away :P \$\endgroup\$ Oct 26, 2022 at 12:19
  • \$\begingroup\$ Hey why'd you claim exec? \$\endgroup\$ Oct 27, 2022 at 6:42
  • \$\begingroup\$ Because it's boring. Note that duplicate answers are technically allowed but don't expect any votes \$\endgroup\$
    – mousetail
    Oct 27, 2022 at 6:52
  • \$\begingroup\$ But it wasn't your idea to use it... I had to rack my brain for a minute to figure out an alternative approach :( \$\endgroup\$ Oct 27, 2022 at 11:10
3
\$\begingroup\$

Does the sequence shut the box?

\$\endgroup\$
4
  • \$\begingroup\$ small nitpicks: "roles" should be "rolls", "d6 dice" is redundant and should probably just be "six sided dice", and [] input is an edge case, and i think we should probably be able to assume input wont be empty. \$\endgroup\$ Oct 10, 2022 at 21:58
  • \$\begingroup\$ actual feedback: cool challenge idea :-) a worked out example of how a specific sequence could be used to shut the box would be very helpful in allowing everyone to understand the challenge more \$\endgroup\$ Oct 10, 2022 at 21:58
  • \$\begingroup\$ Thanks for the feedback. I cleaned those things up and added the example. Personally I like the inclusion of the [] input as it adds more thinking to the golfing (can one save bytes by formulating the general case to include the edge case). But I admit it may be weird to say "not playing the game is not a winning sequence". \$\endgroup\$ Oct 10, 2022 at 23:29
  • \$\begingroup\$ ultimately up to you :-) thanks for humoring me :P \$\endgroup\$ Oct 11, 2022 at 2:03
3
\$\begingroup\$

50 digits of π in HQ0-9+-INCOMPUTABLE?

\$\endgroup\$
2
  • \$\begingroup\$ Sorry few questions: are those the only commands avaiable? How would I append to the buffer then if I didn't want to use q/Q? \$\endgroup\$
    – DialFrost
    Nov 11, 2022 at 0:18
  • \$\begingroup\$ @DialFrost 1) Yes. 2) It's your job to figure it out. \$\endgroup\$
    – Bubbler
    Nov 11, 2022 at 0:19
3
\$\begingroup\$

Totally random Catan number distributions

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

Base Neutral Numbering System

\$\endgroup\$
3
  • \$\begingroup\$ Can we output as a nested list? Like <<1> * <<1>>> = [[[1],[[1]]]] \$\endgroup\$
    – Steffan
    Nov 22, 2022 at 20:40
  • \$\begingroup\$ Also, can we omit the *? Most of the test cases omit it, only the last two use it. \$\endgroup\$
    – Steffan
    Nov 22, 2022 at 20:41
  • \$\begingroup\$ Omitting the * from the test cases was a mistake. I think [[[1],[[1]]]] already qualifies by my rules since I allow replacing <> with [] and * with , . \$\endgroup\$
    – mousetail
    Nov 22, 2022 at 21:08
3
\$\begingroup\$

Turn two dice into one die

\$\endgroup\$
4
  • \$\begingroup\$ Is it guaranteed that target is divisible by die1 * die2? Seems all your test cases assume so \$\endgroup\$
    – mousetail
    Dec 8, 2022 at 10:42
  • 1
    \$\begingroup\$ What if there are multiple possibilities? Or will the input configuration guarantee an unique solution? \$\endgroup\$
    – pajonk
    Dec 8, 2022 at 11:16
  • \$\begingroup\$ @mousetail that issue is already addressed in the assumptions: "assume N is a divisor of A*B" \$\endgroup\$
    – Noah
    Dec 8, 2022 at 20:25
  • \$\begingroup\$ @pajonk if there are multiple solutions (such as making a one-sided), you only need to return one of them. I will clarify that in the post. I have not proven mathematically if there are unique solutions for the examples in the post, but I will postulate that they are (up to permutation) \$\endgroup\$
    – Noah
    Dec 8, 2022 at 20:27
3
\$\begingroup\$

KotH: Build an Organism

A while back, I made a thing that simulates a world, with a bunch of organisms with randomly chosen genes:

dieplife

These would code for things like how the organism would manage resources (such as sugar, water, and starch), whether it could photosynthesize, hunt, run away from enemies, or defend itself using tough materials like wood or stone spikes.

In this challenge, you'll design an organism which can effectively survive. This could be through strategies including:

  • Being an effective hunter, chasing down and killing prey
  • Being difficult to attack, such as by having tough armor and fleeing from stronger organisms
  • Growing and dividing too quickly for it to be possible that all organisms of your species are killed at the same time

I will update this post with a spec listing all available resources, materials, genetic traits, and actions later.

\$\endgroup\$
1
  • \$\begingroup\$ this seems fun. \$\endgroup\$
    – mathcat
    Dec 23, 2022 at 20:18
3
\$\begingroup\$

Rotate an Image

This challenge is simple: Take a path to an png image file as input and overwrite that file with a copy of itself rotated 90 degrees clockwise. You may assume that the file has dimensions of 128x128 pixels.

Examples

Input:

A cat

Output:

A weird cat

This is , so shortest answer wins.

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

Change the subject

Posted here

\$\endgroup\$
4
  • \$\begingroup\$ Nice challenge! Is it guaranteed that there will be an order in which each sentence connects to the next? \$\endgroup\$
    – The Thonnu
    Jan 15 at 19:02
  • 1
    \$\begingroup\$ @TheThonnu Yes, I'll add that to the challenge rules \$\endgroup\$
    – Jacob
    Jan 15 at 19:19
  • \$\begingroup\$ How are words defined in a sentence? Space separated or any non-letter/punctuation separated? \$\endgroup\$
    – lyxal
    Jan 16 at 4:31
  • \$\begingroup\$ @lyxal, to not make things too complicated, they will just be space separated. But the first and last words of the sentence can be compared with the others as well, and the first word starts with a capital letter while the last word ends with a period. I'll clarify this in the challenge. \$\endgroup\$
    – Jacob
    Jan 16 at 13:58
3
\$\begingroup\$

Simplify a Cycle

\$\endgroup\$
13
  • 2
    \$\begingroup\$ A worked out example would be great. I don't get why aren't all vertices from the input in the output. \$\endgroup\$
    – pajonk
    Aug 19, 2022 at 8:16
  • \$\begingroup\$ Is this really "given a path that may include cycles, remove the cycles"? \$\endgroup\$ Aug 19, 2022 at 8:42
  • 1
    \$\begingroup\$ @UnrelatedString no, it's "given a cycle between two vertices, return the shortest cycle" \$\endgroup\$
    – lyxal
    Aug 19, 2022 at 8:43
  • 3
    \$\begingroup\$ The last test case seems to contradict that, since the shortest cycle between 1 and 7 seems like it should just be [1,2,7]. I guess what I'm getting at is there needs to be a clearer spec and/or worked through example :P \$\endgroup\$ Aug 19, 2022 at 8:47
  • \$\begingroup\$ So is this a 1-D graph or what? Are you only giving us the X coordinates or what? Or am I just not understanding a thing? If we have the coordinates (both X and Y) is this basically removing adjacent coordinates that are colinear? \$\endgroup\$
    – Steffan
    Aug 19, 2022 at 15:50
  • \$\begingroup\$ @Steffan these numbers arent coordinates; they're just the labels on the nodes of the graph. the input is a path that exists on the graph, meaning you can get from each node in the list to the next node in the list by following a single edge \$\endgroup\$ Aug 22, 2022 at 2:35
  • \$\begingroup\$ Maybe an example program will help. \$\endgroup\$ Aug 26, 2022 at 14:29
  • \$\begingroup\$ Questions for @lyxal . \$\endgroup\$ Aug 26, 2022 at 14:39
  • \$\begingroup\$ Suppose his answers are what I expect, this should work. \$\endgroup\$ Aug 26, 2022 at 14:40
  • \$\begingroup\$ It was at 4 char left. Sorry for another post. Why must [1,2,7,2,7,2,3,7] be [1,2,3,7] ([1,2,...,3,7]) and not [1,2,7] ([1,2,7,...])? \$\endgroup\$ Aug 26, 2022 at 14:42
  • \$\begingroup\$ The mathematical definition deems that both are correct. You might want to clarify or add that to the test case. \$\endgroup\$ Aug 26, 2022 at 14:42
  • \$\begingroup\$ E \$\endgroup\$ Aug 26, 2022 at 14:44
  • \$\begingroup\$ ...dited to match the test cases. The last post was too long. \$\endgroup\$ Aug 26, 2022 at 14:44
3
\$\begingroup\$

Change the Temperature of Swatchlings

Warning: Contains Deltarune Chapter 2 Spoilers

Summary

Your challenge today is to determine the minimum number of turns needed to change the temperatures of an arbitrary amount of Swatchlings to all be the same.

Context

In the second chapter of Deltarune, there are enemies you encounter that are called Swatchlings (suit-wearing butler-like characters that serve the main antagonist of the chapter). These enemies always appear in battle in groups with at least one outlier in terms of suit colorsource. Swatchlings are defeated by making all Swatchlings in the group have the same suit colour.

Each Swatchling's color can be changed in stages, either becoming warmer (redder) or colder (bluer). While there are only five colours in the actual game, for this challenge, there will be an infinite amount, represented by positive integers.

During each turn of a battle, there are 3 ways to change the temperature of a Swatchling: increasing/decreasing the temperature by 2 stages, increasing the temperature by 1 stage or decreasing the temperature by 1 stage. You may perform up to 3 of these actions per turn, and no action twice in a turn.

Task and Worked Example

Given a list of Swatchling temperatures, determine the minimal number of turns needed to change the temperatures of all Swatchlings to be the same. Note that

For example, given the following temperatures:

[1, 5, 5]

The optimal solution would be:

Turn 1:
Increase Swatchling #1 by 2 stages: [3, 5, 5]

Turn 2:
Increase Swatchling #1 by 2 stages: [5, 5, 5]

Meaning a minimum of 2 turns are needed to change the temperatures of all Swatchlings to be the same.

Another example is:

[1, 5, 3, 4, 4, 2, 1]

One optimal solution might be:

Turn 1:
Increase Swatchling #1 by 2 stages: [3, 5, 3, 4, 4, 2, 1]
Decrease Swatchling #4 by 1 stage: [3, 5, 3, 3, 4, 2, 1]
Increase Swatchling #6 by 1 stage: [3, 5, 3, 3, 4, 3, 1]

Turn 2:
Decrease Swatchling #2 by 2 stages: [3, 3, 3, 3, 4, 3, 1]
Decrease Swatchling #5 by 1 stage: [3, 3, 3, 3, 3, 3, 1]

Turn 3:
Increase Swatchling #7 by 2 stages: [3, 3, 3, 3, 3, 3, 3]

Meaning a minimum of 2 turns are needed to change the temperatures of all Swatchlings to be the same.

Rules

  • Input will be a list of positive integers representing the temperatures of each Swatchling. The list will contain at least one Swatchling.
  • Output will be an integer representing the minimum number of turns needed to change the temperatures of all Swatchlings to be the same.

Test Cases

[1, 5, 5] => 2
[1, 2, 5] => 1
[1, 5, 3, 4, 4, 2, 1] => 3
[1, 7] => 2
[1, 10, 3] => 4

As this is , the aim of the game is to get y'all's byte count as low as possible, just like the turn count to make all the Swatchlings the same temperature.

Sandbox Meta

  • Is the explanation clear enough?
\$\endgroup\$
5
  • \$\begingroup\$ For your first case, can't you decrease #3 by 2, decrease it by 1 again, and increase #1 by 1? \$\endgroup\$
    – emanresu A
    Jul 2, 2022 at 0:09
  • \$\begingroup\$ @emanresuA huh, so you can. Just tested it in deltarune lol \$\endgroup\$
    – lyxal
    Jul 12, 2022 at 13:43
  • \$\begingroup\$ I'm assuming the line after the solution for [1, 5, 3, 4, 4, 2, 1] should say "Meaning a minimum of 3 turns are needed..." \$\endgroup\$ Jul 15, 2022 at 19:01
  • \$\begingroup\$ Is [2,5,5] 1 or 2, or say is multiple operating on one item allowed in one turn? \$\endgroup\$
    – l4m2
    Jan 22 at 7:44
  • \$\begingroup\$ [1, 10, 3] should be 3 if I understand it correctly. [1, 10, 3] => [3, 8, 4] => [5, 6, 4] => [5, 5, 5] \$\endgroup\$
    – AndrovT
    Jan 22 at 16:46
3
\$\begingroup\$

Yakko's New World Order

\$\endgroup\$
5
  • 2
    \$\begingroup\$ I don't think allowing outputting the indices into the list should be allowed \$\endgroup\$ Jan 24 at 21:11
  • \$\begingroup\$ @RydwolfPrograms Why not? \$\endgroup\$ Jan 24 at 21:17
  • \$\begingroup\$ Everybody's just going to do that, and that rules out some interesting approaches involving the text itself (such as using some sort of custom digest as a sorting key). \$\endgroup\$ Jan 24 at 21:18
  • \$\begingroup\$ Given a list of countries in alphabetical order -> are we always given the full list, or could it be a subset of this list? \$\endgroup\$
    – Arnauld
    Jan 29 at 20:23
  • \$\begingroup\$ @Arnauld You are always given the full list. (If you want, you can ignore the input completely.) \$\endgroup\$ Jan 29 at 21:12
3
\$\begingroup\$

Sum of Consecutive Squares

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

Reverse the polyglot, change the language.

Write a polyglot in two languages that when run, outputs the language it is run in. Additionally, this program must also output the other language's name if the source code is reversed

Example:

Program ABC in language foo outputs foo. ABC in language bar outputs bar. CBA in language foo outputs bar, and CBA in language bar outputs foo.

This is code-golf, so the shortest code wins

\$\endgroup\$
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\$ 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
    Jun 9, 2017 at 15:30
1
21 22
23
24 25
141

You must log in to answer this question.

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