567
\$\begingroup\$

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

Sandbox FAQ

Posting

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

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

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

Discussion

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

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

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

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

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

Other

Search the sandbox / Browse your pending proposals

The sandbox works best if you sort posts by active.

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

\$\endgroup\$
0

4480 Answers 4480

1
43 44
45
46 47
150
2
\$\begingroup\$

Fastest approximate square root of a floating point number


In this challenge, you'll approximate the square root of a floating point number, using some basic arithmetic, bitwise, and control flow operators. Your score will take into account the number of operations your code performs, in addition to its accuracy.

Background:

(Expanation of how IEEE-754 double precision floats work)

Opcodes:

(A list of operations, including basic arithmetic, bitwise, and conditionals/jumps)

(Paradigm, probably either based on a finite number of registers or a stack, is yet to be decided)

Task:

Given a 64-bit floating point number, approximate its square root in the fewest number of operations possible. Your score is a sum of the scores for (around 100k) individual test cases, each of which is scored as the following, where \$n_{ops}\$ is the number of operations the program took to complete, \$x\$ is the input, and \$f(x)\$ is the program's output:

$$\frac{{n_{ops}}^2}{\max(\frac{f(x)}{\sqrt{x}},\frac{\sqrt{x}}{f(x)})}$$

(Note that the actual \$\sqrt{x}\$ will be used for scoring, rather than the closest floating point value, meaning that even a maximally accurate submission would still be slightly penalized for inaccuracy)

\$\endgroup\$
7
  • \$\begingroup\$ The question of the fastest floating-point calculation of the square root has been coming up again and again for decades, but is hardly relevant anymore on modern architectures and standard libraries. I have a little doubt whether such a question still makes a lot of sense, since there are already excellent answers, e.g., stackoverflow.com/questions/71336291/… . Everyone who wants to win such a challenge has hardly any other option than to copy the already known methods. \$\endgroup\$ Nov 14, 2022 at 16:56
  • \$\begingroup\$ @HugoPfoertner I disagree. This question will likely encourage answers which use just a few instructions total. Not having to be very accurate, at all, will allow for some really neat bitwise stuff. \$\endgroup\$ Nov 14, 2022 at 17:13
  • \$\begingroup\$ In order for me to be able to imagine anything concrete under the problem statement, you would have to be a bit more specific as to what you want to allow under "basic arithmetic". Without division, for example, it gets tricky. Likewise, you would have to say if you have an instruction to get the exponent of the floating point number. Without such information it is difficult to decide whether this will be a worthwhile task. \$\endgroup\$ Nov 14, 2022 at 17:49
  • \$\begingroup\$ @HugoPfoertner I'm thinking something like this: chat.stackexchange.com/transcript/message/62332621#62332621 \$\endgroup\$ Nov 14, 2022 at 19:28
  • \$\begingroup\$ FYI you can use \left and \right to make the brackets the right size. \$\endgroup\$ Nov 14, 2022 at 21:35
  • \$\begingroup\$ @RadvylfPrograms It is a bit strange when reference is first made to IEEE-754 double precision floats, but then in the task no use is made of the division of the number into mantissa and exponent described there. If you deny direct access to the exponent, then most of the "neat bitwise stuff" mentioned is probably needed to get the exponent information anyway. \$\endgroup\$ Nov 16, 2022 at 21:02
  • \$\begingroup\$ @HugoPfoertner All of the bitwise operations would act on the 64-bit integer representation of the float, not the float itself (just like in assembly/machine code). So to shift the mantissa for example, you could mask that, shift it, and xor the old sign and exponent back in. I considered adding operations which acted specifically on either the mantissa or exponent, but that seemed like it would make it too easy. \$\endgroup\$ Nov 17, 2022 at 3:43
2
\$\begingroup\$

Counting Stripey Bracelets

\$\endgroup\$
2
  • \$\begingroup\$ I suggest adding an example (or examples) which will correspond to the bracelets on the image, which is used to explain the idea of stripes (e.g (6,3,4) and/or (6,3,6)). \$\endgroup\$
    – pajonk
    Nov 2, 2022 at 13:56
  • 1
    \$\begingroup\$ Good call @pajonk, I've aligned a couple of the examples and highlighted which are where in the image. \$\endgroup\$ Nov 2, 2022 at 18:08
2
\$\begingroup\$

Maximum average ord

\$\endgroup\$
2
  • \$\begingroup\$ It may be rounded to any number of decimal places. Can that number be zero (integer)? Can we floor or ceil instead of round? \$\endgroup\$
    – naffetS
    Nov 4, 2022 at 18:58
  • \$\begingroup\$ @Sʨɠɠan - I'll say it must be 1 or more (I've edited that into the post). And, it must be rounded properly (e.g. 1.23 cannot be "rounded" to 1.3). \$\endgroup\$
    – The Thonnu
    Nov 4, 2022 at 19:36
2
\$\begingroup\$

Simple Boolean Algebra Calculator

\$\endgroup\$
2
  • \$\begingroup\$ This is an interesting idea, but the current structure of the post makes it hard to tell what the program is supposed to do. Perhaps you could break down and explain one of the examples in full? \$\endgroup\$
    – Ginger
    Nov 11, 2022 at 14:55
  • \$\begingroup\$ @Ginger Thanks for commenting! I added some explanations above. \$\endgroup\$ Nov 11, 2022 at 15:04
2
\$\begingroup\$

Transform a lattice polygon to minimum diameter by shearing

\$\endgroup\$
8
  • \$\begingroup\$ The math formulas look... strange. Why not MathJax? \$\endgroup\$ Nov 14, 2022 at 8:55
  • \$\begingroup\$ I hope it's better now. I have little practice in writing LaTeX, but I'll give it a try. \$\endgroup\$ Nov 14, 2022 at 9:54
  • \$\begingroup\$ That'll do. Besides, you might want to align the testcases so that it'll be easier to comprehend the inputs and outputs \$\endgroup\$ Nov 14, 2022 at 9:56
  • \$\begingroup\$ The alignment is done. Now the input has to handle spaces between the numbers correctly. \$\endgroup\$ Nov 14, 2022 at 10:27
  • 1
    \$\begingroup\$ We generally discourage cumbersome I/O formats and prefer to let answerers choose what's best for them (within reason). \$\endgroup\$
    – pajonk
    Nov 14, 2022 at 19:16
  • \$\begingroup\$ @pajonk I don't think the proposed output format is cumbersome. I would just like to be able to directly interpret the results of the programs. If someone is unable to separate the coordinates with commas, for example, then the program is of little use to me. \$\endgroup\$ Nov 14, 2022 at 22:53
  • \$\begingroup\$ You may ask to provide code to convert from golfiest representation to your output, but score only the main task. I suspect that in some languages the boilerplate code to handle specific I/O may be as long as the code to solve the task. This makes the challenge less interesting in my opinion. Also, I suggest rephrasing the winnning criteria, as now ("the shortest code able to solve all test cases wins") it is closer to test-battery than code-golf - in code-golf we usually require the code to theoretically handle any case of the specified problem. \$\endgroup\$
    – pajonk
    Nov 15, 2022 at 7:24
  • \$\begingroup\$ @pajonk Thanks for the suggestions! I have now tried to change the task description accordingly. I suppose that it goes without saying that all the specified tests must be passed. \$\endgroup\$ Nov 16, 2022 at 20:40
2
\$\begingroup\$

Where are zeros? Self-describing sequence

Background

A167519: Lexicographically earliest increasing sequence which lists the positions of the zero digits in the sequence.

3, 10, 11, 12, 11000, 11111, 11112, 11113, 11114, 11115, 11116, 11117, 11118, 11119,
11121, 11122, 11123, 11124, 11125, 11126, 11127, 11128, 11129, 11131, 11132, 11133,
11134, 11135, 11136, 11137, 11138, 11139, 11141, 11142, 11143, 11144, ...

If we list the digits, we get

3 1 0 1 1 1 2 1 1 0 0 0 1 1 1 1 1 1 1 1 1 2 1 1 1 1 3 1 1 1 1 4 ...
    ^             ^ ^ ^

The digits at index 3, 10, 11, 12, 11000, ... are zeros, and all the other digits are nonzero.

It looks a bit boring after a few terms. It becomes a bit more interesting if we consider the same sequence in smaller bases:

Base 5

(in base 10)
3, 5, 10, 11, 150, 156, 157, 158, 159, 161, ...

(in base 5)
3, 10, 20, 21, 1100, 1111, 1112, 1113, 1114, 1121, ...

Explanation:

  1. The first term cannot be 1 (the first base-5 digit is 1, not 0) or 2 (the next number would have a leading zero), so it is 3.
  2. The next term cannot be 4 (leading zero), so it must be 5 = 10(5). It satisfies the first term (3rd base-5 digit is 0).
  3. The third term must have at least 2 digits and its 2nd digit is 0. The smallest number that satisfies this is 10 = 20(5).
  4. Another 2-digit number can fit here without causing a leading zero. The smallest such number exceeding 10 is 11 = 21(5).
  5. The next number cannot be 2-digit or 3-digit, so it must have 4 digits, giving 1100(5). We don't have any more zeros for a while, giving a series of zeroless numbers starting with 1111(5).

Base 4

(in base 10)
3, 8, 9, 80, 85, 86, 87, 89, 90, 91, 93, 94, 95, 101, 102, 103, 105, 106, 107,
109, 110, 113, 1344, 16448, 21824, 32833, 34133, 38229, 38230, 38231, ...

(in base 4)
3, 20, 21, 1100, 1111, 1112, 1113, 1121, 1122, 1123, 1131, 1132, 1133, 1211, 1212,
1213, 1221, 1222, 1223, 1231, 1232, 1301, 111000, 10001000, 11111000, 20001001,
20111111, 21111111, 21111112, 21111113, ...
  1. The first term is 3 by the same logic.
  2. The next term cannot be 10(4) due to leading zero, and the next fitting number is 20(4).
  3. The rest goes on by the "long-term logic". The next interesting part comes earlier than in higher bases, so I decided to include it here.

Starting here, the "long-term logic" refers to the following:

  • If the last number has k digits, the next number will also have k digits unless such a number does not exist or it causes a leading zero in the next term.
  • Otherwise, increase the number of digits until the next term won't have a leading zero, and fill the nonzero digits with 1.

Base 3

(in base 10)
4, 6, 10, 12, 19, 22, 24, 111, 121, 122, 124, 125, 130, 131, 133, 134, 148, 149,
151, 152, 157, 158, 160, 161, 202, 283, 1089, 6921, 6925, 9837, 13482, 13486,
16402, 16403, 16405, 16408, 16411, 16412, 16414, 16415, 16429, 16430, 16432,
16433, 16435, ...

(in base 3)
11, 20, 101, 110, 201, 211, 220, 11010, 11111, 11112, 11121, 11122, 11211, 11212,
11221, 11222, 12111, 12112, 12121, 12122, 12211, 12212, 12221, 12222, 21111,
101111, 1111100, 100111100, 100111111, 111111100, 200111100, 200111111, 211111111,
211111112, 211111121, 211111201, 211111211, 211111212, 211111221, 211111222,
211112111, 211112112, 211112121, 211112122, 211112201, ...
  1. The first term cannot be 3 since it is 10(3) but 2 is not in the sequence. Therefore, the first term is 4 = 11(3).
  2. The sequence goes on with the long-term logic.

Base 2

(in base 10)
2, 4, 5, 7, 31, 63, 127, 191, 255, 511, 1021, 1023, 2047, 4095, 8191, 16383, 28671,
32767, ...

(in base 2)
10, 100, 101, 111, 11111, 111111, 1111111, 10111111, 11111111, 111111111,
1111111101, 1111111111, 11111111111, 111111111111, 1111111111111, 11111111111111,
110111111111111, 111111111111111, ...

Determining the initial terms here is particularly tricky.

  1. The first term is 2 = 10(2) because it satisfies the first zero position.
  2. The next term cannot be 3, but 4 = 100(2) works. This also fixes the next two terms 5 = 101(2) and 7 = 111(2).
  3. The next term should be at least 12, but:
    • 12 doesn't work because <1>100 (<x> marks where 0 has to be)
    • 13 doesn't work because 1<1>01
    • 14 doesn't work because 11<1>0
    • 15 doesn't work because 111<1> Therefore the number has at least 5 bits, the first 4 of which must be 1. Then the last bit cannot be 0 either (16 is not in the sequence), so it becomes 31 = 11111(2).
  4. Now the rest follows the long-term logic, except that it continues to grow exponentially. This is because, for every number k, there is only one k-bit number that does not contain 0.

Code used for handcrafting these sequences.

Challenge

Given the base n >= 2, output the sequence generated by the definition of A167519 in base n.

I/O rules apply. You may choose one of the following:

  • Given n, output the terms of the sequence indefinitely;
  • Given n and a 0- or 1-based index k, output the kth term of the sequence n;
  • Given n and a positive integer k, output the first k terms of the sequence n.

You may output the terms in base 10 or base n.

Standard rules apply. The shortest code in bytes wins.

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

How much STAB do I get?

With the new Terastal mechanic in Pokémon Scarlet and Violet, moves can now get a variety of Same Type Attack Bonuses.

This bonus, known as STAB for short, varies depending on the type of the move and the Pokémon's type(s) (Pokémon can have multiple types) and Tera type, but also on whether the Pokémon has the Adaptability ability.

The rules are as follows:

  • If the Pokémon hasn't been Terastallised:
    • If the move is not one of the Pokémon's types then the STAB is 1× (i.e. no bonus)
    • Otherwise if the Pokémon has the Adaptability ability then the STAB is 2×
    • Otherwise the STAB is 1.5×
  • If the Pokémon has been Terastallised into a different type:
    • If the move is neither the Tera type or its regular types then the STAB is 1×
    • Otherwise if the move is the Tera type and the Pokémon has the Adaptability ability then the STAB is 2×
    • Otherwise the STAB is 1.5×
  • If the Pokémon has been Terastallised into one of its regular types:
    • If the move is not one of the Pokémon's types then the STAB is 1×
    • Otherwise if the move is not the Tera type then the STAB is 1.5×
    • Otherwise if the Pokémon has the Adaptability ability then the STAB is 2.25×
    • Otherwise the STAB is 2×

As a table:

Is Terastallised Move has Tera type Move has regular type Adaptability STAB
No No No No
No No No Yes
No No Yes No 1.5×
No No Yes Yes
No Yes No No
No Yes No Yes
No Yes Yes No 1.5×
No Yes Yes Yes
Yes No No No
Yes No No Yes
Yes No Yes No 1.5×
Yes No Yes Yes 1.5×
Yes Yes No No 1.5×
Yes Yes No Yes
Yes Yes Yes No
Yes Yes Yes Yes 2.25×

Your task is to write a program or function that, given a move's type, a Pokémon's Tera type and set of base type(s) as some kind of comparable value (e.g. type names as strings), and its Terastallisation and Adaptability states as a byte-sized flag, outputs the move's resulting STAB. Instead of a seprate Terastallisation flag you can also use a sentinel value for the Tera type to indicate that the Pokémon hasn't been Terastallised. You can optionally take an additional argument which is the set of types that are boosted by Adaptability.

This is , so the shortest program or function that breaks no standard loopholes wins!

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

Santa's Shortest Path Problem

1st-time trying to come up with a challenge. Please provide feedback if this is a nice challenge/if it's doable and/or if anything is unclear.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Thanks for using the Sandbox! What do you mean by "readable format"? I suggest skipping the "readable" as it may be too vague. I also suggest relaxing the output to allow any kind of coordinates, not just letter+number. Finally, I suggest finishing the route at the last village as returning to A1 is unnecessary IMHO (but that just my preference). \$\endgroup\$
    – pajonk
    Dec 7, 2022 at 10:16
  • \$\begingroup\$ @pajonk, thanks for the prompt response. I hope the edit makes for a more approachable challenge. \$\endgroup\$
    – JvdV
    Dec 7, 2022 at 14:34
2
\$\begingroup\$

But can it run Fibonacci?

Cops will write a programming language interpreter (or transpiler or compiler) in 2048 bytes or less. It must be capable of a Fibonacci program which works until limited by integer sizes or some similar restriction (no finite look-up tables or similar, and a reasonable number of fibonacci numbers must be supposed, with 17711 being a reasonable minimum).

Robbers will try to find this fibonacci program.

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

Approximate my Atomic Weight

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

Number program police

This question is related to Counting and so on

Content

With some clever engineering, we now have program that can count like us.

But this is not enough. We are making a math society and society isn't as simple as counting.

There is always some blatant number police who calls you out if you makes a single bit of mistake. Without those police it will never be a complete society.

Task

Make a program that identifies whether a shape is a number shape.

shape

A shape is a Boolean matrix, here represented using spaces and #s:

 ########
 #   ####
 ########

(the above shape is also an example of valid Number shape which represents 0)

Number shape

Basic Number shape:

  #  ###  ###  # #  ###  ###  ###  ###  ###  ###
  #    #    #  # #  #    #      #  # #  # #  # #
  #  ###  ###  ###  ###  ###    #  ###  ###  # #
  #  #      #    #    #  # #    #  # #    #  # #
  #  ###  ###    #  ###  ###    #  ###    #  ###

A basic Number shape can be enlarged, twisted, be longer or shorter, while still being valid, as long as it Resembles the shape:

Is a Number shape of 9:
#####  ######  ###
##  #  #    #  # #
##  #  ######  ###
#####       #  ###
    #       #    #
    #       #    #
            #

Is not a Number shape of 9:

###   #####   ###  ####
# #   # #     # #  # ##
# #   ###     ###  ###
  #     #      #     #
        #      #     #

Acceptable reshape:

Original shape:

   ###               ####
   # #      widen    #  #
   ###     ------->  ####
     #                  #
     #                  #

           extend   ###       shorten   ###
           -------> # #   or            # #
                    ###                 ###
                      #                   #
                      #
                      #



          lengthen  ###
           -------> # #
                    # #
                    ###
                      #
                      #

     enlarge line   ###      ####      ####      ###           ###
           -------> ###  or  # ##  or  ## #  or  # #  but not  # # (this is
                    # #      ####      ####      ###           ###  not 9
                    ###        ##         #      ###           ###  but a 0)
                      #        ##         #        #           ###  
                      #        


You can do 1 or more of those actions on the same digit for any amount of units, for example:

#####
#  ##
#  ##
#####
   ##
   ##
   ##
   ##

Is Widened, extended, enlarged, and lengthened.

The Basic Number shape (or smaller varient of it) should be obtained if you repeatedly remove one out of two consecutive identical rows or columns.

For digits that contain a hole in it (6, 8, 9, 0), the hole should exist for it to be valid (length, wide does not matter).

For U-shaped holes (2, 3, 4, 5, 6), those should have at least one empty byte that resembles a hole:

###  is done by shortening, and then enlarge line.
 ##  
###  Valid.
 ##
###

For numbers that have more than one digit, digits should have at least one spacing between them, and they should not be connected in any way:

#  ###
#    #
#  ###
#  #
   ###
is identified as 12

A Number shape can also start with 0:

### #
# # #
### #

I/O

Basically follows the standard I/O rules:

The input can be request in any convenience format

For example, list the split with newline can be the input.

The output follows the standard output rule

Testcase

Is Number shape: (- is to separate each testcase out)

###
# #
###
------------------
#####
#####
## ##
#####
#####
------------------
### 
# # #
### 
------------------
 ####
 #  #
 ####
 #  #
 ####
------------------
# # # # #

Is not Number shape:

 ##
  #
###
# 
###
------------------
#####
#####
## ##
#####
  ###
------------------
######
# #  #
# ####
# ##
######
------------------
#######
#   # #
### ###
# #   #
###   #
------------------
####
# ##
####
## #
####
------------------
###
  #
 ##
  #
###

Rules

  • No standard loopholes
  • is code golf so shortest police is the best police.

Meta

  • Is the title good?
  • Any tag that suits this question but not included?
  • Is it clear?
  • Extra suggestion would help me out a lot!
\$\endgroup\$
1
2
\$\begingroup\$

Calculate my income tax

\$\endgroup\$
2
  • \$\begingroup\$ If I earn £100,003, does my personal allowance go down by £1 or £1.50? \$\endgroup\$
    – pxeger
    Dec 20, 2022 at 11:49
  • \$\begingroup\$ @pxeger Actually I'll change that. It's up to you which one you want. \$\endgroup\$
    – The Thonnu
    Dec 20, 2022 at 12:01
2
\$\begingroup\$

Find the Prime Signature

\$\endgroup\$
4
  • \$\begingroup\$ I find it a tad confusing that you group inputs with identical signatures together, using ", " but also use "," as thousands separator. \$\endgroup\$
    – Adám
    Dec 27, 2022 at 22:29
  • \$\begingroup\$ @Adám I've updated it to be consistent (removing thousands separator) with the Wikipedia list. I considered thousands separator with "; " as the list separator, but that looked worse (imo) \$\endgroup\$ Dec 27, 2022 at 22:40
  • \$\begingroup\$ Do I understand correctly that outputting a set is not allowed as it has no defined order of elements? \$\endgroup\$
    – pajonk
    Dec 28, 2022 at 9:09
  • \$\begingroup\$ @pajonk Correct. Not only is a set generally implemented without preserving insertion order, a set in computer science can only contain unique items; for example 6 has a prime signature of {1, 1} (in number theory set notation), but a set would only be able to store one of the 1s \$\endgroup\$ Dec 28, 2022 at 9:12
2
\$\begingroup\$

Find the first run of numbers summing to n

Given as input a number n and a sequence of digits 0-9 (which may be taken as a string or a list), find the first contiguous subsequence of digits in the sequence that sums to n and output the start and end indexes. You may use zero- or one-based indexing. If no such subsequence exists, your program may output any constant value.

Examples

These examples use zero-based indexing.

Input: 10 123456789
Output: 0 3
Input: 32 444444444
Output: 0 7
Input: 33 444444444
Output: No solutions
\$\endgroup\$
3
  • \$\begingroup\$ Shouldn't the first test case be 0 4? \$\endgroup\$
    – mousetail
    Jan 9 at 14:48
  • \$\begingroup\$ @mousetail That would be the subsequence 12345, which sums to 15. \$\endgroup\$
    – Ginger
    Jan 9 at 14:53
  • \$\begingroup\$ My bad I assumed exclusive range \$\endgroup\$
    – mousetail
    Jan 9 at 14:53
2
\$\begingroup\$

Estimate the world population, at 5 different levels of accuracy

Your goal is, given a year between 1952 and 2020 to output the world population in that year. However, you must write 5 different expressions. Each program must contain the previous as a substring.

Your first program is 5 bytes, the second 10, then 20, 40 finally a program or function with a maximums size of 80 bytes. Each expression must contain the previous expression as a substring.

For the purpose of this challenge, snippets that take input from global variables are allowed, for all programs except the last. which must be either a complete program or a function.

Scoring

For each year, your score is the product of the proportional square error. In other words:

$$\frac{(X_0 - V)^2 \cdot (X_1 - V)^2 \cdot (X_2 - V)^2 \cdot(X_3 - V)^2 \cdot (X_4 - V)^2}{V^{10}}$$

Where Xn is the estimate your nth program outputs and V is the true population at that year. Your total score is sum of the score of each year.

Lower score is better.

Example

Python, Score \$7.503\cdot10^{59}\$

Note the numbers here are absolute nonsense, I hope most submissions will have a score that can be written in the title without needing scientific notation

5 byte solution:

413e7

10 byte solution

413e7+x**2

20 byte solution

413e7+x**2.46*4.321

40 byte solution

0*413e7+x**2.46*4.321+x**3.718232*8.3e9

80 byte solution

lambda x:0.010*413e7+x**2.46*4.321+x**3.718232*8.3e9+__import__("math").sin(x/2)

Data

Year Population
2020 7,794,798,739
2019 7,713,468,100
2018 7,631,091,040
2017 7,547,858,925
2016 7,464,022,049
2015 7,379,797,139
2014 7,295,290,765
2013 7,210,581,976
2012 7,125,828,059
2011 7,041,194,301
2010 6,956,823,603
2009 6,872,767,093
2008 6,789,088,686
2007 6,705,946,610
2006 6,623,517,833
2005 6,541,907,027
2004 6,461,159,389
2003 6,381,185,114
2002 6,301,773,188
2001 6,222,626,606
2000 6,143,493,823
1999 6,064,239,055
1998 5,984,793,942
1997 5,905,045,788
1996 5,824,891,951
1995 5,744,212,979
1994 5,663,150,427
1993 5,581,597,546
1992 5,498,919,809
1991 5,414,289,444
1990 5,327,231,061
1989 5,237,441,558
1988 5,145,426,008
1987 5,052,522,147
1986 4,960,567,912
1985 4,870,921,740
1984 4,784,011,621
1983 4,699,569,304
1982 4,617,386,542
1981 4,536,996,762
1980 4,458,003,514
1979 4,380,506,100
1978 4,304,533,501
1977 4,229,506,060
1976 4,154,666,864
1975 4,079,480,606
1974 4,003,794,172
1973 3,927,780,238
1972 3,851,650,245
1971 3,775,759,617
1970 3,700,437,046
1969 3,625,680,627
1968 3,551,599,127
1967 3,478,769,962
1966 3,407,922,630
1965 3,339,583,597
1964 3,273,978,338
1963 3,211,001,009
1962 3,150,420,795
1961 3,091,843,507
1960 3,034,949,748
1959 2,979,576,185
1958 2,925,686,705
1957 2,873,306,090
1956 2,822,443,282
1955 2,773,019,936
1954 2,724,846,741
1953 2,677,608,960
1952 2,630,861,562

In JSON format:

{2020: 7794798739, 2019: 7713468100, 2018: 7631091040, 2017: 7547858925, 2016: 7464022049, 2015: 7379797139, 2014: 7295290765, 2013: 7210581976, 2012: 7125828059, 2011: 7041194301, 2010: 6956823603, 2009: 6872767093, 2008: 6789088686, 2007: 6705946610, 2006: 6623517833, 2005: 6541907027, 2004: 6461159389, 2003: 6381185114, 2002: 6301773188, 2001: 6222626606, 2000: 6143493823, 1999: 6064239055, 1998: 5984793942, 1997: 5905045788, 1996: 5824891951, 1995: 5744212979, 1994: 5663150427, 1993: 5581597546, 1992: 5498919809, 1991: 5414289444, 1990: 5327231061, 1989: 5237441558, 1988: 5145426008, 1987: 5052522147, 1986: 4960567912, 1985: 4870921740, 1984: 4784011621, 1983: 4699569304, 1982: 4617386542, 1981: 4536996762, 1980: 4458003514, 1979: 4380506100, 1978: 4304533501, 1977: 4229506060, 1976: 4154666864, 1975: 4079480606, 1974: 4003794172, 1973: 3927780238, 1972: 3851650245, 1971: 3775759617, 1970: 3700437046, 1969: 3625680627, 1968: 3551599127, 1967: 3478769962, 1966: 3407922630, 1965: 3339583597, 1964: 3273978338, 1963: 3211001009, 1962: 3150420795, 1961: 3091843507, 1960: 3034949748, 1959: 2979576185, 1958: 2925686705, 1957: 2873306090, 1956: 2822443282, 1955: 2773019936, 1954: 2724846741, 1953: 2677608960, 1952: 2630861562}
\$\endgroup\$
4
  • \$\begingroup\$ Just some nitpicking: you say 1951 in the question but only supply data back to 1952. Also I believe your 5 byte solution was meant to be 413e9. Also it can be nice to provide a scoring script/link in some common language, not necessary of course. \$\endgroup\$ Jan 12 at 17:23
  • \$\begingroup\$ 413e7 is correct, that comes out to 4,130,000,000. Note the lack of a decimal point in 413 \$\endgroup\$
    – mousetail
    Jan 12 at 17:35
  • 1
    \$\begingroup\$ Oh, I meant because that's what it is in the next four. Something needs to change for it to adhere to the substring rule. \$\endgroup\$ Jan 12 at 17:57
  • \$\begingroup\$ Oops, thanks for pointing that out \$\endgroup\$
    – mousetail
    Jan 12 at 18:04
2
\$\begingroup\$

Cheat at Chess

Inspired by a meme my brother showed me that I cannot find anymore, that looked something like this with the caption "White to play, mate in one:"

The image shows the white rook going over several pieces to end up putting the black king in checkmate (Rxh8). Of course this is not a legal move, but it shows a misunderstanding of chess that I thought could make a fun code challenge.

Task

Given a chess board as input, output the move that puts the king in check, using algebraic notation.

The move must follow the movement rules for a chess piece, but can ignore any pieces standing in its way.

Input format: string matrix or multiline string, each element either empty (empty string, empty matrix element, whatever makes sense) or with a letter - K: King, Q: Queen, R: Rook, B: Bishop, N: Knight, or P: Pawn, uppercase for white and lowercase for black.

The input will always be a board that has a valid solution for this. If there are multiple possible solutions, choose any of them.

Test cases:

r n b - k - - r
p p - p p p - p
- q p - - n p b
- - - - - - - -
- P - - - - - -    -> Rxh8
- - P P - - - -
P Q - - P P P P
R N B - K B N R

More test cases whenever I have time to make them.

This is , so shortest code in bytes wins.

Sandboxing

  • Should the task be "find the move that causes checkmate" instead of just check? I feel like this might make golfing too difficult though.
  • I haven't had time to work on an implementation for solving this yet, so not sure how hard this will be.
  • Any suggested test cases?
\$\endgroup\$
8
  • 1
    \$\begingroup\$ In the example you gave, the king is in check, it's not checkmate because the knight and bishop can block \$\endgroup\$
    – c--
    Jan 16 at 21:03
  • \$\begingroup\$ @c-- Sorry, I suck at chess haha. That's why I need to cheat >.< I'll edit the example. The challenge does say to put the king in check, not checkmate though. \$\endgroup\$
    – noodle man
    Jan 16 at 22:09
  • \$\begingroup\$ can ignore any pieces standing in its way. What about the last position(dst)? \$\endgroup\$
    – l4m2
    Jan 17 at 3:10
  • \$\begingroup\$ Won't F1-B5 is another way to check? \$\endgroup\$
    – tsh
    Jan 17 at 5:31
  • \$\begingroup\$ I don't have time to finish this right now, I'll most likely come back to it in a few days. \$\endgroup\$
    – noodle man
    Jan 18 at 2:13
  • \$\begingroup\$ Can I cheat to capture king? \$\endgroup\$
    – l4m2
    Jan 18 at 10:56
  • 3
    \$\begingroup\$ Note that this cheat method isn't actually cheating - it's what chess anarchists like myself call the Siberian swipe. Note that it can only be performed if the h file pawn hasn't been moved and if the h file rook hasn't been moved as well. \$\endgroup\$
    – lyxal
    Jan 23 at 1:18
  • \$\begingroup\$ @lyxal that was the meme I was thinking of haha. I’ll mention it in the challenge, but I already wrote the challenge so I don’t think I’m going to change it \$\endgroup\$
    – noodle man
    Jan 23 at 15:37
2
\$\begingroup\$

Print the notes of an increasing octave-repeating scale.

\$\endgroup\$
1
  • \$\begingroup\$ Maybe talk some about half steps? \$\endgroup\$ Jan 18 at 8:22
2
\$\begingroup\$

Enable 2char-JsFuck

Write some javascript code that allow using only 2 characters to execute all javascript code.

As I can tell, these are possible chosen set:

f`   [a-zA-Z$_]`
f;   [a-zA-Z$_][;,\n]
f.   [a-zA-Z$_]\.
[]   \[\]

Obviously, the first one is lot easier than the other few. Therefore, different set are not scored together.

Shortest code in each (environment,chosen set) wins.

Sandbox Notes

  • Any other sets? (not counting g+ or other variable name)
  • Why I remember I've sandboxed this idea?
\$\endgroup\$
2
  • \$\begingroup\$ Is the code we write a function or a full program? Can we choose to insert the two-char JSFuck at an arbitrary point of our code, or does it have to be at the end? \$\endgroup\$
    – noodle man
    Jan 23 at 15:43
  • \$\begingroup\$ @Jacob Full program(actually snippet). Must be at the end otherwise it turns into binary converting \$\endgroup\$
    – l4m2
    Jan 23 at 16:18
2
\$\begingroup\$

Output a random value from the last 16 values outputted

Output the numbers 1-16 (or any other set of 16 distinct items). Then, repeatedly, output a random value chosen uniformly from the last 16 items outputted.

After the same item is printed 16 times in a row or more, halt.

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

Roll a painted cube

\$\endgroup\$
2
  • \$\begingroup\$ May we map the directions to values 1-4 (or 0-3)? May we map the colors to values 1-6 (or 0-5)? \$\endgroup\$
    – pajonk
    Dec 12, 2022 at 5:24
  • \$\begingroup\$ Yes, that would be fine also \$\endgroup\$
    – mousetail
    Dec 12, 2022 at 6:20
2
\$\begingroup\$

Avoid the hole

xkcd

Given a continuous black-box function as a input, output a different continuous, differentiable function that never enters the box \$ 1.5 \leq x \leq 2 \wedge 1 \leq y \leq 1.5 \$. Your output must exactly match the input function when \$ x \leq 0 \vee 3 \leq x \$.

The output must be continuous. You may assume the input function is continous, and defined over all \$ \mathbb{R} \$. The function you output must also be defined over all \$ \mathbb{R} \$. Both functions must also be differentiable, meaning their derivative is defined everywhere.

You may optionally take the derivative of the input function as a second input.

You may assume your languages float type has infinite precision and all operations on it are exact.

\$\endgroup\$
9
  • \$\begingroup\$ An issue you are going to run into is that it's basically impossible (mathematically impossible not just really hard) to anything non-trivial with arbitrary real numbers. For example it is very easily undecidable whether the input even passes through the box. This is because an arbitrary real number takes up an infinite amount of memory, so any program that deals with them has to be able to treat them like streams of digits. \$\endgroup\$
    – Wheat Wizard Mod
    Feb 8 at 20:47
  • \$\begingroup\$ Yes, so for the width of the box the output of the original function would need to be entirely ignored, as like you say you don't know about any weired outlier that could happen in that area. In the space around the edges of the box, where this doesn't really matter, you would need to create some kind of interpolation function. \$\endgroup\$
    – mousetail
    Feb 8 at 20:49
  • \$\begingroup\$ There is actually a really simple solution of just creating 2 cubic beziers around the edges of each box \$\endgroup\$
    – mousetail
    Feb 8 at 20:50
  • \$\begingroup\$ I'm not sure if that "simple" solution is actually possible, for two reasons 1) Doing even basic math with arbitrary reals tends to be undecidable (addition is undecidable for example) 2) determining the derivative of a black box function is like super impossible, even with some pretty hefty assumptions. \$\endgroup\$
    – Wheat Wizard Mod
    Feb 8 at 20:54
  • 1
    \$\begingroup\$ I agree. This might be do-able if you limit it from a black-box function to a Polynomial with known values instead, although that may make it trivial \$\endgroup\$
    – ATaco
    Feb 8 at 21:00
  • \$\begingroup\$ @WheatWizard that's why you can take the derivative as a second input \$\endgroup\$
    – mousetail
    Feb 9 at 5:16
  • \$\begingroup\$ Also your solution just needs to theoretically work, ignoring numerical limits like always \$\endgroup\$
    – mousetail
    Feb 9 at 5:20
  • 1
    \$\begingroup\$ Hole on your picture displaces with your word \$\endgroup\$
    – l4m2
    Feb 10 at 8:28
  • \$\begingroup\$ @l4m2 sorry what do you mean? \$\endgroup\$
    – mousetail
    Feb 10 at 8:29
2
\$\begingroup\$

Time to shortest transposition

\$\endgroup\$
7
  • \$\begingroup\$ Is the answer 13:14? \$\endgroup\$
    – Seggan
    Feb 9 at 16:18
  • 1
    \$\begingroup\$ Why 14:21 not 413? \$\endgroup\$
    – l4m2
    Feb 9 at 17:36
  • \$\begingroup\$ Once you fix that, suggested test case: 23:41 -> Nil \$\endgroup\$
    – The Thonnu
    Feb 9 at 18:51
  • 1
    \$\begingroup\$ 413 is not a valid 24-hour string. Note that when you shift around the numbers, you must pick the time string that comes after the input and is a valid 24-hour string \$\endgroup\$ Feb 10 at 2:31
  • \$\begingroup\$ In this case, the transpositions are 12:41, 11:24, 11:42, and 14:12. Since all of these come before 14:21 , they cannot be picked. \$\endgroup\$ Feb 10 at 2:32
  • \$\begingroup\$ So the time must be in the same day? \$\endgroup\$
    – mousetail
    Feb 10 at 8:21
  • \$\begingroup\$ Technically, yes. \$\endgroup\$ Feb 10 at 8:31
2
\$\begingroup\$

Generate an emoticon

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

Ptolemy's table of chords

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

Print all pandigital numbers

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

Split some points

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

Primes with Distinct Prime Digits

\$\endgroup\$
7
  • \$\begingroup\$ This challenge seems a bit boring, also please use the default sequence rules \$\endgroup\$
    – noodle man
    Feb 12 at 0:55
  • \$\begingroup\$ @Jacob I thought I did follow the default sequence rules. Also, could you clarify what exactly makes the challenge boring? \$\endgroup\$
    – Bob th
    Feb 13 at 2:34
  • \$\begingroup\$ The default sequence rules state that the submission may either output the nth term, the first n terms, or the entire sequence. I changed my mind about the challenge being boring, there are likely a few interesting ways to solve this \$\endgroup\$
    – noodle man
    Feb 13 at 2:46
  • 1
    \$\begingroup\$ @Jacob Oh, I see, I misread the sequence page; it says the answers may use "one of the following", not "any of the following", so I got confused. Thanks for pointing that out \$\endgroup\$
    – Bob th
    Feb 13 at 2:50
  • \$\begingroup\$ Is infinite loop allowed if choosing to output whole sequence, like other sequence? \$\endgroup\$
    – l4m2
    Feb 14 at 4:57
  • \$\begingroup\$ @l4m2 it's not an infinite sequence, though. Could you elaborate on what would be printed in an infinite loop? \$\endgroup\$
    – Bob th
    Feb 14 at 19:25
  • \$\begingroup\$ Print nothing lol \$\endgroup\$
    – l4m2
    Feb 15 at 0:25
2
\$\begingroup\$

Generate a permutation from the high-water marks

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

Convert imperative code to functional

The imperative language

We'll consider 2 very simple languages, first a imperative one. The imperative language is a list of statements. Statements can be one of these 4 expressions:

[literal] // a literal
[variable] // a variable
([expression] # [expression]) // Some arbitrary operator
([expression] $ [expression]) // Some different arbitrary operator

Or one of these 3 statements:

[variable] = [expression] // assignment
while [expression] { [statement]* } // looping
if [expression] { [statement] } // condition

The last line is guaranteed to be a expression and it's value is the output of the entire program.

Note: Syntax isn't important here. You can take input as any arbitrary tree like structure. There is also no fixed format for variables, you can use any arbitrary infinite series to represent variables, and the same for literals. As long as all 6 values are distinct, you are good to go.

For example, take this code:

a=(5 # 2)
a=(a $ 7)
8 # a

This program would output 8. You could represent this like this:

[
    {op:'=', left:0, right:{op:'#',left:5,right:2},
    {op:'=', left:0, right:{op:'$',left:{op:'var',var:0},right:7},
    {op:'#',left:8,right:{op:'var',var:0}}
]

Or even more concisely [[4,0,[2,[0,5],[0,2]]],[4,0,[3,[1,0],[0,7]]],[2,[0,8],[1,0]]]. Where the first number represents the index of the expression type and the rest the arguments.

The operators are guarenteed to be saf

The functional language

The functional language has the following builtins:

[literal]         // a literal
[variable]        // a variable
(# [expr] [expr]) // one arbitrary operation, you don't need to worry about what it does
($ [expr] [expr]) // another arbitrary operation
(? [expr] [expr] [expr]) // if expression is truthy, return the first expression, otherwise the second
(lambda [variable]* [expr]) // define a function
([expr] [expr]*) // call a function with the given arguments

Again, you can use any built in tree-like structure to represent this, and use any data type with a potentially infinite number of items to represent variables and literals.

Your task

Your task is to convert some code in the imperative language to the functional equivalent. For example:

a=12
b=15
if b # a {
   b = b $ 12
}
c=(a # 12) $ (a # 18)
while a $ (a # b) {
    a = a # (c $ b)
}
a

Could be converted to:

(
   (lambda a:
       (lambda b:
           (lambda b:
                (lambda c:
                    (lambda y:
                        (
                            y,
                            lambda q: (
                                  lambda a: (
                                       lambda a: a,
                                       (? ($ a ($ a b)) (q (# a ($ c b)))) a)
                                  )
                            ), a)
                        )
                    ),
                    lambda a: (a a)
                ),
                ($ (# a 12) (# b 18))
           ),
           ((? (# b a) ($ b 12) b)
       ),
       15
   )),
   12
)

This is code golf, shortest code wins. Size of the output is irrelevant, specifically you can just repeat the y combinator every loop.

\$\endgroup\$
1
  • 3
    \$\begingroup\$ Could you explain in the challenge how to convert imperative to functional? In addition, a worked out simple example would be helpful. \$\endgroup\$
    – pajonk
    Mar 10 at 7:43
2
\$\begingroup\$

All languages have their strengths, right? (Cops' thread)

This is the cops' thread of a challenge. The robbers' thread can be found [here] (sandbox note: will add the link when posting)

The main idea of this challenge is whether there are languages which are always better than some other ones for .

As a cop, you will choose two languages, and claim that one of them (let's call it A) will always be better for code golf than the other (let's call it B). You post starts as uncracked.

Robbers have two avenues to crack your post:

  • If they think your claim is wrong, they can crack it by giving a code golf task and a solution to it in B.
  • If they think your claim is right, they can crack it by proving it.

If your post was cracked by a counterexample, you can counter the crack by giving a solution in A which is shorter than the given solution in B. This invalidates their crack, and makes your post uncracked once again. Robbers can now attempt to crack it again.

If your post was cracked by a proof you can't counter it (assuming the proof is correct).

Scoring

Each cop gains 1 point for every uncracked post, and loses 1 point for every cracked post. The goal is to get as many points as possible.

Rules

  • Both languages must be freely available.
  • Your claim must not be implied by transitivity from existing uncracked posts.
  • The robbers' tasks must disallow all standard loopholes, and allow all standard I/O methods.
  • The robbers' tasks must only have textual I/O.

All languages have their strengths, right? (Robbers' thread)

This is the robbers' thread of a challenge. The cops' thread can be found [here] (sandbox note: will add the link when posting)

The main idea of this challenge is whether there are languages which are always better than some other ones for .

As a robber, you have two ways to crack a claim:

  • If you think the claim is wrong, you can crack it by giving a code golf task and a solution to it in the language the claim states is worse.
    • This crack can be countered by the cop finding a shorter solution in the language they claim is better.
  • If you think the claim is right, you can crack it by proving it.
    • This crack can't be countered as long as the proof is correct.

Scoring

Each robber gains 1 point for every uncountered robbers post, and loses 1 point for every countered robbers post. The goal is to get as many points as possible.

Rules

Sandbox Questions

Does this format, where a cop claims something, a robber tries to refute/prove it, and the cop tries to defend it, seem interesting? Is it on-topic? What tags are appropriate? I haven't seen it before.

How should I disallow some types of builtins? I want to avoid the challenge turning to "find a builtin the other language doesn't have". In particular, if eval is allowed then the challenge of "Evaluate this language" will almost always be a universal robbers solution. However, I'm not sure how those builtins can be classified.

How should I prevent "output this specific error string" as a universal robbers solution?

\$\endgroup\$
2
  • 1
    \$\begingroup\$ I feel this might be too easy when considering languages that are very verbose but also have a very limited set of builtin like shakesphere. I'd suggest cops would need to have a challenge in mind that would crack it, to ensure it's actually possible, then reveal it after some time \$\endgroup\$
    – mousetail
    Mar 6 at 11:44
  • 1
    \$\begingroup\$ @mousetail I thought about that, but for a hard challenge the solution for the problem in the two languages will be close in length, so it's quite possible the cop will miss some optimization which turns their proposed counterexample false. Additionally, this changes the challenge from finding pairs of languages with one strictly better to finding pairs of languages with very similar code-golf performances, which while an interesting challenge isn't what I want. I'd prefer to prevent trivial submissions in some other way, for example by allowing a proof of the cop's claim as a crack. \$\endgroup\$ Mar 6 at 11:59
1
43 44
45
46 47
150

You must log in to answer this question.

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