573
\$\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\$
2
  • \$\begingroup\$ What if I posted on the sandbox a long time ago and get no response? \$\endgroup\$
    – None1
    Commented May 15 at 14:05
  • \$\begingroup\$ @None1 If you don't get feedback for a while you can ask in the nineteenth byte \$\endgroup\$
    – mousetail
    Commented May 29 at 13:27

4766 Answers 4766

1
147 148
149
150 151
159
-1
\$\begingroup\$

Output your Votes

The challenge is simple: Print to stdout the current total score (upvotes minus downvotes) of your answer.

For obvious reasons, you do not have to include the ID of your answer in the program; you may take it from stdin or specify a place in the code where the ID must be inserted. Your program may assume that the computer it is running on has internet access and isn't restricted by SE API ratelimiting. Your program does not need to use the SE API to get the vote count, but that's probably the easiest way to do it.

This is , so shortest code wins.

Meta

Is this a dupe? I wouldn't be surprised if it is.

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

Help Santa Rest

Santa has finally delivered presents to everyone at the stroke of midnight in a day, and now he wants some well-deserved rest. He is just about to sit down when the present managing computer suddenly calls out: "ME NEED REST." Now he is asking you to write him a program that does... nothing.

You will be given command line args, you will be given STDIN input. You must swallow/ignore the input and after some length of time, quit the program. You may have no externally visible effects (other than RAM/CPU usage obviously), and the program must not hang.

Testcases

NullPointerException

Scoring

Santa wants to type this in as quickly as possible so he can rest, so this challenge is scored in bytes, as standard .

\$\endgroup\$
8
  • \$\begingroup\$ So, what should the program do? Quit? Probably loads of 0-byte solutions to this. \$\endgroup\$
    – Adám
    Commented Dec 8, 2022 at 17:14
  • \$\begingroup\$ @Adám the program should do nothing and exit. true, but 0 byters are not possible in quite a few languages (i.e. Java, golflangs that implicitly print the input, C(++)(#), Kotlin) \$\endgroup\$
    – Seggan
    Commented Dec 8, 2022 at 18:04
  • \$\begingroup\$ Wouldn't it be more fun to have to swallow all input? \$\endgroup\$
    – Adám
    Commented Dec 8, 2022 at 18:12
  • \$\begingroup\$ @Adám isn't that what I'm describing? \$\endgroup\$
    – Seggan
    Commented Dec 8, 2022 at 18:13
  • 1
    \$\begingroup\$ Well, if the program quits immediately, it won't have a chance to swallow any input. \$\endgroup\$
    – Adám
    Commented Dec 8, 2022 at 18:52
  • \$\begingroup\$ @Adám not quit immediately. i meant do nothing from an outside perspective, as in no STDOUTput or alert boxes or whatever \$\endgroup\$
    – Seggan
    Commented Dec 8, 2022 at 21:57
  • \$\begingroup\$ Then it is still unclear to me. How long should the program run and swallow input before quitting? \$\endgroup\$
    – Adám
    Commented Dec 9, 2022 at 12:16
  • \$\begingroup\$ @Adám as long as it does not hand (i.e. enter into an infnite loop) \$\endgroup\$
    – Seggan
    Commented Dec 9, 2022 at 16:22
-1
\$\begingroup\$

Print my Country

This challenge is simple: Your program must output the two-character country code of the computer it is currently being run on, according to the computer's locale settings. (Capitalization doesn't matter.) For example, were I to run your program on a computer with a location set to the United States, your program should output US. You may assume that your program is running on the latest version (Bullseye, if I'm not mistaken) of Debian Linux. This does not need to be accurate; I could easily set the locale to Antartica while being in Britain.

This is , so shortest program wins!

\$\endgroup\$
3
  • \$\begingroup\$ Is this even possible? How can the computer possibly know where it is physically? And who says it even is in a country? \$\endgroup\$
    – Adám
    Commented Dec 13, 2022 at 14:34
  • \$\begingroup\$ @Adám The program only needs to print the system's locale, which may or may not be accurate. I could easily set my location to Antartica while actually being in Britain. I have clarified some of the language. \$\endgroup\$
    – Ginger
    Commented Dec 13, 2022 at 15:48
  • 1
    \$\begingroup\$ Removing the restriction of system could make it more interesting. Answers should specify what system / environment they are running on. \$\endgroup\$
    – tsh
    Commented Dec 15, 2022 at 7:13
-1
\$\begingroup\$

Generate 3d Christmas tree using "3d pixel art"

Imagine you are building Christmas tree from 1x1x1 sized cubes that can be attached just by sharing a side. At least one input - size, but can be more granular (height, bottom_width, etc.).

Any kind of algorithm you like, data representation should be set of 3d int vectors containing positions of each cube.

The more natural looking, the better.

If you want to visualize it too, even better. But it's not essential (I may try to visualize some of your creations).

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

Halting detector (Archived)

Choose a Turing complete language and write a program in this language that, given a program in 1TB, decide whether it halts.

Your program would likely be more than 1TB, which results in transferring difficulty1. Therefore, you only need to show your construction and prove your code length.

Shortest code in each language win. A Turing complete language which write 2TB of program to do a loop would make short submission but that won't be fun (and I don't think such language exist now)

1 Yeah it isn't the main point

\$\endgroup\$
8
  • 3
    \$\begingroup\$ This is literally impossible, due to the halting problem. Also how are we to submit an entire terabyte of code? \$\endgroup\$
    – Seggan
    Commented Dec 18, 2022 at 19:53
  • \$\begingroup\$ @Seggan I'm not the OP, but the problem specifies the program is less than 1 TB, so in principle you could solve it with a very large lookup table. And it says "you only need to show your code construction and prove your code length". \$\endgroup\$ Commented Dec 18, 2022 at 22:27
  • 1
    \$\begingroup\$ There are so many unsolved conjectures in mathematics that may impact the halting or non-halting of a program. For example, if you can express Collatz conjecture (specifically, an infinite loop that stops when a nontrivial Collatz loop is found) in 1TB of code in language X, you can't solve this challenge in X. \$\endgroup\$
    – Bubbler
    Commented Dec 18, 2022 at 23:13
  • \$\begingroup\$ @Bubbler True, I suppose whether you can solve it depends on what counts as a solution. Like could you just say "this solution exists, but you would need to solve a lot of conjectures to actually construct it"? \$\endgroup\$ Commented Dec 18, 2022 at 23:55
  • \$\begingroup\$ @Seggan For example, you can just create a language called PythonAfter1TB which simply ignore first 1TB source and interpret following source as Python 3.10 (or do nothing and halt if your source is less than 1TB). \$\endgroup\$
    – tsh
    Commented Dec 19, 2022 at 9:23
  • \$\begingroup\$ @tsh "but that won't be fun (and I don't think such language exist now)" \$\endgroup\$
    – l4m2
    Commented Dec 19, 2022 at 9:46
  • \$\begingroup\$ @tsh and what would that achieve? besides, it doesn't change the fact that this problem is impossible \$\endgroup\$
    – Seggan
    Commented Dec 19, 2022 at 14:05
  • \$\begingroup\$ @l4m2 I don't think there is some language (defined by implementation) support a single file with 1TB content as its source code currently. \$\endgroup\$
    – tsh
    Commented Dec 20, 2022 at 6:09
-1
\$\begingroup\$

Character Values Quine

Write a program that prints out a sequence of integer values corresponding to the character codes in the source code.

For example, the program:

print("Hello, World!")

has a valid corresponding integer sequence of:

112 114 105 110 116 40 34 72 101 108 108 111 44 32 87 111 114 108 100 33 34 41

Rules

  • You may use a non-numerical sequence delimiter of your choice.
  • The order of values in the sequence must be maintained for this challenge.
  • The integers may be in any numerical base with a radix of 2 or larger. (binary or up). All the integers must have the same base.
  • Code comments are permitted.
  • The program may not read from its own source code.
  • Standard loopholes apply.
  • This is , so the shortest-length solution wins.
__

My first question, so please let me know what could/should be changed. Thanks!

\$\endgroup\$
7
  • \$\begingroup\$ Why the arbitrary restriction to ASCII? \$\endgroup\$
    – Adám
    Commented Jan 5, 2023 at 18:49
  • \$\begingroup\$ @Adám That might be unecessary, but I was mainly thinking that a mixture of unicode and ascii characters wouldn't be great. Is that a concern? \$\endgroup\$
    – Bob th
    Commented Jan 5, 2023 at 18:51
  • \$\begingroup\$ @Adám Okay, after thinking about I believe you're right in that it's rather arbitrary so I've removed it. \$\endgroup\$
    – Bob th
    Commented Jan 5, 2023 at 19:18
  • \$\begingroup\$ Welcome to Code Golf SE and thanks for using the Sandbox! You may want to think though the allowed encodings - some languages use special code pages (not Unicode nor ASCII, see 05AB1E or Nibbles). I suggest allowing them and even allowing code pages specifically crafted for this challenge (eg. program adcbd returning 1 2 3 4 2 with encoding a:1 b:4 c:3 d:2). But that's up to you. \$\endgroup\$
    – pajonk
    Commented Jan 6, 2023 at 14:08
  • \$\begingroup\$ This seems to fall under generalized quines, a type of challenge we'd rather avoid \$\endgroup\$
    – mousetail
    Commented Jan 7, 2023 at 16:03
  • \$\begingroup\$ @mousetail I think that allowing custom encodings may result in some clever solutions that don't use the standard quine approach. I don't know however if the probability is high enough to post it against the generalized quine discouragement. \$\endgroup\$
    – pajonk
    Commented Jan 8, 2023 at 8:30
  • \$\begingroup\$ I really don't see how using a different base could ever lead to a different approach to the standard quine \$\endgroup\$
    – mousetail
    Commented Jan 8, 2023 at 9:19
-1
\$\begingroup\$

Generate a random halting Brainfuck code

Generate a random valid halting brainfuck program using []+-<>. Every halting program should be possibly outputted.

Sandbox Notes:

  • What should I add?
  • Is it a duplicate?
\$\endgroup\$
3
  • \$\begingroup\$ I would add "Every halting program should be possibly outputted with nonzero probability." Also, I don't think there is any other viable approach than implementing this and randomly quitting when a program halts, which makes your challenge a possible duplicate. \$\endgroup\$
    – Bubbler
    Commented Jan 19, 2023 at 6:52
  • \$\begingroup\$ @Bubbler They can be wrapped into each other(this+loop outputting unseen entries=that,that+random output then halt=this), if these two wrappers treated non-trivial then it's not a dup \$\endgroup\$
    – l4m2
    Commented Jan 20, 2023 at 0:07
  • \$\begingroup\$ This is impossible, since you can't determine if a program halts. Should it generate a program that solves the colatz conjecture or not? \$\endgroup\$
    – mousetail
    Commented Jan 20, 2023 at 8:38
-1
\$\begingroup\$

Sum of numbers of arbitrary length

\$\endgroup\$
2
  • 1
    \$\begingroup\$ This seems like it would be fairly trivial in most languages. Maybe add a restriction that there shouldn't be floating point errors (e.g. in many languages, 0.2 + 0.3 = 0.30000000000000004) \$\endgroup\$ Commented Feb 5, 2023 at 20:13
  • \$\begingroup\$ @Jacob If there will be floating point errors then it will not meet the condition "summarize them as normal numbers" \$\endgroup\$
    – EzioMercer
    Commented Feb 5, 2023 at 20:54
-1
\$\begingroup\$

Route Planning for a delivery driver

There is a town that consists of a single long road with evenly spaced houses. Each house either needs rice, represented with a negative number, or can provide rice, represented with a positive number.

[-1, 50, -1, -1, -1, -50, 3, 1]

Here, the first house needs one grain of rice, the second can provide 50 etc.

Your delivery truck starts at the very left edge of the town, and must end on the very right edge. It has a infinite capacity, but you can't deliver if it's empty.

The sum of the input list is guaranteed to be 0.

Given a list of requirements, output the shortest possible path that can deliver to every address.

Test cases

Test Case Output
5, -5 3
-5, 5 5
2, -5, 3 6
-1, -1, 1, 1, 1, -1 12
\$\endgroup\$
-1
\$\begingroup\$

Paint you a Picture for Piet Perfection

Make a Piet program that does whatever you want—but make it pretty.

Your goal is to paint a beautiful picture with an interesting function.

Your program may do anything, but your submission is much more likely to gain upvotes if it is interesting or related to the content of the picture. Likewise, your program could be incredibly interesting but look very ugly and thus receive few upvotes.

This is a , so the submission with the highest score wins.


Meta sandboxing: This might be too vague, should the program guidelines be more specific?

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Unfortunately, this kind of popularity contest ("do whatever you want but make it pretty") is exactly the kind that we've decided isn't a good fit for the site. \$\endgroup\$
    – DLosc
    Commented Mar 10, 2023 at 16:38
  • \$\begingroup\$ @DLosc Booooooo \$\endgroup\$ Commented Mar 10, 2023 at 22:13
  • \$\begingroup\$ Yeah, I understand the reasons, but I'm not entirely happy with the outcome. Tweetable Mathematical Art was one of my favorite questions. Popularity contests and what to do with them have been one of the site's most vexing questions for a lot of years. We still discuss the issues in chat from time to time. \$\endgroup\$
    – DLosc
    Commented Mar 11, 2023 at 16:20
  • 1
    \$\begingroup\$ Improvement idea: Create a Piet program that describes what it does in the picture itself. \$\endgroup\$
    – Bubbler
    Commented Mar 17, 2023 at 1:07
-1
\$\begingroup\$

My prefix is food, my suffix is rude

My prefix is food.
My suffix is rude.
My infix comes in rounds.
I keep you off the ground.

What am I?

This is the first "Riley riddle", created by Riley. Riddles of this sort has become a trend in the Puzzling SE. Let us do a code-golf challenge equivalent of it.

The challenge is to write a program/function that expects the input to be one of three distinct values of your choice, and output what the code's prefix/suffix/infix of your choice would output, respectively.

To be more specific, the affixes shall have no input or have the input ignored, and have a constant output. For sake of preventing trivial solutions, their outputs shall be all different.

As for the full code, any input that matches none of the three values falls into don't care situation. Standard loopholes apply.

This is a code-golf, so the submission whose full code is the shortest in length wins. The lengths of the affixes won't compromise the score.

If possible, please include a poem describing what your code does, just like the original riddle! ;-)

\$\endgroup\$
2
  • \$\begingroup\$ Why the downvote tho? \$\endgroup\$ Commented Mar 25, 2023 at 9:56
  • \$\begingroup\$ I'm not the downvoter, but I'm not sure I understand what the challenge means. Could you give an example program? \$\endgroup\$ Commented Mar 29, 2023 at 19:02
-1
\$\begingroup\$

Play turn-less chess


In this challenge, you'll write a chess bot...but with a twist. Your bot can move at any time, so the faster you can think, the faster you can move. You'll write code for a custom CPU (so that precise timing is possible), and try to checkmate your opponent's king.

Rules:

  • White doesn't necessarily move first; both bots get the opportunity to start moving immediately. If both bots move in the same CPU cycle, white moves first
  • If a bot tries to make an impossible move (pushing a pawn into an occupied square, castling with a moved rook, etc.), the move fails, and the bot is penalized by losing a certain amount of time (or possibly the number of cycles the opponent spent on their last move)
  • Instructions will cost 1 CPU cycle, with the exception of moving, which will cost a fairly large number of cycles to make rapidly moving pieces to overwhelm smart bots less overpowered
  • Bots will play each other round-robin style, both as white and black. Bots will be scored using a modified Elo system (designed such that the order in which the games are played doesn't matter)
\$\endgroup\$
13
  • \$\begingroup\$ I would strongly suggest 1 cycle per instruction, anything else will get way to complex way to quickly while adding nothing. Plus it won't actually effectively prevent ties. Instead, one bot could maybe start half a cycle earlier like in regular chess. \$\endgroup\$
    – mousetail
    Commented Mar 27, 2023 at 14:59
  • \$\begingroup\$ @mousetail I don't see how it would get "way too complex", or how it would "add nothing". It would happen all the time that two bots move at the same time. I don't want white to have an advantage, since it would cause the games to run faster and result in less draws if only one balanced game took place. As for complexity, I don't see how it adds any, and it adds a bit of strategy for people who really want to optimize their bots. \$\endgroup\$
    – rydwolf
    Commented Mar 27, 2023 at 15:01
  • \$\begingroup\$ You could run games with both sides starting once, then the overall winner is whoever won the fastest to eliminate ties \$\endgroup\$
    – mousetail
    Commented Mar 27, 2023 at 15:03
  • \$\begingroup\$ @mousetail Maybe, but draws still exist. And if everything is integer numbers of cycles, bots winning in the same number of cycles could also happen. And I don't see how that's less complex than what I'm doing. \$\endgroup\$
    – rydwolf
    Commented Mar 27, 2023 at 15:05
  • 1
    \$\begingroup\$ I really dislike needing do do anything complex to calculate the speed of a section of code other than counting the lines. I'd need to reference the instruction set to understand the impact of every line, instead of being able to quickly tell if one snippet is faster than another \$\endgroup\$
    – mousetail
    Commented Mar 27, 2023 at 15:05
  • \$\begingroup\$ If I wanted to need a calculator to find the speed of my program I'd rather program in x86 \$\endgroup\$
    – mousetail
    Commented Mar 27, 2023 at 15:06
  • \$\begingroup\$ Ties to exist, obviously if one side ties the other decides. If both games tie then they'd tie without a first move advantage also so it wouldn't make any difference \$\endgroup\$
    – mousetail
    Commented Mar 27, 2023 at 15:06
  • 1
    \$\begingroup\$ Hmm, I do see your point. I think I'm still going to make some things cost multiple cycles (moving, mainly), but I guess I'll do integers. \$\endgroup\$
    – rydwolf
    Commented Mar 27, 2023 at 15:08
  • 1
    \$\begingroup\$ On a completely different note: I'd recommend making the winning condition capturing the king since keeping track of check rules in such a limited time would be near impossible \$\endgroup\$
    – mousetail
    Commented Mar 27, 2023 at 15:08
  • \$\begingroup\$ @mousetail You'd still need to do the exact same process as "keeping track of check rules" if you want to avoid your king being captured... \$\endgroup\$
    – rydwolf
    Commented Mar 27, 2023 at 15:09
  • \$\begingroup\$ True, but that adds a interesting element to the game. Can you move out of check before your opponent realizes? Instead of having to wait for a long time due to a invalid move in which your opponent will probably have time to checkmate you anyways \$\endgroup\$
    – mousetail
    Commented Mar 27, 2023 at 15:11
  • \$\begingroup\$ @mousetail Ooh...what if when you're put in check, you receive some sort of interrupt, like your opponent saying "check"? And maybe making a move that would put your king in check is handled differently from just making an invalid move. \$\endgroup\$
    – rydwolf
    Commented Mar 27, 2023 at 15:15
  • \$\begingroup\$ Possible, but that might make the rules too complex. Not sure how to fix it though \$\endgroup\$
    – mousetail
    Commented Mar 27, 2023 at 16:58
-1
\$\begingroup\$

Minimum Buttons on a calculator to get from one number to another

My calculator looks like this:

·   ·   ·   ·   ·
  1   2   3   +
·   ·   ·   ·   ·
  4   5   6   -
·   ·   ·   ·   ·
  7   8   9   * 
·   ·   ·   ·   ·
  =       0   /  
·   ·   ·   ·   · 

Given a starting number and a ending number, calculate the minimum sequence of button presses required to get from the starting number to the ending number.

Notes:

  • / is integer division, no need to handle floats
  • Simple calculators always calculate from left to right, no complex order of operations
  • A = is needed at the end to display the final number if the start and ending number are different

Test Cases

A B Result
1 1
2 3 +1=
49 33 -16=
120 1081 *9+1=

More test cases TBD

\$\endgroup\$
5
  • \$\begingroup\$ Should there be an = at the end of the third test case? \$\endgroup\$
    – The Thonnu
    Commented Apr 13, 2023 at 14:36
  • \$\begingroup\$ Isn't +97= a shorter solution for the last test case? \$\endgroup\$
    – Arnauld
    Commented Apr 15, 2023 at 14:36
  • \$\begingroup\$ Is +961= also a valid answer for the last test case? \$\endgroup\$
    – The Thonnu
    Commented Apr 15, 2023 at 16:54
  • \$\begingroup\$ @TheThonnu Yes. \$\endgroup\$
    – mousetail
    Commented Apr 15, 2023 at 17:13
  • 2
    \$\begingroup\$ Must first button be one of "+-*/" or what happened if I press "1" at beginning? May input already equals to output? Could you add more testcases that require using more than 1 operators in the output? \$\endgroup\$
    – tsh
    Commented Apr 18, 2023 at 5:51
-1
\$\begingroup\$

Infinitely spew corporate B.S.

(Throwing a touch of shade at some recent decisions made by StackExchange over on Mother Meta)

If you've ever read a company's announcement blog post or press release, you've likely thought to yourself "Wow, I couldn't understand a word of that!" This is the effect of a marketing technique often referred to as "spewing corporate B.S." which, while often frustrating to read, can help you and your business dodge a lot of bullets in the P.R. department (or at least, buy yourself some time).

After recent actions made by the company you work at, you've realized that your company doesn't have time to formulate a good response to its customers' concerns—but you know just how to deal with the problem.

In this challenge, your task is to write a program or function that, given no input (or a seed), infinitely writes randomized company-speak.

How to write it

SANDBOXING: TODO - spec out how this will work. I'm thinking that it will just choose random words from a word bank, but I should specify how grammar can work, or maybe it would make sense for the word list to be taken as input (leaning towards this)f? Please comment any suggestions.

Here are some example corporate sentences your program may write, according to the above criteria:

We are revisiting a broader approach to improving the user-experience of our new experiment concerning recent concern.

We are excited to share a more holistic initiative to produce positive results from our new experiment.

We are committed to hearing your feedback in a more positive exploration of the product.

Rules

  • Your submission can be a full- program or a function
  • You may return a generator that can generate sentences infinitely rather than printing.
  • Your “randomness” doesn’t have to be evenly distributed, but there must be a non-zero chance that any possible sentence might be outputted by your submission.
  • SANDBOXING: more rules
\$\endgroup\$
5
  • \$\begingroup\$ +1 for humor but needs to be a lot better specified. You need a objective scoring critereum at least \$\endgroup\$
    – mousetail
    Commented May 31, 2023 at 12:47
  • \$\begingroup\$ @mousetail Sorry, it’s code-golf, forgot to mention. I don’t have time to update it yet but will do so in a few hours. It’s definitely not specified enough, since I’m still not entirely sure how the challenge will work—just wanted to post it so I A) don’t forget it, and B) can get some initial feedback on the idea \$\endgroup\$ Commented May 31, 2023 at 12:48
  • \$\begingroup\$ How are you going to check if a answer is sufficiently corporate or sufficiently B.S.? \$\endgroup\$
    – mousetail
    Commented May 31, 2023 at 12:48
  • \$\begingroup\$ Just make sure none of the SE staff notice… \$\endgroup\$ Commented May 31, 2023 at 12:52
  • \$\begingroup\$ Is the output allowed to be a sentence that doesn't make sense? \$\endgroup\$ Commented May 31, 2023 at 15:05
-1
\$\begingroup\$

Detect sloppy randomness

The C rand() function is often implemented like so:

static unsigned long int next = 1;

int rand(void) // RAND_MAX assumed to be 32767
{
    next = next * 1103515245 + 12345;
    return (unsigned int)(next/65536) % 32768;
}

void srand(unsigned int seed)
{
    next = seed;
}

However this is not a very good rng and definitely not a cryptographically secure one. Your task is to write code that detects this rng.

Specifically your code takes as input a sequence of 100 (or fewer if you so wish) numbers between 0 and 32767 inclusive.

For a random input the code should output "random" with at least 99.9999% probability and "not random" with at most 0.0001% probability (one in a million).

However if the input is generated using (successive calls to) rand then your code must output "not random" always.

Also, your code must be fast. Specifically it should run in under a second. Output as per standard rules.

Meta

Should I use a 64-bit rng? Solutions in fast languages can probably just brute force the 31-bit state. This would also allow to relax the time requirements.

\$\endgroup\$
1
  • \$\begingroup\$ Under a second on what hardware? \$\endgroup\$
    – mousetail
    Commented Jun 1, 2023 at 13:26
-1
\$\begingroup\$

Inspired, maybe copy background from there

Given a map, output a shortest possible input.

Test cases:

Start here _ /  => / \
             \

               /
              /
Start here _ /\/ => /// /\/ \/\
             \/\

                /
               /
              /\
Start here _ /\/\ => //// //\\ /\/ \/\/
             \/\/

                   /\/
                /\/\/
Start here _ /\/\/\/\/ => /\//\//\/ \/\//\// /\/\/\/\/ \\\
             \/\/\  /\    \/\/\\//\
              \   \/
               \
\$\endgroup\$
5
  • \$\begingroup\$ What do you mean by shortest? Smallest number of paths? Do the paths also need to be shortest possible? (in your last example first or second path can skip the last / and the third I think is simply wrong (: ) \$\endgroup\$
    – pajonk
    Commented Jul 13, 2023 at 6:09
  • \$\begingroup\$ @pajonk Sum new path, ` \ ` and /. Don't see problem on Test Case 3 or 4 \$\endgroup\$
    – l4m2
    Commented Jul 13, 2023 at 6:16
  • \$\begingroup\$ In the last test case, third path, you have /\/\\//\/ but the 6th segment (first of two /s) doesn't exist in the map (or I don't understand something). \$\endgroup\$
    – pajonk
    Commented Jul 13, 2023 at 9:48
  • \$\begingroup\$ @pajonk Misread as 3rd test case. \$\endgroup\$
    – l4m2
    Commented Jul 13, 2023 at 10:32
  • \$\begingroup\$ It is unclear what the challenge is. \$\endgroup\$ Commented Jul 13, 2023 at 11:37
-1
\$\begingroup\$

Python: *args or **kwargs?

Is it a regular argument or a keyword argument?


In Python, there are two types of specifying arguments to a function:

Regular argument (arg): f("regular argument")

Keyword argument (kwarg): f(keywordarg="keyword argument")

Keyword arguments in Python always are in the form of variable assignments and regular arguments are anything else. If you want to learn more about *args and **kwargs, click here.


Task

Given an input string consisting of only ascii characters that is a valid argument (regular or keyword), you must output one of two constant, distinct values representing whether it is an arg or a kwarg. Shortest code wins!


Test cases

3              -> arg
num = 3        -> kwarg
num=3          -> kwarg
a=b            -> kwarg
a==b           -> arg
False          -> arg
"kwarg"        -> arg
arg="arg"      -> kwarg
"kwarg=kwarg"  -> arg
a=b==c         -> kwarg
b==c           -> arg
[1,2,3,4]      -> arg
dict={1:2,3:4} -> kwarg
[1,2][i:=0]    -> arg

"""more than
one line!"""   -> arg

mstring='''
indeed more
than one line
'''            -> kwarg

Tags


Meta

  • Is this a duplicate?
  • Is this too focused on Python?
  • Do I need more test cases?
  • Any other feedback?
\$\endgroup\$
17
  • \$\begingroup\$ Explain the downvote. \$\endgroup\$ Commented Jul 14, 2023 at 11:17
  • \$\begingroup\$ Can we assume ASCII only? \$\endgroup\$
    – mousetail
    Commented Jul 14, 2023 at 11:32
  • \$\begingroup\$ @mousetail why are you asking? \$\endgroup\$ Commented Jul 14, 2023 at 11:32
  • \$\begingroup\$ This question is just "does this string start with a valid python identifier followed by a =". But what constituted a valid identifier in python for weired unicode edge cases gets extremely complex. If you want unicode support you need a very large number of unicode test cases. \$\endgroup\$
    – mousetail
    Commented Jul 14, 2023 at 11:34
  • \$\begingroup\$ @mousetail sure I’ll allow ascii only (but maybe a bonus for non-ascii as well?) \$\endgroup\$ Commented Jul 14, 2023 at 11:35
  • \$\begingroup\$ codegolf.meta.stackexchange.com/a/8106/91213 \$\endgroup\$
    – mousetail
    Commented Jul 14, 2023 at 11:36
  • \$\begingroup\$ @mousetail no bonus it is. \$\endgroup\$ Commented Jul 14, 2023 at 11:38
  • \$\begingroup\$ @mousetail and about the valid identifier part: I said only valid arguments, so you won’t get £=1 or class. \$\endgroup\$ Commented Jul 14, 2023 at 11:39
  • \$\begingroup\$ What if you get some weird Unicode character that normalizes to =? \$\endgroup\$
    – mousetail
    Commented Jul 14, 2023 at 11:56
  • \$\begingroup\$ @mousetail i only allowed ascii \$\endgroup\$ Commented Jul 14, 2023 at 11:58
  • \$\begingroup\$ £ is not ASCII \$\endgroup\$
    – mousetail
    Commented Jul 14, 2023 at 12:02
  • \$\begingroup\$ @mousetail wait WHAT!?! \$\endgroup\$ Commented Jul 14, 2023 at 12:03
  • \$\begingroup\$ @mousetail should we move this to chat? \$\endgroup\$ Commented Jul 14, 2023 at 12:07
  • \$\begingroup\$ Let us continue this discussion in chat. \$\endgroup\$ Commented Jul 14, 2023 at 12:07
  • \$\begingroup\$ Suggested test case [1,2][i:=0] is args \$\endgroup\$
    – mousetail
    Commented Jul 15, 2023 at 15:12
-1
\$\begingroup\$

Decimal to fraction

In the pool of questions on CGCC, I have not found a question with this task.


Given a float in base 10, output a fraction in simplified form, improper or mixed. The output format can be any reasonable one. In , the shortest answer wins!

Test cases

3.5 -> 7/2 or 3 1/2
0.25 -> 1/4 or 0 1/4

Meta

  • Did I miss a duplicate?
  • Any more test cases?
  • Anything else?
\$\endgroup\$
1
-1
\$\begingroup\$

Catouterr

A cat that writes to two streams? I’m in!


Write a full program with these properties:

  • If the input string ends in outerr, remove that part and write the rest to STDOUT and STDERR.
  • If the input string ends in err but not outerr, remove that part and write the rest to STDERR only.
  • If the input string ends in out, remove that part and print the rest to STDOUT.
  • Otherwise, print it to STDOUT unchanged.

STDOUT and STDERR can be replaced by a reasonable alternative. Also, STDERR is not ignored. However, insuppressible messages are ignored.

This is , so shortest code wins!


Meta

  • Ask away!
\$\endgroup\$
5
  • \$\begingroup\$ Does it have to be a program or are functions allowed? \$\endgroup\$ Commented Sep 28, 2023 at 17:30
  • \$\begingroup\$ I assume when you say "remove that part and write it" you mean "remove that part and write the rest to"? \$\endgroup\$ Commented Sep 28, 2023 at 17:40
  • \$\begingroup\$ @CommandMaster full programs only. Added. \$\endgroup\$ Commented Sep 28, 2023 at 19:08
  • \$\begingroup\$ @noodleman yes. Fixed. \$\endgroup\$ Commented Sep 28, 2023 at 19:09
  • \$\begingroup\$ What would be examples of "reasonable alternatives" for stdout and stderr? Would it be reasonable to output both to stdout, but with a separator? \$\endgroup\$ Commented Sep 29, 2023 at 7:50
-1
\$\begingroup\$

Optimal Span for Multiple Byte Ranges [Draft]

I recently ran into the following challenge, you have a list of common crawl URL indexes that have the setup below. Each of the GZ files are ~1.2GB, the challenge would be to design the most efficient solution that can respond with the decrypted byte span. If this if of interest, I'll revise this to flush out more details.

In the real world, some considerations are:

  1. Cache vs HTTP fetch byte range
  2. Minimizing number of requests by making requests spanning multiple offsets
  3. Decrypting entire file for multiple offsets vs decrypting offsets individually
  4. Trade off between # of connections, download time, local storage space, decryption time

Example:

FileName.gz Offset Length CONTENT_DIGEST
crawl-data/CC-MAIN-2023-23/segments/1685224648465.70/warc/CC-MAIN-20230602072202-20230602102202-00011.warc.gz 755688583 1327 123456789
crawl-data/CC-MAIN-2023-23/segments/1685224648465.70/warc/CC-MAIN-20230602072202-20230602102202-00011.warc.gz 86276364 3404 ABCDEFGHI
crawl-data/CC-MAIN-2023-23/segments/1685224648465.70/warc/CC-MAIN-20230602072202-20230602102202-00011.warc.gz 963176585 1294 987654321
\$\endgroup\$
-1
\$\begingroup\$

Write a check

Given the check (cheque, for non-Americans) number, payee, amount, and memo as input, print or return a check in this format:

+------------------------------------------------------------------------------+
| John and Jane Doe                                                      ***** |
| 123 Pine St.                                                                 |
| Anytown, USA 12345                                Date: ____________________ |
|                                                                              |
| Pay to the                                                                   |
|   order of _____________________________________________________ $__________ |
|                                                                              |
| ________________________________________________________________ DOLLARS     |
|                                                                              |
| First Bank of Code Golf - Anytown, USA                                       |
|                                                                              |
| For ________________________________  AUTHORIZED-SIGNATURE-JOHN-AND-JANE-DOE |
|                                                                              |
| [314159265[  2718281828/  *****                                              | 
+------------------------------------------------------------------------------+

Parts marked with underscores are for the account holder to fill out, while parts with asterisks are printed on the check.

  1. Fill out the check number in the top-right corner, left-padded with spaces. Check 101 has 101 in place of the asterisks.
  2. Fill out the check number on the bottom, left-padded with zeros. Check 101 has 00101.
  3. Anywhere the user writes, leave one underscore before the user's writing and as many afterwards as necessary to fill the space. You may assume your program will not be given input that will overflow any of the fields.
  4. Write the current date in the format November 2, 2023 (%B %-d, %Y).
  5. Write the numerical amount in the format 1,234.56. The comma is required for amounts greater than or equal to $1,000.
  6. Write out the amount in the format "One thousand two hundred thirty-four and 56/100". Pad the rest of the field for it with hyphens (-), except one underscore at the end.

Here is an example of check #9876 written on September 30, 2023, paying $1,234.56 to Acme Inc. for a Widget purchase:

+------------------------------------------------------------------------------+
| John and Jane Doe                                                       9876 |
| 123 Pine St.                                                                 |
| Anytown, USA 12345                                Date: _September 30, 2023_ |                
|                                                                              |
| Pay to the                                                                   |
|   order of _Acme Inc.___________________________________________ $_1,234.56_ |        
|                                                                              |
| _One thousand two hundred thirty-four and 56/100---------------_ DOLLARS     |                           
|                                                                              |
| First Bank of Code Golf - Anytown, USA                                       |
|                                                                              |
| For _Widget purchase________________  AUTHORIZED-SIGNATURE-JOHN-AND-JANE-DOE |             
|                                                                              |
| [314159265[  2718281828/  09876                                              |
+------------------------------------------------------------------------------+

Standard loopholes are prohibited. You may write a function or a complete program, and you may output the check to stdout or return it either as a newline-delimited string or as a list of strings. A trailing newline is optional.

\$\endgroup\$
7
  • \$\begingroup\$ Is the check itself part of the input or also generated by the code? \$\endgroup\$
    – Philippos
    Commented Nov 30, 2023 at 11:59
  • \$\begingroup\$ @Philippos the check is generated. \$\endgroup\$
    – Someone
    Commented Nov 30, 2023 at 14:31
  • \$\begingroup\$ Then maybe there is too much stuff that cannot really be golfed. \$\endgroup\$
    – Philippos
    Commented Nov 30, 2023 at 14:44
  • \$\begingroup\$ @Philippos do you think making it an input would be an improvement? \$\endgroup\$
    – Someone
    Commented Nov 30, 2023 at 15:36
  • \$\begingroup\$ "Fill out the check number in the top-left corner" You mean top right? \$\endgroup\$
    – Bbrk24
    Commented Dec 2, 2023 at 3:18
  • \$\begingroup\$ Also, what's the maximum amount we have to handle? Can the limit be $999,999.99 or does it have to handle millions? \$\endgroup\$
    – Bbrk24
    Commented Dec 2, 2023 at 3:20
  • 1
    \$\begingroup\$ @Bbrk24 oops, yes, top right. And it is limited to $9,999.99, because any more would overflow the numeric amount field. \$\endgroup\$
    – Someone
    Commented Dec 2, 2023 at 3:24
-1
\$\begingroup\$

All good regex come in threes

We've had challenges about two regex matching each other but not themselves and a complicated relationship of five regex, but don't they say that all good things come in threes?

  • Write a chain of three regex, each one matching the next, but neither itself not the previous one
  • Thus, of our three regex, RE1 will only match RE2, RE2 will only match RE3 and RE3 will only match RE1
  • Don't ask about slashes. We are talking about regular expressions, not slashes. Even if some languages use slashes as one way to delimit regular expressions, the slashes are not part of the regular expressions, so they are not part of the challenge
  • If you use an extension to the POSIX regex standard, define our dialect and compete in that dialect league. Champions league is pure POSIX basic regular expressions
  • This is regex golf, so the shortest sum of the three lengths wins
\$\endgroup\$
-1
\$\begingroup\$

Vim (not) Golf

Starting vim with --clean; what is the longest English sentence you can type without leaving normal mode?


Should a clause be added for / not being a valid key?

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

Fast group action

For the purposes of this challenge permutation is a bijective function on the natural numbers.

Cycles are a particular kind of permutation, which we can represent as lists of integers enclosed in a set of parentheses. If a number appears in the list then the result of the permutation is the next value of the list, or in the case of the last value the first in the list. If the number does not appear, its result is itself.

So for example (1 2 4) maps 1 to 2, 2 to 4, 4 to 1 and every other natural number to itself.

We can compose two permutations to get a new permutation. In the notation we compose two cycles by placing them next to each other, the result is that the right permutation is applied first followed by the left one.

So (3 2)(1 2 4) maps 1 to 3, 2 to 4, 3 to 2 and 4 to 1. Everything else maps to itself. This can be written more simply as a single cycle (1 3 2 4). Not every permutation written this way can be simplified. For example (1 2)(9 8) cannot be written as a single cycle since the two cycles being composed do not contain any common numbers. However cycles that are disjoint like this do commute, so this is equivalent to (9 8)(1 2).

While not every permutation can be written as a cycle, every permutation can be written as the composition of cycles. So the above notation is enough to represent permutations.

However as we have seen there is a degree of ambiguity here. Permutations can be written multiple ways, so we will define a normal form which gives every permutation a single representation.

To be normal a representation must satisfy the following qualities:

  • No number appears in more than one cycle.
  • The first number in each cycle must be the largest in the cycle.
  • Cycles whose first number is smaller should appear earlier.

Here are a few examples rewritten in normal form:

(1 2 4) -> (4 1 2)
(3 2)(1 2 4) -> (4 1 3 2)
(1 2)(9 8) -> (2 1)(9 8)
(1 4 7)(2 3) -> (3 2)(7 1 4)

Task

In this challenge you will take as input two finite permutations (a finite number of cycles each of finite length) in normal form and output the composition of those two permutations also in normal form.

You may take input as a list of lists or any suitable list-like structure. You may exclude or include 0 from the natural numbers.

The goal is to minimize Big-O asymptotic time complexity in terms of the number of bits in the input.

Sandbox

I wrote this up because I needed to implement this for work I was doing, and had fun. This seems like the sort of thing that would have existing fast algorithms, but when researching for my work and again when writing this I wasn't able to find any.

If this is a well known problem, I don't think it's worth asking a fastest code about it. I'd probably modify it to be and . So if anyone knows of an algorithm in the literature, let me know.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ I might be missing something, but couldn't you normalize the values in the cycles, so they are at most \$O(n)\$ (this step actually isn't trivial, but it's possible in deterministic linear time), compose the permutations in \$O(n)\$ time, partition it back to cycles in \$O(n)\$, and then undo the normalization? \$\endgroup\$ Commented Dec 24, 2023 at 5:01
  • 1
    \$\begingroup\$ Just as to the title, I don't believe 'group action' is the best way to describe this, especially since you do not directly reference groups or group actions in the post. While yes it is a group action (of \$\mathrm{Sym}(\mathbb{N})\$ on \$\mathbb{N}\$), it may just be best to focus on fast composition of permutations. \$\endgroup\$
    – Tbw
    Commented Dec 25, 2023 at 5:08
  • \$\begingroup\$ @CommandMaster If elements are widely spreaded but only a few different elements, then the complexity would fall on map (besides fast scanning, there's a step of sorting) \$\endgroup\$
    – l4m2
    Commented Dec 26, 2023 at 21:46
-1
\$\begingroup\$
\$\endgroup\$
-1
\$\begingroup\$

Given 3 of angles and edges of a triangle, decide how many possible triangles exist.

Input:

3 conditions. You can take 6 inputs to represent all edges and angles, with an invalid value to mean not present; or just three.

All given edges and angles are positive and less than 100000, but angles are not bounded by 180 degree so you need to check that.

Output:

An integer describing how many possibilities. You can assume that answer remain same if any input differ by at most 0.001%, which also results that some edge cases needn't be taken care.

Test cases

E A(degree) E A E A
3 -  4 - 5 - => 1
3 90 4 - - - => 1
5 60 - - 4 - => 2
4 60 - - 5 - => 1
5 60 - - 1 - => 0
5 200 3 - - - => 0
4 30 - - 2 - (Invalid input)
\$\endgroup\$
2
  • \$\begingroup\$ I assume that decided means that all the angles and sides are consistent with another, and actually form a Euclidean triangle. Impossible would be the opposite of this. Multi possibility only makes sense if the input is incomplete, i.e. not enough information (angles and edges) to finalise an entire triangle with all sides and angles known. Is that correct? Could you please elaborate what your definition of those three categories are? Also, you have stated that the input is 3 angles and 3 edges. Can it actually be 0-3 angles and 0-3 edges? \$\endgroup\$ Commented Apr 1 at 7:39
  • 1
    \$\begingroup\$ @DanielOnMSE Angles and edges may be missing, which I said maybe (take) 0 or non-number \$\endgroup\$
    – l4m2
    Commented Apr 1 at 8:11
-1
\$\begingroup\$

Compute digits of sqrt(2)

Your answer must display sequentially the digits of sqrt(2), one by one, without halting.

You are free to pick the base of your choice as long as it is an integer greater or equal to 2.

You may bundle the point that separate the integer part from the rest with the previous digit, the next one, or as a separate output.

Shortest wins.

For a matter of checking correctness, your program should be able to run up to the precision 1/INT_MAX (the one from C) on a laptop in less than an hour.

\$\endgroup\$
2
  • \$\begingroup\$ Perhaps answers could be allowed to output 14142... without the decimal point? \$\endgroup\$ Commented May 27 at 5:09
  • \$\begingroup\$ @CommandMaster I don't have a strong opinion on the matter tbh. Do you think it would be more fun? \$\endgroup\$ Commented May 27 at 12:34
-1
\$\begingroup\$

Hey Slobs! Organize your Code - Alphabetically!

So much code being written these days is an unorganized, illegible disaster, and this Code Golf website seems to attract the worst offenders - I mean, just look around! Lets add some order to this chaos.

The Challenge

Build a Quine (i.e. a program that prints out its own source code). No "cheating" quines are allowed.

Additionally, the source code should be organized alphabetically according to the following algorithm:

  1. Take your source code and split it on any non-alphabet characters to form "words" (an alphabet character is a character between a and z, and can be lowercase or uppercase). For example, the program echo "Hello 123 World!" contains three words - echo, Hello, and World.
  2. Verify that each word is in alphabetical order. More precisely:
  • Ignore casing (APPLE should comes before banana)
  • a word that is the prefix of another word comes first (at comes before attitude)
  • You can have multiple of the same words next to each other. (APPLE (Banana) "banana"; BANANA+CAKE is in alphabetical order)

You are allowed to break the alphabetical ordering, but at a heavy penalty. Every time a word is found that comes before the previous word in alphabetical order, you get a 1000 byte penalty. For example, cake apple banana would create a 1000-byte penalty because "apple" comes before "cake", and cake banana apple would create a 2000 byte penalty, because both "banana" and "apple" are out of place compared with the previous word.

Additional Notes

I don't recommend doing this challenge in a language that doesn't use alphabetical characters (e.g. whitespace, brainf***, etc). You can do it if you really want, but then its just a regular old quine challenge which makes for a very uninteresting answer.

\$\endgroup\$
2
  • \$\begingroup\$ You might want to link to the site standards for proper quines instead of specifying your own rules. Not that your spec isn't clear, but people are generally more used to the standard quine rules. \$\endgroup\$
    – emanresu A
    Commented May 29 at 9:34
  • \$\begingroup\$ Thanks for the tip - updated. \$\endgroup\$ Commented May 31 at 20:21
-1
\$\begingroup\$

.

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

Question:

You need to implement two functions: one for compressing ASCII art using Run-Length Encoding (RLE) and another for decompressing RLE-encoded ASCII art.

Problem Description

Run-Length Encoding (RLE) is a compression technique that replaces sequences of the same character with a single character and its count. For example, the string "aaaabbbbccccdd" would be compressed to "4a4b4c2d". Make sure it also works with new lines incase you want to compress ascii art images.

I have ASCII message as input, and I want to compress it using RLE and then decompress it back to its original form.

Requirements Compression Function:

Input: A string representing ASCII message.
Output: The RLE-compressed version of the ASCII message.

Decompression Function:

Input: The RLE-compressed string.
Output: The original ASCII message

\$\endgroup\$
1
1
147 148
149
150 151
159

You must log in to answer this question.

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