541
\$\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 needs more feedback, but it's been ignored, you can ask for feedback in The Nineteenth Byte. It's not only allowed, but highly recommended!

It is recommended to leave your posts in the sandbox for at least several days, and until it receives upvotes and any feedback has been addressed.

Other

Search the sandbox / Browse your pending proposals

The sandbox works best if you sort posts by active.

To add an inline tag to a proposal use shortcut link syntax with a prefix: [tag:king-of-the-hill]. To search for posts with a certain tag, include the name in quotes: "king-of-the-hill".

Get the Sandbox Viewer to view the sandbox more easily!

\$\endgroup\$
0

3617 Answers 3617

1
60 61
62
63 64
121
1
\$\begingroup\$

Posted

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

Solve a Quartic Equation

Since I saw the quartic formula and it's a behemoth to solve, your task is to implement it in as few bytes as possible. That's it.

To clarify some concerns, input and output can be as a string representing the equation, a list of numbers, numbers on different lines of STDIN/STDOUT, or a tuple of numbers - anything that represents clearly the four solutions is acceptable, and anything that clearly represents the five inputs is acceptable.

You may assume the equation will always have a real root, and if you choose not to support imaginary roots, you just need to output the real root(s).

On advice from TNB, builtins are now banned The formula for reference:

For a quartic equation \$ax^4 + bx^3 + cx^2 + dx + e = 0\$:

\$x_1, x_2 = \frac{-b}{4a} - S \pm \frac{1}{2}\sqrt[]{4S^2 - 2p + \frac{q}{S}}\$

\$x_3, x_4 = \frac{-b}{4a} + S \pm \frac{1}{2}\sqrt[]{4S^2 - 2p + \frac{q}{S}}\$

where:

\$p = \dfrac{8ac - 3b^2}{8a^2},\$ \$q = \dfrac{b^3 - 4abc + 8a^2d}{8a^3}\$

where:

\$S = \frac{1}{2}\sqrt[]{-\frac{2}{3}p + \frac{1}{3a}\left(Q + \frac{\Delta_0}{Q}\right)},\$ \$Q = \sqrt[3]{\frac{\Delta_1 + \sqrt[]{\Delta_1^2 - 4\Delta_0^3}}{2}}\$

and where:

\$\Delta_0 = c^2 - 3bd + 12ae,\$ \$\Delta_1 = 2c^3 - 9bcd + 27b^2e + 27ad^2 - 72ace\$

I learnt LaTeX just to do all those formulae. I hope you're proud of me. As always, this is , so shortest wins.

Tags:

\$\endgroup\$
8
  • 1
    \$\begingroup\$ Are we supposed to support non-real roots? \$\endgroup\$
    – Arnauld
    May 17 '21 at 23:37
  • 1
    \$\begingroup\$ I had gave up to read these crazy formula and I will try some languages with a built-in to solve this... \$\endgroup\$
    – tsh
    May 18 '21 at 2:28
  • \$\begingroup\$ "quartic equation: ax4+bx3+cx2+dx+e"; should be "ax4+bx3+cx2+dx+e = 0" \$\endgroup\$
    – tsh
    May 18 '21 at 2:28
  • 1
    \$\begingroup\$ I cannot get the formula work. Had I made some typo? \$\endgroup\$
    – tsh
    May 18 '21 at 2:41
  • \$\begingroup\$ @Arnauld yes - if your language can support it, it should. \$\endgroup\$ May 18 '21 at 5:59
  • \$\begingroup\$ I don't think that if your language can support it is a sound criterion, as any TC language can simulate complex numbers (although it can quickly turn into a nightmare with some of them). It may be better to just say "supporting roots with imaginary parts is optional" if that's what you decide. \$\endgroup\$
    – Arnauld
    May 18 '21 at 6:58
  • \$\begingroup\$ OK - I'll make that optional - but would it be better to make that a bonus or just make it optional \$\endgroup\$ May 18 '21 at 7:31
  • \$\begingroup\$ @tsh Give me a second to check that \$\endgroup\$ May 18 '21 at 7:35
1
\$\begingroup\$

Assume a standardized test has \$15\$ multiple choice questions, with \$3\$ options each. A school has some students and wishes for at least one of the students to get \$10\$ correct answers. Since no one in the school knows any math they are going to cheat and tell each student which answers he has to give without knowing what the actual correct answers are.

The problem is to find a number of students \$n\$ such that it is possible to do this and guarantee at least one of the students gets at least \$10\$ correct.

For example one way to do it is with \$n=3\cdot2^{15}-1\$ students, where we do all possible exams with at most two different options, since one of the options appears at most \$5\$ times we can get at least one test with \$10\$ correct solutions.

Another possible \$n\$ is \$a^5\$ where we can get a set of \$a\$ tuples of length \$3\$ that interesect every option of length \$3\$ at least twice ( so basically a solution to the problem with length \$3\$ and at least \$2\$ correct). It seems that the vectors \$(1,1,1),(2,2,2),(3,3,3)\$ along with the 6 permutations work, so \$9^5\$ is also a valid \$n\$.

The score will be \$\frac{1}{n}\$ where \$n\$ is a value for which it is possible to prove a configuration exists.

\$\endgroup\$
5
  • \$\begingroup\$ I'm not sure this is on-topic on our site, since it is not explicitly about code. I think primarily mathematics-based puzzles are allowed on Puzzling. \$\endgroup\$ May 26 '21 at 15:09
  • \$\begingroup\$ You think the best arrangement can be found with math? Without a lot of computation? \$\endgroup\$
    – user103863
    May 26 '21 at 15:10
  • 3
    \$\begingroup\$ I'll admit that it might be ok to post it here with the tag code-challenge \$\endgroup\$ May 26 '21 at 15:12
  • \$\begingroup\$ It seems like something which can be solved optimally with vertex cover, since 3^15 is reasonably small. \$\endgroup\$ May 28 '21 at 6:11
  • \$\begingroup\$ Actually it's set cover, not vertex cover. Maybe the best result would be from approximation algorithms, or maybe someone would be able to find a polynomial algorithm for this specific case. \$\endgroup\$ May 28 '21 at 6:18
1
\$\begingroup\$

Gödel numbering of a string

Posted!

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

Compute the generalised XOR (posted)

\$\endgroup\$
2
  • \$\begingroup\$ \$(b - ((p + q) \bmod b)) \bmod b\$ can be simplified to \$-(p+q)\bmod b\$. \$\endgroup\$
    – Bubbler
    May 28 '21 at 4:00
  • \$\begingroup\$ Yes, I was a bit worried about people getting confused about negative modulo, but I'll make that edit, it's probably easier to understand that way. \$\endgroup\$
    – xavc
    May 28 '21 at 4:02
1
\$\begingroup\$

Construct a Modulo Multiplication Table

\$\endgroup\$
5
  • \$\begingroup\$ You could change it to be "consider a (n-1) x (n-1) ... and modulo n" (e.g. 5x5, taken modulo 6) so you don't have to ignore the last row and column \$\endgroup\$
    – pxeger
    Jun 2 '21 at 13:47
  • \$\begingroup\$ @pxeger Thanks. Any suggestions for the title? \$\endgroup\$ Jun 2 '21 at 13:48
  • \$\begingroup\$ short and simple: "Modulo tables"? \$\endgroup\$
    – pxeger
    Jun 2 '21 at 14:00
  • \$\begingroup\$ Usually I think of quadrants as axis-aligned.Maybe "Modulo multiplication table", maybe throw in the word partial. \$\endgroup\$
    – qwr
    Jun 2 '21 at 14:26
  • \$\begingroup\$ Also more test cases just for good measure \$\endgroup\$
    – qwr
    Jun 2 '21 at 14:26
1
\$\begingroup\$

Salvage My Chemistry Homework

Introduction

I pulled an all-nighter yesterday doing my Chemistry homework. However, I now realize that was a terrible idea, because my handwriting was too sloppy and now I can’t distinguish the capital letters from the lowercase letters in my chemical formulas! This is a problem, because now the formulas can mean completely different things - for example, co could be cobalt or carbon monoxide. I want to know which of the formulas in my homework are ambiguous in this manner.

Since there are many formulas in the homework, I would like a program to help me so I don’t have to go over them all manually. Also, because the homework is due in an hour, I would like to have the code be as short as possible so I don't have to type as much.

Background

A chemical formula consists of one or more 'units'. Each 'unit' consists of a symbol of an element (the list of valid symbols is given in the input) optionally followed by a number greater than 1. For example, c, co and br2 are valid units (assuming that "c", "co" and "br" are in the list of symbols), while c0 and br2r are not valid units.

Input

The input consists of

  • The list of elements (all letters are lowercase)
  • A chemical formula with all letters in lowercase

Output

Output an integer, the number of ways there to separate the formula into individual units.

Test Cases

Input: ['c', 'o', 'co'], 'co2'
Output: 2 ('c o2', 'co2')

Input: ['c', 'h', 'o', 'ch', 'oh'], 'ch3cooh'
Output: 4 ('c h3 c o o h', 'c h3 c o oh', 'ch3 c o o h', 'ch3 c o oh')

Input: ['c'], 'c1'
Output: 0 (1 is not a valid subscript)

Input: ['a'], 'co2'
Output: 0 ("c" and "co" are not valid symbols)

Input: ['n', 'h', 'o', 'nh', 'no'], 'nh4no3'
Output: 4 ('n h4 n o3', 'n h4 no3', 'nh4 n o3', 'nh4 no3')

Standard loopholes apply. All reasonable input and output methods are allowed. This is , shortest code in each language wins.

Meta

  • Would it be better to output each way to split the formula or just the number of ways?
  • Any general wording improvements?
  • Anything I should be more specific about?
  • Suggested tags?
  • Given that l4m2's question has now been posted, should I remove this? (While the challenge is similar, there are some significant differences).

Credit to l4m2 for original idea

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

Closest Binary Fraction


For this challenge, a binary fraction is a fraction where the denominator is a power of two. Your program (or function) should take a rational number as input, and return the nearest binary fraction with a given denominator.

Input:

The first input will consist of a rational number. This can be represented by any of the following:

  • A floating point number, or built-in rational type
  • A fraction, either as a built-in rational or fraction type, or represented as a pair of numbers
  • A decimal string, in any chosen base

The second input is an integer \$d \ge 1\$, representing the denominator of the binary fraction.

Output:

Output must be able to represent the exact fraction. This means any of the input formats for fractions are valid, as long as they can reasonably represent any valid outputs.

You may choose if you want to simplify the output. Given the fraction 9 / 19 and the denominator 8, either 4 / 8 or 1 / 2 would be accepted.

Test cases:

Other:

This is . Unfinished and also a dupe of a newer challenge of mine

\$\endgroup\$
2
  • \$\begingroup\$ the output part seems.. unfinished? \$\endgroup\$
    – Razetime
    Apr 30 '21 at 2:47
  • \$\begingroup\$ @Razetime It is, yeah. I was halfway through drafting this when I had to go work on something else, so I just left it here as a reminder to finish it :p \$\endgroup\$ Apr 30 '21 at 3:21
1
\$\begingroup\$

Is it a Fischer random chess starting board?

\$\endgroup\$
2
  • \$\begingroup\$ Scoring? (code-golf?) Tags? (string chess decision-problem?) \$\endgroup\$
    – pxeger
    Jun 2 '21 at 14:11
  • \$\begingroup\$ @pxeger yes. I always forget to put them in these posts \$\endgroup\$
    – qwr
    Jun 2 '21 at 14:12
1
\$\begingroup\$

Task

You are an angry dude who manages the servers, but you are terrible in it. People often text you, and sometimes they ask why are the servers down - you got very annoyed!


Given a non-empty string. If it contains "server" and "down" (capitalization doesn't matter), you infinitely output the >:( face with a newline after. If it doesn't, you only output "Hi" once, because you are busy and angry.

Examples

server down -> >:( (infinitely)
server -> Hi
down -> Hi
serverANDLETTERSAFTERTHEWORD down -> >:( (infinitely), as it contains both server and down
Why are your servers down? -> >:( (infinitely)
sErVeR dOwN -> >:( (infinitely)
serverDowN -> >:( (infinitely)
serverd0wn -> Hi

Rules

  • This is , so the answer with shortest bytes wins.
  • These loopholes are, obviously, forbidden.
  • Standard code-golf rules apply.
  • Please specify the language you are using and the amount of bytes.
  • It would be great if you would put a link to a sandbox where your code can be ran in action, such as TIO.
  • Explaining your code is very welcomed.





Please comment down below if this is good enough or not, or if it's a duplicate (hope not)
\$\endgroup\$
3
  • 1
    \$\begingroup\$ I don't want to discourage you, but this seems a little trivial and uninteresting to me. Of course, that's just my opinion, and others may enjoy answering this challenge. For the output, would a an infinite list of characters be okay? \$\endgroup\$
    – user
    Jun 6 '21 at 18:20
  • \$\begingroup\$ It seems like combine two tasks together: 1. test if a string contains both two words or a not; 2. implement a truth machine \$\endgroup\$
    – tsh
    Jun 7 '21 at 8:21
  • \$\begingroup\$ Damn, that question looks very similar \$\endgroup\$ Jun 7 '21 at 8:23
1
\$\begingroup\$

Ambiguous Chemical Formula

\$\endgroup\$
4
  • 2
    \$\begingroup\$ This looks like an interesting challenge, but in its current form needs a lot more details (as do most of your sandbox posts - I'd recommend using a question template to make sure you've always included everything). For people who don't know chemistry, it's lacking crucial information: what is the exact syntax of a chemical formula, and how does one know it's ambiguous? \$\endgroup\$
    – pxeger
    Apr 22 '21 at 16:28
  • 3
    \$\begingroup\$ I also think it would be more interesting if the element list were provided as input. Otherwise most of the actual golfing part of the challenge boils down to compressing the list (or finding a short built-in to output them) \$\endgroup\$
    – pxeger
    Apr 22 '21 at 16:29
  • \$\begingroup\$ @pxeger Quite unrelated to chemical though, ClI and CLi both don't fell good \$\endgroup\$
    – l4m2
    Apr 22 '21 at 18:23
  • \$\begingroup\$ I've edited this down to a stub now that it's been posted to save space \$\endgroup\$ Jun 7 '21 at 1:01
1
\$\begingroup\$

Dr. Lamport's Unfinished Business

\$\endgroup\$
6
  • \$\begingroup\$ Also, I'd love some advice on how to specify what to do for some of the more "exotic" languages. e.g. C and such will obviously just use function-pointers… but I really don't want to alienate any Jelly golfers; those solutions are always my favorite! \$\endgroup\$ May 20 '21 at 15:49
  • 1
    \$\begingroup\$ Even function-pointers in C wouldn't help afaik, because you can't dynamically construct the function. Maybe you should allowed output the source of G, or alternatively make R take m and pk and just function directly as G. \$\endgroup\$ May 21 '21 at 7:48
  • \$\begingroup\$ @CommandMaster Thanks for the feedback! Would defining R(digest, pk) work? Such that it gets composed: R(φ(m), pk) -- (this would also align better with the way the original paper defined them.) \$\endgroup\$ May 21 '21 at 14:08
  • 1
    \$\begingroup\$ I think it would work, and it's also good because it lets programming languages without the concept of a "function" participate \$\endgroup\$ May 21 '21 at 14:15
  • \$\begingroup\$ @CommandMaster How's the current setup look? I'm certainly worried about accomodating languages without functions. Especially Jelly; I'm not really sure how it works, but I always enjoy those answers a lot! \$\endgroup\$ May 21 '21 at 14:39
  • \$\begingroup\$ I've edited this down to a stub now that it's been posted to save space \$\endgroup\$ Jun 7 '21 at 1:02
1
\$\begingroup\$

Write a netquine

There are a lot of online interpreters, for a lot of languages. For example, there's the ubiquitous TIO, but there are also smaller, more specific ones that others have made, such as interpreters for Vyxal, ngn/k, or Grok.

Your goal today is to write a netquine, or a program whose output, when used as a URL, leads to that program in an interpreter for that language. Essentially, if you have a link to your program, and the program outputs that link, it's valid.


For example, say you write a Vyxal program λλλλ. In the Vyxal interpreter, if you press the button to generate a permalink for this program, you get:

http://lyxal.pythonanywhere.com?flags=&code=%CE%BB%CE%BB%CE%BB%CE%BB&inputs=&header=&footer=

However, due to the way that the interpreter processes links, you could also leave out the blank fields:

http://lyxal.pythonanywhere.com/?code=%CE%BB%CE%BB%CE%BB%CE%BB

You can even replace the HTML character codes with the actual characters:

http://lyxal.pythonanywhere.com/?code=λλλλ

All of these are valid links to the Vyxal program λλλλ, and therefore would all be valid outputs for a netquine.


  • Obviously, Standard loopholes are forbidden.

  • Even though URL shorteners are forbidden by default, shortening a URL, like in the example, is fine, as long as it still leads to the right place.

  • For the purposes of this challenge, a web browser may be considered an "interpreter" for HTML and/or CSS. This means you might be able to do something along these lines.

  • Usual rules apply, as do usual rules.

  • This is , so the shortest answer in bytes wins!


Meta

  • Is this question clear enough?

  • Should I change the scoring to accommodate the potential differences in URL length? (lengths of domain names, different ways of generating links, the ability to shorten links, etc.)

  • Any other feedback?

  • This question is quite similar, but it only allows for TIO languages, and it doesn't allow any sort of shortening, even if the link is still valid (there was a comment asking about that), so I would consider it to be related, but not a dupe.

\$\endgroup\$
2
  • \$\begingroup\$ Interesting idea, but you won't get many answers because most online interpreters use an external compression algorithm to shorten the hash (and then base64 it), which is quite lengthy to implement from scratch. Note that the existing answers on the linked challenge all use a gzip or zlib builtin. \$\endgroup\$
    – Bubbler
    Jun 9 '21 at 23:12
  • \$\begingroup\$ You should provide a list of valid online interpreters, as someone could just make a.com lead to an interpreter for a program in some language that outputs https://www.a.com. Also, please define "in" in the sentence "leads to that program in an interpreter". Does the HTML of that page simply have to contain the program? \$\endgroup\$
    – Makonede
    Jun 9 '21 at 23:40
1
\$\begingroup\$

Encode USB packets

Posted

\$\endgroup\$
2
  • \$\begingroup\$ Is the fifth example correct? Shouldn't 11111111 (8 ones) be JJJJJJKKK? If not, how is 11111111 distinguished from 11111101? (This also applies to your other challenge). \$\endgroup\$ Jun 6 '21 at 19:33
  • \$\begingroup\$ @Spitemaster yep, you're right - fixed. \$\endgroup\$
    – pxeger
    Jun 9 '21 at 14:33
1
\$\begingroup\$

A Cat’s Game to Claim

Posted

\$\endgroup\$
1
  • \$\begingroup\$ Any catchy name suggestions also appreciated! "Proclaim" rhymes as well, but I can't get it to roll of the tongue. \$\endgroup\$
    – AviFS
    Jun 12 '21 at 1:15
1
\$\begingroup\$

Build a raw string


In this challenge you will write code which, given a single line string as input, can output the raw version.

Full spec

The input will be a string like ab\n or ab\u0065e\\ or e\'543. These strings should be converted to a raw version which uses the backslashes.

This probably sounds confusing, so here is a more complete explanation.

The most common form of strings used is simply a quote (one of ', " or the backtick), followed by a sequence of unicode characters, then a closing quote which corresponds the opening one. "abc" or 'THIS is a St%ing', for instance. Note that this is not the case in all languages, for instance most golfing languages do not represent strings in this manner.

However, it's not that simple. Backslashes allow for much more complex strings, and their importance in allowing certain characters means a string like 'ab\' is invalid as the backslash "escapes" the quote. Making a backslash appear in a string requires \\ instead.

Also, quotes can be escaped via \', \" or \`. A sequence like \\" should become \" while \"\ is invalid.

The newline and tab are both important, and so most mainstream languages have the \n and \t which are actually 1 character each, representing the newline and the tab, respectively. So \\t\"\n becomes

\t"

(note the trailing newline)

To add an extra layer of usability for "exotic" characters, it is possible to use a unicode escape: of the form \uxxxx where each x is one of 0123456789abcdef. This transforms into the unicode character whose codepoint is equal to the base-10 equivalent to the hexadecimal number xxxx. For instance \u0031 becomes 1 because 31 in hexadecimal is 49 in base 10, and 1 has a codepoint of 49.

Your input string may contain any of these escapes, and you may assume that \ is followed only by the backtick or any of \'"tnu. The unicode escape sequence is guaranteed to be valid, and escapes as well.

You should output this string after the transformations described.

Worked example

Input string:

ab%\u0035e\n\n\t5\'e\\54c%&\u0095

The first three characters remain identical.

\u0035 becomes 5 because 35 in hex -> 53 in base 10, and String.fromCharCode(53) === '5'.

\n\n\t becomes two newlines followed by a tab.

5 remains 5.

\' becomes ': escaped by backslash

e remains e.

\\ becomes \.

54c%& stays the same.

\u0095 is an unprintable with codepoint 149.

Result:

ab%5e

    5'e\54c%&<unprintable>

(<unprintable> is an actual unprintable character) Note that functions capable of evaluating such strings are disallowed. To be clear, your program must handle the syntax described above, and not any other format.

Other test cases:

Input Output
\'\" '"
\\"\' \"'
\\u0031\u0031\\t \u00311\t
\\\\\\\\\n \\\\<NL>

[<NL> is newline]


Meta:

  • Duplicate? Couldn't find anything.
  • Any clarifications needed?
  • Better title / tags?
\$\endgroup\$
13
  • \$\begingroup\$ A bit unclear to me: Do we need to handle the string syntax of the answer's implementation language, or the syntax you specify here? If that of our own language, then what if our own language has few special characters? (E.g. in the APL family, it is common that the only unusual character in a string is the quote, which must be doubled.) \$\endgroup\$
    – Adám
    Jun 14 '21 at 11:54
  • \$\begingroup\$ "eval functions or similar are not allowed." but what constitutes being similar to eval? What about functions in the JSON object of JavaScript? What about library functions to do this? Does APL's built-in ⎕JSON utility function constitute an eval, though the language's real eval is ? \$\endgroup\$
    – Adám
    Jun 14 '21 at 11:56
  • \$\begingroup\$ @Adám In some calculator questions, the OP (wisely) decides to disallow eval functions. Basically these functions. In essence, from my point of view, if your language is capable of evaluating these strings, then the function that does them is disallowed. However since APL's eval seems different, you can use them. Also in response to the one asking about string syntax, you must handle the one in the question, otherwise it gets a little unfair. \$\endgroup\$
    – user100690
    Jun 14 '21 at 12:06
  • \$\begingroup\$ Will the input include surrounding quotes? \$\endgroup\$
    – Adám
    Jun 14 '21 at 12:56
  • \$\begingroup\$ @Adám Nope. (filler) \$\endgroup\$
    – user100690
    Jun 14 '21 at 13:01
  • \$\begingroup\$ Then do we have to handle unescaped ' and/or "? \$\endgroup\$
    – Adám
    Jun 14 '21 at 13:03
  • \$\begingroup\$ @Adám You still have to handle them, just not surrounding the entire string. \$\endgroup\$
    – user100690
    Jun 14 '21 at 13:08
  • \$\begingroup\$ Suggested test cases: '" and \'\" \$\endgroup\$
    – Adám
    Jun 14 '21 at 13:09
  • \$\begingroup\$ @Adám You know what, the idea of not having surrounding quotes is a little confusing, so I'll remove that rule. That means that your suggested test cases are identical to each other, so I'll add the latter to demonstrate. Also will add about five more cases. \$\endgroup\$
    – user100690
    Jun 14 '21 at 15:28
  • \$\begingroup\$ "if this string were to be enclosed in quotes in your language and parsed as an expression, no errors should show." makes it sound as if my language matters. \$\endgroup\$
    – Adám
    Jun 15 '21 at 15:35
  • \$\begingroup\$ @Adám I'll change that \$\endgroup\$
    – user100690
    Jun 15 '21 at 15:37
  • \$\begingroup\$ Am I allowed to use \n in my code to insert a newline, or is this considered "functions capable of evaluating such"? \$\endgroup\$
    – Adám
    Jun 15 '21 at 15:58
  • \$\begingroup\$ Let us continue this discussion in chat. \$\endgroup\$
    – user100690
    Jun 15 '21 at 15:59
1
\$\begingroup\$

COBOL(lite) interpreter.

Your task is to code an interpreter for COBOL. This interpreter should use the free format, and cut a few corners, as long as a few test scripts will run on it.

Notice; here I say interpreter, as that is the general direction of my inclination, however if it may save bytes, a compiler would also count.

Try to:

  • Golf your code as much as possible
  • Remove some parts of COBOL (But ask here first to see if they make sense to remove.
  • Accept both CAPITAL and lowercase

Avoid:

  • Standard Loopholes
  • Don't remove too many parts of COBOL. I think that IDENTIFICATION DIVISION. could be ignored, but DATA DIVISION. is probably necessary.

If there is a thing that you would like to clarify on, ask in the comments. I think that this question still could stand with improvement, before it can be added to the main cg se.

\$\endgroup\$
3
  • 3
    \$\begingroup\$ You need to be a lot more specific with what you want implemented. This can range from syntax requirements to required keywords and function. You need to specify anything and everything yo want in a possible golfed interpreter. \$\endgroup\$
    – Razetime
    Jun 15 '21 at 10:39
  • \$\begingroup\$ I recommend checking questions tagged under interpreter on the main site for guidance. \$\endgroup\$
    – Razetime
    Jun 15 '21 at 10:40
  • \$\begingroup\$ @Razetime I didn't have a lot of time to make this post, but that is why it is in the sandbox. I know that I will need to improve it a fair bit, before putting it on the main code golf se \$\endgroup\$
    – smarnav
    Jun 15 '21 at 23:17
1
\$\begingroup\$

Implement a feature-rich calculator


We have a few many calculator challenges on this site, but none seem to require a complex one with many different features.

This challenge is about emulating my old Casio calculator, except with no digit limit. Features are:

  • basic (start with a single operation, then input the next operation)
  • all clear (AC) which starts a new "calculation". No need for C.
  • errors for dividing by zero (otherwise there should be no error)
  • memory: M to set the memory to current number, MC as an inline expression representing the memory, and M- to get rid of the memory.

This should operate either on STDIN (prompt for commands each time) or in a function (taking in either a list of commands or a newline-separated string with commands).

Spec

Your solution should be able to handle floating-point numbers, hence float division should be used regardless of context.

When first run, the calculator will be "ON". It is guaranteed that input syntax is valid.

The chain of commands begins with a number operation number-like structure, where number can be replaced with MC to represent a reference to the memory. For instance 5 * 5 or 6/MC. Whitespace can be ignored or forbidden, that can be your call, but whatever it is you may assume syntax is valid.

Then, if your submission is a full program, it should output the result of that operation, or if a function, append that to the end of a list.

Subsequent operations can either be:

  • M this sets the memory to current number. Memory can only ever hold a single number at a time.
  • AC. Clears the current calculation and resets the number. Memory, however, will still be accessible.
  • M-. (M minus) This clears the memory, and calls to MC will never be given when there is nothing in the memory.
  • operation number where operation is + - * or /, and number is a number which can be replaced with MC.
  • Off. The command Off stops the program, and it is reasonable to assume that no commands will be passed afterwards.
  • - (minus sign) negates current number. Outputs the new number.

Division by zero should trigger an error and restart the calculation. AC, M, MC and M- should output nothing. Memory can be overwritten.

Note that operation number operation number will never be given, and that all operations will be performed on non-negative numbers. (we have the negate operator for negative numbers)

Also note that this challenge is less about parsing / processing a string input and more about responding to different kinds of input.

Example

As a full program, but note that a function will have to take in an array of commands. There is a > before each command, but in your version, it is not required.

> 5*5
25
> *4
100
> /200
0.5
> /0
ERROR (or some form of ERROR)
> 15*6
90
> M
> +MC
180
> AC
> 7-MC
-83
> -
83
> M-
> +5
88
> Off

Meta: is it a duplicate? any clarifications needed?

\$\endgroup\$
3
  • \$\begingroup\$ Maybe use the [interactive] tag, and make it clearer that this is less parsing/processing a given string input and more responding to inputs as given \$\endgroup\$ Jun 18 '21 at 14:16
  • \$\begingroup\$ The "error" should not terminate the program, right? Would it be OK to, on error, print anything that is distinguishable from numeric output (say "X", "Inf", or "NaN"), or print something different across errors (e.g. "Inf" for 1/0, "NaN" for 0/0)? \$\endgroup\$
    – Bubbler
    Jun 25 '21 at 1:40
  • \$\begingroup\$ @Bubbler Yes, that's fine. You can also output Inf for 1/0 and NaN for 0/0 like you mentioned. \$\endgroup\$
    – user100690
    Jun 25 '21 at 5:10
1
\$\begingroup\$

KotH - Floating Point Prisoners Dilemma

\$\endgroup\$
8
  • 1
    \$\begingroup\$ Looks intriguing! How does each round work though? So one is coop, zero is defect, but what do the floats in between do? Look forward to hearing! \$\endgroup\$
    – AviFS
    Jun 11 '21 at 2:33
  • \$\begingroup\$ It uses an equation from the wonderful fellows at math.stackexchange.com to determine what the in-between states should do. I'm bad at explaining, so the equations in my (probably horrible) code are in the play function of the controller class in controller.py \$\endgroup\$
    – 4D4850
    Jun 11 '21 at 2:46
  • \$\begingroup\$ @4D4850 Would you link to the question if you have it still? \$\endgroup\$
    – mathcat
    Jun 11 '21 at 17:46
  • 1
    \$\begingroup\$ It was actually closed, but here it is: math.stackexchange.com/questions/4152360/… \$\endgroup\$
    – 4D4850
    Jun 11 '21 at 18:27
  • \$\begingroup\$ Understood it now, good challenge! \$\endgroup\$
    – mathcat
    Jun 12 '21 at 8:39
  • \$\begingroup\$ Should I post the question? \$\endgroup\$
    – 4D4850
    Jun 17 '21 at 19:08
  • \$\begingroup\$ Posted on Main: codegolf.stackexchange.com/questions/229926/… \$\endgroup\$
    – 4D4850
    Jun 17 '21 at 22:14
  • \$\begingroup\$ I've edited this down to a stub now that it's been posted to save space \$\endgroup\$ Jun 18 '21 at 14:33
1
\$\begingroup\$

Encode a Lenguage

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

Outputting Blum Integers

\$\endgroup\$
2
  • \$\begingroup\$ I'd suggest using standard sequence rules (answers can choose \$n\$th, first \$n\$ or infinite output). I've also edited the challenge a bit, feel free to revert any changes you dislike \$\endgroup\$ Jun 18 '21 at 14:29
  • \$\begingroup\$ @cairdcoinheringaahing thanks for the suggestion, i'll apply it \$\endgroup\$
    – user100752
    Jun 19 '21 at 9:14
1
\$\begingroup\$

What's missing

\$\endgroup\$
5
  • \$\begingroup\$ This is probably too easy \$\endgroup\$
    – rak1507
    Mar 22 '21 at 12:14
  • \$\begingroup\$ Some languages may have built-in Bag / MultiSet / Counter collection type. And a simple - operate or something similar may get correct result. Will you allow answers use such type of values as input / output? \$\endgroup\$
    – tsh
    Mar 23 '21 at 1:41
  • \$\begingroup\$ @tsh I guess? It doesn't really make a difference for the languages that have creative solutions. It's code-golf, which means answers generally don't get accepted. \$\endgroup\$
    – emanresu A
    Mar 23 '21 at 8:22
  • \$\begingroup\$ ruby does have array-array, but it wont work for arrays with similar items missing. I feel this is easy, but it can work as a codegolf challenge \$\endgroup\$
    – user100752
    Jun 22 '21 at 9:24
  • \$\begingroup\$ @EliteDaMyth Honestly, I'm really surprised this doesn't already exist. \$\endgroup\$
    – emanresu A
    Jun 22 '21 at 9:33
1
\$\begingroup\$

Do these points approximately make up a regular n-sided polygon?

The input is a set of integer coordinates. Take them in whatever form you want, e.g. a list of tuples or just a plain list. The question is: can you draw an n-sided regular polygon with vertices at the specified coordinates? n being the amount of coordinates provided. Answer truthy if yes, falsy if no.

The level of precision is in integers. That means you will have to round both the x and y values of the ideal polygons to the nearest whole values. Here's an example diagram:

example

The triangle has points at approximately (5,5), (4,9), (1,6). You can draw a regular triangle to fit these coordinates, even if the actual points of the triangle are not at quite the same coordinates.

The same goes for the pentagon. The coordinate set (3,1), (7,1), (8,5), (5,8), (2,5) makes up a regular pentagon-ish, accurate to whole values.

Any set of coordinates can make up an infinite amount of approximate n-gons. You answer truthy if there's at least one such n-gon. One way of thinking about it is to consider a unit square around each of the points, then determine if you can stretch a regular polygon such that every point falls within the squares.

Further ground rules:

  • The coordinates will always be positive.
  • They do not have to be in any particular order.
  • Coordinate values will never exceed 1000.
  • There will not be more than 10 coordinates.

Examples:

(5,5), (4,9), (1,6) -> true
(3,1), (7,1), (8,5), (5,8), (2,5) -> true
(1,1), (1,1), (2,1) -> true
(1,1), (2,1), (130,1) -> false

(42,42) -> true

(according to Wikipedia, a monogon is still a polygon, meaning any input is valid.)

(0,1), (5,10) -> true 

(any two points make up a regular bigon so this should work for any coordinates)

(1,3), (1,3), (1,3), (1,3) -> true

(any set identical coordinates will always match an n-gon that's tiny enough to have all of its coordinates rounded to the same integer).

\$\endgroup\$
4
  • \$\begingroup\$ I just found this question. It seems to be the same question but it is closed with no answers, and I think my requirements are clearer. \$\endgroup\$
    – KeizerHarm
    Jun 16 '21 at 12:12
  • 1
    \$\begingroup\$ I don't think the current explanation addresses the question "How are we supposed to tell if the polygon is regular without knowing its exact coordinates?" I feel like there's some way to gradually constrain polylines, but not quite sure. Also, I think "One way of thinking about it is to consider a circle of half a unit around one of the points" should be "... consider a unit square around one of the points" instead. What is the expected output of (0, 0), (0, 2), (1, 1), (2, 0) (which is borderline square with (-0.5, -0.5), (-0.5, 1.5), (1.5, 1.5), (1.5, -0.5))? \$\endgroup\$
    – Bubbler
    Jun 23 '21 at 0:15
  • \$\begingroup\$ @Bubbler Thanks for the comment. You are correct about the circle being a square! My mistake. And yes, that would be truthy. \$\endgroup\$
    – KeizerHarm
    Jun 23 '21 at 8:59
  • \$\begingroup\$ @Bubbler regarding "how are we supposed to tell if polygon is regular" that's asking for an example answer strategy, not part of the question, is it? The question is whether there is any regular polygon where each point falls in those unit squares. That's almost an optimisation problem. It's not impossible, all the required information is there. But I could try to come up with one approach - but answers should not feel obligated to copy that approach because for this problem there would be many solution strategies. \$\endgroup\$
    – KeizerHarm
    Jun 23 '21 at 9:03
1
\$\begingroup\$

Alteration of a challenge that was considered too close to an older quasi-duplicate.

ISO Computus

? ?

Your task is to calculate
from a signed integer year number provided as sole input
the integer number of the calendar week of Easter Sunday as sole output
with as few variables and as few arithmetic operations as possible.

Easter Sunday is determined according to the Gregorian Computus (as used in the all Western Christian churches, including the Catholic Church, and some others), for which there are several equivalent algorithms available, see Computus, Gent, Stockton and ESTRALGS.TXT. There are 35 possible month-day dates, from 22 March to 25 April.

The week of the year is specified by international standard ISO 8601-1: All weeks start on Monday; there are no partial weeks; the first week of the year has 4 January (or the first Thursday) in it. For more information see the Mathematics of the ISO 8601 Calendar. There are just six possible results, weeks 12 through 17. W17 will hardly ever occur – it has never occurred since 1583 at least.

Restrictions

  • You may use all basic arithmetic operators, including addition, substraction, multiplication, division, integer division, modulo, rounding (with integer flooring and ceiling), exponentiation, logarithms.
  • You may not use library functions that implement an algorithm for the Computus, like PHP's easter_date(), EasterSunday[year] from the Mathematica Calendar package or Easter from the R package timeDate.
  • You may not use other predefined date functions, e.g. to find the day of the week or the week of the year.

Ranking score

Your solution must be an algorithm implemented as an executable script in a programming language of your choice.

  • Each numeric variable and constant, whether used by name or literal value, counts as 1 point.
  • Each basic operation, as defined above, counts as 1 point.
  • Each definition of a custom function or method counts as 5 points.
  • Each call of a custom function or method counts as 1 point.
  • Imports and similar initializations required by the programming language are ignored.
  • Neither reading the input value and returning or printing the output value nor eventual variables for storing their value are counted.
  • The answer with the fewest points wins.
  • TBD…

Example:

function CFAQEaster(year) { // Calendar FAQ
                       // 5 points for Div() function
  var G, C, H, I, J, L // 6 points for variables
  G = year % 19        // 2: 1 point for modulo, 1 point for 19
  C = Div(year,100)    // 2: 1 point for function call, 1 point for 100
  H = (C - Div(C,4) - Div(8*C+13,25) + 19*G + 15) % 30 
    // 16: 8 points for operations, 2 points for function calls, 6 points for additional numeric values
  I = H - Div(H,28)*(1 - Div(29,H+1)*Div(21-G,11)) 
    // 13: 5 points for operations, 3 points for function calls, 5 points for additional numeric values
  J = (year + Div(year,4) + I + 2 - C + Div(C,4)) % 7
    // 6 points for operations, 2 points for function calls, 2 points for additional numeric values
  L = I - J            // 1 point for substraction
  EasterMonth = 3 + Div(L+40,44) 
    // 6: 2 points for addition, 1 point for function call, 3 points for additional numeric values
  EasterDay = L + 28 - 31*Div(EasterMonth,4)
    // 6: 3 points for operations, 1 point for function call, 2 points for additional numeric values
  return {Y:year, M:EasterMonth, D:EasterDay} 
    // 63 points in total
}

Contemporary example input: output

  • 2001: 15
  • 2002: 13
  • 2003: 16
  • 2004: 15
  • 2005: 12
  • 2006: 15
  • 2007: 14
  • 2008: 12
  • 2009: 15
  • 2010: 13
  • 2011: 16
  • 2012: 14
  • 2013: 13
  • 2014: 16
  • 2015: 14
  • 2016: 12
  • 2017: 15
  • 2018: 13
  • 2019: 16
  • 2020: 15
  • 2021: 13

The return value must be correct for a complete cycle of input years, i.e. from 1 through 5700000, even though the original event that is celebrated did not occur before 30 (if ever at all) and the Gregorian Calendar was not used before late 1582, i.e. the proleptic Gregorian Calendar applies.

Resources

\$\endgroup\$
2
  • \$\begingroup\$ I believe this challenge is better done as alorithm-golfing rather than counting characters or bytes. However, I'm not sure at all how to award a score. The rules above are very much up to bikeshedding. \$\endgroup\$
    – Crissov
    Jul 1 '21 at 10:46
  • \$\begingroup\$ The main problem with any kind of scoring like this is that many programming languages have very different set of built-in operations and features. How many points for using a mapping or reduction on an array? How many points for swapping two numbers on the stack? How many points for using the 1 command in Hexagony, which computes x>=0?x*10+1:x*10-1? A better way would be to fix the language and a very strict set of features available, like "use Python 3, only +-*/ operators on numbers are allowed". \$\endgroup\$
    – Bubbler
    Jul 14 '21 at 7:28
1
\$\begingroup\$

Days of the Week

\$\endgroup\$
1
  • 1
    \$\begingroup\$ A few suggestions: perhaps add the date tag, and say something like Output "WEEKDAY" if there are 5 dates, all from Monday-Friday, "WEEKEND" if there are two dates: Saturday and Sunday, and "ALLDAYS" if the input contains seven days all from Monday-Sunday. \$\endgroup\$
    – user100690
    Jun 28 '21 at 12:46
1
\$\begingroup\$

Generate the shortest regex to match these but not those

\$\endgroup\$
3
  • \$\begingroup\$ A possible short program just enumerates all the regexes. This might not be feasible, and you may not be able to score submissions at all... \$\endgroup\$
    – Trebor
    Jun 28 '21 at 17:34
  • \$\begingroup\$ @Trebor I don't quite understand what you mean by this. \$\endgroup\$
    – user197974
    Jun 28 '21 at 18:13
  • \$\begingroup\$ If you just write a program and tests, one by one, all the regexes, and outputs the first one that works, this would be a valid program. But to give it a score, you would probably need days, if not years. \$\endgroup\$
    – Trebor
    Jun 29 '21 at 8:06
1
\$\begingroup\$

Solve Fermat's Last Theorem with matrices

Fermat's Last Theorem states that there is no such triple of positive integers \$(x, y, z)\$ such that

$$x^n + y^n = z^n$$

for all integers \$n > 2\$. However, this conjecture does not hold for integer matrices:

$$\begin{align} & \left( \begin{matrix} 1 & 3 \\ 0 & 1 \end{matrix} \right)^3 + \left( \begin{matrix} -1 & 0 \\ 1 & -1 \end{matrix} \right)^3 \\ = & \left( \begin{matrix} 1 & 9 \\ 0 & 1 \end{matrix} \right) + \left( \begin{matrix} -1 & 0 \\ 3 & -1 \end{matrix} \right) \\ = & \left( \begin{matrix} 0 & 9 \\ 3 & 0 \end{matrix} \right) \\ = & \left( \begin{matrix} 0 & 3 \\ 1 & 0 \end{matrix} \right)^3 \\ \end{align}$$

You are to take two \$k \times k\$ integer matrices \$A\$ and \$B\$, and a positive integer \$n \ge 2\$, and output a \$k \times k\$ integer matrix \$C\$ such that

$$A^n + B^n = C^n$$

You may output any such \$C\$. You may also take \$k\$ as an input, and you may assume that \$k \ge 2\$

This is , so the shortest code in bytes wins

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Is it guaranteed that A, B, n are given so that such a C exists? Why not just make a challenge about taking the nth root of a given matrix (which is arguably the hardest part of the task)? \$\endgroup\$
    – Bubbler
    Jul 14 '21 at 6:54
1
\$\begingroup\$

Chess Squad March

Chess Squad March

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

I'm a lizard, cut here!

\$\endgroup\$
2
  • \$\begingroup\$ I think you mean n≥2 \$\endgroup\$
    – xnor
    Jul 21 '21 at 5:05
  • \$\begingroup\$ @xnor sure thanks \$\endgroup\$
    – Domenico
    Jul 21 '21 at 5:11
1
\$\begingroup\$

How effective is compression?

Vyxal has a very simple format for compressed integers: a base-255-encoded string of characters wrapped in «.

With base 255, the number to be compressed needs to be 10000 or greater for the compression to have an advantage against ordinary numbers, as with 1 character, you can represent at most 3-digit numbers (up to 254), which ties with 1 + 2 (for the «) = 3 bytes; with 2 you can represent up to 65024 in 4 bytes, beating 5 for n ≥ 10000.

But what if it was another base?

If it were base 99, it would take 3 bytes, since you can only express up to 9800 with 2, which isn't any better than expressing the number. But with 3, you can reach 970299, which has 6 digits

Your challenge is to take a positive integer \$b\$ 11 or greater, and find the smallest number \$n\$ such that \$log_b(10^n) + 2 < n\$. You may output \$10^n\$ instead.

This is , shortest wins!

Testcases coming soon.

\$\endgroup\$
3
  • \$\begingroup\$ Be sure to include the testcases for n=100 and n=1000. The correct answer is floor(1+2/(1-1/log10(n))), but the wrong answer ceil(2/(1-1/log10(n))) gives identical results for all other n. (The correct answer can be also wrong due to floating-point imprecision. Is this allowed?) \$\endgroup\$
    – Bubbler
    Jul 29 '21 at 7:45
  • \$\begingroup\$ @Bubbler Ok, will do. Shouldn't be wrong as it's taking a single logarithm. \$\endgroup\$
    – emanresu A
    Jul 29 '21 at 7:56
  • \$\begingroup\$ Straightforward Python fails because of 1/3, not single logarithm btw \$\endgroup\$
    – Bubbler
    Jul 29 '21 at 8:13
1
60 61
62
63 64
121

You must log in to answer this question.

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