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

3988 Answers 3988

1
37 38
39
40 41
133
2
\$\begingroup\$

Play a Dumb Coin-flip Game

Write an interactive program (or function?) which allows the user to play The Coin-flip Game! The object of the game is to flip a coin and get the same result n times in a row.

Specific behavior of the program/function is as follows:

  • At the start of the game, the user inputs a positive integer n>1
  • The program should then "flip a coin", and show the result to the user. Results can be any two distinct outputs, e.g. 1 or 0, and must be chosen non deterministically such that each result has a nonzero chance of appearing.
  • Next, the following loop occurs
    1. Prompt the user to input whether they wish to continue trying or quit. If they choose to quit, exit the program and take no further input.
    2. If they choose to continue, flip another coin.
    3. If the coin-flip's result is the same as the previous n-1 flips and the total number of flips is >=n, print a congratulatory message (i.e. any message that is not one of the coinflip values) and exit, taking no more input.
    4. If the result is different, return to step 1 of the loop.

This is , so shortest implementation in bytes wins.

Meta

Commenter noted that I should be more strict with output values / messages, but I wonder if it's more fun to give more freedom to be golfy, like with truthy/falsy values. Maybe letting the coinflip be anything is freedom enough, and the continue prompt / congratulatory message should be specific?

Leave your opinions in the below!

\$\endgroup\$
7
  • 1
    \$\begingroup\$ You say "If the coinflip's result is the same as the previous n-1 flips..." What if this is the first flip? There wouldn't be any previous flips. I assume this would not print the congratulatory message, correct? \$\endgroup\$
    – 00Her0
    May 31 at 16:34
  • 2
    \$\begingroup\$ I suggest showing a sample output of the program and expecting everyone to match its structure precisely. Allowing people to choose any congratulatory message and prompt will lead to chaos. \$\endgroup\$
    – chunes
    May 31 at 18:15
  • \$\begingroup\$ @chunes and what is wrong with chaos? i'll consider your suggestion though :) \$\endgroup\$ May 31 at 18:20
  • 1
    \$\begingroup\$ @thejonymyster I think it still has a problem. If n = 20 for example, there will still be undefined flips. I think you could fix it like this: 3. If the coin-flip's result is the same as the previous n-1 flips and total flips >= n, print a congratulatory message (i.e. any message that is not one of the coinflip values) and exit, taking no more input. \$\endgroup\$
    – 00Her0
    May 31 at 21:44
  • \$\begingroup\$ @00Her0 ah, in my mind that wasn't a contradiction, as a valid coinflip can not equal a nonexistent coinflip, so that didn't occur to me. It would be much clearer your way, so I'll make the change. thank you \$\endgroup\$ May 31 at 21:48
  • 1
    \$\begingroup\$ @thejonymyster define "at random". is always "1" "at random"? Is 1% getting a "1" "at random"? Or does it have to be "uniformly random"? \$\endgroup\$ Jun 4 at 2:09
  • \$\begingroup\$ @Nobody I've clarified in the post now, it only needs to be a nonzero chance of getting each result. Thank you \$\endgroup\$ Jun 4 at 2:14
2
\$\begingroup\$

`lol` is an ambigram, `dad` isn't

\$\endgroup\$
3
  • 1
    \$\begingroup\$ I think you mean "rotated", not "reversed". Related, Related, but not a dupe of either. \$\endgroup\$ May 30 at 5:20
  • 2
    \$\begingroup\$ I think using the word "palindrome" is misleading here. Palindrome is specifically for reversing right-to-left, not rotating. Also, please specify what kind of rotation is required (180 deg around the center of the word, isn't it?) and add some longer test-cases with various length. Please also specify what to do with empty string. Suggestion: make this decision-problem, not a "filter a list", because now it requires some needless boring boilerplate code. \$\endgroup\$
    – pajonk
    May 30 at 10:08
  • \$\begingroup\$ I've edited this down to a stub now that it's been posted to save space \$\endgroup\$ Jun 4 at 2:50
2
\$\begingroup\$

How much more to a repdigit?

\$\endgroup\$
1
2
\$\begingroup\$
\$\endgroup\$
0
2
\$\begingroup\$

Enumerate all pure sets

\$\endgroup\$
2
  • 1
    \$\begingroup\$ sequence IO? \$\endgroup\$
    – pxeger
    Jun 12 at 9:05
  • \$\begingroup\$ @pxeger Good idea :P \$\endgroup\$
    – emanresu A
    Jun 12 at 9:13
2
\$\begingroup\$

Is it a heapable sequence?

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

Find the walls!

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

Interpret BigTalk

Talk is a language which takes a single bit of input and has four commands:

  • 00 If the accumulator is 0, set the accumulator to 0.
  • 01 If the accumulator is 0, set the accumulator to 1.
  • 10 If the accumulator is 1, set the accumulator to 0.
  • 11 If the accumulator is 1, set the accumulator to 1.

These can be interpreted as replacement commands. We're going to extend that concept to positive integers, and make the language more complicated.

The language we're going to be defining is called BigTalk. It has an accumulator, which is a list of positive integers, initially set to only the input.

Programs are a series of commands. Each command is a pair of lists of integers, like ([24, 2], [32, 1]), and means to replace the first as a sequence with the second, as many times as it occurs.

The program runs repeatedly until the accumulator does not change. Finally, the accumulator is output.

For example, with the input [5, 5, 5, 5] and the program ([5, 5], [3, 2, 1]), ([3], [5]), ([2, 1, 5], [5, 1, 2]), the list goes:

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

Your challenge is to interpret this language. You may take input and program in any reasonable format.

This is , shortest wins!

Testcases

In the format of input, commands.

[5, 5, 5, 5], ([5, 5], [3, 2, 1]), ([3], [5]), ([2, 1, 5], [5, 1, 2]) -> [5, 2, 1, 1, 2, 2, 1]
[4], ([4], [4, 4]) -> Infinite loop
[2, 19, 13], ([13, 19], [2]) -> [2, 19, 13]
[39, 1, 23], ([1], [39, 23]), ([39, 39], [1, 1]), ([23, 23], [1]) -> [39, 23, 39, 23, 39, 23]

This language may be Turing-complete, and I have a +50 bounty for someone who proves it either way.

\$\endgroup\$
0
2
\$\begingroup\$

How far from binary?

\$\endgroup\$
3
  • \$\begingroup\$ Your description doesn't quite match your test cases. It should probably say that the absolute value of n-m is composed only of 1s and 0s. Since negative numbers have -. \$\endgroup\$
    – Wheat Wizard Mod
    Jun 15 at 8:40
  • \$\begingroup\$ @WheatWizard Why is it necessary? -101 is a signed binary. \$\endgroup\$
    – Fatalize
    Jun 15 at 8:46
  • 1
    \$\begingroup\$ -101 is not a binary string. It might be reasonable to interpret the intended way, but I certainly think it's more reasonable to interpret it another way. I don't see any harm in being explicit. \$\endgroup\$
    – Wheat Wizard Mod
    Jun 15 at 8:48
2
\$\begingroup\$

Parse this handy graph format

There's a great number of ways to represent directed graphs like the following: Three examples of directed graphs

Most representations are tailored towards being easy to work with, either for humans (like the picture above) or for computers (like an adjacency matrix representation). A middle ground I found useful in the past is this format:

        A -> B <-> C                A -> B -> C -> A           A -> C -> D; B <-> C <-> E

It is basically a condensed edge list, which is still relatively close to a graphical representation (good for humans) but not too hard to parse for a computer.

The goal in this challenge is to take a string representing a graph in this format as input and output a list of the graph's nodes and a list of the graph's edges.

This is , so try to use as few bytes as possible in the language of your choice.

Input specification

  • Each node has a unique name consisting of alphabetical letters, for example A, b, or Node. It is also fine if you only support upper or lower case names.
  • Three types of arrows can appear: ->, <-, and <->.
  • A chain is formed by a sequence starting with node and then alternating between arrows and nodes, for example A -> B <- C or also just A.
  • A chain may be followed by another chain with ; as a separator in between.
  • Between node names and the arrows and the semicolon can be any number of spaces (including zero).
  • Self-loops are possible, i.e., A -> A describes an arrow from node A to itself.
  • You may assume the input string is a valid encoding of a graph.

Output specification

  • The list of nodes can be returned or printed in any reasonable format and order, e.g., A, B, C, ["A", "B", "C"], A\nB\nC, ...
  • Edges are represented as ordered tuples in any reasonable format, e.g., ("A", "B") or A B for the edge A -> B and ("D", "C") or D C for the edge C <- D.
  • The list of directed edges can again be output in any reasonable format.

Test cases

"A -> B <-> C"               : ["A", "B", "C"], [("A", "B"), ("B", "C"), ("C", "B")]
"A -> B -> C -> A"           : ["A", "B", "C"], [("A", "B"), ("B", "C"), ("C", "A")]
"A -> C -> D; B <-> C <-> E" : ["A", "B", "C", "D", "E"], [("A", "C"), ("C", "D"), ("B", "C"), ("C", "B"), ("C", "E"), ("E", "C")]
"AA<->BB"                    : ["AA", "BB"], [("AA", "BB"), ("BB", "AA")]
"A;B"                        : ["A", "B"], []
" "                          : [], []
"   A   <- B  ;  C  "        : ["A", "B", "C"], [("B", "A")]
"A -> A; B <- B"             : ["A", "B"], [("A", "A"), ("B", "B")]

Sandbox question:

Given that this is foremost a question, I'm tempted to drop the validity assumption and require answers to raise an error if the input does not follow the spec. What do you think, would that still be fun?

\$\endgroup\$
2
  • \$\begingroup\$ Definitely don't require validity. That makes the challenge two quite different challenges, and is in no way necessary. \$\endgroup\$
    – emanresu A
    Jun 27 at 1:00
  • 1
    \$\begingroup\$ ^ probably a typo, but 100% you should allow users to assume validity. Don't require validity checking. \$\endgroup\$ Jun 30 at 2:06
2
\$\begingroup\$

Flood fill by distance

\$\endgroup\$
2
  • 1
    \$\begingroup\$ I suggest using a different character than O for obstacles, because it looks too much like 0. Maybe #? \$\endgroup\$
    – pxeger
    Jun 20 at 12:52
  • 1
    \$\begingroup\$ In your third code block, the last two grids still use O intead of X in two places each. \$\endgroup\$
    – Steffan
    Jun 25 at 21:24
2
\$\begingroup\$

Nest some addition

  • There's a natural follow-up with the other possible addition operator, where the order of application is reversed, i.e. \$\operatorname{add2}\overparen{\underparen a}\overparen{\underparen b} f=\left(\overparen{\underparen b} f\right)\circ\left(\overparen{\underparen a} f\right)\$.
\$\endgroup\$
10
  • \$\begingroup\$ Even though I've dabbled in similar contexts before, I would have been helped by a reminder to think of everything in terms of functions which act on functions. \$\endgroup\$
    – aschepler
    Jun 20 at 21:05
  • \$\begingroup\$ It looks like the multiplication and exponentiation rules are part of the "Task", but if I understand right they're really an additional comment on add or a hint which could be useful for understanding / implementing / verifying behavior. \$\endgroup\$
    – aschepler
    Jun 20 at 21:14
  • 1
    \$\begingroup\$ @thejonymyster add is binary, since it expects two curried arguments. \$\endgroup\$
    – att
    Jun 20 at 22:14
  • 1
    \$\begingroup\$ May I take input as \$x_0,\dots,x_a\$ without taking the integer \$a\$? \$\endgroup\$
    – alephalpha
    Jun 23 at 11:45
  • \$\begingroup\$ @alephalpha that should be allowed by default \$\endgroup\$
    – att
    Jun 23 at 18:21
  • \$\begingroup\$ I think the \$ \mathsf {formatting} \$ might be clearer if you denoted "Church numeral of \$ n \$" as something like \$ [n] \$. Because at the moment, it's not entirely clear that \$ \mathsf {a^b} \$ means \$ [a^b] \$ and not \$ [a]^{[b]} \$ (which doesn't make any sense) \$\endgroup\$
    – pxeger
    Jun 27 at 7:21
  • \$\begingroup\$ @pxeger What do you think about \$\overparen{\underparen n}\$ / \$\overparen{\underparen{a^b}}\$ (or maybe only over)? I feel like braces add a bit too much clutter. \$\endgroup\$
    – att
    Jun 27 at 19:36
  • \$\begingroup\$ @att Sure, both work fine \$\endgroup\$
    – pxeger
    Jun 27 at 19:38
  • \$\begingroup\$ @thejonymyster I think "where ∘ denotes function composition" should be sufficient (it's also defined as the multiplication rule) \$\endgroup\$
    – att
    Jun 27 at 22:22
  • \$\begingroup\$ Oh, i skimmed right over that. I guess my friend just can't read :P \$\endgroup\$ Jun 27 at 22:35
2
\$\begingroup\$

Is this a squashed series?

Given a string of digits, determine whether it is the concatenation of at least two ascending consecutive integers. (in decimal, with no leading zeroes)

For example, the string 7891011 is valid, because it's the concatenation of the sequence [7, 8, 9, 10, 11].

However, the string 54 could only be formed by concatenating [5, 4] (which is not ascending), or [54] (which does not have at least two numbers in it), so it is not valid.

(This challenge is essentially asking "Is it a valid input to Decipher a squashed series")

You should output using two distinct values of your choice to represent "valid" and "not valid".

Take care with leading zeroes: for example, 809 is not valid, even though it could be decomposed into [8, 09], because 09 is not a valid decimal integer.

You may assume the input does not start with a 0, and has a length of at least 2. The input will also only contain digits (and not -, so you don't need to handle negative numbers).

This is , so the shortest code in bytes wins.

Test cases

Valid

1234
7891011
293031323334
9991000

Invalid

54
66
28
3131
809

Valid numbers are given by A035333 in the OEIS.

Meta

  • Is this interesting enough? (It was just a byproduct of Decipher a squashed series)
  • Is my handling of the 809 case good? Or should I allow either output for inputs like that?
\$\endgroup\$
2
  • 2
    \$\begingroup\$ In Brachylog, this would be the exact same answer as your previous challenge, but I guess it’s mostly because of the declarative nature of the language. I don’t know if that’s interesting enough compared to the othe challenge with other language paradigms. \$\endgroup\$
    – Fatalize
    Jun 28 at 13:58
  • \$\begingroup\$ Fwiw, in 05AB1E it isn't the exact same answer, but it's a pretty trivial change. My initial answer was .œ.Δ¥P; and it would be .œʒ¥P}g without the 809 test case or .œʒ¥P}ïJQO with the 809 test case (both outputting 2/1 for truthy/falsey respectively). So the 8,09 test case definitely adds something interesting, although 05AB1E's "09"==9 being truthy makes it a bit more tricky than in most other languages I'd imagine. \$\endgroup\$ Jul 8 at 14:21
2
\$\begingroup\$

Is it shuffled FizzBuzz?

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

Round it up Nicely

When I work out, I often don't have a good plan for how many times to repeat an exercise, but in the interest of pushing myself I always keep going until I've done a "nice" number. Multiples of 5 are ideal, but multiples of 4 are acceptable too--unless they're 1 less than a multiple of 5, in which case I may as well do one more, or they're 1 more than a multiple of 5, in which case why didn't I already stop?

The challenge

Given an integer \$n\$ and a descending, pairwise coprime list of integers \$k_1, k_2, ..., k_m\$, output the least integer \$x \geq n\$ which is a multiple of some \$k_i\$ but is not 1 more or less than any multiple of any \$k_j\$ with \$j<i\$.

Test cases

n    k[1]...k[m]                        result
1    [5, 4]                             5
15   [5, 4]                             15
12   [5, 4]                             12
16   [5, 4]                             20
7    [5, 4]                             8
996  [5, 4]                             1000
1    [11, 7]                            7
15   [11, 7]                            22
133  [11, 7]                            140
1    [5, 3, 2]                          3
6    [5, 3, 2]                          10
6    [5, 3]                             10
11   [5, 3, 2]                          12
1    [100, 49, 9]                       9

Sandbox

  • Would it be more interesting without the descending/coprime guarantees?
  • Test cases are a WIP, but any additional suggestions?
  • Better title?
  • [How] should I note that the 1-above exclusion only matters if it would exclude the input itself? Should the task not be "rounding up" to make it more relevant?
\$\endgroup\$
2
  • 1
    \$\begingroup\$ the "dynamic goal" thing made me think it was going to be a challenge about determining how much excersize youd be doing in a given section, the sequence thing seems way out of left field and the requirements to be a workout number arbitrary, even in relation to the lore \$\endgroup\$ Feb 3 at 4:43
  • 1
    \$\begingroup\$ Could you please use words to describe the challenge? I do read quantifiers, but I suspect not all golfers do. \$\endgroup\$
    – pajonk
    Jun 30 at 6:25
2
\$\begingroup\$

Draw the Progress Pride flag

\$\endgroup\$
3
  • \$\begingroup\$ This is a really nice flag. It breaks one of the "golden rules of flag design" (no more than 3 colours) but that's the point. And harder to draw than it looks because you can't just draw a triangle for the brown and black. The spec is slightly inconsistent: the height calculated from the diagonal strip width is (340+170+170)*2 = 1360 which differs from the height calculated from the horizontal strip width 224 * 6 = 1344 so you might wat to fix that. \$\endgroup\$ Jun 24 at 21:04
  • 1
    \$\begingroup\$ I think the spec is reasonably clear but I have a few suggestions: 1. delete the word "obviously" - too patronizing. BTW this is covered by one of the standard loopholes here on Meta - it's always good to reference them. 2. So the minimum size is 1100 x 672? I recommend you state it explicitly rather than saying "only half" . "Exactly" is a strong word (especially when 170/sqrt 2 is actually 120.20815 ) - I recommend an error of 1 pixel. \$\endgroup\$ Jun 24 at 21:12
  • \$\begingroup\$ Thanks for the response I’ll fix this on Wednesday \$\endgroup\$
    – Oliver F
    Jun 26 at 19:01
2
\$\begingroup\$

Draw this fractal generated by applying Newton's method to cosh(x) - 1

\$\endgroup\$
1
  • 1
    \$\begingroup\$ ooh seems really fun \$\endgroup\$
    – mathcat
    Jul 1 at 18:13
2
\$\begingroup\$

Implement Binary Exponentiation

\$\endgroup\$
1
  • \$\begingroup\$ interestingly you can do the same thing with binary or peasant multiplication. actually any associative operation. \$\endgroup\$
    – qwr
    Jun 26 at 21:16
2
\$\begingroup\$

Is it an ordinal?

\$\endgroup\$
2
  • \$\begingroup\$ An interesting variation could be an open-ended-function sequence with the task being to generate every ordinal. Although I don't know if there would be a better way to answer that than smushing this decision problem and a "generate every ragged list" algorithm. \$\endgroup\$
    – pxeger
    Jun 30 at 10:54
  • \$\begingroup\$ @pxeger There's no way to generate every ordinal. There are uncountable ordinals, as well as countable but incomputable ordinals. \$\endgroup\$
    – Wheat Wizard Mod
    Jul 7 at 7:23
2
\$\begingroup\$

Draw the USA flag

\$\endgroup\$
1
  • 3
    \$\begingroup\$ "then your score is half of your program's length" is generally not welcomed here. \$\endgroup\$
    – tsh
    Jul 4 at 6:33
2
\$\begingroup\$

Generate a Tiefling's Traits

A Tiefling is a character race from Dungeons and Dragons, that have a list of possible traits:

  • small horns
  • fangs or sharp teeth
  • a forked tongue
  • catlike eyes
  • six fingers on each hand
  • goat-like legs
  • cloven hoofs
  • a forked tail
  • leathery or scaly skin
  • red or dark blue skin
  • cast no shadow or reflection
  • exude a smell of brimstone.

Given a list of Tiefling traits as input, randomly pick 1d4 + 1 (uniform distribution from 2-5) traits form that list and output them.

This challenge uses the standard definition of random, and the number and trait selection from the list must separately conform to definition 1 from here:

  • All possible [numbers] should be produced with the same probability;
  • All possible [traits] should be produced with the same probability;

You are allowed to pick randomly from the list by first shuffling it and taking the top 1d4+1 traits, so long as the shuffle does not favour any item in the list. Repeat selections if the same trait are not allowed.

Here is a non-golfed implementation in Javascript:

const dice = (n, s, a, doSum) => {
  const die = () => Math.floor(Math.random() * s) + 1;
  const dieResults = Array.from(Array(n)).map(each => {
    return die();
  });
  const sum = dieResults.reduce((sum, curr) => sum + curr + a, 0);
  // logging not required here
  console.log(`rolled '${dieResults.join(` + ${a}, `)} + ${a}${doSum ? ` = ${sum}`:''}'`);
  return doSum ? dieResults.reduce((sum, curr) => sum + curr + a, 0) : dieResults.map(each => each + a);
};

const shuffleSlice = (num, array) => array
  .map(value => ({
    value,
    sort: Math.random()
  }))
  .sort((a, b) => a.sort - b.sort)
  .map(({
    value
  }) => value).slice(0, num);

// you can take the list as a string or an actual list
const tieflingTraits = "small horns; fangs or sharp teeth; a forked tongue; catlike eyes; six fingers on each hand; goat-like legs; cloven hoofs; a forked tail; leathery or scaly skin; red or dark blue skin; cast no shadow or reflection; exude a smell of brimstone".split(/;\s+/);

// calling the function
console.log(shuffleSlice(dice(1, 4, 1, true), tieflingTraits))

// outputs like
//  ['goat-like legs', 'cast no shadow or reflection', 'a forked tongue']

This is so shortest in bytes wins.

\$\endgroup\$
4
  • \$\begingroup\$ Seems largely like a kolmogorov-complexity challenge, so maybe include that tag as well \$\endgroup\$ Jul 7 at 21:02
  • \$\begingroup\$ @mathjunkie It isn't, since you get the list as input \$\endgroup\$ Jul 18 at 4:01
  • \$\begingroup\$ Can we assume we will get this particular list, or should the program work in general for all lists? \$\endgroup\$ Jul 18 at 4:01
  • \$\begingroup\$ @CommandMaster you can assume you get that list \$\endgroup\$ Jul 18 at 8:30
2
\$\begingroup\$

Convert from Greeklish to modern Greek

\$\endgroup\$
4
  • 1
    \$\begingroup\$ I like natural language challenges, however it seems a bit boring. I feel like we must have a few challenges already that are a substitution cypher with digraphs, and it probably isn't really doing anything new there. \$\endgroup\$
    – Wheat Wizard Mod
    Jul 7 at 7:18
  • \$\begingroup\$ @WheatWizard I understand that character substitution challenges do exist in the site. However, from my search of the sandbox, there isn't one for Greek, so it's somewhat original. Therefore, I am considering to post this. If this goes any well, I will also consider posting a harder natural language Greek challenge ;) \$\endgroup\$
    – solid.py
    Jul 7 at 8:08
  • \$\begingroup\$ I just don't think that it being Greek actually makes the task any more interesting. Like it's neat, but the task is just a very simple substitution. If you have a harder challenge about Greek I'd say go with that one first. \$\endgroup\$
    – Wheat Wizard Mod
    Jul 7 at 8:26
  • \$\begingroup\$ @WheatWizard Since I made the effort to post and polish this a bit, I will post it. If it doesn't receive as much upvotes due to its unoriginality so be it. This site runs for many years and its hard to come up with something 100% original. Personally, I think its a solid challenge. Finally, I will also link to other related challenges, as I've seen quite recently on non-original challenges. \$\endgroup\$
    – solid.py
    Jul 7 at 8:41
2
\$\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.

Here's a video example of a Swatchling battle

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

(More tests coming soon)

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\$
3
  • \$\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 at 0:09
  • \$\begingroup\$ @emanresuA huh, so you can. Just tested it in deltarune lol \$\endgroup\$
    – lyxal
    Jul 12 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\$
    – Adam
    Jul 15 at 19:01
2
\$\begingroup\$

Solve a Card Suit Puzzle

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

Every possible pairing

\$\endgroup\$
1
  • \$\begingroup\$ Seems a little on the simple side I think. Might be a duplicate, but if it's not I think it's likely to score very well. :) \$\endgroup\$
    – Wheat Wizard Mod
    Jul 8 at 20:05
2
\$\begingroup\$

Implement level-index addition

Level-index is an alternative system of representing numbers to floating-point. It claims to virtually eliminate overflow (and underflow, in its symmetric form) issues from the vast majority of computations.

The usual floating-point format internally represents a number \$x\$ in terms of a mantissa and an integer exponent, such that \$x=\mathit{mantissa}\times2^\mathit{exponent}\$. In contrast, level-index represents a number as a power tower \$e^{e^\cdots}\$, as parametrized by an integer level \$l\ge0\$ and an index \$i\in[0,1)\$. These are defined by
\$l+i=\psi(x)=\begin{cases} x,&x<1\\ 1+\psi(\log x),&\text{otherwise.} \end{cases}\$
Equivalently, \$l\$ and \$i\$ together represent the floating-point number \$x=\psi^{-1}(l+i)=\operatorname{exp}^l(i)\$. Accordingly, \$\psi^{-1}\$ grows very fast; \$\psi^{-1}(5)\$ has more than a million digits to the left of the decimal point.

Task

Given two numbers \$\psi(a),\psi(b)\$, compute their level-index sum \$\psi(a+b)\$. You may choose to input or output the level and index together or separately.
Your solution should terminate in a reasonable time for all \$\psi(a+b)<8\$, and may not assume your floating-point representation has infinite precision.

Test cases

ψ(a)    ψ(b)    ψ(a+b)              a           b           a+b
0       0       0.0000000000000000  0           0           0
1       1       1.6931471805599453  1           1           2
2       2       2.5265890341390445  2.718281828 2.718281828 5.436563657
3       3       3.2046791426805201  1.5154262e1 1.5154262e1 3.0385245e1
4       4       4.0161875057657443  3.8142791e6 3.8142791e6 7.6285582e6
5       5       5.0000000044114734  2.3e1656520 2.3e1656520 4.7e1656520
1.70879 2.44303 2.6490358380192970  2.031531618 4.746554831 6.778086449
0.8001  3.12692 3.1367187876590184  0.8001      2.2470167e1 2.3270267e1
2.71828 3.14159 3.2134989633416356  7.774915755 2.368508064 3.1459996e1
4.6322  4.6322  4.6322790036002559  1.79455e308 1.79455e308 3.58910e308
4.80382 4.80382 4.8038229252940133  5.9037e4932 5.9037e4932 1.1807e4933

Sandbox Notes

  • Should I include a reference implementation? Most relevant papers seem to be behind paywalls.
  • How much precision to require in the index? Currently thinking 6 decimal digits.
\$\endgroup\$
2
\$\begingroup\$

Infinite Fibonacci word

The famous Fibonacci sequence of integers is defined as follows:

\$ F_0 = 0 \\ F_1 = 1 \\ F_n = F_{n-1} + F_{n-2} \$

But what if we use this same recurrence relation to produce an infinite sequence of strings? Instead of addition, we'll use concatenation. We'll also change the base case slightly:

\$ F_0 = \$ 0
\$ F_1 = \$ 01
\$ F_n = F_{n-1}F_{n-2} \$

The first few strings are:

0
01
010
01001
01001010
0100101001001
...

Each of these "words" is a prefix of the next, so they are all prefixes of the single infinite word \$ F_\infty = \$

010010100100101001010010010100100101001010010010100101001001010010010100101001001010010010100101001...

Your task is to output this infinite string as a sequence; you may choose to use any two distinct values to use for 0 and 1.

As with standard challenges, you may choose to either:

  • Take an input \$ n \$ and output the \$ n \$th item in the sequence
  • Take an input \$ n \$ and output the first \$ n \$ item
  • Output the sequence indefinitely, e.g. using a generator

and you may use 0-based or 1-based indexing for \$ n \$.

This string can be defined in many different ways; its OEIS entry A003849 and Wikipedia page have more information.

Errors due to floating-point imprecision are not allowed.

This is , so the shortest code in bytes wins.

\$\endgroup\$
11
  • 1
    \$\begingroup\$ Seems very similar to the infinite quote escaping sequence \$\endgroup\$
    – mousetail
    Jul 15 at 13:05
  • 1
    \$\begingroup\$ It doesn't seem like it should be binary - 0 and 1 have no special meaning, they can be replaced by any symbol \$\endgroup\$ Jul 15 at 13:07
  • \$\begingroup\$ @mousetail Yes, in fact the Fibonacci word can also be formed using a string rewriting rule like the quote sequence. In this case it's replacing 0 with 01, and 1 with 0. \$\endgroup\$
    – pxeger
    Jul 15 at 13:11
  • \$\begingroup\$ This is a interesting challenge, however, of the 2 challenges I think the other one is slightly more interesting. \$\endgroup\$
    – mousetail
    Jul 15 at 13:13
  • 1
    \$\begingroup\$ @mousetail Who's to say we can't have both? I think they're definitely not duplicates, because both have multiple different approaches, only one of which somewhat overlaps. \$\endgroup\$
    – pxeger
    Jul 15 at 13:14
  • \$\begingroup\$ Fair enough, though I'd suggest to post it after the other challenge to acknowledge the inspiration \$\endgroup\$
    – mousetail
    Jul 15 at 13:24
  • \$\begingroup\$ @mousetail It was not deliberately inspired by that challenge at all. (Maybe subliminally, because I had seen that challenge perviously?). But I learnt about the Fibonacci sequence independently while browsing Wikipedia. \$\endgroup\$
    – pxeger
    Jul 15 at 13:26
  • \$\begingroup\$ I think you should specify if floating-point errors are allowed, since some approaches will probably use the golden ratio \$\endgroup\$ Jul 16 at 4:35
  • \$\begingroup\$ @CommandMaster I've added a note about that, but AFAICT there aren't any direct methods to make the string using φ \$\endgroup\$
    – pxeger
    Jul 16 at 7:43
  • 3
    \$\begingroup\$ Duplicate? \$\endgroup\$
    – pajonk
    Jul 16 at 8:57
  • \$\begingroup\$ @pxeger the OEIS entry says "a(n) = floor((n+2)*r) - floor((n+1)*r) where r=phi/(1+2*phi) and phi is the Golden Ratio." \$\endgroup\$ Jul 16 at 9:22
2
\$\begingroup\$

All Crossword Grids

In crossword terminology, the grid is the region into which the crossword answers are inserted, consisting of white and black squares. The crossword answers, called entries, are inserted into contiguous sequences of white squares in a row or column, separated by black squares.

Example of a crossword grid

For straight (American) crosswords, the grids usually follow a specific set of rules:

  • They should have 180 degree rotational symmetry (if there is a black square in the \$x\$th row and \$y\$th column, there should be a black square in the \$x\$th-to-last row and \$y\$th-to-last column).
  • All entries must be at least 3 squares long.
  • All white squares must be joined in a single region.
  • No row/column can be completely filled with black squares.

Some examples of invalid and valid crossword grids:

Valid and invalid crossword grids

Your challenge: given a grid consisting of two unique values representing black and white squares, determine if it's a valid crossword grid. Assume that it's a square grid with \$n\$ rows and columns (so there are \$n^2\$ white/black cells), where \$n \geq 3\$. For example, if \$n=3\$ there is only one valid grid (I'm using . for white cells and # for black cells):

...
...
...

If \$n=4\$, there are 3 valid grids:

....  #...  ...#
....  ....  ....
....  ....  ....
....  ...#  #...

If \$n=5\$, there are 12 valid grids:

.....  #....  ##...  #....  ##...  ##...  
.....  .....  .....  #....  #....  ##...  
.....  .....  .....  .....  .....  .....  
.....  .....  .....  ....#  ....#  ...##  
.....  ....#  ...##  ....#  ...##  ...##  

....#  ...##  ....#  ...##  ...##  #...#  
.....  .....  ....#  ....#  ...##  .....  
.....  .....  .....  .....  .....  .....  
.....  .....  #....  #....  ##...  .....  
#....  ##...  #....  ##...  ##...  #...#  

Examples:

Input Output Explanation
......... True Valid grid
#..............# True Valid grid
...#........#... True Valid grid
...#........#... True Valid grid
......... True Valid grid
#...#......#...# True Valid grid
......................... True Valid grid
##...#.............#...## True Valid grid
................................................. True Valid grid
........................#........................ True Valid grid
....###.....##......##.....##......##.....###.... True Valid grid
................................................................ True Valid grid
##....####....##...........##......##...........##....####....## True Valid grid
...##.......#...........##.....##.....##...........#.......##... True Valid grid
#............... False No 180 degree symmetry
#..##..##..##..# False 2-letter entries, filled-in columns
#........................ False No 180 degree symmetry
.......#...###...#....... False 1-letter and 1-letter entries
######....#....#....#.... False No 180 degree symmetry, filled-in column & row
######...##...##...###### False Filled-in columns & rows
...#......#......#......#......#......#......#... False White squares not contiguous, filled-in column
.................###....#....###................. False 1-letter entries
...#......#...............##..................... False No 180-degree symmetry
....#.......#.......#........######........#.......#.......#.... False White squares not contiguous
..#.........#.......#......##......#.......#.......#.........#.. False 1-letter and 2-letter entries
.#......#..............................................#......#. False 1-letter entries, white squares not contiguous
...........................##......#............................ False No 180-degree symmetry
####............................................................ False No 180-degree symmetry
#......##......##......##......##......##......##......##......# False Filled-in columns

Standard loopholes are forbidden. Shortest code wins.

Sandbox Questions

I may have misused some crossword terminology above, let me know if I can improve the explanation.

I also don't know if the final rule should be included, since it's not usually explicitly stated when constructing crosswords.

I'm considering adding an optional parameter \$n\$ which describes the number of rows/columns to make the input easier to parse, but I don't know how people feel about optional parameters.

\$\endgroup\$
4
  • \$\begingroup\$ Suggested tags: crossword, grid \$\endgroup\$
    – pajonk
    Jul 27 at 5:45
  • 3
    \$\begingroup\$ I don't see any clever algorithm for grid generation (that doesn't mean that there isn't one), so the answers are most likely use the brute-force method of generating all grids and checking their crosswordness - why not make it decision-problem then? \$\endgroup\$
    – pajonk
    Jul 27 at 5:51
  • \$\begingroup\$ @pajonk I've modified the question as per your suggestion, but that makes it very similar to a previously posed question, so I'm unsure about this. I think there are some meaningful shortcuts to make the generation easier. \$\endgroup\$
    – Adam
    Jul 27 at 18:56
  • \$\begingroup\$ if you see shortcuts that make generation easier than brute-force, then ignore my previous comment. \$\endgroup\$
    – pajonk
    Jul 28 at 9:36
2
\$\begingroup\$

Add parentheses to Polish notation

In most programming languages, arithmetic is written with infix notation -- i.e. the operator is put in between the operands -- e.g. 1+2. In contrast, with Polish notation (a.k.a prefix notation), the operator comes before the operands -- e.g. +1 2. As long as the number of operands for each operator is fixed, this means that parentheses are never necessary, unlike with infix notation.

Given a string consisting of nonnegative numbers, whitespace, +, -, *, and / consisting of an expression in Polish notation, add parentheses around each sub-expression, maintaining the whitespace. The parentheses should start right before the operator and end right after the last operands. You can assume each function has arity 2 (i.e. it takes in exactly 2 operands).

Test Cases

Input Output
+1 2 (+1 2)
++ 1 2 3 (+(+ 1 2) 3)
+1 +2 3 (+1 (+2 3))
* *1 2 /3 0 (* (*1 2) (/3 0))
/ -20/30 30 999 (/ (-20(/ 30 30)) 999))

Questions

This probably has been done before, but I couldn't find it in a cursory search.

\$\endgroup\$
4
  • \$\begingroup\$ Suggested test-case with multi-digit numbers. Also, are the numbers guaranteed to be positive? \$\endgroup\$
    – pajonk
    Aug 4 at 12:50
  • \$\begingroup\$ I think they should be limited to being positive. \$\endgroup\$
    – Adam
    Aug 4 at 14:47
  • 2
    \$\begingroup\$ I'd suggest removing the edge case that extra trash at the end needs removed (such as the + 1 2 3 test case). Of course, it's your challenge, just a suggestion. I prefer sticking to the actual problem, and not adding extra edge cases. \$\endgroup\$
    – Steffan
    Aug 4 at 17:23
  • \$\begingroup\$ That was just a typo, sorry about that \$\endgroup\$
    – Adam
    Aug 4 at 18:29
2
\$\begingroup\$

Polyglot quiz

In this challenge as a cop you will choose two programming languages A and B, as well as a non-empty string S. You are then going to write 4 programs:

  1. A program which outputs exactly S when run in both A and B.
  2. A program which outputs S in A but not in B.
  3. A program which outputs S in B but not in A.
  4. A program which doesn't output S in either A or B.

You will then reveal all 4 programs (including which number each program is) and S, but keep the languages hidden.

Robbers will try to figure out a pair of languages such that they print the correct results for the 4 programs, and cops will try to make posts that are hard to figure out while remaining as short as possible.

Warning: There is a difference in the rules from "normal"

Cops' posts here can have 4 different states:

  1. Vulnerable
  2. Cracked
  3. Safe
  4. 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 A and B 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

Robbers will receive 1 point for every cop answer they solve, with the goal being to gather as many points as possible.

Cops will be scored on the sum size of all four programs as measured in bytes, with the goal being to have an answer with as low a score as possible. Answers that are not revealed effectively have infinite score by this measure until they become revealed.

Languages and Output

In the interest of fairness both languages you choose must be freely available on Linux and FreeBSD. This includes languages which are Free and Open Source.

Your selected languages must predate this challenge.

Since this challenge requires that A and B produce different outputs for the same program, there is no requirement for what "counts" as a different language, the fact that they produce different outputs is enough.

Programs do not have to compile, run without error or "work" in the cases where they do not need to output S. As long as S is not the output it is valid.

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

Running a program means is assumed to be done with no input. All four programs must be deterministic, within an environment. It is fine for example if as a part of an error the program outputs the name of the file or your host name, this is not considered non-determinism since it is determined by the enviroment running the program

\$\endgroup\$
6
  • 1
    \$\begingroup\$ Not a comment on the challenge itself, but I like the four-state CnR system! \$\endgroup\$ yesterday
  • \$\begingroup\$ Does the cop reveal which of the 4 programs satisfy which of the criteria 1-4? \$\endgroup\$
    – pajonk
    yesterday
  • \$\begingroup\$ @pajonk Yes they should. I'll edit it to make it clear. \$\endgroup\$
    – Wheat Wizard Mod
    yesterday
  • \$\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
    yesterday
  • 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
    yesterday
  • \$\begingroup\$ @WheatWizard I get it now, thanks! This indeed makes more sense. \$\endgroup\$
    – pajonk
    yesterday
1
37 38
39
40 41
133

You must log in to answer this question.

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