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

3371 Answers 3371

1
57 58
59
60 61
113
1
\$\begingroup\$

Complete the Square (UNFINISHED)


Completing the square is (part of) a method for solving quadratic equations, which involves turning something like this:

$$ax^2+bx+c=0$$

Into something like this:

$$a(x-h)^2+k=0$$

I'll show how I was taught to do this. Take the quadratic \$2x^2-8x+16\$. First, divide out \$a\$:

$$2(x^2-4x+8)$$

Now, the goal is to add a number inside the parentheses which makes the quadratic the square of an \$(x-n)\$ term. In this case, adding \$-4\$ would result in \$x^2-4x+4\$, the square of \$(x-4)\$.

Importantly, you also need to subtract a number (outside the parentheses) in order to keep it the same overall. This results in:

$$2(x^2-4x+4)+8$$

Which can be simplified to:

$$2(x-2)^2+8$$

This can be used to solve quadratics, by rearranging that result a bit:

$$\begin{align}2(x-2)^2+8&=0\\2(x-2)^2&=-8\\(x-2)^2&=-4\\x-2&=\pm\sqrt{-4}\\x&=\pm\sqrt{-4}+2\\x&=2\pm2i\end{align}$$

Task:

Not sure what a good task would be for this but I already typed it all so I'm not discarding it :p

\$\endgroup\$
5
  • \$\begingroup\$ I believe you can simply apply the quadratic formula and get \$k=c-\frac{b^2}{4a}\$ and \$h=-\frac{b}{2a} \$. Since you are just writing down how to get that formula step by step \$\endgroup\$ – tsh May 12 at 9:38
  • \$\begingroup\$ @tsh That's the main reason I'm not sure what the task should be, since I don't want it to just be another "stick these values into a formula" challenge :p \$\endgroup\$ – Redwolf Programs May 12 at 13:29
  • \$\begingroup\$ @RedwolfPrograms Not sure there's a good challenge buried in here. \$\endgroup\$ – Don Thousand May 12 at 20:05
  • \$\begingroup\$ @DonThousand Yeah that's what I was worried about :/ \$\endgroup\$ – Redwolf Programs May 12 at 20:17
  • \$\begingroup\$ Any feedback on either of my two recent challenges in Sandbox? \$\endgroup\$ – Don Thousand May 12 at 20:19
1
\$\begingroup\$

Randomized Calculator

Imagine you want to make a random number generator. What if you want it more complex? For this challenge, you should write a program which:

  • Takes 3 inputs:

    • An integer \$a\$
    • An integer \$b\$, such that \$b \ge a\$
    • A string, \$s\$, detailed below, representing one of addition, subtraction, multiplication and division
  • Generates two random integers, \$x, y\$, such that \$a \le x\$ and \$b \ge y\$. The integers should be uniformly random, and chosen independently

  • And, outputs the result of applying \$x\$ and \$y\$ to the mathematical operation detailed by \$s\$

The following strings should be the expected ones to be used for \$s\$:

  • Addition: a, +, plus, p, add
  • Subtraction: s, -, subtract, minus
  • Multiplication: m, *, x, times, t, multiply, ×, ·
  • Division: d, /, divide, :, ÷

One example:

[4, 17, "times"] -> 35

as we generated \$5\$ and \$7\$ from the range \$[4, 17]\$, and multiplied them together.

Shortest code in bytes wins.

Feedback

  • Any more tags?
  • Anything unclear?
  • More rules to apply?
\$\endgroup\$
13
  • \$\begingroup\$ I'd write "two" instead of "2" not to confuse it with the list item number. \$\endgroup\$ – Adám May 12 at 17:35
  • \$\begingroup\$ I find it very unclear. Where did that times come from? \$\endgroup\$ – Adám May 12 at 17:42
  • \$\begingroup\$ I've edited the spec a bit to make it clearer. Feel free to change anything I got wrong or that you dislike \$\endgroup\$ – caird coinheringaahing May 12 at 17:56
  • \$\begingroup\$ @Adám x · y can be rewritten as "x times y". \$\endgroup\$ – r u even listening to me May 12 at 18:02
  • \$\begingroup\$ No minus for subtraction? \$\endgroup\$ – Adám May 12 at 18:33
  • \$\begingroup\$ @MatthewDaniels I have no idea why the fact x · y can be rewritten as "x times y" would cause times in the output. Anyway, with caird's rewrite, it isn't in the output any more. \$\endgroup\$ – Adám May 12 at 18:35
  • \$\begingroup\$ Is it possible that \$a≤0≤b\$? \$\endgroup\$ – Adám May 12 at 18:36
  • \$\begingroup\$ @Adám Yes. Both integers can be negative, yet \$b\$ shouldn't be negative if \$a\$ is positive. \$\endgroup\$ – r u even listening to me May 12 at 18:42
  • \$\begingroup\$ @Adám No, the mathematical expression. Multiplication can mean times. For example, "6 multiplied by 10" is the same as "6 times 10". \$\endgroup\$ – r u even listening to me May 12 at 18:43
  • 2
    \$\begingroup\$ @MatthewDaniels If only one of them can be negative at any one time, you should state so. Also, your statement that \$a≤x\$ and \$b≥y\$ needs clarification. Does this mean that both are unbounded to one side, i.e. \$a≤x≤∞\$ and \$b≥y≥-∞\$? \$\endgroup\$ – Adám May 12 at 18:45
  • 2
    \$\begingroup\$ What happens if \$b=0\$ and the input requests division? \$\endgroup\$ – Adám May 12 at 18:46
  • \$\begingroup\$ What happens if the output of division isn't an integer? With what precision should it be printed? \$\endgroup\$ – Command Master May 13 at 12:58
  • \$\begingroup\$ Do we need to support all the strings for \$s\$ or may we choose one per category? \$\endgroup\$ – pajonk May 13 at 19:49
1
\$\begingroup\$

Random Move 2x2 Scrambling

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

Create word lightning

Inspired by a Codingame challenge I tried (and failed at) recently.

Given a binary tree of words, say:

             HELLO
            /     \
        WORLD     EATING
        /         /   \
     ARCH     IDAHO   GUARD
                      /
                   DOOZY
                   /
                ZEPPELIN
                 /    \
             POWER   LIGHT
  1. Take the root of the tree.
  2. Print it in the South direction.
H
E
L
L
O
  1. For each of the tree's children, given iteration index i:
    1. Find the first common character of the child and the root.
    2. The drawn root word will have to intersect the child at this point.
    3. Draw the left node down to the left, and the right node down to the right.
   H
   E
   LA
   LWT
   O  I
  R    N
 L      G
D
  1. Now, for each of the diagonal words' children, do the same, except always print downward.
   H
   E
   LA
   LWT
  AO  I
  R   DN
 LC   A G
D H   H U
      O A
        R
        D
  1. Go back to step3 and perform the steps again until the tree is complete.
   H
   E
   LA
   LWT
  AO  I
  R   DN
 LC   A G
D H   H U
      O A
        R
        D
       O
      O
     Z
    YE
     P
    OP
   W E
  E  L
 R   II
     N G
        H
         T

Clarifications

  • No two child words will have the same first common letter.
  • Words from different subtrees will never intersect.
  • input will always be a valid binary tree.
  • trees can be taken in any suitable and reasonable format for your language.

Meta

  • any possible duplicates?
  • is there anything important to clarify that I left out?
  • Is there anything I can simplify?
\$\endgroup\$
2
  • \$\begingroup\$ Trees can be taken in different formats, right? \$\endgroup\$ – Wzl May 14 at 13:35
  • \$\begingroup\$ yes, they can be taken in any suitable format. \$\endgroup\$ – Razetime May 14 at 13:47
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 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 at 2:28
  • \$\begingroup\$ "quartic equation: ax4+bx3+cx2+dx+e"; should be "ax4+bx3+cx2+dx+e = 0" \$\endgroup\$ – tsh May 18 at 2:28
  • 1
    \$\begingroup\$ I cannot get the formula work. Had I made some typo? \$\endgroup\$ – tsh May 18 at 2:41
  • \$\begingroup\$ @Arnauld yes - if your language can support it, it should. \$\endgroup\$ – StackMeter May 18 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 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\$ – StackMeter May 18 at 7:31
  • \$\begingroup\$ @tsh Give me a second to check that \$\endgroup\$ – StackMeter May 18 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\$ – Wzl May 26 at 15:09
  • \$\begingroup\$ You think the best arrangement can be found with math? Without a lot of computation? \$\endgroup\$ – Onir May 26 at 15:10
  • 3
    \$\begingroup\$ I'll admit that it might be ok to post it here with the tag code-challenge \$\endgroup\$ – Wzl May 26 at 15:12
  • \$\begingroup\$ It seems like something which can be solved optimally with vertex cover, since 3^15 is reasonably small. \$\endgroup\$ – Command Master May 28 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\$ – Command Master May 28 at 6:18
1
\$\begingroup\$

Gödel numbering of a string

Posted!

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

Demonstrate some easier abstract algebra

From my related challenge, Demonstrate some advanced abstract algebra

Consider a binary operator \$*\$ that operates on a set \$S\$. For simplicity's sake, we'll assume that \$*\$ is closed, meaning that its inputs and outputs are always members of \$S\$.

Let's define some basic terms describing the properties of \$*\$. We can say that \$*\$ can have any of these properties, if they hold for all \$a,b,c \in S\$:

  • Commutative: \$a*b = b*a\$
  • Associative: \$(a*b)*c = a*(b*c)\$
  • Distributive: \$a*(b+c) = (a*b)+(a*c)\$, for some binary operator \$+\$ on \$S\$

We can also define 3 related properties, for a unary operation \$-\$ on \$S\$:

  • Anti-commutative: \$a*b = -(b*a)\$
  • Anti-associative: \$(a*b)*c = -(a*(b*c))\$
  • Anti-distributive: \$a*(b+c) = -((a*b)+(a*c))\$

Finally, we define 3 more, that only describe \$*\$ if the complete statement is true for \$a,b,c \in S\$:

  • Non-commutative: There exists \$a, b\$ such that \$a*b \ne b*a\$ and \$a*b \ne -(b*a)\$
  • Non-associative: There exists \$a, b, c\$ such that \$(a*b)*c \ne a*(b*c)\$ and \$(a*b)*c \neq -(a*(b*c))\$
  • Non-distributive: These exists \$a,b,c\$ such that \$a*(b+c) \ne (a*b)+(a*c)\$ and \$a*(b+c) \ne -((a*b)+(a*c))\$

We now have 9 distinct properties a binary operator can have: commutativity, non-commutativity, anti-commutativity, associativity, non-associativity, anti-associativity, distributivity, non-distributivity and anti-distributivity.

This does require two operators (\$-\$ and \$+\$) to be defined on \$S\$ as well. For this challenge we'll use standard integer negation and addition for these two, and will be using \$S = \mathbb Z\$.

Obviously, any given binary operator can only meet a maximum of 3 of these 9 properties, as it cannot be e.g. both non-associative and anti-associative.


Let's create a "table" of these properties:

Commutative Associative Distributive
Regular Commutative Associative Distributive
Anti Anti-commutative Anti-associative Anti-distributive
Non Non-Commutative Non-associative Non-distributive

Your task is to write 3 programs (either full programs or functions. You may "mix and match" if you wish).

Each of these 3 programs will:

  • take two integers, in any reasonable format and method

  • output one integer, in the same format as the input and in any reasonable method

  • be a surjection \$\mathbb Z^2 \to \mathbb Z\$ (takes two integers as input and outputs one integer). This means that for any distinct output, there is at least one input that yields that output

  • has exactly 3 of the 9 above properties. However, those three properties muse be in different rows and columns in the above table from each other. This means that it can be (for example) commutative, non-associative, anti-distributive; non-commutative, anti-associative, distributive; or anti-commutative, associative, non-distributive. But, it cannot be (for example) commutative, associative, distributive; non-commutative, non-associative, non-distributive; or non-commutative, anti-distributive, anti-associative.

This is ; the combined lengths of all 3 of your programs is your score, and you should aim to minimise this.

Additionally, you should include some form of proof that your programs do indeed have the required properties and do not satisfy the other properties. Answers without these are not considered valid.


Meta

\$\endgroup\$
5
  • \$\begingroup\$ There exists no anti-distributive surjection when \$S=\mathbb Z\$. Maybe there exists one when \$S=(\mathbb Z/2\mathbb Z)^k\$, but I haven't found one yet. \$\endgroup\$ – Robin Ryder 7 hours ago
  • \$\begingroup\$ I would remove the constraint that the operator be a surjection, and just impose that it be non-constant. If you convince yourself that such an anti-distributive operator exists, you could post the challenge, but I would change it to "write 3-9 programs, such that each property is verified by (at least) one program". That would be an incentive to have programs which verify several properties at once, but make it more manageable. \$\endgroup\$ – Robin Ryder 7 hours ago
  • \$\begingroup\$ Also, there are problems with your "anti-associativity": if b = 1, then (a*b)*c = a*(b*c). \$\endgroup\$ – anatolyg 5 hours ago
  • \$\begingroup\$ @RobinRyder If \$ S=(\mathbb Z / 2\mathbb Z)^k\$, aren't anti-distributive operators the same as distributive operators? \$\endgroup\$ – Nitrodon 2 hours ago
  • \$\begingroup\$ @Nitrodon Yes, of course you are right. \$\endgroup\$ – Robin Ryder 1 hour ago
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 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 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 at 13:47
  • \$\begingroup\$ @pxeger Thanks. Any suggestions for the title? \$\endgroup\$ – polfosol ఠ_ఠ Jun 2 at 13:48
  • \$\begingroup\$ short and simple: "Modulo tables"? \$\endgroup\$ – pxeger Jun 2 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 at 14:26
  • \$\begingroup\$ Also more test cases just for good measure \$\endgroup\$ – qwr Jun 2 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 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\$ – Redwolf Programs Apr 30 at 3:21
1
\$\begingroup\$

Shuffle a subsequence

Thanks to rak1507 for the suggestion

"Random" in this challenge always refers to "uniformly random" - of all possible choices, each has an equal chance of being chosen. Uniform shuffling means that every possible permutation of the array has an equal chance of being chosen.

Given an array consisting of positive digits (123456789), select a randomly chosen, non-empty subsequence from the array, shuffle the elements and reinsert them back into the array in the former indices, outputting the result

For example, take L = [5, 1, 2, 7, 4]. We choose a random non-empty sublist, e.g. [5, 2, 4]. These are at indices 1, 3, 5 (1-indexed). Next, we shuffle [5, 2, 4] to give e.g. [2, 5, 4]. We now reinsert these into the list, with 2 at index 1, 5 at index 3 and 4 at index 5 to give [2, 1, 5, 7, 4].

You may also take the input as an integer or a string, and output it as such, or you may mix and match types.

This is , so the shortest code in bytes wins


Meta

\$\endgroup\$
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 at 14:11
  • \$\begingroup\$ @pxeger yes. I always forget to put them in these posts \$\endgroup\$ – qwr Jun 2 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 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 at 8:21
  • \$\begingroup\$ Damn, that question looks very similar \$\endgroup\$ – LeopardL GD Jun 7 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 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 at 16:29
  • \$\begingroup\$ @pxeger Quite unrelated to chemical though, ClI and CLi both don't fell good \$\endgroup\$ – l4m2 Apr 22 at 18:23
  • \$\begingroup\$ I've edited this down to a stub now that it's been posted to save space \$\endgroup\$ – caird coinheringaahing Jun 7 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\$ – JamesTheAwesomeDude May 20 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\$ – Command Master May 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\$ – JamesTheAwesomeDude May 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\$ – Command Master May 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\$ – JamesTheAwesomeDude May 21 at 14:39
  • \$\begingroup\$ I've edited this down to a stub now that it's been posted to save space \$\endgroup\$ – caird coinheringaahing Jun 7 at 1:02
1
\$\begingroup\$

Is this a Permutation

Input a non-empty array with \$n\$ positive integers. Test if the input array contains every integers in \$1\cdots n\$.

In case you prefer 0-indexed numbers, you may choice to input an array of non-negative integers, and test if the input array contains every integers in \$0\cdots (n-1)\$ instead. All testcases listed blow use 1-index. You may need to adjust these testcases if you choose this option.

Input / Output

Input is an array with \$n\$ positive integers. outputting two distinct values, or truthy vs falsey values (swap meaning of truthy / falsy is allowed).

Rules

  • This is , shortest codes win. And since this is code-golf, don't worry about time / memory complexity of your code. You may even timeout on TIO as long as your program work when give it more time to run.

Testcases

Truthy

1
1,2
2,1
1,3,2
3,1,2
1,2,3,4,5,6,7,8,9,10,11,12
12,11,10,9,8,7,6,5,4,3,2,1
6,3,8,12,1,10,4,2,7,9,5,11
16,37,14,15,23,8,29,35,21,6,5,34,38,9,36,26,24,32,28,7,20,33,39,12,30,27,40,22,11,41,42,1,10,19,2,25,17,13,3,18,31,4

Falsy

2
12
1,1
1,3
2,3
1,4,3,1
4,1,2,4
8,7,5,3,4,1,6
5,7,1,4,6,1,8,3
6,3,5,4,7,1,8,1,2
6,5,3,8,2,7,9,4
1,1,1,1,1,1,1,1
1,5,9,13,11,7,3
14,6,12,4,10,8,16,2
34,33,38,17,35,11,36,31,28,14,6,15,18,2,19,40,29,41,9,1,27,23,20,32,26,25,37,8,13,30,39,7,5,3,21,4,11,16,10,22,12,24
38,27,20,23,31,6,2,24,21,31,33,7,26,12,14,17,3,2,28,31,5,23,28,27,37,32,7,39,22,6,35,42,19,3,35,17,35,40,22,13,27,7

  • Is this too trivial?
  • Really not a dupe?
  • Should I allow \$0\cdots (n-1)\$ as alternative?
\$\endgroup\$
6
  • \$\begingroup\$ In other words: "Is the input a permutation vector?" and as such, yes, 0…n‒1 should be allowed. \$\endgroup\$ – Adám Jun 7 at 8:44
  • \$\begingroup\$ It is pretty trivial in array languages; 3 bytes in Jelly and 5 in APL. \$\endgroup\$ – Adám Jun 7 at 8:48
  • \$\begingroup\$ This is trivial, but no more so than multiple other challenges on the sites. Plus, this looks to have multiple different ways to approach it, which makes it more interesting that most trivial challenges. Also, I agree with @Adám, you should allow answers to test from 0 instead if they want \$\endgroup\$ – caird coinheringaahing Jun 7 at 13:04
  • \$\begingroup\$ Suggested tag: permutations \$\endgroup\$ – Adám Jun 7 at 13:21
  • \$\begingroup\$ @Adám I don't speak Jelly, And I would wonder how the 3 bytes works. Is it: (1: get length; 2: generate all permutation of 1..n; 3: test input in generated results) or (1: sort input; 2. generate a 1..n list; 3. compare equals) or (1: sort input; 2: prepend a 0 and get difference of every two numbers; 3. check all value equals to 1) Anyway, as it would cost 3 bytes instead of a built-in, it is not quite trivial <small>(imo)</small>. \$\endgroup\$ – tsh Jun 8 at 1:48
  • \$\begingroup\$ Turns out there are a few ways to do it. What I had in mind was actually Grade, Grade, Compare to input. \$\endgroup\$ – Adám Jun 8 at 2:06
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 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 at 23:40
1
\$\begingroup\$

Determine Centrosymmetric String

, ,

Let's define a centrosymmetric string as follows:

  • First, add spaces to the end of each line to make the input a rectangle \$ A_{m×n} \$.
  • The input is "centrosymmetric" string if and only if you get the original rectangle if you rotate the rectangle 180°. That is, it has 2-fold rotational symmetry, so \$ A_{i,j}=A_{m+1-i,n+1-j} \$ holds for all \$ A_{1\dots m, 1\dots n} \$.

Input

Input a non-empty string. You may assume it:

  • does not contain leading / trailing new lines;
  • does not contain trailing spaces on any lines;
  • only contains new line, space, and lowercase a-z.

You may choose to handle any of CR, LF, or CR-LF as the new line character in your program.

Input may be in any reasonable format, including but not limited to:

  • A built-in string type;
  • A NULL-terminated character array;
  • An array of integer code-points;

Note that you are not allowed to take the string padded already as it trivialises the challenge.

Output

Determine if the given input is a centrosymmetric string (as defined in this post), outputting two distinct values, or truthy vs. falsey values (they can be swapped relative to their normal meaning).

Test cases

Truthy

a
aba
a
b
a
ab
ba
abc
cba
abc
ded
cba
a a
a

a
a

 a
a c
 b
c a
a

 b
 b

  a
zzzzz
   z
  z
 z
zzzzz
n  n  oo   oo  n  n
nn n o  o o  o nn n
n nn o  o o  o n nn
n  n  oo   oo  n  n

Falsy

ab
a
a
b
a
 b
a
ab
ab
aa
bb
   a
  a a
 aaaaa
a     a
\$\endgroup\$
7
  • \$\begingroup\$ I think a worked-through example would be a good idea \$\endgroup\$ – pxeger May 31 at 8:29
  • \$\begingroup\$ What is the meaning of the 2 in A_{m×n}2? \$\endgroup\$ – pxeger May 31 at 8:37
  • \$\begingroup\$ Related \$\endgroup\$ – pxeger May 31 at 8:39
  • \$\begingroup\$ When you say "square", do you actually mean "rectangle"? \$\endgroup\$ – Bubbler May 31 at 8:40
  • 1
    \$\begingroup\$ @pxeger the 2. is accidentally left there after I remove the line break between 1. blah 2. blah. Should be removed. \$\endgroup\$ – tsh May 31 at 8:44
  • \$\begingroup\$ @Bubbler changed to rectangle. I'm not quite sure about these words. \$\endgroup\$ – tsh May 31 at 8:44
  • \$\begingroup\$ @tsh A square is a rectangle with all side lengths the same, so m == n. Sometimes in English, "square" is used as an adjective to simply mean "having right-angled corners", which is quite confusing \$\endgroup\$ – pxeger May 31 at 8:46
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\$ – Spitemaster Jun 6 at 19:33
  • \$\begingroup\$ @Spitemaster yep, you're right - fixed. \$\endgroup\$ – pxeger Jun 9 at 14:33
1
\$\begingroup\$

Side to side, not up and down

Given a list of nodes representing a binary tree of positive integers serialized depth-first, return a list of nodes representing the same tree serialized breadth-first. To represent an absent child, you may use null, 0, 'X', Nothing, [], or any other value that is distinct from your representation of a node's value, which can be represented by an integer or an integer in a singleton list or other collection.

For example, here is an example tree :

         1
      /     \
     /       \
    5         3
   / \       / \
  #   4     10  2
     / \   / \ / \
    4   6 #  # #  7
   /\  /\         /\
  # # #  #       #  #

Serialized depth-first, this would be [1, 5, #, 4, 4, #, #, 6, #, #, 3, 10, #, #, 2, #, 7, #, #] (here, # indicates that a child does not exist). This list was made using a pre-order traversal (add a node's value to the list, serialize its left child or add # if it doesn't exist, serialize its right child or add # if it doesn't exist).

Serialized breadth-first, this would be [1, 5, 3, #, 4, 10, 2, 4, 6, #, #, #, 7, #, #, #, #, #, #] (you may trim the #'s at the end, I just wanted to make them explicit). Here, you write the root node's value, then the values of all the nodes on the level below, left to right (with a # where a node doesn't exist), then values of the level below, until all the nodes are added to the list.

Test cases

[1] -> [1, #, #] //or [1], whatever you wish

Tree: 1   //or just 1
     / \
    #   #
([1, #, #] and [1, #] yield the same result as above)

[100, 4, 5, #, #, #] -> [100, 4, #, 5, #, #]
Tree:    100
        /   \
       4     #
      / \
     5   #
    /
   #   

[10, 5, 2, 2, #, #, 2, #, #, #, 4, 8, 4, #, #, 1, #, #] -> [10, 5, 4, 2, #, 8, 1, 2, 2, 4]
Tree:     10
        /    \
       5      4
      / \    / \
     2   #  8   1
    / \    /   / \
   2   2  4   #  #
  /\  /\  /\
 # # # # # #

[100, #, 4, 5, #, #, #] -> [100, #, 4, 5, #, #]
Tree:    100
        /   \
       #     4
            / \
           5   #
          / \
         #   #

Questions for Meta

  • Is this a duplicate?
  • Is this interesting?
  • Needs more clarification/test cases?
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Node 10 seems to be missing a # child in the first diagram. \$\endgroup\$ – aeh5040 Jun 12 at 1:42
  • \$\begingroup\$ @aeh5040 Whoops, you're right, let me fix that \$\endgroup\$ – user Jun 12 at 13:27
1
\$\begingroup\$

Minkowski sum of two convex polygons

Background

Minkowski addition is a binary operation on two sets of points (usually geometric objects) in the Euclidean space. The Minkowski sum of two sets \$A\$ and \$B\$ is formally defined as follows:

$$ A+B = \{\mathbf{a}+\mathbf{b}\,|\,\mathbf{a} \in A,\ \mathbf{b} \in B\} $$

To say in plain English, given the origin and two figures, the Minkowski sum of the two figures is formed by the vector sum of each pair of points in the two figures. In the following image, the red shape is the Minkowski sum of the green and blue shapes.

enter image description here

It has an interesting property for two convex polygons: the edges of the original polygons are preserved (modulo translation) and sorted by polar angle (relevant info in Wikipedia), and therefore can be computed in \$\mathcal{O}(m+n)\$ (where \$m\$ and \$n\$ denote the number of vertices of two polygons respectively).

Task

Given two convex polygons, compute their Minkowski sum.

Each polygon is given as an ordered list of \$(x,y)\$ coordinates, and the output polygon should be represented in the same way. You can choose the ordering of vertices (clockwise or counterclockwise), but your code should handle different starting points of the same polygon ((0,0), (0,1), (1,0) is the same as (0,1), (1,0), (0,0)).

You may take each coordinate pair as a single complex number. The coordinates are guaranteed to be integers.

Standard rules apply. The shortest code in bytes wins.

Test cases

Coming soon.

\$\endgroup\$
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 at 1:15
1
\$\begingroup\$

Counting uniquely solvable polylinks

Related: Counting polystrips

Background

Link-a-Pix is a puzzle on a rectangular grid, where the objective is to reveal the hidden pixel art by the following rules:

  • Connect two cells with number N with a line spanning N cells, so that the two cells are at the two ends of the line.
  • The number 1 is considered connected to itself (which makes it an exception to the rule of "connect two cells").
  • Two different lines are not allowed to overlap.
  • The puzzle is solved when all the given numbers on the grid are connected by the above rules. There may be some unused cells after the puzzle is solved.

The following is an example puzzle and its unique solution.

(Source: The NP completeness of some lesser known logic puzzles, 2019)

A polylink is defined as a single line on a Link-a-Pix puzzle. It is identified by the collection of cells (i.e. a polyomino) the line passes through, the endpoints, and the path (i.e. a Hamiltonian path) defined by the line. It is somewhat similar to a polystrip, except that the strip can touch itself side-by-side, and the two endpoints of the strip are marked.

The following are pairwise distinct polylinks (X's are the endpoint cells and O's are the other cells on the strip). A rotation or reflection of a polylink is different from the original unless they perfectly coincide.

X-O   X X   X-O   O-O
  |   | |     |   | |
X O   O O   O-O   O O
| |   | |   |     | |
O-O   O-O   O-X   X X

Some distinct polylinks have the same underlying polyomino and endpoints, as follows. When such a link is used in a Link-a-Pix puzzle, it makes the entire puzzle have multiple solutions, which is not desirable.

O-O-O   O-O-O
|   |   |   |
O-X O   O X O
    |   | | |
X-O-O   X O-O

Therefore, let's define a uniquely solvable polylink as one which does not have another different polylink with the same polyomino and endpoints.

Challenge

Given the number \$n\$, count the number \$a(n)\$ of distinct uniquely solvable polylinks with \$n\$ cells. Due to the definition of a polylink, \$a(1) = 1\$ (the single cell with only one endpoint is a valid "line" on a Link-a-Pix puzzle).

Standard rules apply. The shortest code in bytes wins.

Test cases

The "uniquely solvable" makes the sequence deviate from A002900 from n = 9. There are exactly 10 "ambiguous" polylinks of 9 cells, all of which happen to have the shape of a 3x3 square:

O O O      O O O      X O O
O X O x4;  O O O x4;  O O O x2
X O O      X O X      O O X

Main test cases:

a(1) = 1
a(2) = 2 (horizontal and vertical)
a(3) = 6 (2 orientations of I, 4 for L)
a(4) = 18 (L*8, I*2, S*4, O*4, T is not a link)
a(5) = 50 (I*2, L*8, N*8, P*8*2, U*4, V*4, W*4, Z*4)
a(6) = 142
a(7) = 390
a(8) = 1086
a(9) = 2938
a(10) = 8070
a(11) = 21690
a(12) = 59022
a(13) = 158106

a(6) through a(13) were generated using a reference program in Python.

\$\endgroup\$
1
  • \$\begingroup\$ This looks like a great challenge, just a minor thing - can you change "since n=9" to "from n=9"? It feels a little odd with "since" to me. \$\endgroup\$ – user yesterday
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 2 days ago
  • \$\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 2 days ago
  • \$\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\$ – Recursive Co. 2 days ago
  • \$\begingroup\$ Will the input include surrounding quotes? \$\endgroup\$ – Adám 2 days ago
  • \$\begingroup\$ @Adám Nope. (filler) \$\endgroup\$ – Recursive Co. 2 days ago
  • \$\begingroup\$ Then do we have to handle unescaped ' and/or "? \$\endgroup\$ – Adám 2 days ago
  • \$\begingroup\$ @Adám You still have to handle them, just not surrounding the entire string. \$\endgroup\$ – Recursive Co. 2 days ago
  • \$\begingroup\$ Suggested test cases: '" and \'\" \$\endgroup\$ – Adám 2 days ago
  • \$\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\$ – Recursive Co. 2 days ago
  • \$\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 yesterday
  • \$\begingroup\$ @Adám I'll change that \$\endgroup\$ – Recursive Co. yesterday
  • \$\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 yesterday
  • \$\begingroup\$ Let us continue this discussion in chat. \$\endgroup\$ – Recursive Co. yesterday
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.

New contributor
smarnav is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
\$\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 yesterday
  • \$\begingroup\$ I recommend checking questions tagged under interpreter on the main site for guidance. \$\endgroup\$ – Razetime yesterday
  • \$\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 17 hours ago
1
\$\begingroup\$

Title: Make a WebCrawler dictionary writer.

Notice: for any text within <> tags, please ignore any .'s. These are simply added so that they are treated as plain text, and not code.

Your goal is to make a program that acts as a regular WebCrawler, but also writes a dictionary using the websites.

You should have a list that is used to store words that have been added called Dictionary, and another list to store website urls called NextSites. You should also have a variable called sites, and a variable called input2. Any other variables or lists are optional.

It should ask for input upon running the program, allowing you to input a website url, from where it will start web crawling. How it asks for input does not matter, so long as you are able to use any website as input. It should then ask for input again, allowing you to chose how many sites it should continue for. The variable much should be set to this input.

Next, it should check the website for any text contained within <.p> tags. It should separate the text contained within the tag at any space contained in it. Next, it will compare each part that has been separated against the current text in the list used to store words. Any part that is not already in the list should be added to it.

After it has checked all of the text within the <.p> tags on the site, it should check for any <.a> tags on the site. Any urls that it finds in the 'href' part of the <.a> tag should be added to the website url list, so long as the url list is not larger than 99 urls. (This means that the maximum amount of urls that can be in the url list at any one time is 100).

When all of the <.a> tags on the site have been checked for urls, it should change the current site it is on to the first site in the list of urls. It should remove this url from the list, and move all other urls to the spot one less than them on the list, so that there is no empty space at the beginning. (eg: If you had 3 urls: 1. google.com , 2. wikipedia.com , 3.stackexchange.com, then after switching to google.com, the list would now be 1. wikipedia.com , 2.stackexchange.com .)

It should then repeat the above steps, and continue to do so until the variables sites and much are the same.

Next, it should should output the entire list of words that it found. The method used to output it does not matter, so long as all words in the list are outputted, with a new line for each word. E.g.: You may have found the words cat, mouse, and food. The output would look as follows:

cat

mouse

food

Scoring: The scoring follows regular code golf rules. The person with the smallest program in bytes wins. However, in the case of a tie between two programs, the program that can webcrawl and write the dictionary starting on wikipedia.com for 100 sites fastest will win.

New contributor
AIDAN GREEN is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
\$\endgroup\$
1
\$\begingroup\$

Implement a cleave function


Think of cleave as the conceptual inverse of map. If map applies a function to each number in a list...

map([1, 2, 3], x -> x * 5) -> [5, 10, 15]

then cleave applies each function in a list to a number.

cleave(5, [x -> x * 2, x -> x - 1, x -> x * x]) -> [10, 4, 25]

More formally, given:

  • \$n\$, an integer, and
  • \$L\$, a list of black box functions with type signature integer -> integer

Apply each function in \$L\$ to \$n\$, collecting the results in a list the same length as \$L\$. (It can be the same list if you want.)

Challenge

Implement the cleave function in your language of choice.

Format

You must accept an integer and a list of black box functions and output a list of integers in any reasonable format. Argument order doesn't matter. The list of functions is flexible. You may accept:

  • A list of black box functions (including a list of function pointers, etc.).
  • A variable number of black box functions as parameters (varargs).
  • A number indicating how many functions to accept.
  • Etc.

Rules

  • Builtins are allowed, but please consider adding a less trivial answer so we can see how cleave might be implemented in your language.
  • Please consider leaving an explanation of your answer(s).
  • Standard loopholes apply.
  • This is , so the code with the fewest bytes (in each language) wins.

Test cases

Note: for simplicity, instead of showing functions like x -> 10 * x, I will show them like 10x and leave it to you to translate these to your language of choice. id(x) refers to a function that returns its input unchanged.

 3, [] -> []
42, [id(x)] -> [42]
 0, [10x, x/2, abs(x), -x] -> [0, 0, 0, 0]
-5, [abs(x), -x, x+10] -> [5, 5, 5]
10, [x*x, x/10, x*x + 2x + 1, 13, x%3 - 3] -> [100, 1, 121, 13, -2]
\$\endgroup\$
11
  • \$\begingroup\$ @Adám You're right. Thanks! \$\endgroup\$ – chunes 9 hours ago
  • \$\begingroup\$ If my language can directly apply a list of functions to a number, giving me a list of results, does that mean a 0-byte answer, or do I have to wrap the application in no-op code? \$\endgroup\$ – Adám 8 hours ago
  • \$\begingroup\$ @Adám I have no idea if there's a precedent for 0-byte answers, but if the mere act of writing a list of functions immediately applies them to some object in your programming language, that's interesting and I would want to see it. Would writing a blurb about builtins/builtin behavior being allowed help? Something like "Builtin functionality is allowed but consider adding a less trivial answer as well."? \$\endgroup\$ – chunes 8 hours ago
  • \$\begingroup\$ Yes, that would help, and asking for non-trivial things is nice. You might also want to ask for people to explain. \$\endgroup\$ – Adám 8 hours ago
  • \$\begingroup\$ @Adám Added, thanks. \$\endgroup\$ – chunes 8 hours ago
  • \$\begingroup\$ While tagged functional-programming, are we still allowed to submit a full program that prompts for \$L\$ and \$n\$? \$\endgroup\$ – Adám 8 hours ago
  • \$\begingroup\$ @Adám I'm not sure. I took a look at some other higher-order function questions for inspiration, for example https://codegolf.stackexchange.com/questions/223881/implement-an-over-function and it uses the language "You may input and output in the most convenient format for your language, and in any convenient method,..." and that seemed to suffice there. \$\endgroup\$ – chunes 8 hours ago
  • \$\begingroup\$ Funny thing is that while you call it a conceptual inverse of map, it is just a map, right? E.g. in JS: (L,n)=>L.map(f=>f(n)) \$\endgroup\$ – Adám 8 hours ago
  • \$\begingroup\$ Indeed, it can be written in terms of map. Does it seem too trivial, you think? Does it need spicing up? \$\endgroup\$ – chunes 8 hours ago
  • \$\begingroup\$ No, I think it is great. \$\endgroup\$ – Adám 8 hours ago
  • \$\begingroup\$ How would one apply a list of functions to a number in something like Python, which is not a functional-programming language \$\endgroup\$ – StackMeter 5 hours ago
1
57 58
59
60 61
113

You must log in to answer this question.

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