574
\$\begingroup\$

This "sandbox" is a place where Code Golf users can get feedback on prospective challenges they wish to post to main. This is useful because writing a clear and fully specified challenge on your first try can be difficult, and there is a much better chance of your challenge being well received if you post it in the sandbox first.

Sandbox FAQ

Posting

To post to the sandbox, scroll to the bottom of this page and click "Answer This Question". Click "OK" when it asks if you really want to add another answer.

Write your challenge just as you would when actually posting it, though you can optionally add a title at the top. You may also add some notes about specific things you would like to clarify before posting it. Other users will help you improve your challenge by rating and discussing it.

When you think your challenge is ready for the public, go ahead and post it, and replace the post here with a link to the challenge and delete the sandbox post.

Discussion

The purpose of the sandbox is to give and receive feedback on posts. If you want to, feel free to give feedback to any posts you see here. Important things to comment about can include:

  • Parts of the challenge you found unclear
  • Comments addressing specific points mentioned in the proposal
  • Problems that could make the challenge uninteresting or unfit for the site

You don't need any qualifications to review sandbox posts. The target audience of most of these challenges is code golfers like you, so anything you find unclear will probably be unclear to others.

If you think one of your posts requires more feedback, but it's been ignored, you can ask for feedback in The Nineteenth Byte. It's not only allowed, but highly recommended! Be patient and try not to nag people though, you might have to ask multiple times.

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

Other

Search the sandbox / Browse your pending proposals

The sandbox works best if you sort posts by active.

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

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

4771 Answers 4771

1
47 48
49
50 51
160
2
\$\begingroup\$

How sorted is this array

Given an array output the minimum number of swaps needed to sort that array

Examples

[1, 2, 3, 4, 5]   ->  0
[1, 3, 2, 4, 5]   ->  1
[5, 4, 3, 2, 1]   ->  2
[1, 3, 4, 5, 2]   ->  3
[2, 3, 4, 5, 1]   ->  4

[3, 5, 1, 11, 7, 3] -> 3

Rules

  • You can assume that the elements of the array are positive integers
  • There might be duplicate elements
  • This is , the shortest code wins

Optional additional requirement

Solve the problem in \$O(n²)\$


Meta

  • Would you consider this challange to be a duplicate? My solution only asks for the number of swaps, which seems to be a much easier task than finding the actual pairs of indices
    These twp challenges I found are very similar but not exactly the same: sorting networks, swaps in bubble sort
\$\endgroup\$
1
  • \$\begingroup\$ Is the input a permutation? If it is the answer is the length of the input minus the number of cycles in the permutation, which seems like it would be a simple modification to an existing question \$\endgroup\$ Commented Sep 11, 2023 at 12:50
2
\$\begingroup\$

Add two rational numbers... esoterically

Objective

Build a binary operator \$\star : \mathbb{Z} \times \mathbb{Z} \to \mathbb{Z}\$ such that, there exists a bijection \$i : \mathbb{Z} \to \mathbb{Q}\$ such that, for every integer \$M\$ and \$N\$, \$M \star N = i^{-1}(i(M) + i(N))\$ holds.

Or in mathematical terms, build a binary operator \$\star\$ that admits a group isomorphism from \$(\mathbb{Z}, \star)\$ to \$(\mathbb{Q}, +)\$.

I/O Format

Flexible. Standard loopholes apply.

Scoring

This is a challenge. The submission whose the asymptotic worst-case time complexity of \$\star\$ is the fastest wins.

The parameter \$n\$ for the time complexity is \$s(M) + s(N)\$, where \$s\$ is defined as follows:

  • \$s(0) = s(-1) = 0\$.
  • For every integer \$D > 0\$, \$s(D) = \lfloor \log_2 D \rfloor + 1\$.
  • For every integer \$D < -1\$, \$s(D) = \lfloor \log_2 (-D-1) \rfloor + 1\$.

The shortness of your code is a tiebreaker. That is, this challenge is secondarily .

Notes and Rules

Note that each bijection \$i\$ induces \$\star\$ uniquely. As such, your submission must specify your choice of \$i\$.

\$\endgroup\$
5
  • 2
    \$\begingroup\$ How would you score built-ins (e.g. for arithmetic operations on integers, int to string, ...)? O(1), the complexity of the languages implementation or the complexity of the best known algorithm? \$\endgroup\$
    – bsoelch
    Commented Sep 20, 2023 at 9:56
  • 1
    \$\begingroup\$ Additionally, I'd suggest having something else other than code-length as the tie breaker, according to Using shortest code as a tie-breaking winning criterion in code-challenge questions \$\endgroup\$ Commented Sep 21, 2023 at 10:52
  • \$\begingroup\$ I thought of an interesting solution, so I removed my comments regarding interesting solutions \$\endgroup\$ Commented Sep 22, 2023 at 11:54
  • \$\begingroup\$ Is O(1) possible on word-RAM model? \$\endgroup\$
    – l4m2
    Commented Oct 8, 2023 at 9:08
  • \$\begingroup\$ @l4m2 it depends on what operators you allow, but O(n) is definitely possible \$\endgroup\$ Commented Oct 8, 2023 at 10:37
2
\$\begingroup\$

Build the smallest self driving Car

Your goal is building the smallest self driving car to get from A to B.

The car is a point mass of 1kg and has a steering wheel to turn left and right, as well as an accelerator pedal to accelerate and brake. Furthermore it is equipped with LIDAR: If 0° is the forward direction, it has a laser every 10° from -90° to +90°. Each of these lasers measures the distance to the next wall. The point A is where the car starts witz zero velocity, pointing north. The destination B is an area where the car has to stop to finish the trip (it can be facing any direction).

Your job is to write a function that is called every second, to steer your car to the B area, without crashing into the wall.

def car(lidar, speed):
    # lidar is an array of 19 floating point numbers
    # speed is an integer
    steering = ... # can be any integer from -3 to 3 and refers to a multiple of 10° (eg -2 means -20°) the car is turned in this step.
    acceleration = ... # can be any of [-1, 0, 1] and refers to the acceleration in `m/s` for the next step
    # first the car is turned by the indicated amount, then the acceleration is added to the current velocity, and then we take a velocity-sized step in the current direction

    return steering, acceleration

The function must be deterministic and a black box function, that is, it can only communicate to the outside world via the inputs arguments and the returned values. The function must be as small as possible (measured in bytes).

If the car crashes into a wall, it will be disqualified.

the car with lidar

Map

the map will need to be determined it will be a closed non-self intersecting polygon. Here is an example but it will be a longer and maybe a little bit more complicated:

enter image description here


Discussion

  • EDIT: I just remembered there was Lab Rat Race: an exercise in genetic algorithms where we did something similar. So maybe it would make sense to take the challenge in a slightly different direction.

  • EDIT2: There is an almost identical challenge in http://janismac.github.io/ControlChallenges/ (go to the menu with the little squares and choose "Vehicle Steering" or "Vehicle Racing"). After playing around with this I think a combination of golfing code and time would be nice.


I'd obviously have to write a controller and come up with a good map that is doable with the velocity/direction constraints. Ideally with some graphical output for the participants to debug their car.

  1. Shall there be multiple maps?
  2. Are the discrete actions a good idea? (And are the ranges any good?) I think for the velocity it is nice so the car can come to a full stop in the target area.
  3. Should the steering be limited when the velocity is higher?
  4. Currently it feels like this will end up as just as a compression challenge i.e. who can hardcode a good sequence of steering inputs. I'm not sure this is all that fun. Are there other challenge modes or variations for this setting that would be more interesting? (The other thing I had in mind was giving the players a fixed code-budget and measuring how far they can get in the map.)
  5. Should the cars be allowed to have some memory between each step? (e.g. limited to a certain number of bits) - after the update from above I think it would make sense to allow some (or maybe even an unlimited) amount of memory between each step. But still it would be nice to have some sort of restriction.
\$\endgroup\$
2
  • \$\begingroup\$ I agree with point 4, I think this is already hard enough and golfing wouldn't make it more interesting. Perhaps a test-battery code-challenge, with the score based on how far the car gets on average? Maybe with hidden tests to prevent overfitting \$\endgroup\$ Commented Sep 13, 2023 at 4:18
  • 1
    \$\begingroup\$ Thanks for the input, yes that would definitely fit. I think I'd like people to overfit in the sense that they do a fixed route. But if we'd make a score based on distance we'd have to impose some other limit or challenge, I think? \$\endgroup\$
    – flawr
    Commented Sep 13, 2023 at 18:44
2
\$\begingroup\$

Transpose a multidimensional array

\$\endgroup\$
2
  • \$\begingroup\$ These kinds of operations are common in libraries/languages for scientific computing, like MATLAB/Octave or in python numpy/pytorch. In these languages you usually find a function like pytorch.org/docs/stable/generated/torch.permute.html or numpy.org/doc/stable/reference/generated/numpy.einsum.html that make it easier to comprehend what dimensions are moved to which other dimensions. Could you maybe phrase your challenge also in terms of these functions? I think it is easier to understand if you explain how the dimensions need to be moved (instead of showing this example). \$\endgroup\$
    – flawr
    Commented Oct 2, 2023 at 9:42
  • \$\begingroup\$ This is also a builtin in BQN. \$\endgroup\$
    – DLosc
    Commented Oct 2, 2023 at 15:42
2
\$\begingroup\$

Golf a LaTeΧ math expression

\$\endgroup\$
3
  • \$\begingroup\$ So, just, remove whitespace before non-alphanumeric? am I missing something here? \$\endgroup\$ Commented Oct 5, 2023 at 10:58
  • \$\begingroup\$ @noodleman It's more complicated, sometimes you can remove {}, and sometimes you can remove a space before an alphanumeric (in a b you can, but not in \a b) \$\endgroup\$ Commented Oct 5, 2023 at 11:19
  • \$\begingroup\$ Definitely add test cases reflecting those behaviours \$\endgroup\$ Commented Oct 5, 2023 at 11:21
2
\$\begingroup\$

Visualise the Euclidean GCD

\$\endgroup\$
1
  • \$\begingroup\$ Again, going to assume code-golf? \$\endgroup\$ Commented Oct 3, 2023 at 10:57
2
\$\begingroup\$

Is this your answer?

NOTE: The scoring criterion for this is currently broken. I'll fix it at some point ™


In this challenge, you must write an answer that distinguishes itself from all previous answers. No matter what input is passed in, it must output one of two consistent values. Your answer must output one value, marked as "truthy", when your answer's code is input, and the other value, marked as "falsy", when the code of at least one previous answer is input. Whichever value it outputs when anything else is input does not matter as long as it outputs one of the two values.

However, you are not only trying to distinguish your answer from others, but also to trick previous answers into thinking your source code is their own and outputting their truthy value.

Your answer's score is \$\frac{\text{byte count} + 2}{\text{tricked answers } \cdot \text{ answers that don't trick you}}\$, where "tricked answers" is the number of previous answers that output their truthy value when given your code, and "answers that don't trick you" is the number of previous answers for which your answer correctly returns the falsy value. Your answer must trick at least one previous answer and not be tricked by at least one previous answer.

For example, if the first answer was the Python program print(1), then a valid second answer would be the JavaScript function x=>x.includes('x'), which checks if the input string contains a x, returning true for itself, false for the singlee previous answer, and either true or false for any input string. As the previous answer prints a 1 when given this code, this would score \$\frac{18+2}{1 \cdot 1} = 20\$.


First answer (will be posted along with the question):

1: Python 3, 8 bytes

print(1)

Try it online!

Outputs 1 for truthy and 0 for falsy.

\$\endgroup\$
14
  • 1
    \$\begingroup\$ So I insta-win by having a 0 byte answer that tricks at least 1 program? \$\endgroup\$
    – lyxal
    Commented Oct 3, 2023 at 23:36
  • \$\begingroup\$ @lyxal Yes, but it'd need to be a zero-byte answer that outputs one of two consistent values no matter what input is provided, so no Vyxal zero-byte cat for you. I don't think a zero-byte answer is possible, and tricking at least one program (the first) is guaranteed. \$\endgroup\$
    – emanresu A
    Commented Oct 3, 2023 at 23:38
  • \$\begingroup\$ Try it Online! \$\endgroup\$
    – lyxal
    Commented Oct 3, 2023 at 23:39
  • \$\begingroup\$ @lyxal Yes, but it also needs to output a different, truthy value when your program is input. \$\endgroup\$
    – emanresu A
    Commented Oct 3, 2023 at 23:40
  • \$\begingroup\$ Try it Online! \$\endgroup\$
    – lyxal
    Commented Oct 3, 2023 at 23:41
  • \$\begingroup\$ @lyxal It needs to output a falsy value when print(1) is input, a truthy value when your source code is input, and one of those two values for any other possible input. \$\endgroup\$
    – emanresu A
    Commented Oct 3, 2023 at 23:42
  • \$\begingroup\$ The first answer doesn't comply with that rule, so it should be considered invalid. \$\endgroup\$
    – lyxal
    Commented Oct 3, 2023 at 23:43
  • \$\begingroup\$ @lyxal The first answer doesn't have to output a falsy value because there are no answers before it which would require outputting said falsy value. \$\endgroup\$
    – emanresu A
    Commented Oct 3, 2023 at 23:44
  • \$\begingroup\$ Let us continue this discussion in chat. \$\endgroup\$
    – lyxal
    Commented Oct 3, 2023 at 23:44
  • \$\begingroup\$ Are duplicate answers (identical source-code as previous answer) allowed? \$\endgroup\$
    – bsoelch
    Commented Oct 4, 2023 at 7:23
  • \$\begingroup\$ @bsoelch ... yes, and that results in one more tricked answer for a slightly lower score... this scoring criterion is annoying. \$\endgroup\$
    – emanresu A
    Commented Oct 4, 2023 at 9:41
  • \$\begingroup\$ Just chipping in to say that i dont think identical answers should be allowed, and that if someone really wants to they should be allowed to tank their score in bytes by just trying really hard not to get tricked :-) it just seems right to me in my opinion so thats all i have to say \$\endgroup\$ Commented Oct 4, 2023 at 16:05
  • \$\begingroup\$ The problem is, people could add an answer that is extremely similar to the previous answer, say with a comment added or two, and under the current scoring system do better \$\endgroup\$
    – emanresu A
    Commented Oct 4, 2023 at 19:00
  • 1
    \$\begingroup\$ A late true detector has ->0 score \$\endgroup\$
    – l4m2
    Commented Oct 8, 2023 at 3:36
2
\$\begingroup\$

Is it a Valid Crossword Grid?

\$\endgroup\$
4
  • \$\begingroup\$ Suggested tags: crossword, grid \$\endgroup\$
    – pajonk
    Commented Jul 27, 2022 at 5:45
  • 3
    \$\begingroup\$ I don't see any clever algorithm for grid generation (that doesn't mean that there isn't one), so the answers are most likely use the brute-force method of generating all grids and checking their crosswordness - why not make it decision-problem then? \$\endgroup\$
    – pajonk
    Commented Jul 27, 2022 at 5:51
  • \$\begingroup\$ @pajonk I've modified the question as per your suggestion, but that makes it very similar to a previously posed question, so I'm unsure about this. I think there are some meaningful shortcuts to make the generation easier. \$\endgroup\$ Commented Jul 27, 2022 at 18:56
  • \$\begingroup\$ if you see shortcuts that make generation easier than brute-force, then ignore my previous comment. \$\endgroup\$
    – pajonk
    Commented Jul 28, 2022 at 9:36
2
\$\begingroup\$

Expected number of rounds for this labeling scheme

\$\endgroup\$
7
  • \$\begingroup\$ If one simulates ALL runs long enough (say up to length \$10^n\$) then it's deterministic and provably returns the correct answer with enough accuracy. Is that something you want? \$\endgroup\$ Commented Oct 19, 2023 at 2:28
  • \$\begingroup\$ @CommandMaster This is something that I would like to prevent if possible, but I am not sure how to go about doing this. The thing is that all these values are exact rational numbers, meaning that if I wanted to, I could require the answer to be (numerator, denominator) pairs and this would prevent simulation answers. But this would be inconvenient for everyone as most languages don't have a built-in fraction type and would have to do extra work to keep track of the numerator and the denominator in the code. \$\endgroup\$
    – Aiden Chow
    Commented Oct 19, 2023 at 3:29
  • \$\begingroup\$ But then by allowing floating point output, I'm allowing for answers like the one you mentioned by just simulating all possibilities up to a certain point, and that isn't what I want. \$\endgroup\$
    – Aiden Chow
    Commented Oct 19, 2023 at 3:29
  • \$\begingroup\$ Perhaps you can require that the algorithm is correct theoretically - if floating-point numbers had infinite accuracy. \$\endgroup\$ Commented Oct 19, 2023 at 3:31
  • \$\begingroup\$ @CommandMaster Is that something that is "observable"? I'm worried about setting any unobservable requirements. \$\endgroup\$
    – Aiden Chow
    Commented Oct 19, 2023 at 3:53
  • \$\begingroup\$ While unobservable requirements are generally discouraged, theoretical correctness is usually an exception. See this meta post, for example \$\endgroup\$ Commented Oct 19, 2023 at 5:02
  • \$\begingroup\$ @CommandMaster I made an edit with what you suggested. Can you check if the wording is correct, or anything wrong with how I said it? \$\endgroup\$
    – Aiden Chow
    Commented Oct 19, 2023 at 5:42
2
\$\begingroup\$

Smallest infinite irreducible program


A typical constraint for challenges, where the goal is to make your code as long as possible, is that your program must be "irreducible". I.e., no subset of characters can be removed from your program with it still being valid. If your code was xyz, none of x, y, z, xy, yz, or xz could do the same task as xyz. This makes it impossible to use comments, string literals, long variable names, or whitespace to pad out your code length, but through some trickery it's possible to design a program which can be made arbitrarily large while still being irreducible.

In this challenge, you'll try to golf the setup for this. Your submission will be a finitely sized "base" program, which is irreducible, and which can be increased to an arbitrarily high size through the addition of substrings at a chosen point (or set of points) in the program.

For example, a base program might look like wx()yz, and continue to function and be irreducible with the insertion of arbitrarily many abs in between the ()s (such as wx(ababab)yz). You can also require substrings to be inserted in multiple places at once (e.g., you might need a 1 to be appended to the program for each ab, so the previous example would look like wx(ababab)yz111).

Scoring:

Your score is the size in bytes of your base program. Your base program must be irreducible on its own; if the insertion of some minimum number of your chosen substring(s) is necessary for this, those must be included in the score.

Task:

Your irreducible program must do any one of the following tasks, of your choice:

  • Truth machine: Take an input. If the input is 0, print 0 and stop. If the input is 1, print 1 indefinitely
  • Arithmetic: Take two inputs, x and y. Print x + y².
  • Constant output: Print the string Good morning!

You may write a program or a function. If you choose to write a function, instead of printing you may return the output. For the truth machine task, you must use an infinite list/iterator if you return a value; you must have some way of getting an arbitrary number of 1s out of the return value in finite time (so you can't just append 1s to a list infinitely, and return the list once the infinite loop would stop).

\$\endgroup\$
5
  • \$\begingroup\$ If your code is xyzw and xz does the job it isn't irreducible, right? \$\endgroup\$ Commented Nov 5, 2023 at 7:37
  • 1
    \$\begingroup\$ I'm fairly sure there isn't an infinite set of strings such that none of them are a subsequence of one another, and this challenge is impossible \$\endgroup\$ Commented Nov 5, 2023 at 7:37
  • 1
    \$\begingroup\$ How can the program continue to be irreducible after inserting characters? The base program can be obtained by removing a subset of characters from the extended program and is also a valid program. \$\endgroup\$
    – bsoelch
    Commented Nov 5, 2023 at 8:49
  • \$\begingroup\$ See Higman's lemma, which says there isn't such an infinite set \$\endgroup\$ Commented Nov 5, 2023 at 19:07
  • \$\begingroup\$ Oh true, I guess I need to change how the insertion works \$\endgroup\$
    – rydwolf
    Commented Nov 6, 2023 at 14:26
2
\$\begingroup\$

Find the fairest partition of a list

\$\endgroup\$
4
  • 1
    \$\begingroup\$ You should clarify that parts don't have to be continuous, currently it has to be deduced from the examples \$\endgroup\$ Commented Oct 30, 2023 at 7:30
  • \$\begingroup\$ Nice challenge. Shouldn’t the result for the fourth one be zero? \$\endgroup\$ Commented Nov 1, 2023 at 23:27
  • \$\begingroup\$ I’d probably also say absolute difference to be clear. \$\endgroup\$ Commented Nov 1, 2023 at 23:30
  • \$\begingroup\$ Are the lists guaranteed to be sorted? If so, may a submission take input sorted in descending order rather than ascending? \$\endgroup\$ Commented Nov 2, 2023 at 22:22
2
\$\begingroup\$

Make Code Printing X Without X

\$\endgroup\$
3
2
\$\begingroup\$

How many twisted corners?

Posted here

\$\endgroup\$
6
  • \$\begingroup\$ suggested testcase: B U2 D2 F => 0, Also, is repeating a move legal? (like U U' or F2 F) \$\endgroup\$
    – Fmbalbuena
    Commented Oct 31, 2023 at 16:55
  • \$\begingroup\$ Is this a parsing challenge or is the input format loose? (e.g. RRR instead of R', RR instead of R2) \$\endgroup\$ Commented Oct 31, 2023 at 23:11
  • \$\begingroup\$ @noodleman good point, I'll redefine the input. \$\endgroup\$
    – math scat
    Commented Nov 2, 2023 at 18:38
  • \$\begingroup\$ You might want to include a worked example with a GIF or something like that. \$\endgroup\$ Commented Nov 2, 2023 at 21:17
  • \$\begingroup\$ @noodleman right, thanks! \$\endgroup\$
    – math scat
    Commented Nov 2, 2023 at 21:19
  • \$\begingroup\$ Is the input guaranteed to have at least one move, or may it be empty? \$\endgroup\$ Commented Nov 2, 2023 at 21:27
2
\$\begingroup\$

Sub-quadratic base conversion

Write a program that converts a positive integer given in base 10 to its base 2 representation.

Your algorithm must run in a complexity lower that \$O(n^2)\$

Rules

  • You can assume that each arithetic operation (sum/multiplication/division/remainder/exponentation) has the running time of the fastest known algorithm, independent of the implementation that is actually used (This includes methods that you wrote yourself).
    • Addition can be done in linear time
    • Multiplcation/division/remainder run in \$ O(n \cdot \log(n))\$ where \$n\$ is the sum of the numbers of bits of the inputs *.
    • Exponentiation \$N^X\$ runs in \$O((n \log x)(\log(n)+\log(\log x)))\$ where \$n\$ and \$x\$ are the numbers of bits in \$N\$ and \$X\$ respectively.
    • If you know faster algorithms please link them in your challenge
  • All built-in base-conversion/library methods for base-conversion are assumed to run in quadratic time in the input, even if the actual implementation is faster
  • (... more rules will follow)


* The \$O(n log (n)) \$ multiplication algorithm assumes that both arguments have the approximately same size, for simplicity you may assume that the same complexity can be archived for arbitrary argument sizes (this may not be true in practice).


Meta

  • Is this a duplicate?
  • Is there interest in a challenge of this form?
  • Is my explanation clear?
\$\endgroup\$
6
  • \$\begingroup\$ This seems like it would be extremely hard for languages with no built-in big integers, because they probably have to implement subquadratic multiplication \$\endgroup\$ Commented Nov 16, 2023 at 0:37
  • \$\begingroup\$ @CommandMaster I clarified that the running time of the multiplication method does not matter \$\endgroup\$
    – bsoelch
    Commented Nov 16, 2023 at 9:51
  • \$\begingroup\$ I see. Does it that apply to multiplying by 10 \$a\$ times, or do you have to calculate \$10^a\$ once and then multiply by it? \$\endgroup\$ Commented Nov 16, 2023 at 10:49
  • \$\begingroup\$ @CommandMaster repeated multiplication by fixed number will be calculated as exponentiation, multiplication until a given condition is satisfied will be calculated as individual multiplications \$\endgroup\$
    – bsoelch
    Commented Nov 17, 2023 at 8:08
  • \$\begingroup\$ Is x in exponent width or value? \$\endgroup\$
    – l4m2
    Commented Nov 19, 2023 at 10:46
  • \$\begingroup\$ Need O(nm) multiplication when m>>n \$\endgroup\$
    – l4m2
    Commented Nov 19, 2023 at 10:50
2
\$\begingroup\$

Compute the Kimberling Sequence

Introduction

The Kimberling Seqeunce is a sequence of sequences. Each sequence is defined in terms of the previous sequence and the previous stage number. At each stage, the numbers from the previous stage are rearranged as follows:

  • For k in [1,...,i], write term i+k and then term i-k.
  • Discard the ith term.
  • Write the remaining terms in order.

We start with all positive integers at stage 0, so the first few rows look like this.

1  2  3  4  5  6  7  8  9 10 11
2  3  4  5  6  7  8  9 10 11 12
4  2  5  6  7  8  9 10 11 12 13
6  2  7  4  8  9 10 11 12 13 14
8  7  9  2 10  6 11 12 13 14 15

At each stage, the number on the diagonal is expelled (it is an open question as to whether every number is eventually expelled).

Challenge

Your program must take a positive integer n as input and output the integer expelled in forming the nth stage of the Kimberling sequence.

Answer the following questions for your readers.

Standard loopholes are forbidden. As this is , shortest program wins.

Example Input and Output

1 -> 1
2 -> 3
3 -> 5
4 -> 4
5 -> 10
6 -> 7
7 -> 15
8 -> 8
9 -> 20
10 -> 9
11 -> 18
12 -> 24
25 -> 2
100 -> 229
1000 -> 394
1001 -> 1274

More test cases from OEIS.

Sandbox Questions

This is my first code golf question, so I'm not sure what to ask other than is there anything that I can improve/need to fix before asking the main site?

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Thanks for using the sandbox! I think your initial explanation is a bit imprecise/incorrect. I think you want to say something like: "at stage k write the value from stage k-1 at index k+1, then index k-1, then write the rest of the sequence from stage k-1 excluding the value at index k" or something (probably format it better, as you have already). As it is, you don't define i or k. Also, I think you should consider the default template for sequence in the tag info (click "more info" to see it). Good luck! \$\endgroup\$ Commented Nov 23, 2023 at 6:14
  • \$\begingroup\$ Thanks, I mostly just copied the definition from the MathWorld link, but I'll make it more understandable. \$\endgroup\$
    – Tbw
    Commented Nov 23, 2023 at 16:15
  • 1
    \$\begingroup\$ Darn. I almost finished submitting this question, and then I saw it was a duplicate of codegolf.stackexchange.com/questions/67542/… \$\endgroup\$
    – Tbw
    Commented Nov 24, 2023 at 21:51
2
\$\begingroup\$

Implement the "Wolontis" Function

While looking at old ALGOL documentation with someone else on Discord, this "famous" function popped up:

An image claiming to show "the famous Wolontis Function"

Although it’s mentioned in other language manuals of the era, such as Runcible and FORTRAN, we haven't managed to find any references to this function online — so let's change that.

The expression in the image is \$\frac{\sin x}{\sqrt{1-e^{-x^3}}}\$. Your task is to implement this function for positive inputs, as its value is undefined at \$x=0\$ and imaginary for negative \$x\$. Floating-point inaccuracies are permitted.


Sandbox questions

  • How accurate/precise should answers be required to be?
  • What other tags should I add?
  • Any other information I should include? e.g. would a graph be helpful?
\$\endgroup\$
1
  • 2
    \$\begingroup\$ there's references to it here and here. I think 'famous' means famous to 1950s mathematicians ^^ \$\endgroup\$
    – guest4308
    Commented Nov 22, 2023 at 9:58
2
\$\begingroup\$

Read a French number

We have Spell out numbers in French, Translate numbers to French and Telling time in French, but we were missing the reverse:

Challenge

Read a French expression, and output the corresponding integer.

Input

In true French, a French number expression is a list of French words separated by whitespace, and possibly by hyphens "-" and the word "et", such as "sept mille deux cent vingt-et-un".

For this challenge, you do not need to care about hyphens and the word "et": you are free to take input as a whitespace-separated string of words such as "sept mille deux cent vingt un", or as a list of single-word strings such as ["sept", "mille", "deux", "cent", "vingt", "un"].

All French words will be taken from the following list:

        'zero': 0, 'un': 1, 'deux': 2, 'trois': 3, 'quatre': 4,
        'cinq': 5, 'six': 6, 'sept': 7, 'huit': 8, 'neuf': 9,
        'dix': 10, 'onze': 11, 'douze': 12, 'treize': 13,
        'quatorze': 14, 'quinze': 15, 'seize': 16,
        'vingt': 20, 'trente': 30, 'quarante': 40, 'cinquante': 50,
        'soixante': 60,
        'cent': 100, 'mille': 1000, 'million': 1000000, 'milliard': 1000000000

Note that in correct French, "million" and "milliard" must take an -s in plural form, but for this challenge you do not need to care about that. "zéro" also must take an accent on the "e", but you do not need to care about that. "un" must be "une" in feminine form, but you do not need to care about that.

Also note that if we simply added "septante: 70, octante: 80, huitante: 80, nonante: 90" then we would be able to handle Belgian French and Swiss French, but for this challenge you do not need to care about that.

Output

Output one integer, corresponding to the French input number.

Conversion rule

Once the sequence of French words has been converted into a sequence of numbers, this sequence can be converted into one number by identifying the largest number in the sequence, then using one simple recurrence rule:

convert(left, largest, right) = convert(left) * largest + convert(right)

with of course the special cases that convert(empty left sequence) = 1 and convert(empty right sequence) = 0.

For instance:

"sept mille deux cent vingt un"
[7    1000  2    100  20    1]
[7] * 1000 + [2 100 20 1]
7000 + ([2] * 100 + [20 1])
7000 + (200 + ([] * 20 + [1]))
7221

Test cases

"sept mille deux cent vingt un"     ->  7221
"soixante douze cent vingt un"      ->  7221
"dix sept cent quatre vingt neuf"   ->  1789
"mille sept cent quatre vingt neuf" ->  1789
"trois mille neuf cent quatre vingt dix sept" -> 3997
"cent mille milliard cent trente sept million trois cent quatre vingt quinze mille huit cent cinq"
    ->  100_000_137_395_805
"zero"                 -> 0
"un milliard dix sept" -> 1_000_000_017
"mille"                -> 1000

Rules

  • This is code-golf, the shortest code in bytes wins!
\$\endgroup\$
2
\$\begingroup\$

Generate the snowflake pattern sequence

A snowflake figure looks like this:

1,2,4,8

A few rules govern how many points each ring can have.

  • The innermost ring must have between 1 and 8 inclusive points.
  • Every other ring must have between 2 and 8 inclusive points.
  • To ensure symmetry, every ring must have a integer multiple times as many points as the next smallest ring.

Valid snowflake sequences, from the smallest to the largest ring, include: 1,2,2,6, 2,4,8, 3,6, 8,8,8,8,8, etc.

Your task is, given a number, output the best configuration of rings.

If there are multiple solutions, find the most aesthetically pleasing one as follows:

  • The one with the most unique ring sizes
  • Unique ring sizes being equal, the one with the biggest smallest ring. Eg. 2,6 is better than 1,7.
  • If the unique ring sizes and smallest ring are both equal, output the one with the smallest total number of rings.

If there are still multiple options, you may choose one arbitrarily.

Test Cases

Number Pattern Image
1 1 1
2 2 2
3 1,2 1,2
4 1,3 1,3
5 1,4 1,4
6 2,4 2,4
7 1,2,4 1,2,4
8 2,6 2,6
9 1,2,6 1,2,6
10 1,3,6 1,3,6
11 1,2,8 1,2,8
12 4,8 4,8
13 1,4,8 1,4,8

1 2 1,2 1,3 1,4 2,4 1,2,4 2,6 1,2,6 1,3,6 1,2,8 4,8 1,4,8

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

Parse nested absolute values

Part1

Part2

\$\endgroup\$
1
  • 4
    \$\begingroup\$ Implicit products would definitely lead to some obvious ambiguous cases like |1|2|3|. I notice that you do account for ambiguity in the spec; are there any such cases without implicit products? One way or another, the solutions would be radically different, since you can’t just assign a parenthesis direction based on local context. \$\endgroup\$ Commented Dec 1, 2023 at 17:53
2
\$\begingroup\$

Implement a simple stack language

\$\endgroup\$
10
  • 3
    \$\begingroup\$ Do you plan to prevent stack based languages just eval'ing it? Also, what's the Winning Criteria..? code-golf? \$\endgroup\$
    – ATaco
    Commented Dec 1, 2023 at 1:48
  • \$\begingroup\$ @ATaco No, I think eval for stack languages would still be interesting. How am I supposed to mark the winning criteria? \$\endgroup\$
    – RubenVerg
    Commented Dec 1, 2023 at 6:08
  • \$\begingroup\$ I suggest to represent the stack in a left=bottom/right=top manner, like it is typically done in every stack based language I know. \$\endgroup\$
    – Philippos
    Commented Dec 1, 2023 at 6:20
  • \$\begingroup\$ »and push them in the opposite order (swap)« strictly thought, they are not pushed in the opposite order: A swap pushes the value first that was popped first. But I doubt this will be misunderstood by anyone. (-; \$\endgroup\$
    – Philippos
    Commented Dec 1, 2023 at 13:51
  • \$\begingroup\$ @Philippos would "and push them so that they are in opposite order" be better? woudln't change it to "and push them in the same order" which would definitely make it more confusing \$\endgroup\$
    – RubenVerg
    Commented Dec 1, 2023 at 13:53
  • \$\begingroup\$ Agreed! »opposite order« is the clearest. \$\endgroup\$
    – Philippos
    Commented Dec 1, 2023 at 17:27
  • \$\begingroup\$ I am definitely trying this in Trilangle if/when it's posted. Maybe give me a couple weeks because it's exam week, but \$\endgroup\$
    – Bbrk24
    Commented Dec 2, 2023 at 3:15
  • \$\begingroup\$ Would it be allowed to only support integers in some bounded interval (e.g. 32-bit integers), or do you require support for arbitrary size integers? Is the integer format required to be decimal or would unary integers be allowed? \$\endgroup\$
    – bsoelch
    Commented Dec 2, 2023 at 15:20
  • \$\begingroup\$ Can the instructions be represented as a sequence of smaller instructions? Can I for example have an an instruction # that pops two numbers and pushes the sum and difference and then define addition as #drop and subtraction as # swap drop or do all operations have to be atomic \$\endgroup\$
    – bsoelch
    Commented Dec 2, 2023 at 16:04
  • \$\begingroup\$ @bsoelch Bounded integers are fine (but maybe specify if it's a very low bound), and you can input them in whatever way you like. Instructions can be not atomic as long as they're deterministic. \$\endgroup\$
    – RubenVerg
    Commented Dec 2, 2023 at 16:45
2
\$\begingroup\$

Simulate a Plant Cell

Now posted! (old drafts still in version history)

\$\endgroup\$
2
  • \$\begingroup\$ The challenge seems good, but the way it's explained/presented could use some improvement. The C6H12O6+6O2 part should be in any order. \$\endgroup\$
    – Jakav
    Commented Dec 14, 2023 at 22:43
  • \$\begingroup\$ Done and posted \$\endgroup\$
    – W D
    Commented Dec 14, 2023 at 22:54
2
\$\begingroup\$

Generate an efficient A* heuristic

A* is a pathfinding algorithm that can be much faster than Dijkstra's, but it requires a heuristic. The heuristic is a function that takes as input one point of the graph, and the destination, then gives a lower bound for the distance between the two points.

The closer the heuristic is to the true shortest distance, the faster the algorithm runs. But if the heuristic ever overestimates, the algorithm is no longer guaranteed to find the shortest path.

Your task is to generate a good heuristic for a given graph. You must submit 2 functions:

  • The first functions must run in at most O(n^3) time. Given a graph, output up to three 64-bit floating point numbers per point.
  • The second function must run in O(1). Given two points on the graph, output a lower bound for the distance between the two points.

Edges of the graph are guaranteed to have positive, non-zero, integer lengths.

Scoring

Your score is the sum of the differences of your heuristic and the true shortest distance on the (yet to be decided) large graph below. The smallest score wins.

\$\endgroup\$
6
  • \$\begingroup\$ I think you meant that if it overestimates the algorithm might not find the shortest path, not underestimate \$\endgroup\$ Commented Dec 20, 2023 at 4:13
  • \$\begingroup\$ Do you want answers to optimize for the particular graph you give, or work well for some general class? \$\endgroup\$ Commented Dec 20, 2023 at 4:19
  • \$\begingroup\$ @CommandMaster I'm looking for a general algorithm but the test case will be just one large graph, which will be the street grid for a real life city, distances scaled by the speed limit. Hard coding the results for that specific graph is a standard loophole but using patterns that may be present is encouraged \$\endgroup\$
    – mousetail
    Commented Dec 20, 2023 at 6:17
  • \$\begingroup\$ Does the second function only get the numbers for the two points, or can it access the entire graph (and just has to use \$O(1)\$ time)? \$\endgroup\$ Commented Dec 22, 2023 at 4:08
  • \$\begingroup\$ If you take a real life city, does it mean the graph is planer? \$\endgroup\$ Commented Dec 22, 2023 at 4:13
  • \$\begingroup\$ I think it would be better to look at the ratio, not the difference, between the heuristic and the true distance. I also think allowing \$O(\log^k(n))\$ time might allow more interesting solutions \$\endgroup\$ Commented Dec 22, 2023 at 4:16
2
\$\begingroup\$

ASCII flood fill

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

This question was marked as posted and is therefore not shown - you can see the revision history for details.

\$\endgroup\$
8
  • 2
    \$\begingroup\$ Can I use random simulation? If so, how many trials are required? Can I output as a floating point number or a fraction? If you think that 0, 00 => 100% should be handled, you may want 00, 0 => 0% too. \$\endgroup\$
    – Bubbler
    Commented Dec 26, 2023 at 6:26
  • \$\begingroup\$ @Bubbler No typical random. Output flexible. I don't place both X,Y and Y,X in the list \$\endgroup\$
    – l4m2
    Commented Dec 26, 2023 at 6:32
  • 1
    \$\begingroup\$ Suggested test cases: 000, 101 => 41.7% (5/12), 000, 011 => 40% (2/5), 111, 1001 => 57.1% (4/7) \$\endgroup\$
    – Bubbler
    Commented Dec 26, 2023 at 6:37
  • \$\begingroup\$ This was going to be Part 2 of my proposal. \$\endgroup\$
    – Neil
    Commented Jan 5 at 1:22
  • 1
    \$\begingroup\$ An interesting test case is 111, 0011; 111 is expected to appear after 14 bits while 0011 is expected to appear after 16 bits... but before 111! \$\endgroup\$
    – Neil
    Commented Jan 5 at 1:27
  • \$\begingroup\$ @Neil Is it 5/12? I currently don't have a solver \$\endgroup\$
    – l4m2
    Commented Jan 5 at 3:33
  • \$\begingroup\$ My source gives 7/12 but I assume that's the probability for 0011. \$\endgroup\$
    – Neil
    Commented Jan 5 at 8:01
  • 1
    \$\begingroup\$ 01, 1 is a valid test case. You have a 1/2 chance of the first bit being 1, in which case 1 will appear first, otherwise 01 will appear first. I think 0, 00 is also a valid test case; 0 always appears first of course. \$\endgroup\$
    – Neil
    Commented Jan 5 at 8:29
2
\$\begingroup\$

How many people are awake?

The following data contains the population of each timezone in the world (source):

UTC;Population (in thousands)
-11;57
-10;1853
-9.5;8
-9;639
-8;66121
-7;41542
-6;272974
-5;332101
-4.5;31923
-4;77707
-3.5;499
-3;248013
-2;4855
-1;3285
0;285534
+1;857443
+2;609921
+3;496279
+3.5;81889
+4;129089
+4.5;31042
+5;305642
+5.5;1458945
+6;199668
+6.5;50112
+7;439650
+8;1679526
+9;220112
+9.5;1814
+10;29482
+11;5267
+11.5;2
+12;6112
+13;308
+14;11

(for the sake of simplicity, I'm removing +X.75 UTC times from the list)

Assuming that every person in the world wakes up at 8AM and goes to sleep at 12AM (in their local time), how many people are simultaneously awake in the world at a given UTC time?

For example, suppose the given time is 2PM UTC. These are the timezones where the local time at 2PM UTC is between 8AM inclusive and 12AM exclusive:

  -6 08:00 272974
  -5 09:00 332101
-4.5 09:30 31923
  -4 10:00 77707
-3.5 10:30 499
  -3 11:00 248013
  -2 12:00 4855
  -1 13:00 3285
  +0 14:00 285534
  +1 15:00 857443
  +2 16:00 609921
  +3 17:00 496279
+3.5 17:30 81889
  +4 18:00 129089
+4.5 18:30 31042
  +5 19:00 305642
+5.5 19:30 1458945
  +6 20:00 199668
+6.5 20:30 50112
  +7 21:00 439650
  +8 22:00 1679526
  +9 23:00 220112
+9.5 23:30 1814

Now, just add the population of these timezones and output 7818023 (corresponding to ~7.8 billion people).

Input

An UTC time. You may accept one of the following (or all of them):

  • two natural numbers h and m, where 0 ≤ h ≤ 23 and m ∈ {0, 30}
  • a real number r, where 0 ≤ r < 24 and its fractional part can be only 0 or 5 (so r = 23.5 corresponds to 23:30).

Standard I/O applies, so you can accept them as lists, strings, etc. You can even accept m as a boolean value, where 0 means HH:00 and 1 means HH:30.

There are two ways of solving this question: hardcoding the output (since there are only 48 possible inputs) or hardcoding the population data and solving by time arithmetic. However, to make this challenge more interesting, you are allowed to accept the population data as an additional input, so you don't need to hardcode it (thus saving you some bytes) and focusing only on the time arithmetic. So you can read it as additional lines from STDIN or an additional function argument.

Output

How many people are awake at the given time, in thousands.

Test cases

00:00 -> 1942634
00:30 -> 1942634
01:00 -> 2382284
01:30 -> 2432396
02:00 -> 2632064
02:30 -> 4091009
03:00 -> 4396651
03:30 -> 4427693
04:00 -> 4556782
04:30 -> 4638671
05:00 -> 5134950
05:30 -> 5134950
06:00 -> 5744871
06:30 -> 5744871
07:00 -> 6602314
07:30 -> 6602314
08:00 -> 6887848
08:30 -> 6887848
09:00 -> 6891133
09:30 -> 6891133
10:00 -> 6895977
10:30 -> 6895977
11:00 -> 7143682
11:30 -> 7144181
12:00 -> 7215776
12:30 -> 7247697
13:00 -> 7574531
13:30 -> 7574531
14:00 -> 7818023
14:30 -> 7816209
15:00 -> 7637639
15:30 -> 7637639
16:00 -> 6024234
16:30 -> 6024234
17:00 -> 5585223
17:30 -> 5535119
18:00 -> 5337304
18:30 -> 3878359
19:00 -> 3572774
19:30 -> 3541732
20:00 -> 3412643
20:30 -> 3330754
21:00 -> 2834475
21:30 -> 2834475
22:00 -> 2224554
22:30 -> 2224554
23:00 -> 1367111
23:30 -> 1367111

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

Accumulate spare change


In this challenge, given some cash, you'll pay for various items as simply as possible (e.g., one $20 bill for a $17 purchase), and get spare change in return. Your task is to figure out what bills and coins you have after all of the transactions.

Input

Your input will be an initial amount of cash, and a list of zero or more prices of items. You can take these either as dollars with decimal cents ($12.95 = 12.95) or as an integer number of cents ($12.95 = 1295). The prices of items will not exceed the initial amount of cash, and no inputs will contain fractional cents.

Task

First, you'll break down your inputted cash into bills and coins, such that the minimum number of bills/coins are required. The following bills and coins exist:

  • $100
  • $50
  • $20
  • $10
  • $5
  • $1
  • $0.25
  • $0.10
  • $0.05
  • $0.01

Then, for each item purchased, you'll pay using the fewest bills/coins possible, and you'll get change in return. If there are multiple possibilities (e.g., for $45, you could pay with a $50 or a $100), choose the closest one. The only exception is if it's possible to pay using 2-4 of the same bill/coin instead of one that's larger than doing it that way (e.g., $40 as 2 $20 bills, or $1 as 4 $0.25 coins if a $1 bill isn't available, but never 4 $5 bills instead of one $20 bill).

Output

Your output will be the number of each bill and coin remaining after the transactions are complete.

Example

Initial cash: $70

You now have $50 + $20.

Transactions:

  • $11: You pay with $20, and now have $50 + $5 + $1 × 4
  • $0.65: You pay with $1, and now have $50 + $5 + $1 × 3 + $0.25 + $0.10
  • $18.75: You pay with $50, and now have $20 + $10 + $5 + $1 × 2 + $0.25 × 2 + $0.10
  • $25: You pay with $20 + $5, and now have $10 + $1 × 2 + $0.25 × 2 + $0.10
  • $2: You pay with $1 × 2, and now have $10 + $0.25 × 2 + $0.10
  • $4.25: You pay with $10, and now have $5 + $0.25 × 5 + $0.10
  • $1: You pay with $0.25 × 4, and now have $5 + $0.25 + $0.10

Output: $5 + $0.10

\$\endgroup\$
7
  • \$\begingroup\$ I like it a lot because there are a real-world applications. At the final step in the example, you "now have $5 + $0.25 + $0.10". Shouldn't the output be that instead of "$5 + $0.10"? \$\endgroup\$ Commented Jan 19 at 8:59
  • \$\begingroup\$ Possible clarification: "than doing it that way (e.g., $40 as 2 $20 bills, or $1 as 4 $0.25 coins if a $1 bill isn't available, …" → "then use the smaller denomination (e.g., $40 as 2 $20 bills instead of a $50 bill, or $1 as 4 $0.25 coins instead of a $5 bill when a $1 bill isn't available, …" \$\endgroup\$ Commented Jan 19 at 9:02
  • 1
    \$\begingroup\$ Is there a "black box" issue? The output will in many cases be the same regardless of how you handle the transactions. An answer that disregards the rules may still show the expected result for many inputs — e.g., if you just subtract the total transactions from the initial cash and break down the result into allowed denominations, you probably get the right output much of the time. Proposal: make the test case enforce "the only exception" rule. Or ask that an answer should show the intermediate steps. The rules would be easier to refer to if they were in a numbered list. \$\endgroup\$ Commented Jan 19 at 9:04
  • \$\begingroup\$ @doubleunary No, that won't give you the right answer. If I have two $100s and deduct $4.16 a few dozen times, I accumulate tons of pennies. Simply taking the remaining cash and making chasnge with it would give you a totally wrong result, and that's the main point of the challenge \$\endgroup\$
    – rydwolf
    Commented Jan 22 at 15:06
  • \$\begingroup\$ Thats understood, but the "output will in many cases be the same". The test case in the question would be vulnerable to this shortcut. I suggest you modify the test case or request intermediate steps be shown. It is also unclear whether "the only exception" rule would be observed when calculating the denominations you get as change. It would be good to specify explicitly how change is calculated. \$\endgroup\$ Commented Jan 22 at 15:20
  • \$\begingroup\$ @doubleunary I'll have more test cases in the final challenge yeah \$\endgroup\$
    – rydwolf
    Commented Jan 22 at 23:36
  • \$\begingroup\$ Great. I left a couple other comments as well. Perhaps you could edit the sandbox question to its final form so that others could review those edits before you post the question on the main site. \$\endgroup\$ Commented Jan 23 at 8:46
2
\$\begingroup\$

Complete a mystery sequence

Given a sequence of three integers, determine if the sequence is arithmetic (of the form [a, a+d, a+2*d]) or geometric (of the form [a, a*r, a*r^2]) by outputting a fourth term that completes it (a+3*d for arithmetic, a*r^3 for geometric).

Examples:

[1, 2, 3] -> 4 (This is an arithmetic sequence with a difference of 1)
[2, 4, 8] -> 16 (This is a geometric sequence with a ratio 2)
[20, 15, 10] -> 5 (arithmetic sequence, d=-5)
[6, 6, 6] -> 6 (arithmetic with d=0 OR geometric with r=1)
[3, -9, 27] -> -81 (geometric with r=-3)
  • Input is guaranteed to be a valid arithmetic and/or geometric sequence (so you won't have to handle something like [10, 0, 99])
  • For geometric sequences, the ratio between terms is guaranteed to be an integer (ie. [4, 6, 9] would be an invalid input, as the ratio would be 1.5)
  • If a sequence could be either arithmetic or geometric, you may output either term that completes it
  • This is code golf, so aim for the lowest byte count possible!

Notes:

  • I'm not sure if there is a sequence that could be completed two different ways. I'd assume there is, but I haven't found it (although I haven't been looking for very long)
  • Is this too much of an A/B challenge?
  • Title suggestions would be appreciated
\$\endgroup\$
9
  • \$\begingroup\$ I think it's fine. Maybe you can just call it a "mathematical progression" instead of "mystery sequence", to recall AP and GP, but it's just my opinion \$\endgroup\$
    – enzo
    Commented Jan 15 at 17:39
  • \$\begingroup\$ Some minor things to be added: 1. you need to define a scoring method (it usually is [code-golf], fewer bytes better) and 2. maybe change the wording "determine if a sequence is ... and output a fourth term that completes it" to "determine if a sequence is ... by outputting a fourth term that completes it", because the former makes it look like there are two outputs instead of just one. Again, it's just an opinion of mine, it's fine either way \$\endgroup\$
    – enzo
    Commented Jan 15 at 17:42
  • 1
    \$\begingroup\$ You should consider adding some info on how to handle invalid input (e.g. [10, 0, 99]): does the answerers need to validate the input first, outputting some value such as "not valid"? Or they can assume that only valid input will be passed (recommended)? \$\endgroup\$
    – enzo
    Commented Jan 15 at 17:47
  • 1
    \$\begingroup\$ That all said, you made a very interesting question! Keep going. \$\endgroup\$
    – enzo
    Commented Jan 15 at 17:51
  • \$\begingroup\$ @enzo thank you for the input! i'll try and make some of the points clearer \$\endgroup\$
    – nyxbird
    Commented Jan 15 at 18:00
  • \$\begingroup\$ 'd like to see if solution use no branch \$\endgroup\$
    – l4m2
    Commented Jan 15 at 20:05
  • \$\begingroup\$ @l4m2 I almost have one, but it fails on a constant sequence. It might be a bit of an annoying edge-case, seeing as no other sequence can be both geometric and arithmetic \$\endgroup\$ Commented Jan 18 at 3:57
  • \$\begingroup\$ Is [1, 0, 0] (geometric with ratio 0) a possible sequence? \$\endgroup\$ Commented Jan 18 at 4:11
  • \$\begingroup\$ @CommandMaster Saw similar question but three inputs are all positive so they did c%b?2*c-b:c*c/b \$\endgroup\$
    – l4m2
    Commented Jan 18 at 5:03
2
\$\begingroup\$

Fastest bytewise comparisons in 64-bit unsigned integers

[tag:i-don't-know]

  • Create a function that accepts two 64-bit unsigned integers, a and b.

  • Compare each corresponding byte in a and b.

  • If the byte in a is greater than the byte in b in the same position, set the corresponding byte in the output to 255.

  • Otherwise, set the corresponding byte in the output to 0.

  • Return the resulting 64-bit unsigned integer.

Restrictions: the code runs in x86-64, no explict SSEx, AVXx (is ok if the compiler vectorices the result)

Example:

a =      202      141        1      123       61       53       40 251
b =      158       75      185      217        5       90      152 225
a = 11001010 10001101 00000001 01111011 00111101 00110101 00101000 11111011
b = 10011110 01001011 10111001 11011001 00000101 01011010 10011000 11100001
a>b=     255      255        0        0      255        0        0      255
a>b=11111111 11111111 00000000 00000000 11111111 00000000 00000000 11111111
\$\endgroup\$
3
  • \$\begingroup\$ GCC compiles it to a single instruction so it seems bad \$\endgroup\$
    – l4m2
    Commented Jan 29 at 6:48
  • \$\begingroup\$ @l4m2 I see, is SSE. What about non using bytes, like using numbers with 4 bits, 5 bits, 9 bits \$\endgroup\$
    – vakvakvak
    Commented Jan 29 at 18:11
  • \$\begingroup\$ @l4m2 The compiler using SSE means that 64 bit registers (non SIMD) remain unused, so there is an opportunity to extract more performance, although I don't know how to take advantage of it. By the way, without compiler optimizations, your code does unnecessary branches. If you store 0,1 in each byte, C[i] = A[i] > B[i], and at the end return the C*255, you avoid a lot of ?. \$\endgroup\$
    – vakvakvak
    Commented Jan 30 at 8:50
2
\$\begingroup\$

How quickly can you type this string?

Given a string consisting of only printable ascii (letters, numbers, symbols, and spaces), how many keystrokes are required minimally to type out this string from scratch? The allowed keystrokes are:

  • one character from printable ascii for 1 keystroke
  • Ctrl-C (copy) any contiguous substring you have typed so far and replace whatever is currently copied (2 keystrokes) and Ctrl-V (paste) any string in your clipboard (1 keystroke)
  • Backspace the last typed character for 1 keystroke

For example, the string "cbaabcbabaacba" can be typed in 12 keystrokes, which is optimal.

Test Cases

cbaabcbabaacba => 12
ababcc => 6
aaaaaaaaaaaaa => 9

Scoring

This is , so the shortest code wins!

\$\endgroup\$
4
  • \$\begingroup\$ The common definition of key stroke in typing competitions is each key press. Thus you must count <kbd>⇧</kbd> (shift), too. From your description it is unclear what <kbd>Ctrl‑C</kbd> and <kbd>Ctrl‑V</kbd> mean. As a terminal user I’d say ETX and SYN respectively. \$\endgroup\$ Commented Jan 27 at 17:40
  • \$\begingroup\$ Ctrl C and Ctrl V and copy and paste, respectively. \$\endgroup\$ Commented Jan 27 at 17:48
  • \$\begingroup\$ Does Ctrl-C append to the clipboard, or reset it? \$\endgroup\$ Commented Feb 4 at 17:04
  • \$\begingroup\$ It could be either, I'm not sure which would be best for the challenge. \$\endgroup\$ Commented Feb 4 at 20:14
2
\$\begingroup\$

Divmod continuosly until the remainder is 1 or 0, then get the remainder

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Doesn't 88,7 continue 12,4 and 3,0, resulting in 0? \$\endgroup\$
    – Philippos
    Commented Jan 9 at 6:27
  • \$\begingroup\$ @Philippos Thanks! Probably it's a typo. \$\endgroup\$
    – Fmbalbuena
    Commented Jan 10 at 17:30
  • \$\begingroup\$ I liked this challenge! \$\endgroup\$
    – enzo
    Commented Jan 10 at 22:17
  • \$\begingroup\$ @enzo Just a reminder for you: if you see a good post, then you can upvote it, not just commenting it. \$\endgroup\$
    – Fmbalbuena
    Commented Jan 10 at 22:26
1
47 48
49
50 51
160

You must log in to answer this question.

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