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

4670 Answers 4670

1
132 133
134
135 136
156
0
\$\begingroup\$

Dumbo octopus flashing time

Day 11 of this year's AoC was about a grid of bioluminescent dumbo octopi. I'll try to explain how this works, click on the link for a better explanation.

You have a 10x10 grid of octopi which slowly gain energy and flash when fully charged. Each octopus has an energy level from 0 to 9:

5483143223
2745854711
5264556173
6141336146
6357385478
4167524645
2176841721
6882881134
4846848554
5283751526

The flashes and energy amounts can be modelled with steps. At each step:

  • All octopi have their energy level increase by 1
  • Then, any octopus with an energy level greater than 9 flash. When an octopus flashes, it increments the energy levels of all octopi adjacent (orthogonally or diagonally) to it.
  • The flashing repeats until no more octopi can flash. Then, all octopi that flashed during this step have their energy level reset to 0.

For example (stolen from AoC):


Before any steps:
11111
19991
19191
19991
11111

After step 1:
34543
40004
50005
40004
34543

After step 2:
45654
51115
61116
51115
45654

In step 1, the octopi with energy level 9 are all incremented to 10 and flash, all incrementing the octopus with energy level 1 (initially incremented to 2) in the center and causing it to flash as well.

The second part of the puzzle asks how long a configuration will take to have all octopi simultaneously flash in the same step. However, not all configurations will do this - some will fall into other cycles. Eventually, all of the configurations will repeat themselves, as there's only \$10^{10 \cdot 10 } = 10^{100}\$ possible configurations - still a huge amount, but finite.

Your challenge is to find a configuration that takes as many steps as possible to return to a configuration it's reached before, with more steps being better.

People on reddit have been investigating this, and the number to beat is 372886.

Meta

Is the explanation good, and should I bother with it?

Better examples?

\$\endgroup\$
4
  • \$\begingroup\$ Why is it not \$10^{100}\$ configurations? Also, it's not initially clear that an octopus increments for each adjacent flash. \$\endgroup\$
    – att
    Dec 20, 2021 at 8:40
  • \$\begingroup\$ @att Oops, made a mistake with #1 and I'll try clear up #2 \$\endgroup\$
    – emanresu A
    Dec 20, 2021 at 8:42
  • \$\begingroup\$ @att Better now? \$\endgroup\$
    – emanresu A
    Dec 20, 2021 at 8:45
  • \$\begingroup\$ Yeah, I think so \$\endgroup\$
    – att
    Dec 20, 2021 at 8:51
0
\$\begingroup\$

Kolmogorov-simple numbers

\$\endgroup\$
3
  • \$\begingroup\$ Why not more flexible IO? Requiring a print seems pointless to me. \$\endgroup\$
    – pxeger
    Dec 15, 2021 at 8:50
  • 1
    \$\begingroup\$ Seems like this would be better if the challenge was stated as "output the smallest number \$l\$ in \$n\$ bytes, such that \$l < \log_{256}(n + 1)\$". I agree with pxeger, it's a lot more simple to just use our standard I/O rules, and not worry about it \$\endgroup\$ Dec 15, 2021 at 12:58
  • \$\begingroup\$ I've edited this down to a stub now that it's been posted to save space \$\endgroup\$ Dec 22, 2021 at 1:04
0
\$\begingroup\$

Return all letter counts as an integer

\$\endgroup\$
1
  • \$\begingroup\$ I've edited this down to a stub now that it's been posted to save space \$\endgroup\$ Dec 22, 2021 at 1:06
0
\$\begingroup\$

Repeat average

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

Event scheduler

\$\endgroup\$
6
  • \$\begingroup\$ Thanks for using the sandbox. The challenge type is called fastest-code here. It looks well-defined in general, but a few minor issues: 1) You don't really need restrictions in time complexity (the requirement 1) when submissions are competing for it. 2) Looks like the build instruction is linux only. If so, it should be clarified as such (along with dependencies like cmake). \$\endgroup\$
    – Bubbler
    Dec 7, 2021 at 2:12
  • \$\begingroup\$ FYI, language-specific challenges are discouraged here, and fastest-code without language restriction is usually done by measuring time on a single machine (often the challenge author's). (Not sure how to adapt this specific challenge to other languages, so I'd say it's fine in this case.) And unfortunately we're not like other competitive programming sites, and therefore have very few C++ users. \$\endgroup\$
    – Bubbler
    Dec 7, 2021 at 2:15
  • \$\begingroup\$ OK so would it be acceptable to submit a sample C++ code as guidance and open up to other languages? \$\endgroup\$
    – user108147
    Dec 7, 2021 at 5:12
  • \$\begingroup\$ @Bubbler Changed the terms of the challenge. Does it look better as it stands? \$\endgroup\$
    – user108147
    Dec 7, 2021 at 5:28
  • \$\begingroup\$ I mean, because of the nature of the task, it's near impossible to establish fair comparison across languages. Normally you'd feed input in a standardized format through STDIN and let the programs output through STDOUT, but 1e8 events would make I/O time way too significant. And not all languages have mt19937 implementation or seeding it like C++ does. So I was saying that limiting to C++ is fine here, though it won't get many answers on this site. \$\endgroup\$
    – Bubbler
    Dec 7, 2021 at 5:36
  • \$\begingroup\$ Hmm we could then open by classes like in boxing but per language here. Winner for C++, Java, Python, etc. And the IO is really not much - just generate a random sequence in memory and submit. \$\endgroup\$
    – user108147
    Dec 7, 2021 at 5:38
0
\$\begingroup\$

Identify basic building blocks for new natural and programming language

(Based on Invent new programming language and write Hello world in it, but re-phrased to gain more focus on answers).

There exists variety of different programming languages, also variety of natural languages.

If we could create new programming language, which in a turn would possess structure of natural language - what basic structures you would choose ?

one example of artificially constructed language is Toki Pona:

It can be used as natural language, but not as a programming language.

One example of programming language is for loop - for example in C#:

for(int i = 0; i < 10; i++) { ... }

If you want to express this kind of sentence in natural language - then you can use 'equals' for '=', 'less than' for '<' - but scope begins ('{') and scope ends ('}') is relatively long and difficult to express / pronounce.

What basic language structures you would propose for new language ?

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

Identify a "reverse checkers" position

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

High throughput prime numbers

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

Find out what type of adjective it is

There are 3 types of adjectives: absolute, comparative, and superlative adjectives. Your program should print what type of adjective it is. The program is supposed to work on finite lost of words.

Notes

  • Input will be a string, (not always 1 word), it'll be an adjective (the number of words will be finite)
  • Output must be anything that represents absolute, comparative and superlative.
  • The 15 adjectives below should be used as test cases and your program should be at least 90% accurate.

This is , so the shortest code wins!

Test cases:

pushiest => superlative
homeliest => superlative
most miserly => superlative
outgoing => absolute
most feline => superlative
roasted => absolute
frightening => absolute
fairer => comparative
more uncomfortable => comparative
more classic => comparative
smarter => comparative
muffled => absolute
scratchier => comparative
meager => absolute
tartest => superlative
\$\endgroup\$
6
  • \$\begingroup\$ So it only needs to correctly identify 14 of the below 15? \$\endgroup\$
    – rak1507
    Jun 30, 2021 at 14:00
  • \$\begingroup\$ @rak1507 at least. I'll add more test cases if necessary \$\endgroup\$
    – math scat
    Jun 30, 2021 at 14:02
  • \$\begingroup\$ @rak1507 but checking everyone of these won't work, as it's code golf \$\endgroup\$
    – math scat
    Jun 30, 2021 at 14:04
  • 4
    \$\begingroup\$ It should be made very clear whether the program is supposed to work on a finite list of words (as currently suggested) or on any input in theory. The approaches are going to be very different. \$\endgroup\$
    – Arnauld
    Jun 30, 2021 at 22:34
  • \$\begingroup\$ "tartest" is a superlative. \$\endgroup\$
    – DjinTonic
    Jul 17, 2021 at 2:04
  • \$\begingroup\$ @DjinTonic okay done \$\endgroup\$
    – math scat
    Jul 25, 2021 at 7:58
0
\$\begingroup\$

Is it a mathy-county-number

So I recently reinvented some boring type of number. Reinvented because I'm sure somebody else invented it before, but 'till figure out who invented it, let's call these numbers mathy-county-numbers.

So what is a mathy-county-number?

A mathy-county-number number is a number whose count of each digit also appears in the number.
It's a number like 332222410.
The number 3 occurs 2 times, so there must be a 2. The number 2 occurs 4 times, so there must be a 4. 4 occurs once, and some other numbers occur 0 times.

The task is simple: Write a program to check if a number (received by the input) is a mathy-county-number.

Rules

  • Default loopholes apply
  • Default I/O rules apply
  • Output must be a truthy or falsy value
  • No digit will appear more than 9 times

Examples

332222410
> True

33222241
> False

1
> False

33222
> False

1122233334444455555566666667777777788888888890
> True

112223333444445555556666666777777778888888889
> True

122334444456789
> False
\$\endgroup\$
14
  • \$\begingroup\$ "The number 3 occurs 2 times, so there must be a 2." Does this means "3312" is valid as there is a 2 (although not followed by 3). \$\endgroup\$
    – tsh
    Jul 26, 2021 at 9:48
  • \$\begingroup\$ Why "1" is falsy? \$\endgroup\$
    – tsh
    Jul 26, 2021 at 9:49
  • \$\begingroup\$ @tsh 1: no, because in 3312 there's no 0. but eg. 03312 would be valid \$\endgroup\$
    – math scat
    Jul 26, 2021 at 10:44
  • \$\begingroup\$ @tsh 2: 0 is not there, 9, 8, 7, ... occur 0 times \$\endgroup\$
    – math scat
    Jul 26, 2021 at 10:54
  • \$\begingroup\$ "If any digit occurs more than 9 times, count it as a 9." is needlessly complicated, I'd suggest just saying that no digit will appear more than 9 times. The output for the last 2 test cases in unclear - what is sdcvbhnjmk supposed to represent? Just use something like true/false. Additionally, why is 33222 true? By the same logic as the example, it contains "some other numbers zero times", so should include a 0 to be true, right? \$\endgroup\$ Aug 6, 2021 at 14:02
  • \$\begingroup\$ @cairdcoinheringaahing thanks, don't know why I'm addressing the changes now \$\endgroup\$
    – math scat
    Jan 7, 2022 at 13:06
  • \$\begingroup\$ If 0 does not occur in a number, does that mean that some number appears 0 times? \$\endgroup\$ Jan 7, 2022 at 14:03
  • \$\begingroup\$ 0 has to occur in a mathy-county-number if some number appears 0 times. Example 112 would be invalid because eg. 3 appears 0 times. 1120 would be valid though. \$\endgroup\$
    – math scat
    Jan 7, 2022 at 14:20
  • \$\begingroup\$ @thejonymyster ^ \$\endgroup\$
    – math scat
    Jan 7, 2022 at 14:30
  • \$\begingroup\$ in the number 123456789, 0 appears 0 times. However, there is no 0 in the number to account for the fact that there is 0 of that number. Should this output truthy or falsy? (Note: this is a paradox) \$\endgroup\$ Jan 7, 2022 at 14:33
  • \$\begingroup\$ Also, you should clearly define what makes a number mathy-county, rather than having the user rely on an example. (Not that it isn't easy to figure out, but it's good practice and leads to no assumptions being made, especially ones you weren't prepared for) \$\endgroup\$ Jan 7, 2022 at 14:34
  • \$\begingroup\$ @thejonymyster I'd say the number itself should be falsy because there is no 0. However even if you add a 0 it'll stay falsy. \$\endgroup\$
    – math scat
    Jan 7, 2022 at 14:40
  • \$\begingroup\$ Thanks, I'll add that in \$\endgroup\$
    – math scat
    Jan 7, 2022 at 14:41
  • \$\begingroup\$ @mathcat so any number without a 0 in it should return falsy. Also, numbers with leading 0s being counted differently is strange. How is 01 different from 1, numerically? \$\endgroup\$ Jan 7, 2022 at 15:09
0
\$\begingroup\$

Wordle

The game Wordle is quickly becoming popular.

The way the game works is as follows:

  1. Players are trying to guess a 5 letter word
  2. Players have six attempts to guess the word
  3. After each attempt, the player is told:
    a. which letters are in the right place (index)
    b. which letters are in the word, but in the wrong place
    c. which letters are not in the word at all

The aim of this challenge is to create a Wordle application, as follows:

  1. On startup, the application reads from a dictionary of 5-letter words and selects one at random
  2. The application then prompts for user input
  3. On input:
    a. The application checks the input to ensure it is also a 5-letter word and in the dictionary. If it is not, the application returns any failure message and exits
    b. If the input matches the chosen word, the application returns any success message and exits
    c. Otherwise, the application checks the letters of the input against the letters of the word, and returns to the user an indicator for each letter of whether it is correct, misplaced, or incorrect. If a letter is repeated in the input, only the first occurrence is marked as correct/misplaced; the remaining occurrences are marked as incorrect [sandbox qn: what happens if the actual word has repeated letters?]
  4. Steps 2 and 3 are repeated until the application aborts in step 3a or 3b; or the user has failed to guess the word a total of 6 times; at which point the application exits

Sandbox

I am thinking of this as a code-golf challenge, but I wonder if there's another approach where people create the bot that's trying to solve the challenge, as well as the bot that's providing the challenge? Or something else? I'm not familiar with the other types of challenges to suggest something.

\$\endgroup\$
6
  • \$\begingroup\$ The hints wordle gives back are a little more complicated when a letter appears multiple times in the guess. I still think there's a lot left to clear up here otherwise but that sort of edge case behavior is one thing that should be made clear. \$\endgroup\$
    – Wheat Wizard Mod
    Jan 11, 2022 at 15:04
  • \$\begingroup\$ @WheatWizard Thanks, yes, that's why I put it in the sandbox! \$\endgroup\$ Jan 11, 2022 at 15:04
  • \$\begingroup\$ Somone has played around and found out the repeated letter rules over on Reddit. \$\endgroup\$ Jan 11, 2022 at 23:29
  • \$\begingroup\$ IMHO in its current form it contains too many tasks in one challenge. Maybe limit the challenge just to step 3 or even just step 3c? \$\endgroup\$
    – pajonk
    Jan 12, 2022 at 11:10
  • \$\begingroup\$ @JonathanAllan thanks, that's helpful. \$\endgroup\$ Jan 13, 2022 at 15:24
  • \$\begingroup\$ @pajonk I agree it's probably too complicated as-is for a [code-golf] challenge. I don't want to reduce it to just a simple "which letters are in the correct place" challenge though - I feel that's not interesting enough. What about a different scoring system - perhaps a variant of [king-of-the-hill], where you're either trying to create a bot that can solve Wordle quickly; or a bot that picks words to avoid being solved easily - and they're pitted against each other. Would that be more interesting? \$\endgroup\$ Jan 13, 2022 at 15:27
0
\$\begingroup\$

Universal Command Sequence

Posted

\$\endgroup\$
4
  • \$\begingroup\$ Very related: Shortest universal maze exit string \$\endgroup\$
    – xnor
    Jan 6, 2022 at 8:59
  • \$\begingroup\$ I feel like answers to this will be very hard to test. Probably the golfiest way to do this, if it's valid, would be not to code anything with mazes, but just to produce some massive random-looking sequence of directions. If the length is a fast-enough growing function of n, with overwhelmingly high probability works on every maze for every n. Like, "output 3^3^3^n in base 4". \$\endgroup\$
    – xnor
    Jan 6, 2022 at 9:06
  • \$\begingroup\$ @xnor This question is actually from a Russian math olympics problem :D that problem is to prove the existence of the sequence. The proof states that the sequence can be generated by following a certain rule (a certain rule of concatenating sequences, to be exact). And I think I do have written a testing code for it; see section "Test Code" (sorry but English isn't my mother tongue, but now I know it is called a "validator", isn't it?) \$\endgroup\$
    – atzlt
    Jan 6, 2022 at 15:42
  • \$\begingroup\$ Ah yes I rembered, it's 1998 All-Russian Math Olympiad, Grade level 9, Day 1, Problem 4. Original problem is 8x8 grid, but the proof can be generalized to nxn. \$\endgroup\$
    – atzlt
    Jan 6, 2022 at 16:02
0
\$\begingroup\$

Generate Fmbalbuena Numbers

\$\endgroup\$
6
  • 1
    \$\begingroup\$ so both the second step and last step need to be true? \$\endgroup\$
    – Razetime
    Jan 6, 2022 at 11:35
  • \$\begingroup\$ @Razetime 2nd and last step need to be true. \$\endgroup\$
    – Fmbalbuena
    Jan 6, 2022 at 11:36
  • 1
    \$\begingroup\$ combine step 1 and step 2 into "Check if the number of digits is a multiple of 3", and change "N/3 digits" to something like "Length / 3 digits" (to me "N/3" reads like the original number divided by 3). \$\endgroup\$ Jan 6, 2022 at 14:22
  • \$\begingroup\$ "equals the last digits" → "equals the last digits modulus 10" \$\endgroup\$
    – Adám
    Jan 7, 2022 at 0:01
  • \$\begingroup\$ You should state clearly that having a digit count that isn't divisible by 3 is enough to make a number not be a Fmbalbuena number. \$\endgroup\$
    – Adám
    Jan 7, 2022 at 0:02
  • \$\begingroup\$ What is the goal of the challenge? decision-problem for a single given number (I recommend that) or generating the sequence up until a limit, or maybe the first n number, or "infinitely" spitting out more? \$\endgroup\$
    – Adám
    Jan 7, 2022 at 0:04
0
\$\begingroup\$

Generate Matching Regexes

Write a program that takes two lists of strings and generates a javascript regex that fully matches all the strings in the first list and matches none of the strings in the second list.

To be specific when for all stings in the first list str.match(\[your-output]\) must produce a match for the full string and for all strings in the second list str.match(\[your-output]\) must produce no matches.

Scoring

Your score is the average length of the regexes you produce not the length of your program. Your regexes should be tested against this file: https://pastebin.com/9iNZvGJC. Take the file in blocks of 20 lines and run your program with the first ten lines to match and the second ten to not match. Average the lengths of your program's output for all of the blocks in the file. This is your score.

Rules

  • Do not output the \ for the regex
\$\endgroup\$
4
  • \$\begingroup\$ I'm guessing this is a Sandbox for this closed challenge, and that you're aiming to improve the existing one? I'd suggest clarifying the scoring system, I'm not fully sure how exactly it works? \$\endgroup\$ Jan 15, 2022 at 22:46
  • \$\begingroup\$ Yes it is. Could you clarify what you find confusing about the scoring system? \$\endgroup\$
    – user197974
    Jan 15, 2022 at 23:06
  • \$\begingroup\$ "Take the file in blocks of 20 lines and run your program with the first ten lines to match and the second ten to not match." Seems like it would be simpler to just provide the lines to match and the lines to not match, rather than doing the line splitting bit \$\endgroup\$ Jan 15, 2022 at 23:10
  • \$\begingroup\$ You would have to split it into blocks anyway so that doesn't really seem easier? \$\endgroup\$
    – user197974
    Jan 15, 2022 at 23:17
0
\$\begingroup\$

Help Bob split his apples

Bob has a lot of apples, and he wants to split his apples with his friends evenly. (Including Bob.) However, every \$k\$th friend already has a lot of apples and does not need apples. Bob, however, is kind so he gives each \$k\$th friend the number of apples the friend already gives floor divided by three.

Given the input format below, a number \$k\$, and a number \$a\$ for the number of apples, output the number of apples that each person receives.

Test Cases

[0, 0, 80, 0, 0, 45, 0, 0, 12] 3 100 => [10, 9, 26, 9, 9, 15, 9, 9, 4]
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Could you try to clarify a bit more? \$\endgroup\$
    – ophact
    Jan 19, 2022 at 13:46
0
\$\begingroup\$

Move the next greater number, with fewest fails to take everything

Your task is to move the next greater number, with fewest fails to take everything

A fail counts if a step is the lower number (not counting equal)

The starting point is the top left corner

Example:

1234

Can do like this

>>>

Because moves to right, the next number is greater than previous

But

124
435

Is impossible so the fewest fails are:

>>v<<

1 > 2 > 4 > 5 < 3 > 4

So this has 1 fail

Test cases:

The output is not exact, there are multiple possible solutions

Input:

123
245
175

Output:

>>vv<<^>

Input:

123456789

Output:

>>>>>>>>

Input:

987654321

Output:

>>>>>>>>

Input:

123
456
789

Output:

>>vv<<^>

Input:

123456789
987654321
123456789

Output:

>>>>>>>>v<<<<<<<<v>>>>>>>>

Meta:

  • Any feedback?
\$\endgroup\$
2
  • \$\begingroup\$ I like the idea but I think the explanation could include the definition of a "fail". \$\endgroup\$
    – Wezl
    Jan 19, 2022 at 16:28
  • \$\begingroup\$ @Wezl-yizl Added \$\endgroup\$
    – Fmbalbuena
    Jan 19, 2022 at 16:31
0
\$\begingroup\$

Euler Irregular Primes

Your challenge is to find all Euler Irregular Primes (A prime p is Euler-irregular if it divides an Euler number E(2n) with 0<2n<p−1) under n.

Scoring

Your goal is to use the least amount of bytes.

\$\endgroup\$
2
  • \$\begingroup\$ welcome to this website ! challenges should be self contained, so while the link is good, you should Also include a definition of euler irregular primes \$\endgroup\$ Jan 20, 2022 at 1:49
  • \$\begingroup\$ Could you provide some test cases and define "storage" clearly? \$\endgroup\$
    – ophact
    Jan 21, 2022 at 10:45
0
\$\begingroup\$

Approximate Euler's Number

Euler's number (e) is one of the most well-known mathematical constants, with a simple way to approximate it. All you have to do is repeatedly generate a random number between 0 and 1, and add it to an accumulator. Record how many times you have to do this for the accumulator to exceed 1. If you do this over and over again, the average of all the times it takes will approximate Euler's number.

Your task

Write a program that, given a number of iterations as input, approximates Euler's number using the above method and prints the result.

Scoring

This is code-golf, so shortest answer wins. However, if the language you are using does not include a random number builtin, you may exclude from your byte count the code required to import it. For example, if you're writing a Python program to do this, you may assume the random module is already imported.

Example implementation (ungolfed)

import random
acc = 0
res = []
for x in range(int(input())):
    while acc < 1:
        acc += random.random()
    res.append(acc)
    acc = 0
total = 0
for x in res:
    total += x
print(total / len(res))
\$\endgroup\$
5
  • \$\begingroup\$ This looks like it's probably unobservable. You also need to specify how "random" is defined: can programs use a biased RNG (which will inevitably produce an inaccurate approximation)? \$\endgroup\$
    – pxeger
    Jan 24, 2022 at 17:55
  • \$\begingroup\$ @pxeger Good point. How should I go about fixing this? \$\endgroup\$
    – Ginger
    Jan 24, 2022 at 17:56
  • 1
    \$\begingroup\$ IMO the interesting part of this is never going to be the random number generation, but the calculation based on them. You could just have challenges accept an input of a list of numbers, presuming them to be random, and give the approximation calculated if that list were the source of random numbers. So an input of [random.random() for _ in range(10000)] will produce about 2.72, but an input of [0] * 10000 will produce some completely wrong output \$\endgroup\$
    – pxeger
    Jan 24, 2022 at 18:00
  • \$\begingroup\$ Also, your code is missing a parenthesis on line 4, and outputs \$ e / 2 \$, not \$ e \$. \$\endgroup\$
    – pxeger
    Jan 24, 2022 at 18:00
  • \$\begingroup\$ @pxeger I know the RNG is boring, whcih is why it's excluded from the byte count. Sorry about the bugs, I'm fixing them. \$\endgroup\$
    – Ginger
    Jan 24, 2022 at 18:02
0
\$\begingroup\$

Count the ways to transform (3)

Your input is an infinite matrix (2d array) of nonneagtive integers where a finite area of the matrix contains nonzero integers. For example:

\begin{matrix} \ddots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \cdot^{\cdot^\cdot} \\ \cdots & 0 & 0 & 0 & 0 & 0 & 0 & \cdots \\ \cdots & 0 & 1 & 0 & 0 & 6 & 0 & \cdots \\ \cdots & 0 & 3 & 5 & 4 & 3 & 0 & \cdots \\ \cdots & 0 & 2 & 0 & 0 & 0 & 0 & \cdots \\ \cdots & 0 & 0 & 0 & 0 & 0 & 0 & \cdots \\ \cdot^{\cdot^\cdot} & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \ddots \end{matrix}

A semi-continuous transformation of this matrix is a rearranging of elements that preserves immediate neighbors. For example, we can swap the \$2\$ in the bottom left, and the \$1\$ in the top left corner.

\begin{matrix} \ddots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \cdot^{\cdot^\cdot} \\ \cdots & 0 & 0 & 0 & 0 & 0 & 0 & \cdots \\ \cdots & 0 & 2 & 0 & 0 & 6 & 0 & \cdots \\ \cdots & 0 & 3 & 5 & 4 & 3 & 0 & \cdots \\ \cdots & 0 & 1 & 0 & 0 & 0 & 0 & \cdots \\ \cdots & 0 & 0 & 0 & 0 & 0 & 0 & \cdots \\ \cdot^{\cdot^\cdot} & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \ddots \end{matrix}

This is fine, because every number has the same neighborhood counts. That is, we can make a sorted list of numbers and their neighbors, and verify the they are the same:

\begin{matrix} \text{Number} & \text{Neighbors} \\ 0 & 0,0,0,1 \\ 0 & 0,0,0,1 \\ 0 & 0,0,0,2 \\ 0 & 0,0,0,2 \\ 0 & 0,0,0,3 \\ 0 & 0,0,0,3 \\ 0 & 0,0,0,3 \\ 0 & 0,0,0,4 \\ 0 & 0,0,0,6 \\ 0 & 0,0,0,6 \\ 0 & 0,0,1,4 \\ 0 & 0,0,2,5 \\ 0 & 0,0,4,6 \\ 1 & 0,0,0,3 \\ 2 & 0,0,0,3 \\ 3 & 0,0,3,6 \\ 3 & 0,1,2,5 \\ 4 & 0,0,3,5 \\ 5 & 0,0,3,4 \\ 6 & 0,0,0,3 \\ \end{matrix}

(we ignore zeros that have 4 zeros next them, since there are always infinitely many)

TODO: Add example where non-semi-continuous transformation changes the neighborhood counts.

To clarify, semi-continuity is a property of a transformation on some specific matrix.

Your code will take an infinite matrix as input and output the number of (unique) semi-continuous transformations. Two transformations are different if the output is different. So for example swapping two identical numbers is the same as the identity transform (doing nothing). Also, since it's an infinite grid, simply translating the pattern is the same as doing nothing. You can assume the output number will always be finite.

Or in other words: Your code will take an infinite matrix and return the number of matrices with those dimensions that have the same neighbor-list.

Input can be in any reasonable format. You can decide if your input has to have some amount zero-padding or if you always take a square matrix, etc.

Meta

Is this more interesting than version 2?

\$\endgroup\$
3
  • \$\begingroup\$ not sure what being infinite adds to the challenge since all that changes is that translations do nothing \$\endgroup\$ Jan 25, 2022 at 15:39
  • \$\begingroup\$ If the non-zero portions form multiple disconnected segments with more than one \$0\$ separating them the result can be infinite. For example if the finite portion is entirely within \$1,0,0,2\$ then you can just move the two non-zero values as far apart as you wish. \$\endgroup\$
    – Wheat Wizard Mod
    Jan 25, 2022 at 15:41
  • \$\begingroup\$ @WheatWizard Yes, that's what I meant with "You can assume the output number will always be finite" \$\endgroup\$
    – AnttiP
    Jan 25, 2022 at 16:22
0
\$\begingroup\$

Alternate indexing

Task

(To do.)

Test Cases

[[1, 2, 3], [4, 5, 6]] => [[1, 4], [[1, 2, 3], [4, 5, 6], [3, 2, 1], [6, 5, 4]]
[[1, 1, 1]] => [[1, 1, 1], [1, 1, 1]]
[[]] => [[]]
\$\endgroup\$
0
\$\begingroup\$

Ragged Matrix

A ragged matrix, is a matrix that has a different number of elements in each row. Your challenge is to write a program in any favorable language to find the indices of all occurrences of target in the ragged matrix.

Input:

A list of ragged lists (can be empty) of positive integers and a target range e.g. 56-26. The target range, given 2 positive integers. For languages that do not support this type of list, you can input it as a string representation

You may assume that a<=b

Output:

If a number in the ragged list is within the range or equal to a or equal to b, output the index of the ragged list then the index of the number in that ragged list e.g. 0 4 - The 0 is the first ragged list in the input and the 4 is the index of the number in the first ragged list

Test cases:

[[[1,3,2,32,19],[19,2,48,19],[],[9,35,4],[3,19]],19-53]
->
[[0,3],[0,4],[1,0],[1,2],[1,3],[3,1],[4,1]]

[[[1,2,3,2],[],[7,9,2,1,4]],2-2]
->
[[0,1],[0,3],[2,2]]

You can choose to follow the output format above or output it in the following as well:

[[[1,3,2,32,19],[19,2,48,19],[],[9,35,4],[3,19]],19-53]
->
0 3 0 4 1 0 1 2 1 3 3 1 4 1

0-based and 1-based indexing is allowed

You can output your answers in any way as long as it is distinguishable what the indexes of the number and matrix are

Tags:

code-golf

matrix

ragged-list

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Please consider adding tags below the title (refer to the template). Please also consider loosening the input criteria (e.g. allowing also inputting a and b as integers with assumption that a<=b). You may also explicitly state that both 1- and 0- indexing is ok. \$\endgroup\$
    – pajonk
    Jan 26, 2022 at 8:24
  • \$\begingroup\$ are my edits ok now pajonk? i have changed it accordingly \$\endgroup\$
    – DialFrost
    Jan 26, 2022 at 10:44
0
\$\begingroup\$

Truncatable Primes

A left-truncatable prime is a prime number and when the first digit is removed continuously, the result is always prime.

A right-truncatable prime is a prime number and when the last digit is removed continuously, the result is always prime.

If the integer is only a left-truncatable prime, return left

If the integer is only a right-truncatable prime, return right

Otherwise, return False

Test cases

9137 -> left
9137, 137, 37 and 7 are all prime
5939 -> right
5939, 593, 59 and 5 are all prime
139 -> False
1 and 9 are non-prime, so 139 cannot be truncatable from either way
103 -> False
It contains a 0 digit, which is not prime(even though 103 and 3 are primes)

You may assume that the input will never be both left and right truncatable, so an integer such as 5 will not appear in the input (it is prime and single digit so it is counted as both)

You are allowed to print False as 0 and left as 2 and right as 1

OR

left as 1 and right as 2

Otherwise, you are allowed to use any 3 distinct 3 values/string representation of left, right and False

Tags:

primes

classification

\$\endgroup\$
3
  • \$\begingroup\$ Rather than those specific strings, I would recommend letting people choose their own four distinct values \$\endgroup\$
    – user
    Jan 26, 2022 at 12:24
  • \$\begingroup\$ Also, I think there’s a tag for classification challenges \$\endgroup\$
    – user
    Jan 26, 2022 at 12:25
  • \$\begingroup\$ I'd recommend allowing some inputs to be both left and right-truncatable because it makes the challenge more exciting, but that's all up to you. \$\endgroup\$
    – ophact
    Jan 31, 2022 at 12:54
0
\$\begingroup\$

D'Hondt method

the D'Hondt method is a method for allocating seats in parliaments.

Given the Amount of representatives the district you're calculating for is given, and the total amount of voters, and a map or dictionary of every party alongside all the votes they got, you will be asked to give the amount of representatives each party will get from that electoral district as a map or dictionary depending on your language, or as a list/array of whose first index will contain of the party name as a string or char[], then the second the number of representatives as a number (the type is all fine as long as it doesn't have any decimals and is not negative, and their string conversions are something like this: f123 -> "123")

the algorithm: https://plus.maths.org/content/maths-minute-dhondt-method another link: https://en.wikipedia.org/wiki/D%27Hondt_method#Procedure

like as output those are all fine:

return mapOf(
    "Party A" to 4
    "Party B" to 3
)

or

return arrayOf(
    "Party A",  "1",
    "Party B",  "2"
)

you can even give Array as an output as long as you clarify how to get proper values from it somewhere in the answer (in easy terms of course) like:

arrayOf(
    "Party A" to 4
    "Party B" to 8
)

and you can post a snippet like

for(value in output)
{
    if(value::class == Int::class) println(value as Int)
    if(value::class == String::class) println(value as String)
}

The Input will be similiar as such:

  • mutableMap<String, Int>
    • "Partyname": Amount of votes it got
  • Amount of Voters in that district
  • Amount of representatives to be assigned to the parliament from that district

The Output will be something like:

  • mutableMap<String, Int>
    • "Partyname": Amount of representatives it got or
  • mutableList<String, String>
    • "Partyname": amount of representatives it got in a comprehensible form
      • Like: [PartyA, 5, PartyB, 10, PartyC, 15] or
  • List<List<String, String>
    • Like: [ [PartyA, 5], [PartyB, 10], [PartyC, 15] ]

Those are not strict rules and just a general remind that as long as the input/output is comprehensible it is fine

\$\endgroup\$
5
  • \$\begingroup\$ Challenges must be self-contained so you should describe the method directly in the post instead of relying on links. I'd recommend deferring to our I/O defaults rather than spending so much text describing possible formats (you've said yourself that there are no strict rules). Lastly, avoid using language-specific terms/concepts like mutableMap<String, Int>. \$\endgroup\$
    – Dingus
    Jan 31, 2022 at 11:13
  • 1
    \$\begingroup\$ Actually, looks like this challenge has already been done: It's election time! Hopefully the advice in my previous comment will be helpful for future posts. \$\endgroup\$
    – Dingus
    Jan 31, 2022 at 11:15
  • \$\begingroup\$ will sure keep this in mind, also, thanks for showing me the actual post about this. \$\endgroup\$ Jan 31, 2022 at 12:26
  • \$\begingroup\$ should I delete this \$\endgroup\$ Jan 31, 2022 at 12:26
  • 1
    \$\begingroup\$ I think so, yes. Unfortunately it's a duplicate of the earlier challenge. \$\endgroup\$
    – Dingus
    Jan 31, 2022 at 22:08
0
\$\begingroup\$

Tips for golfing in Seriously

I am looking for tips to code in this golfing language Seriously, and was wondering what some of the best/subtle ways for golfing this language is. Thx in advance!

\$\endgroup\$
1
  • \$\begingroup\$ Not much of a corpus left to learn from by example... \$\endgroup\$ Jan 31, 2022 at 6:25
0
\$\begingroup\$

Where is the line

Given a grid consisting of + and a constant \$m\$, change all the + to x, where the center of the + is above the graph \$y=mx\$.
Considering:

  • The origin is the bottom-left corner of bottom-left +.
  • One unit is the width=height of a plus
  • The center of the bottom-left plus is located in \$(0.5, 0.5)\$

Rules

  • Input can be given as an iterable, string or a binary matrix.
  • Instead of a grid, input can also be the \$height\$ and the \$width\$ of the matrix.
  • Output must consist of + and x
  • Standard Loopholes apply

Example

[In]:
++
++
0

[Out]:
xx
xx

[In]:
++
++
1

[Out]:
xx
x+

[In]:
+++++
+++++
+++++
+++++
2

[Out]:
xx+++
xx+++
x++++
x++++
+++++

Meta

Did I explain it good enough?

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

Solve Rubik's Clock

Posted here.

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

The Futuristic Battle Royale

After a five-year hiatus, I am interested in extending The Futuristic Gun Duel to a battle royale format. If this is not a bad idea, I will propose some rules and then start coding using some JS framework that uses WebWorkers. The primary motivation is that this should significantly change the strategy.

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

Iterate over pairs of nonnegative integers

Find and implement an iteration over the set of pairs of nonnegative integers (\$\mathbb N_0^2\$).

(0, 0) (0, 1) (0, 2) (0, 3) ...
(1, 0) (1, 1) (1, 2) (1, 3) ...
(2, 0) (2, 1) (2, 2) (2, 3) ...
(3, 0) (3, 1) (3, 2) (3, 3) ...
...

For example, diagonalization:

(0, 0), (0, 1), (1, 0), (0, 2), (1, 1), (2, 0), (0, 3), (1, 2), (2, 1), (3, 0), ...

You must specify which iteration your answer uses. You must specify if your answer is 0 or 1-indexed. Your code must be deterministic.

This is a challenge. You may...

  • Take a positive integer \$n\$ and output the first \$n\$ values.
  • Take a positive integer \$n\$ and output the value for \$n\$.
  • Take no input and output values indefinitely.

This is a challenge, so the smallest code as measured in bytes wins. Standard loopholes apply.

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

Loopless unending program

Your task is simple: Make a program in your language of choice that never exits. However, you are forbidden to use any looping constructs (for, while, etc). in your source. You may use any other instruction in your language to accomplish this goal.

This is , so shortest answer wins.

\$\endgroup\$
3
  • 2
    \$\begingroup\$ Can you define what a "looping construct" is? Also, you might run into (this issue)[codegolf.meta.stackexchange.com/a/8067/107310] with langs that just don't loop the same way. \$\endgroup\$ Feb 8, 2022 at 17:34
  • \$\begingroup\$ Related \$\endgroup\$
    – Aiden Chow
    Feb 9, 2022 at 4:14
  • \$\begingroup\$ I think "without looping" is not a well-defined requirement. You could instead require programs to block forever, which is observable at the OS level, although I think that then isn't very interesting challenge. \$\endgroup\$
    – pxeger
    Feb 9, 2022 at 7:21
0
\$\begingroup\$

Return shortest integer-is-one-of function

This is a challenge, where you'll ...

Given a number of integers as input, your task is to write a function in JS, which returns a function that checks if an inputted integer is any of those. For example, f([1, 2, 4]) would return a function which would return true if given 1, 2, or 4, and false for any other inputs.

Task:

Your function will take any nonzero number of integers. There will not be any duplicates. This should return either a function, or a string containing the function's code.

The returned function should take a single integer as input. If the input is one of the integers inputted into the original function, it should return a truthy value, and a falsy one otherwise. You may also choose to return any two consistent values, or a consistent value for one possiblity (such as 0 for truthy) and any other value for the other.

Scoring:

This is metagolf, meaning the average of the byte counts of the returned programs for a certain set of inputs will be your score. This set of inputs will consist of:

  • All combinations one to four integers from -10 to 10
  • A random group of 2000 inputs consisting of between five and ten integers between -100 and 100
  • A random group of 1000 inputs consisting of between two and six integers between -10000 and 10000

Optimizing your solution for these specific test cases is disallowed.

Meta

  • I will generate a random list of test cases that will be used for scoring when I post this
  • This is my first time writing a metagolf challenge. Any improvements y'all can think of?
  • I think the number of bytes should be limited in the submitted function (not the generated ones). Suggestions?
  • Too many test cases? Too few?
\$\endgroup\$
7
  • \$\begingroup\$ The option to return a function is only available in JS, I'm assuming? I think you should disallow it, because it's unclear how you measure the size of a function object. \$\endgroup\$ May 21, 2021 at 14:22
  • \$\begingroup\$ Maybe you should include test cases with more than six integers? If not, is it allowed for a solution to be optimized for small sizes? (given that it's a a minority in the input space) \$\endgroup\$ May 21, 2021 at 14:23
  • \$\begingroup\$ @CommandMaster Oh, I meant to restrict this to JS anyway, wasn't sure if I was going to do that or not. It should be possible in any language with first class functions, though. If I open it up to any language I'll allow returning a string with a program. I'll add some test cases with more items. \$\endgroup\$ May 21, 2021 at 14:56
  • \$\begingroup\$ If you return a function, how do you count the byte count of it? \$\endgroup\$ May 21, 2021 at 15:14
  • \$\begingroup\$ .toString() (plus it's pretty likely they'll return a string form of the function anyway) \$\endgroup\$ May 21, 2021 at 15:28
  • 2
    \$\begingroup\$ .toString() doesn't work for that, for example in function f(i) { var l = a=>a+i return x=>l(x) } f().toString() is just x=>l(x), while that's clearly not the length of the code you want to count (it's also unclear what code you do want to count in such a case) \$\endgroup\$ May 21, 2021 at 15:39
  • 1
    \$\begingroup\$ @CommandMaster The functions will need to be independent from the meta function. As in, no closures/accessing variables in the above scope. \$\endgroup\$ May 21, 2021 at 15:55
1
132 133
134
135 136
156

You must log in to answer this question.

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