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

4667 Answers 4667

1
136 137
138
139 140
156
0
\$\begingroup\$

Write a program that prints a program that's almost quine.

\$\endgroup\$
5
  • \$\begingroup\$ So basically, we write a program, which given n, prints a program which prints its own first n characters, which are also the first n characters of all other programs that can be outputted? And the program has to be irreducible? \$\endgroup\$ Dec 4, 2022 at 2:58
  • \$\begingroup\$ @RadvylfPrograms Correct. \$\endgroup\$
    – Fmbalbuena
    Dec 4, 2022 at 15:58
  • \$\begingroup\$ What should the program do if input number is greater than its length? For example, my program has only 10 bytes, and i got an input 100. \$\endgroup\$
    – tsh
    Dec 15, 2022 at 7:14
  • \$\begingroup\$ Maybe you should define what irreducible is in your question. \$\endgroup\$
    – tsh
    Dec 15, 2022 at 7:18
  • \$\begingroup\$ @tsh The challenge is already uploaded. \$\endgroup\$
    – Fmbalbuena
    Dec 15, 2022 at 15:29
0
\$\begingroup\$

Balanced Candy Distribution

There are 4 piles with 12 pieces of candy each, for a total of 48 pieces, and there are 12 kids to split that candy with, each kid will receive exactly 1 piece of candy from each pile, each piece of candy has a unique number on it, from 0 to 47, the candies with numbers 0 through 11 are on one pile, candies number 12 through 23 are on another, candies 24 through 35 are on a pile, and the last pile is candies 36 to 47. Candy #0 tastes amazing, and candy #47 tastes not so great, so to make it fair, each kid’s 4 candies should add up to the same number (which should be 94), there is an issue though, there are an unknown number of different flavors of candy, completely independent of their tastiness, and it would be unfair if Timmy was to, by bad luck, have all his 4 pieces of candy end up being cinnamon flavored, so, we introduce the idea of “Unfairness points”. For each kid, if we match each of their four candy pieces to their other three candies, for each pairing if both candies are the same flavor, we add one unfairness point, to illustrate this, let’s imagine a smaller example, with only four kids and 16 candies, if Timmy got 4 cinnamon candies, Laura got 1 cinnamon candy, 1 vanilla candy, and 2 chocolate candy, Johnny got 2 vanilla candies and 2 caramel candies, and Sam got 3 cinnamon candy and 1 strawberry candy, this arrangement would receive 12 unfairness points: 6 from Timmy, 1 from Laura, 2 from Johnny, and 3 from Sam.

Your code must output the arrangement that satisfies the two restrictions set forth in the beginning (each kid receives a single candy from each pile, and that for each kid all of their four candies add up to 94) that has the fewest unfairness points, if there is more than one arrangement that is tied for least number of unfairness points, you must output the list of all of those arrangements given a list of all the candies. This input could be formatted in any way that is convenient, but preferably something resembling a list of strings, where the index is the number of the candy and the string is the flavor, for example, in python, a valid input format would be

[“Chocolate”,“Chocolate”,“Chocolate”,“Vanilla”,“Chocolate”,“Apple”,“Chocolate”,“Vanilla”,“Caramel”,“Chocolate”,“Vanilla”,“Chocolate”,“Apple”,“Apple”,“Caramel”,“Chocolate”,“Vanilla”,“Chocolate”,“Apple”,“Sour”,“Chocolate”,“Chocolate”,“Vanilla”,“Chocolate”,“Apple”,“Sour”,“Pear”,“Pear”,“Caramel”,“Vanilla”,“Vanilla”,“Vanilla”,“Apple”,“Sour”,“Chocolate”,“Chocolate”,“Avocado”,“Apple”,“Sour”,“Pear”,“Pear”,“Chocolate”,“Chocolate”,“Chocolate”,“Vanilla”,“Chocolate”,“Vanilla”,“Caramel”]

Or it could also be already separated into the piles

[[“Chocolate”,“Chocolate”,“Chocolate”,“Vanilla”,“Chocolate”,“Apple”,“Chocolate”,“Vanilla”,“Caramel”,“Chocolate”,“Vanilla”,“Chocolate”],[“Apple”,“Ap...

Or it could include the number itself, if you wanted

[[0,“Chocolate”],[1,“Chocolate”],[2,“Chocolate”],[3,“Vanilla”],[4,“Chocolate”],[5,“Apple”],[6,“Chocola...

The output can be given in any way that makes it easy to interpret what the numbers of the candies each kid should get, for example, in python, an output like

[[0,19,30,45],[9,14,28,43],[5...

Is totally fine, and anything that is easy or understandable like that is fine.

This is code-golf and so lowest bytes wins


Sandbox questions:

  • English is my second language, is this written clearly enough?
  • Would it be better if it was generalized? As in "There are N piles with C pieces of candy each, for a total of N*C pieces, and there are C kids to split that candy with, each kid will receive exactly 1 piece of candy from each pile..."
  • Any other thoughts?
\$\endgroup\$
0
\$\begingroup\$

Wordle games without repeating letters

Given a word list, find all sets of five words of five letters each, such that the words of each set have 25 distinct letters in total.

This challenge was inspired by this video by Matt Parker.

Example

TODO

Rules

  • Standard I/O rules apply.
  • Words contain only lowercase (or alternatively, uppercase) alphabetic characters.
  • You can assume there are no empty words (i.e. words of length zero).
  • The input can contain words that cannot belong to any set that satisfies the property, such as words with less then five letters, words with more than five letters, and words with repeated letters.
### Scoring

This challenge wants to be an experimental mix of and .

If your solution has:

  • a time complexity of \$\Omega(f(n))\$ (see Big Omega notation) where \$f: \mathbb N \to \mathbb N\$ is a function of the form $$f(n) = (\log_2 \log_2 n)^{k_{-2}} \cdot (\log_2 n)^{k_{-1}} \cdot n^{k_0} \cdot 2^{k_1} \cdot 2^{(2^{k_2})} \cdot \ldots $$ where \$n\$ the the total number of the letters of the words in the input;
  • a length of \$x\$ bytes (or whatever unit of measurement is commonly used in the language of your choice);

then the score is \$f(x + 4)\$. Lowest score wins!

Notes
  • obviously, infinitely many terms \$k_i\$ can be \$0\$;
  • terms with three or more \$\log\$ (e.g. \$\log_2 \log_2 \log_2 n\$) cannot be used in function \$f\$ because they could be used to arbitrarily lower the score, unless we introduce further complex scoring rules (and most probably they won't be used as a real lower bound).
Scoring example

If your solution has a length of \$37\$ bytes, and it has a time complexity of \$\Omega(n^5)\$, then it has a score of \$(37+4)^5\$.

Edit: scoring rules postponed for a future challenge

\$\endgroup\$
4
  • \$\begingroup\$ can we assume that there will be at least one solution in the input? \$\endgroup\$ Oct 17, 2022 at 15:47
  • \$\begingroup\$ I didn't think about it. In which case this assumption can be useful? \$\endgroup\$
    – matteo_c
    Oct 17, 2022 at 22:01
  • 2
    \$\begingroup\$ I think that since the number of distinct five-letter words is constant, any algorithm taking a subset of them would be constant time. So, once you filter out words that not five letters long and de-duplicate, the complexity of the rest of the algorithm doesn't matter. Having the length "5" instead be a parameter could address this. \$\endgroup\$
    – xnor
    Oct 18, 2022 at 2:22
  • 1
    \$\begingroup\$ assuming at least one solution avoids the "no solutions" edge case, which (depending on algorithm) may require extra code to handle logic that the bulk of the code doesn't handle. Example: if i have a loop which doesn't stop until i've seen some N>0 eligible words, it would run into an infinite loop in the "no solutions" scenario. Considering you have to output all solutions, however, I can see how it might not make a huuge difference. It's up to you tbh :-) \$\endgroup\$ Oct 18, 2022 at 13:15
0
\$\begingroup\$

Tell 32-bit from 64-bit with most disallowed bytes

Decide an integer N. Take an array of length N, and output a x86 opcode, which when running, reach different position after leaving the opcode you provided.

  • No bytes given in input is allowed in your output.
  • You'll be in Ring 3, so some x86 features can't be used.
  • You can destroy all available registers, including XMM, x87, etc.
  • You can assume 4096 bytes of available stack under stack pointer.
  • It's not necessary a jmp. Other ways to reach different position are fine.
  • The destination should be in 256 bytes before your code or in 256 bytes after your code. jmp .+0xEBEBEBEB is not allowed unless your output is at least ~336859924 bytes.
  • Each destination should have at least 2 bytes of space to fit a jmp short instruction.

Here is a possible program with N=0:

([])=>[0x66, 0x40, 0x3D]

Assuming this program is loaded into 0, then in 32-bit mode, it goes to 7; in 64-bit mode, it goes to 5, both of which have enough space.

This is an example with N=1:

([x])=>x==0x66||x==0x41||x==0x3D?[0x24,0x00,0x40,0x74,0x74]:[0x66,0x41,0x3D]

Largest N wins.

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

Title Case (WIP)

Input a string with letters and spaces, input a set of mirror words. Output a string which is title case of the input string that follow the following rules:

  1. Cases is unchanged for any words already contains uppercase letters;
  2. Cases is unchanged for any words in the set mirror words AND is not the first word;
  3. For all other words, the first letter is changed into uppercase

The set mirror words is an input to your program / function. Here are mirror words used in testcases:

["a", "an", "and", "as", "as", "at", "but", "by", "for", "if", "in", "nor", "of", "off", "on", "or", "per", "so", "the", "to", "up", "via", "yet"]

Testcases

Input -> Output
the sun and the moon -> The Sun and the Moon
jQuery tutorial using jQuery plugins -> jQuery Tutorial Using jQuery Plugins
\$\endgroup\$
0
\$\begingroup\$

A map maps keys(string) to values(string). Basic operations includes:

  • Set. Given a key and a value, store it.
  • Get. Given a key, get the last value stored into this key, or ""(empty string) if nothing ever stored into.

For some reason, sometimes keys looks same but need to refer to different object. Therefore, color is introduced. Provided keys is therefore splitted, and to get colored key, color each part of keys, and concat them.

For example, say a is colored red, then

  • a is \$\color{red}a\$
  • aa is \$aa\$
  • a a is \$\color{red}{aa}\$
  • ab is \$ab\$
  • a b is \$\color{red}{a}b\$

Therefore, another operation is

  • Color. Given a keypart, resign its color. Color is a non-negative integer and default to 0.

Shortest code in each languages wins.

Test cases

[[GET, "a"],         // ""
 [SET, "a", "1"],
 [GET, "a"],         // "1"
 [COL, "a", 1],
 [GET, "a"],         // ""
 [SET, "a bb", "2"],
 [GET, "a b b"],     // "2"
 [COL, "a", 0],
 [GET, "a"]]         // "1"
\$\endgroup\$
0
\$\begingroup\$

Solve the "/" puzzles from Taiji

Spoilers for the game Taiji, it's really good you should play it

Taiji puzzles consist of a rectangular grid, a solution consists of setting each square to either on or off:

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

However, not every solution is correct, a puzzle can contain any number of colored slashes, here represented by numbers:

....
1...
..1.
....

A shape is defined as a contiguous area that all has the same state. (either on or off). Areas with the same color slash must have the same shape, but it may be rotated (but not mirrored). For example, if you have this:

1..1.
.22.1
1..1.

A solution would be:

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

Note that all 1s are a single square while both 2s are a T shape, in this case in opposite colors.

Note that the number must be in the same place in every shape, so if you have this puzzle:

.....
1..1.
.....

This would Not be valid:

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

Because one 1 is in the tip and the other is in the tail.

If the numbers are different the shapes must be different. However, they count as different if the number is in a different part of the shape.

A shape can contain multiple numbers. If it contains the same number twice that means the shape must be irrationally symmetric.

Your challenge is: Given a number, output a possible solution. Note: There will always be at least 2 possible solutions, since the inverse of any valid solution is also valid.

Test Cases

Note many of these have many valid solutions.

1..1.            #..#.
.22.1        --> ....#
1..1.            #..#.

....              ##..
123.          --> #.##
....              #.##


...               #.#
111           --> #.#
...               #.#

1.22.1            ##..##
......            ######
2....2        --> .####.
2....2            .####.
......            ######
1.22..            ##..##

3....             #####
11211         --> ..#..
3....             #####

....              #.#.
111.          --> #.#.
....              #.#.

You may take input as a 2d list of numbers, and use any constant value to represent a cell that does not create a hint (eg. 0). For output you may also use any 2 constant values to represent on and off.

\$\endgroup\$
1
  • \$\begingroup\$ Is mirrored same? \$\endgroup\$
    – l4m2
    Jan 25, 2023 at 13:27
0
\$\begingroup\$

Best Iterated Prisoner's Dilemma

\$\endgroup\$
1
  • \$\begingroup\$ this is a great idea. an example would be nice :-) \$\endgroup\$ Oct 8, 2022 at 20:30
0
\$\begingroup\$

Optimize your score on a biased multiple choice test

In a multiple choice test, sometimes, by chance, multiple questions in a row will have the same answer. Some of my teachers dislike this, and will change questions to ensure there are never more than \$n\$ of the same answer in a row.

In this challenge, for each question in a test, you'll be given a set of four confidence values from 0 to 1, which sum to 1. These represent the odds of each choice being correct (for example, [0.1, 0.6, 0.2, 0.1] if you're 60% sure that B is correct, 20% sure C is correct, and 10% sure either of the others could be right). You'll also be given a positive integer n, the maximum number of same-answer questions in a row.

Given this information, you must output the set of answers that would give you the highest estimated score.

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

Pushing boxes away but

I'll accept first fastest algorithm and then this challenge turns into .

Sandbox Notes

  • Some options are listed in comment
\$\endgroup\$
4
  • \$\begingroup\$ Upvote this comment if this idea of question is good \$\endgroup\$
    – l4m2
    Jan 27, 2023 at 2:24
  • \$\begingroup\$ Upvote this comment if this idea of question is bad \$\endgroup\$
    – l4m2
    Jan 27, 2023 at 2:24
  • 1
    \$\begingroup\$ Upvote this comment if this way of handling fastest-algorithm is good \$\endgroup\$
    – l4m2
    Jan 27, 2023 at 2:24
  • 1
    \$\begingroup\$ Upvote this comment if this way of handling fastest-algorithm is bad \$\endgroup\$
    – l4m2
    Jan 27, 2023 at 2:24
0
\$\begingroup\$

A randomly colored walk

Given two inputs, a distance \$d\$ and a number \$n\$ output \$n\$ random colors which each have distance \$d\$ from the previous.

Background

A random walk is a path which is defined by choosing a random direction and (usually) fixed distance to go at each step. We will be taking a random walk through the RGB color space using Euclidean distance as our metric.

The challenge

For this challenge you will take two inputs, \$n\$ and \$d\$. Let \$n\$ be the number of colors to output, this will always be an integer \$1 \leq n\$, and \$d\$ be the distance between consecutive elements, which will always be \$0 \leq d \leq 128\$. You may additionally assume that \$d\$ is an integer.

For each consecutive pair of elements \$(r_1, g_1, b_1), (r_2, g_2, b_2)\$ of the \$n\$ element sequence output, it must be the case that all values are between 0 and 255 inclusive (or \$[0,256)\$ for floats), and the distance between elements must be within 1 of d, that is \$|\sqrt{(r_1-r_2)^2+(g_1-g_2)^2+(b_1-b_2)^2} - d| < 1\$. This should allow one to restrict their output to integers if they so choose. The walk need not be uniform, but it does need to be random. The starting point of the walk should be random as well.

Standard i/o rules apply, input and output can be in any reasonable format. Graphical output is allowed (and encouraged, though I doubt it will be golfy to do so) so long as the order of the sequence is clear.

This is , so the shortest answer in bytes wins.

Test cases

For these test cases input is in the order \$n,d\$ and output is \$(r, g, b)\$ as integers. These are some possible results.

5, 5 -> (81, 60, 243), (81, 57, 239), (76, 60, 240), (80, 62, 241), (84, 60, 243)
4, 10 -> (163, 89, 77), (162, 83, 85), (166, 75, 79), (166, 82, 87)
4, 50 -> (212, 36, 232), (247, 1, 239), (220, 44, 243), (217, 81, 209)
\$\endgroup\$
0
\$\begingroup\$

Related

Play chess. You can't see the current board, and can only memory two bytes.

Each turn, you're given your current state(initially 0) and opponent's move(a special value if first move in the game). You then output a list of [moves,state] pair, and the first valid one will get used. If no valid move, then you automatically resign.

Aim is obviously win as many games and lose as less as possible.

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

4D rotation matrix to quaternions

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

Ternary 2-input logic gate

Objective

Given an expression of a ternary 2-input logic gate, pretty-print its truth table.

Ternary logic and ternary operators

L, E, and G are the truth values of the ternary logic dealt here. They correspond to Haskell's LT, EQ, and GT.

The expression can have two free variables, hence "2-input".

There are four operators that the expression can have: min, max, compare, and mappend. (Again, from Haskell.) Their truth tables are:

min|L E G
---+-----
L  |L L L
E  |L E E
G  |L E G

max|L E G
---+-----
L  |L E G
E  |E E G
G  |G G G

compare|L E G
-------+-----
L      |E L L
E      |G E L
G      |G G E

mappend|L E G
-------+-----
L      |L L L
E      |L E G
G      |G G G

where the first column is for the first argument and the first row is for the second argument.

I/O format

The input format shall be a nonempty binary tree whose branches are the operator and whose leaves are the free variables and the truth values.

The output format shall be like this:

 |L E G
-+-----
L|? ? ?
E|? ? ?
G|? ? ?

where ? is filled with appropriate truth values represented by L, E, or G. Again, the first column is for the first argument and the first row is for the second argument.

Examples

Let the free variables be P and Q in sake of demonstration.

Given P compare (E compare Q), the truth table is:

 |L E G
-+-----
L|L L E
E|L E G
G|E G G

Given (P min Q) compare (E compare (P max Q)), the truth table is:

 |L E G
-+-----
L|L E E
E|E E E
G|E E G

Given ((E compare (P max Q)) compare (P min Q)) mappend (P compare (E compare Q)), the truth table is:

 |L E G
-+-----
L|G L E
E|L E G
G|E G L

Given Q mappend P, the truth table is:

 |L E G
-+-----
L|L L G
E|L E G
G|L G G
\$\endgroup\$
0
\$\begingroup\$

Floating-point numbers have errors in addition, oh my!

Given two IEEE double-precision floating-point numbers \$x\$ and \$y\$, output the most accurate IEEE double-precision floating-point approximation of the error that would be caused by performing the floating-point addition \$x + y\$.

As a reminder, an error is defined as the approximation subtracted by the exact value.

The inputs must deal with denormal numbers and the negative zero as well. However, infinities and NaNs fall into don't care situation.

If the output is zero, its sign doesn't matter.

\$\endgroup\$
2
  • \$\begingroup\$ "If either of the operands is an infinity or a NaN, the output shall be NaN" - what about languages that don't support infinity/NaN. \$\endgroup\$
    – The Thonnu
    Feb 22, 2023 at 16:53
  • \$\begingroup\$ I'm thinking their equivalent of 1/0... or maybe just the strings "NaN" and "Infinity" \$\endgroup\$ Feb 24, 2023 at 12:10
0
\$\begingroup\$

A Fine sequence with fine interpretations

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

Making one shape out of dissections of another

\$\endgroup\$
7
  • \$\begingroup\$ Can the pieces be rotated and/or flipped? \$\endgroup\$
    – Bubbler
    Feb 22, 2023 at 7:29
  • \$\begingroup\$ @Bubbler Yes, they can be moved, rotated, and flipped, but nothing else. I will clarify in the post. \$\endgroup\$
    – Aiden Chow
    Feb 22, 2023 at 7:30
  • \$\begingroup\$ Two falsy test cases are actually truthy: tio.run/##S07MK0ss/v8/… \$\endgroup\$
    – Bubbler
    Feb 22, 2023 at 7:31
  • \$\begingroup\$ @Bubbler Lol f*** i am bad at puzzles \$\endgroup\$
    – Aiden Chow
    Feb 22, 2023 at 7:32
  • \$\begingroup\$ How am i gonna make the falsey test cases if i can't even make these simple ones lol \$\endgroup\$
    – Aiden Chow
    Feb 22, 2023 at 7:33
  • 1
    \$\begingroup\$ Well, the correct answer here is to write a working code yourself and generate test cases based on it :) \$\endgroup\$
    – Bubbler
    Feb 22, 2023 at 7:38
  • 1
    \$\begingroup\$ @Bubbler You overestimate my coding capabilities :( \$\endgroup\$
    – Aiden Chow
    Feb 22, 2023 at 7:39
0
\$\begingroup\$

Find the sum of the sub-list with the largest sum

Challenge

Given a list that contains an integer, a sub-list is a contiguous non-empty sequence of elements within that list.

For example, the list \$[1, 3, -2, 5, -6]\$, has some sub-lists \$[1]\$, \$[3, -2]\$, \$[3, -2, 5, -6]\$, etc.

Your job today is to find the sum of the sub-list with the largest sum.

For example \$[1, 3, -2, 5, -6]\$, the sum of the sub-list with the largest sum is \$7\$, the sub-list with the largest sum is \$[1, 3, -2, 5]\$.

Specification

  • Input / Output can be taken in any reasonable format, taking a list of numbers and returning a number, which is the sum of the sub-list with the largest sum
  • The input array is guaranteed to have at least one element.

Testcases:

[1, 3, -2, 5, -6] -> 7
[-2, 1, -3, 4, -1, 2, 1, -5, 4] -> 6
[-3, -6, -8, -1] -> -1
[5, 4, -1, 7, 8] -> 23

This is , so shortest answer (in bytes) wins!

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

Canonical form of a cubic Bézier curve

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

Draw Parcly Taxel's cutie mark

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

Generating all possible equations with 10 characters

\$\endgroup\$
6
  • 1
    \$\begingroup\$ Welcome to CGCC, and thanks for using the Sandbox! This looks like an interesting challenge. You'll need to specify a winning criterion; it looks like you're interested in fastest-code (code should run as fast as possible), but we normally don't combine that with restricted-time (code must take less than an hour to run). fastest-code without the 1-hour restriction is one option; code-golf + restricted-time is another. If you're not sure which to choose, I recommend code-golf, since it's easier to calculate the score for submissions. \$\endgroup\$
    – DLosc
    Mar 1, 2023 at 23:21
  • \$\begingroup\$ @DLosc I wanted this "1 hour" part to be just a kind of side note for people to know what to expect regarding the performance. I changed my question to make it seem less like a winning requirement \$\endgroup\$
    – ordptt
    Mar 1, 2023 at 23:32
  • \$\begingroup\$ You need to objectively specify what does "as fast as possible" mean. See codegolf.stackexchange.com/tags/fastest-code/info and refer to other fastest-code challenges for inspirations: codegolf.stackexchange.com/questions/tagged/… \$\endgroup\$
    – pajonk
    Mar 2, 2023 at 7:51
  • \$\begingroup\$ @pajonk is it better? I edited it \$\endgroup\$
    – ordptt
    Mar 2, 2023 at 14:31
  • \$\begingroup\$ @ordptt for me it would be enough. Also, do I understand "There are no operators in sequence" correctly that this applies only to successive operators like 1*-1, but -1+2=1 is ok? What about 1-2=-1 - is this valid? Or 1*(-1)? \$\endgroup\$
    – pajonk
    Mar 2, 2023 at 19:07
  • \$\begingroup\$ @pajonk it means that there cannot be any *,+,-,/ adjacent to each other, so I believe you got it right. So 1*-1 is not valid but -1+2=1, 1*(-1) are. 1-2=-1 is not valid because the RHS is negative. \$\endgroup\$
    – ordptt
    Mar 2, 2023 at 20:30
0
\$\begingroup\$

Generate number from given range

\$\endgroup\$
10
  • 1
    \$\begingroup\$ This downvote was too fast :) \$\endgroup\$
    – EzioMercer
    Mar 3, 2023 at 21:32
  • \$\begingroup\$ Is the output a floating point number? \$\endgroup\$
    – noodle man
    Mar 5, 2023 at 14:29
  • \$\begingroup\$ @Jacob It is doesn't matter. For example if your function for inputs 10.55, -1, 1 will output 0 it is fine but if the output will be 0.55 it is also fine. In general the output can not be only integers because at least for range [0, 1] you can output integers 0 and 1 only for two different speed of wind, another wind speed must give some number from range (0, 1) \$\endgroup\$
    – EzioMercer
    Mar 5, 2023 at 14:41
  • \$\begingroup\$ Could you clarify the sentence "and multiply this value to -1 pseudo randomly"? \$\endgroup\$ Mar 5, 2023 at 18:59
  • \$\begingroup\$ @CommandMaster It is just part of story :) He do it to get the negative numbers too, because of speed of wind can not be negative number \$\endgroup\$
    – EzioMercer
    Mar 5, 2023 at 19:11
  • \$\begingroup\$ What's the range of the speed of wind? Can it be any real number? \$\endgroup\$ Mar 5, 2023 at 19:12
  • 2
    \$\begingroup\$ "Your function must output different result each time for different wind speed" - I think it may be impossible for some inputs because of floating point inaccuracies (there is in practice only a finite amount of floating point numbers between two integers) - or are we to ignore floating point errors? \$\endgroup\$
    – pajonk
    Mar 5, 2023 at 19:33
  • \$\begingroup\$ @CommandMaster It can be any float or double (it is up to you) number which provide your chosen language \$\endgroup\$
    – EzioMercer
    Mar 5, 2023 at 21:03
  • \$\begingroup\$ @pajonk Good point. Yes, of course you can ignore floating point errors. I will add it to challenge \$\endgroup\$
    – EzioMercer
    Mar 5, 2023 at 21:05
  • 1
    \$\begingroup\$ This is a pretty easy challenge but still fairly interesting \$\endgroup\$
    – noodle man
    Mar 5, 2023 at 22:41
0
\$\begingroup\$

The topology of origami

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

Guessing on straws

Dedicated to Martin Gardner, taken from his book

Background

In the old days, the Slavs had such divination. One girl was clutching six straws in her fist, and her friend was pairing first the top and then the bottom ends. If all straws were ring-tagged, the girl would be married.

Goal of challenge

Having the number of straws and the binding schemes, determine whether the single hole cycle will result.

enter image description here

Input

  • Number of straws N, even integer >= 2
  • Two nested lists as top and bottom links, for instance [[1, 4], [2, 5], [6, 3]]

Schemes may be valid or invalid! On one hand, it is guaranteed that are used only positive integers not more N; no self-links ([1, 1]), no tautologies ([[1, 2], [2, 1], …]), no broken lists ([[1], [ ], …]) etc.

Please note, that pairs are unordered, so eg [[1, 4], [2, 5], [6, 3]] and [[4, 1], [2, 5], [3, 6]] both valid (and equivalent) schemes.

But due to the girl’s inattention, followed cases can take place:

  • Missing pair (one or more): [[1, 4], [6, 3]], [[1, 10], [4, 5], [3, 7]] etc.
  • More than two binding straws: [[1, 4], [1, 5], [2, 6]] (and as result, free straws)

All of these inputs are invalid, your program must detect them and stop with appropriate message (see below).

Output

Any three distinct symbols for "Invalid input", "No loop", "Has loop" cases. Suitable for golfing on your language:

  • -1 for invalid input, 0 for "No loop", 1 for "Loop"
  • None, False and True respectively
    etc.

Test cases

N: 2, TopLinks: [[1, 2]], BottomLinks: [[2, 1]]  → True

N: 4, TopLinks: [[1, 2], [3, 4]], BottomLinks: [[2, 1], [3, 4] ]  → False

N: 4, TopLinks: [[1, 2], [2, 4]], BottomLinks: [[2, 1], [3, 4], [3, 2] ]  → Invalid

N: 8, TopLinks: [[1, 2], [3, 4], [6, 5], [7, 8]], 
BottomLinks: [[8, 1], [3, 2], [4, 5], [7, 6] ]  → True
\$\endgroup\$
2
  • 2
    \$\begingroup\$ We usually try to avoid requiring input validation, and I feel like in this case it doesn't make the challenge any more interesting. Is there a particular reason you want it? Additionally, I'll suggest allowing other reasonable input formats, for example a list of values from 1 to n/2 where paired indices have the same value. \$\endgroup\$ Mar 8, 2023 at 11:12
  • \$\begingroup\$ @CommandMaster Well, I thought without validation this is a very simple challenge. But I will take into account your proposal, I will probably remove validation when publishing \$\endgroup\$
    – lesobrod
    Mar 8, 2023 at 11:17
0
\$\begingroup\$

Shortest Code to Implement a Simple Encryption Algorithm

Write a program or function that implements a simple encryption algorithm. The algorithm takes in a string of ASCII characters and a key, and outputs an encrypted string.

Encryption Algorithm

  1. Convert each character in the input string to its ASCII value
  2. XOR each ASCII value with the corresponding byte in the key (cycling through the key if necessary)
  3. Convert the resulting XOR values back to ASCII characters
  4. Concatenate the ASCII characters into a single string and output the result

Your program or function should take in two inputs:

A string of ASCII characters (up to 1024 characters in length) A key string (up to 1024 characters in length) Your program or function should output a single string representing the encrypted message.

Test Cases

Input:
message: "Hello World"
key: "secret"
Output:
"UW]cU\xf3M]Z]^Y"

Input:
message: "12345"
key: "password"
Output:
"^%-'$#"

Input:
message: "This is a test message"
key: "key"
Output:
"\x03\x1a\x1c\x17\x02\x0eP\bI\x11\x07\x14N\x1d\x10M\r\b\x1f"

This is a codegolf challenge, so the goal is to write the shortest possible implementation of the encryption algorithm. The winner will be determined based on the length of their code, with ties broken by earlier submission time.

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

Mapping Passing Through Point

\$\endgroup\$
7
  • \$\begingroup\$ Maybe I'm just missing something, but this function doesn't seem very well-specified. Since you don't have to handle f(x1) or f(x2), isn't f(x)=y0 allowed? \$\endgroup\$
    – Bbrk24
    Mar 9, 2023 at 17:44
  • \$\begingroup\$ @Bbrk24 The image of the function has to be over the whole range -- in other words, for every \$y \in (y_1, y_2)\$, there must be some \$x \in (x_1, x_2)\$, such that \$f(x) = y\$. \$\endgroup\$ Mar 9, 2023 at 17:55
  • \$\begingroup\$ Ah, that makes more sense. I'd be a bit more direct about it in the question, then. \$\endgroup\$
    – Bbrk24
    Mar 9, 2023 at 17:56
  • 1
    \$\begingroup\$ Is it guaranteed \$x_1 < x_0 < x_2\$? \$\endgroup\$ Mar 10, 2023 at 4:56
  • 2
    \$\begingroup\$ Do you have to return \$f\$? Can you take \$ x_0, x_1, x_2, y_0, y_1, y_2, x\$ and return \$ f(x) \$ (deterministically)? \$\endgroup\$ Mar 10, 2023 at 4:59
  • \$\begingroup\$ @CommandMaster I thought this is part of the open-ended-function tag, but yeah, you can do it that way. \$\endgroup\$ Mar 10, 2023 at 17:26
  • \$\begingroup\$ @CommandMaster Yes -- and \$y_1 < y_0 < y_2\$. \$\endgroup\$ Mar 10, 2023 at 17:26
0
\$\begingroup\$

Optimize Distance Travelled By Thrown Object (With Air Resistance)

Suppose you're on a flat surface, then throw an object with some velocity \$v_0\$ at an angle \$\theta\$ above the ground. Assuming the only forces on the object are (constant) gravity and air resistance, we can model the horizontal and vertical components of the acceleration (the instantaneous change in velocity in the x and y direction) as follows:

$$a_x = v_x^2 * \delta$$ $$a_y = v_y^2 * \delta + g$$

Where \$v_x\$ and \$v_y\$ are the horizontal and vertical components of velocity, \$g\$ is acceleration due to gravity, and \$\delta\$ is the coefficient of acceleration due to drag (which is a single number which depends on the drag coefficient, cross-sectional area, and the density of the fluid, and the mass).

The initial x and y components of velocity are $$v_x = v_0 \cos \theta, v_y = v_0 \sin \theta$$

The object stops moving when it hits the ground again (when its \$y\$ position becomes 0 again). We want to find the angle \$\theta\$ which maximizes the horizontal distance \$x\$ that the object travels.

Your challenge is, given the initial velocity \$v_0\$, acceleration due to gravity \$g\$, and coefficient of acceleration due to drag \$\delta\$, output the angle \$\theta\$ (in either degrees or radians) which maximizes the horizontal distance travelled, within one percent of the true maximum.

Standard loopholes are forbidden. Since this is , the shortest program wins.

TODO: Test cases

\$\endgroup\$
1
  • \$\begingroup\$ Can the person who down-voted explain why? \$\endgroup\$ Mar 28, 2023 at 2:09
0
\$\begingroup\$

Soundproof Cell

Here's a message from the organizers:

Hello! We hope this message finds you well.

Unfortunately, your performance in the past games has been sub par. Your team is unimaginative and lacks creativity. Instead of inventing novel techniques to win our games, you keep sticking to the techniques you know. This is not the spirit we would like to encourage in our games.

Tomorrow's game will be very different from what you're used to. To give you some time to prepare, we are telling you the rules in advance.

There are 1000000 balloons whose values you can download here. These balloons lie on a straight line. Your job is to pop these balloons in an order to get the most "brownie points".

A balloon may have zero, one or two adjacent balloons. For example, a balloon at the middle of the line has two adjacent balloons. A balloon at the beginning of the line has one adjacent balloon. And if you pop the second balloon, which is the balloon to the right of the first balloon, the balloon at the beginning of the line will have zero adjacent balloons.

When you pop a balloon, the "brownie points" you will get will be equal to the average value of all balloons adjacent to the balloon you pop. If the balloon you pop has no adjacent balloons, you get no points. Remember that when a balloon is popped, it is totally destroyed and you can't pop it again. And no worries, you don't have to pop all the balloons.

We have a pretty intelligent robot here. It is currently partying inside a soundproof cell. Tomorrow it will pop the balloons on your behalf.

The robot, albeit intelligent, has no knowledge of the values of the balloons. It has knowledge of all programming languages in existence. Before the robot pops the balloons, you have to give it some code that when executed, outputs a sequence of balloon indices. You are allowed to use either 0-based or 1-based indices. If after finishing the instructions the robot doesn't get the most "brownie points" possible on your behalf, you are disqualified. But if the robot successfully gets the most "brownie points", your score will be the length of the code you give the robot. The shorter, the better! This is !

Summary

  • The array of balloon values is fixed in advance.
  • You write a program that outputs the optimal sequence of balloons to pop.
  • Shortest code wins.

Tips

  • The array of values is randomly generated with a CSPRNG and there is no meaningful way to compress it. However, the sequence of balloons has several important patterns that you can exploit.
  • There are many optimal sequences, choose the sequence that is the easiest to compress.
  • Remember you don't have to pop all the balloons.
  • Your code may be too large to fit in an answer. You can host your code elsewhere, and your answer can contain an explanation of how you come up with the code.
\$\endgroup\$
3
  • \$\begingroup\$ I think the normal version, where given a list of balloons you output the best popping order, is a lot more interesting than the kologmonov-complexity verison \$\endgroup\$
    – mousetail
    Apr 12, 2023 at 8:27
  • \$\begingroup\$ Brute force would have a clear advantage over the O(n) solution though. I want to force solvers to come up with an O(n) solution first. The optimal order then has a very clear structure and can be further compressed. The alternative would be to set a fastest-code challenge, but such a challenge would be hard to objectively grade. \$\endgroup\$ Apr 12, 2023 at 9:29
  • \$\begingroup\$ You could make it restricted-complexity to prohibit brute force solutions \$\endgroup\$
    – mousetail
    Apr 12, 2023 at 9:30
0
\$\begingroup\$

Is it a plausible chess move

\$\endgroup\$
3
  • \$\begingroup\$ I would phrase this as "Can any piece move in this way", not "Under any arrangement of pieces ..." because there's never a reason to have more than one piece on the board. \$\endgroup\$
    – isaacg
    Apr 19, 2023 at 15:57
  • \$\begingroup\$ Also, this should say "You need to know how the queen and knight move" - the rook is subsumed by the queen, as you point out. Also, you should mention that castling is ignored. \$\endgroup\$
    – isaacg
    Apr 19, 2023 at 15:59
  • \$\begingroup\$ @isaacg clarified and simplified. I thought I had removed the rook move part from that sentence but I guess I hadn't :p \$\endgroup\$
    – lyxal
    Apr 20, 2023 at 7:54
0
\$\begingroup\$

What is the shortest python code which implements a string-to-float function?


Goal

Write the shortest variadic to_float, str2float, or standardizing function you can write subject to the constraint that the code is written in python.


Rules and Examples

The variadic str2float shall be some callable which accepts any one of the following inputs:

Input Data-type Example Input Output float
string "4.992" float 4.992
float s 4.992 float 4.992
int 9021 float 9021.0
a shallow iterable of strings ["4", ".", "9", "9", "2"] float 4.992
a string with zeros padded on the right and/or white-space padding on left "1234.650000\n" float 1234.65
a string with zeros padded on the left "016" float 16.0
a deeply nested iterable of strigs [["4", [[".", "9"]], ("9", "2")], ((((("1")), "0"), "1"), "0")] float 4.992101
a deeply nested iterable of a mix of floats, int, and strings [[4.0, [[".", 9]], (9, "2")], (((("1")), "0"), "10")] float 4.992101

The main assumptions about the variadic *args parameter are that there is a correctly-working method named __iter__ defined and that if you recursively search until isinstance(obj, str) returns True or not hasattr('__iter__', obj), then the object which is not a string has a __repr__ method which returns a string representation of a decimal point, integer, or floating point number.

Scoring

The scoring will be something in between code-golf and popularity contest:

Score = (upvotes - downvotes) - floor((bytes in code that outputs question) / 3)

The highest score wins.

(You can use http://mothereff.in/byte-counter as a byte counter.)

\$\endgroup\$
1
  • 6
    \$\begingroup\$ It's not recommended to have popularity-contests any more. Also, it's not recommended to restrict the challenge to one language \$\endgroup\$
    – The Thonnu
    Apr 22, 2023 at 11:24
1
136 137
138
139 140
156

You must log in to answer this question.

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