532
\$\begingroup\$

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

Sandbox FAQ

Posting

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

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

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

Discussion

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

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

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

If you think one of your posts needs more feedback, but it's been ignored, you can ask for feedback in The Nineteenth Byte. It's not only allowed, but highly recommended!

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

Other

Search the sandbox / Browse your pending proposals

The sandbox works best if you sort posts by active.

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

Get the Sandbox Viewer to view the sandbox more easily!

\$\endgroup\$
0

3469 Answers 3469

1
101 102
103
104 105
116
0
\$\begingroup\$

Golf Code Golf

Given the transcript (what I mean by full transcript is you can access all days of the transcript) of The Nineteenth Byte (the general chat room of code golf), output the full transcript in text form (you only have to include messages, though including other things is allowed. You can use any format as long as it includes the poster and contents of each message. As for people changing names, the way you handle this is up to you, do whichever is shortest). You can access the full transcript in your code, but you cannot access any other external sources.

Since this is code golf, shortest answer wins, and standard loopholes are not allowed

\$\endgroup\$
6
  • \$\begingroup\$ Any feedback would be helpful, since this is my first question idea here. \$\endgroup\$ May 18 at 20:14
  • \$\begingroup\$ By "access the full transcript" what do you mean? There's no single page with the entire transcript, so do you mean all pages that are https://chat.stackexchange.com/transcript/240/<date>? \$\endgroup\$ May 18 at 20:18
  • \$\begingroup\$ @cairdcoinheringaahing yes that is what I meant, I’ll update it now, thank you. \$\endgroup\$ May 18 at 20:21
  • \$\begingroup\$ You'll need to be much more specific about what "text form" is. I'm assuming ...people talk... is not correct, but there are a huge amount of formats, such as person: message and <person> message, not even considering stars, multi-line messages, and users changing their usernames \$\endgroup\$
    – Wezl
    May 18 at 20:29
  • \$\begingroup\$ @Wzl I edited it, is it more clear now or is there something I missed? \$\endgroup\$ May 18 at 20:48
  • \$\begingroup\$ If people are as lazy as me I think they'll just output the raw HTML, since it complies with your constraints (however I think this is a good thing as answers shouldn't be required to parse HTML) \$\endgroup\$
    – Wezl
    May 18 at 20:54
0
\$\begingroup\$

JS onelineriser

JavaScript is a very easy language to onelinerise. All one has to do is insert a semicolon in the correct locations and concatenate every line.

Your task is to write a program that, given a valid JS program, returns a program in one line but is otherwise identical to the input.

Sandbox

  • Clarifications?
  • Any edge cases where any operation other than inserting semicolons is required?
  • Should I include basic JS syntax in the question or provide a link?
\$\endgroup\$
8
  • \$\begingroup\$ I think you should ask the programs to minimize the amount of semicolons placed. \$\endgroup\$
    – Razetime
    May 19 at 2:42
  • 3
    \$\begingroup\$ You definitely need to tell where one needs to insert ;s. \$\endgroup\$
    – Adám
    May 19 at 2:42
  • \$\begingroup\$ I know where one should place semis but I'm not exactly clear whether those semis are necessary. However, I intended this to be a code-golf and not a code-challenge (shortest source) \$\endgroup\$ May 19 at 2:59
  • 2
    \$\begingroup\$ Either the input code is guaranteed to conform to a format where some simple rules can be applied (and you'll have to include these rules in the challenge), or this is going to require a full parsing and syntax analysis -- which is probably far too complicated for a code-golf challenge. \$\endgroup\$
    – Arnauld
    May 19 at 22:14
  • \$\begingroup\$ Can I make the whole input toJson and then feed it to eval? This is valid since ES2019. (And CSP is never a part of ES specification, so that doesn't matter.) \$\endgroup\$
    – tsh
    May 20 at 2:26
  • \$\begingroup\$ Other than inserting semicolons: remove comments. For example, foo(); // do something<line-break>bar();. And remember to keep line break in comments. For example, ()=>{return//<line-break>42;}. More terrible comments: `${()=>{return // this is a comment in template string. <line-break> }}`. Even even terrible edge case: If hashbang proposal landed... \$\endgroup\$
    – tsh
    May 20 at 10:35
  • \$\begingroup\$ @tsh tbh comments aren't "code" so to speak, so I might not actually have those test cases. They would be hell to deal with. \$\endgroup\$ May 20 at 12:46
  • \$\begingroup\$ Can I just warp the code eval? (ok also asked by tsh) \$\endgroup\$
    – l4m2
    Jun 6 at 5:04
0
\$\begingroup\$

Return shortest integer-is-one-of function

Given a number of integers as input, your task is to write a function in JS, which returns a function that checks if an inputted integer is any of those. For example, f([1, 2, 4]) would return a function which would return true if given 1, 2, or 4, and false for any other inputs.

Task:

Your function will take any nonzero number of integers. There will not be any duplicates. This should return either a function, or a string containing the function's code.

The returned function should take a single integer as input. If the input is one of the integers inputted into the original function, it should return a truthy value, and a falsy one otherwise. You may also choose to return any two consistent values, or a consistent value for one possiblity (such as 0 for truthy) and any other value for the other.

Scoring:

This is metagolf, meaning the average of the byte counts of the returned programs for a certain set of inputs will be your score. This set of inputs will consist of:

  • All combinations one to four integers from -10 to 10
  • A random group of 2000 inputs consisting of between five and ten integers between -100 and 100
  • A random group of 1000 inputs consisting of between two and six integers between -10000 and 10000

Optimizing your solution for these specific test cases is disallowed.

Meta

  • I will generate a random list of test cases that will be used for scoring when I post this
  • This is my first time writing a metagolf challenge. Any improvements y'all can think of?
  • I think the number of bytes should be limited in the submitted function (not the generated ones). Suggestions?
  • Too many test cases? Too few?
\$\endgroup\$
7
  • \$\begingroup\$ The option to return a function is only available in JS, I'm assuming? I think you should disallow it, because it's unclear how you measure the size of a function object. \$\endgroup\$ May 21 at 14:22
  • \$\begingroup\$ Maybe you should include test cases with more than six integers? If not, is it allowed for a solution to be optimized for small sizes? (given that it's a a minority in the input space) \$\endgroup\$ May 21 at 14:23
  • \$\begingroup\$ @CommandMaster Oh, I meant to restrict this to JS anyway, wasn't sure if I was going to do that or not. It should be possible in any language with first class functions, though. If I open it up to any language I'll allow returning a string with a program. I'll add some test cases with more items. \$\endgroup\$ May 21 at 14:56
  • \$\begingroup\$ If you return a function, how do you count the byte count of it? \$\endgroup\$ May 21 at 15:14
  • \$\begingroup\$ .toString() (plus it's pretty likely they'll return a string form of the function anyway) \$\endgroup\$ May 21 at 15:28
  • 2
    \$\begingroup\$ .toString() doesn't work for that, for example in function f(i) { var l = a=>a+i return x=>l(x) } f().toString() is just x=>l(x), while that's clearly not the length of the code you want to count (it's also unclear what code you do want to count in such a case) \$\endgroup\$ May 21 at 15:39
  • 1
    \$\begingroup\$ @CommandMaster The functions will need to be independent from the meta function. As in, no closures/accessing variables in the above scope. \$\endgroup\$ May 21 at 15:55
0
\$\begingroup\$

Gabriel's Horn

Posted

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 1. I don't think I've seen anything very similar. 2. Wording looks fine to me (mainly because the image helps a lot) 3. I don't have a strong opinion on this. Either way should be fine. 4. Looks correct. 5. Actually the whole challenge is simply "generate a specific arithmetic progression based on the input", which can be seen as not so interesting, and it has very little to do with Gabriel's Horn itself. Maybe a better title would be "Convince me Gabriel's Horn is possible"? \$\endgroup\$
    – Bubbler
    May 18 at 0:28
0
\$\begingroup\$

Print a BigNum

Your task is to take an arbitrary precision unsigned integer, separated into 7-bit parts (a.k.a. base 128), and output it in base 10.

Rules:

All arithmetic for conversion (excluding basic indexing) must be done only using values \$-2^{15}\le x \le 2^{15}-1\$, or in simpler terms, the range of a signed 16-bit integer.

So for example, something like this in Python would be illegal because a[i]<<i*7 and sum are capable of making a number larger than \$2^{15}-1\$.

lambda a:str(sum(a[i]<<i*7for i in range(len(a))))

Note that you are not required to explicitly use 16-bit integers or mask every time.

Your algorithm must be able to handle numbers of any length, if the resources are available.

Assume all inputs to be valid and the shortest possible length.

Input/output:

The input is a list of numbers, each 0-127, in big or little endian order. You only need to handle up to 224 bits, or 32 base 128 digits, though.

Any reasonable encoding format is fine, as long as all digits are in base 128 and the length is variable. You could do this: 122 bananas, 0 bananas, 20 bananas, 12 apples, I don't care. ULEB128 is allowed if that is easier.

The output can be either an ASCII string or printed to standard output, all in base 10. These must be ordered in big endian (as if you were to print it).

Test cases

Inputs are in little endian order.

[0] -> 0
[1] -> 1
[127] -> 127
[99] -> 99
[0, 0, 1] -> 16384
[127, 0, 0, 0, 127] -> 34091303039
[21, 26, 111, 58] -> 123456789
[1, 78, 123, 61, 102, 86, 17, 32, 74, 41, 114, 79, 1] -> 31415926535897932384626433
[63, 29, 51, 105, 61, 107, 45, 70, 105, 97, 38, 36, 121, 15, 77, 24, 72, 4, 52, 56, 6, 5, 36, 38, 87, 10, 37, 26, 96, 16, 21, 110] -> 23203475113112158652497114438750306328443726354045579113768791363263
\$\endgroup\$
3
  • 3
    \$\begingroup\$ What does "All calculations must use a maximum of 16 bits" exactly mean? Does it mean I can only use 16 bits RAM (including registers)? \$\endgroup\$
    – tsh
    May 20 at 7:54
  • \$\begingroup\$ You probably want to limit the absolute value, otherwise someone could calculate -<ans>, convert it to a string, and remove the first character \$\endgroup\$ May 21 at 7:38
  • \$\begingroup\$ Your algorithm must be able to handle numbers of any length given a large enough buffer. So given a number with >2^15 base-128 digits how to access? \$\endgroup\$
    – l4m2
    May 21 at 8:46
0
\$\begingroup\$

What is the link to your answer?

Goal

Your task is to create a program taking the input that returns the link to your answer. You can find a link to your answer by clicking at the share button on your answer.

Rules

  1. Standard loopholes apply except for searching for the answer online. In fact, you are expected to find the answer online.
  2. You are allowed to use curl or the equivalent, but you are not allowed to use any html parser library or built-in. If you need to import the library, the import declaration does NOT count into the code size as long as you don't rename any identifiers.
  3. You are not allowed to assume the link to your answer. You have to search this page for your submission. If you remove your submission and readd it, and your code breaks, you fail the challenge.
  4. You may still assume your username, the contents of your code, and the programming language/byte count you are using. You are not allowed to assume the description below your code. If you edit the description part of the submission and the code breaks, you fail the challenge. You may assume that the code of each submission is unique and no one copies your submission into another answer.

Scoring

This is so, the shortest answer in bytes wins.

\$\endgroup\$
8
  • 2
    \$\begingroup\$ What prevents me from simply hardcoding my output? \$\endgroup\$ May 20 at 9:24
  • 1
    \$\begingroup\$ @StackMeter If you delete your answer, and post again the same answer, and it break, it fails the challenge. \$\endgroup\$
    – Xwtek
    May 20 at 9:42
  • \$\begingroup\$ thanks for the clarification \$\endgroup\$ May 20 at 9:44
  • \$\begingroup\$ What if my language has built-in for HTML parser (Yes, JavaScript running in browsers , aka. BOM, does have one)? Also, what will happen if I post 2 answers to this question? \$\endgroup\$
    – tsh
    May 20 at 9:46
  • \$\begingroup\$ Can I assume no one else will contain my code as part of an answer/comment? \$\endgroup\$ May 20 at 10:12
  • \$\begingroup\$ @CommandMaster Yes \$\endgroup\$
    – Xwtek
    May 20 at 11:43
  • \$\begingroup\$ @tsh 1. You have to avoid using that built-in, 2. You have to be able to distinguish your submissions. \$\endgroup\$
    – Xwtek
    May 20 at 11:44
  • \$\begingroup\$ [tag: self-referential] ? \$\endgroup\$ May 21 at 5:10
0
\$\begingroup\$

Recreate the Stack Overflow logo

\$\endgroup\$
4
  • \$\begingroup\$ note that the last time this was attempted, it got closed \$\endgroup\$
    – Wezl
    May 21 at 21:54
  • \$\begingroup\$ @Wzl this one is a code-golf question. I don't see why it can be off-topic. \$\endgroup\$ May 21 at 21:55
  • \$\begingroup\$ @Wzl also, the logo for this one is not the one specified from that question you linked. \$\endgroup\$ May 21 at 22:42
  • \$\begingroup\$ I've edited this down to a stub now that it's been posted to save space \$\endgroup\$ May 22 at 23:09
0
\$\begingroup\$

Lowest effort to fill the bucket

There are two man, Paul and Jhon, who can move only along a line; they are real persons so Paul cannot goes to the right of John and Jhon cannot goes to the left of Paul. At time 0 they start at the opposite ends of the line.

Paul |------X1----X2----BUCKET--------X3-----X4-----| Jhon

Their goal is to pickup the balls along the line (denoted as Xn) and fill the bucket in the lowest time. The time needed to go from A to B along the line is equal to the distance from A to B.

For the challenge the balls are 20 and they are located on the line in the following way:

  1. Paul = 0
  2. X1 = 1
  3. X2 = 2
  4. X3 = 3
  5. x4 = X5 = x6 = 5
  6. BUCKET = 8
  7. X7 = 10
  8. X8 = 11
  9. X9 = X10 = X11 = 15
  10. X12 = 20
  11. X13 = X14 = X15 = 22
  12. X16 = X17 = X18 = 28
  13. X19 = 29
  14. X20 = 30
  15. Jhon = 32

Which is the best sequence to reach they goal?

\$\endgroup\$
0
\$\begingroup\$

Verify the Perfect Rectangle

According to Wolfram Mathworld, a perfect rectangle is defined to be a rectangle that can be divided into squares of different side lengths.

In this challenge, we are going to verify whether or not an arrangement is a perfect rectangle.

For the input, there should be at least 3n+2 non-negative integers in it:

  • The first two numbers should show the size of the rectangle.
  • Rest of the 3n numbers should describe n squares.
  • Each square should be described with 3 numbers, which are the side length(>0) and the position(x, y).
  • Position should be consistent throughout the whole input. For example, If the program gets the position of the bottom left corner of the square for one time, it should do that every other time as well.

For the output, the program should print a truthy value if the given arrangement is a perfect rectangle, and a falsey value otherwise.

  • Every squares should be different in side length.
  • There should be neither overlaps nor voids.

Test cases(Note that any input and output formats are acceptable if they are reasonable):

[[32,33],[9,0,0],[8,9,0],[15,17,0],[10,0,9],[1,9,8],[7,10,8],[14,0,19],[4,10,15],[18,14,15]]
-> True #This is the first example in the above link. 

[[65,47],[23,0,0],[17,23,0],[25,40,0],[6,23,17],[11,29,17],[24,0,23],[5,24,23],[19,24,28],[3,40,25],[22,43,25]]
-> True #This is the second example in the above link. 

[[4,4],[2,0,0],[2,2,0],[2,0,2],[1,2,2],[1,3,2],[1,3,3],[1,2,3]]
-> False #There are sqaures of same sizes. 

[[5,5],[3,0,0],[3,2,2],[2,0,3],[1,3,1],[1,3,0],[1,7,0]]
-> False #There are overlaps and voids. 

This is , which means that codes with least bytes win.

\$\endgroup\$
0
0
\$\begingroup\$

Create a non-renewable world

In Minecraft, we can split all possible items into two categories: renewable and non-renewable. All non-renewable items are simply items that are not renewable.

Minecraft worlds are not infinite, meaning that, for some resources, there are only a finite number available. This number is often huge, but it is finite. For example, diamond ore can only be obtained by mining blocks that are generated when the world is created, meaning that there is only a finite number of them in any given world.

However, some resources can be obtained infinitely. For example, as Raid Captains will spawn indefinitely, it is possible to obtain an arbitrarily large number of Ominous Banners, dropped when you kill a Raid Captain. Or, as you can create an infinite water source, and you can get infinite iron by killing Iron Golems, it is possible to get an infinite number of Water Buckets.

Any resource for which it is possible to obtain an arbitrary amount of is called renewable, and there are a lot of these. Any item that is not renewable is non-renewable. There are 40 of these that are obtainable in a classic Minecraft 1.16 survival world. They are:

A Netherite Tool               Netherite Armor                Ancient Debris                 Block of Diamond              
Block of Netherite             Coal Ore                       Cobweb                         Conduit                       
Dead Bush                      Diamond                        Diamond Ore                    Dragon Egg                    
Dragon Head                    Elytra                         Emerald Ore                    Enchanted Golden Apple        
Enchantment Table              Gilded Blackstone              Gold Ore                       Heart of the Sea              
Iron Ore                       Jukebox                        Lapis Lazuli Ore               Large Fern                    
Lava Bucket                    Lodestone                      Nether Gold Ore                Netherite Ingot               
Netherite Scrap                Iron/Gold/Diamond Horse Armor  Pigstep Music Disc             Quartz Ore                    
Redstone Ore                   Shulker Box                    Shulker Shell                  Snout Banner Pattern          
Sponge                         Tall Grass                     Thing Banner Pattern           Wet Sponge              

Your task is to get as many of these 40 items as you can. A Netherite Tool means any of a Netherite Sword, Pickaxe, Axe, Shovel or Hoe. Netherite Armor is any of a Netherite helmet, Chestplate, Leggings or Boots. Iron/Gold/Diamond Horse Armor means that any of these three are acceptable. Leather Horse Amor is not.


You should create a vanilla Minecraft Java Edition world, in 1.16.5, in Hardcore difficulty. You then have until you die in that world to collect all 40 of these items, or as many as you can. You should provide a double chest containing 1 of each of these and nothing else.

In order to verify attempts, you should provide the save file of your world, as a zip file.

The user who gets the most of these 40 items before dying wins. If multiple users get all 40 items, the user who gets all 40 within the fewest in-game days, which can be found by pressing F3.

Rules

  • You may not change the game rules of the world from the default, nor may you change any other option during world creation aside from the seed. To be fully clear, you may change exactly 3 things from the default values after clicking "Create New World":
    • The name of the world
    • The gamemode, switching from Survival to Hardcore
    • Optionally, the seed, which may be any value of your choosing
  • While in the world, you may not enable commands. This is typically done by pausing the game, opening the world to LAN and enabling commands, but, regardless of how you may do it, enabling commands is completely forbidden.
  • You may not use any mods, datapacks or anything else that can modify the default behaviour of the game. Texture packs are perfectly fine.
  • I will be testing and confirming each world submitted, so you should not include anything in the save file that isn't generated by the world. You may not edit the save file directly, only by interacting with the world
  • Items are allowed to be enchanted, renamed or otherwise legitimately modified in game

Additionally, as of snapshot 21w20a, there are 18 new non-renewable resources (ignoring products from Deepslate and Cobbled Deepslate), and Lava Buckets are noew renewable:

Block Of Raw Copper        Block Of Raw Gold         
Block Of Raw Iron          Budding Amethyst          
Calcite                    Cobbled Deepslate         
Copper Ore                 Deepslate                 
Deepslate Diamond Ore      Deepslate Gold Ore        
Deepslate Iron Ore         Deepslate Lapis Lazuli Ore
Deepslate Redstone Ore     Raw Copper                
Raw Gold                   Raw Iron                  
Spore Blossom              Tuff     

This brings the total number of non-renewable items in the latest version to 57

I will offer a +500 bounty to a submission which also includes a save file for a world which:

  • Is a vanilla Minecraft Java Edition snapshot 21w20a Hardcore world
  • Meets the requirements for the main challenge
  • Has 3 chests with all 57 non-renewable items, as listed above

Meta

  • Is this clear enough?
  • Are there any ways this can be exploited, so that users can trivialise the task? How can I prevent those exploits?
  • Is it a good challenge?
  • Tags are , and (meta discussion). Any suggestions?
  • Minecraft is not free, thus limiting those who can compete. Is this a significant problem?
  • Any further feedback?
\$\endgroup\$
7
  • \$\begingroup\$ I don't think minecraft being not free is a relevant problem here \$\endgroup\$
    – Wezl
    May 27 at 18:32
  • \$\begingroup\$ I'm sure I saw a way to renew Spore Blossoms in one of the recent snapshots \$\endgroup\$
    – pxeger
    May 27 at 18:35
  • 2
    \$\begingroup\$ There are, frankly, trivial ways to exploit this system. For it to be more or less uncheatable you would need a speedrun-style verification system requiring full screen recording and ideally livestreaming for the whole thing. I don't think it's a good fit for the site, to be honest, because there's really nothing code-related about it (Code Golf and Coding Challenges), and because it's too complex to implement a reliable verification system. You could try proposing it as a category extension on speedrun.com, though \$\endgroup\$
    – pxeger
    May 27 at 18:41
  • 4
    \$\begingroup\$ Honestly this might be better as an off-TNB hosted competitive event where a bunch of people can stream (to minimize cheating) and attempt to get [as many of] these resources [as quickly as possible], if we want this to be a thing for people in this community to do for fun. In any case, this was a cool idea initially, but I do agree with pxeger that there isn't really anything in scope for CGCC about this. \$\endgroup\$
    – hyper-neutrino Mod
    May 27 at 18:52
  • \$\begingroup\$ Is Bedrock allowed? \$\endgroup\$
    – emanresu A
    May 28 at 5:30
  • \$\begingroup\$ If you do make it a TNB event (or even if you don't?), maybe the scoring system should take into account the rarity of each item. So coal ore scores 1 point and enchanted golden apple scores 10 or something \$\endgroup\$
    – pxeger
    May 28 at 13:11
  • \$\begingroup\$ Question - are we allowed to start with anything, as long as we get the items from the world itself? \$\endgroup\$ May 28 at 16:04
0
\$\begingroup\$

for (a.length of a)

A recent conversation in The Nineteenth Byte about language design brought about discussion of an "interesting" snippet of JavaScript code:

for (a.length of a) console.log(a)

This loops over the elements of a, but assigns each element to the length of a, and prints out a on each iteration.

  • When an array's length is assigned, it is resized to the length, with excess elements being removed from the end, and additional elements filled with undefined
  • When an array's length is assigned to undefined, an error occurs
  • The loop will halt if the number of iterations is ever greater than a.length

Depending on the contents of a, this produces some wild results.

TODO: finish this post. Posted without finishing to test if Redwolf Program's Sandbox Posts bot is working :)

\$\endgroup\$
0
\$\begingroup\$

Given a black-box function f(x) which take a value x and output true for \$p(x)\$, an unknown continious monotone function(not knowing even whether it's increasing), probable; and false otherwise. Output an infinite sequence \$a_n\$ such that \$\lim_{n\rightarrow \infty}p(a_n)=0.5\$. You can assume that the result exists.

Reasonable I/O allowed. Shortest code win.

A possible solution:

for i=1..infty
    S = [i/2] * i*i*2
    for j=0..i*i*2-1
        for k=1..i
            if f(j/i-i)
                S[j]--
    print minPos([t*t for t in S])/i-i

Try It Online!

\$\endgroup\$
7
  • \$\begingroup\$ So we are given two functions f and p and need to define an infinite sequence? \$\endgroup\$
    – Quelklef
    Mar 26 at 1:55
  • \$\begingroup\$ You're not given p, and you're to find a such that p(a)=0.5 \$\endgroup\$
    – l4m2
    Mar 26 at 5:58
  • \$\begingroup\$ If we are not given p, how are we supposed to find a? Do you want us to generate an a that works for every p? \$\endgroup\$
    – Quelklef
    Mar 26 at 6:51
  • \$\begingroup\$ @Quelklef Try some a and adjust till probable is 0.5 \$\endgroup\$
    – l4m2
    Mar 26 at 7:19
  • 2
    \$\begingroup\$ I completely do not understand. Is it possible you could provide an example solution in your problem? That may help others confused like me. \$\endgroup\$
    – Quelklef
    Mar 26 at 15:13
  • \$\begingroup\$ Say, \$ p\left(x\right) = \frac{4\pi + 2\arctan\left(x\right)}{5\pi} \$. How can you find out such a sequence \$ a_n \$ let \$ \lim_{n\to\infty}p(a_n)=0.5 \$ \$\endgroup\$
    – tsh
    Mar 29 at 8:44
  • \$\begingroup\$ Even if we can safely assume that there is a value \$x\$ that \$p\left(x\right)=0.5\$, I don't think anyone can provide sequence would even convergent with a non-zero probable. \$\endgroup\$
    – tsh
    Mar 29 at 8:51
0
\$\begingroup\$

duplicate :(

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

Convert Lambda Calculus to SKI-notation

Lambda calculus is a formal system in mathematical logic for expressing computation based on function abstraction and application using variable binding and substitution.

However, it's possible to translate pure lambda calculus into SKI-notation. This can be preformed by building an AST for the lambda calculus, converting it through multiple substitution steps where it contains a mixture of lambda calculus and SKI-notation, until a pure SKI-notation expression is reached.

As this is code golf, there is some flexibility on your I/O so that you may be able to accept the lambda calculus and output the SKI-notation as a tree but the examples will use a string-based notation.

  • (c1), where c1 is an arbitrary expression that does not contain a lambda (represented here by >), does not require further substitution.
  • (c1(c2)), where c1 and c2 are arbitrary expressions, can be substituted with (s1(s2)), where s1 and s2 are the result of performing any necessary substitutions on c1 and c2.
  • (x1>x1) can be substituted with I.
  • (x1>c1) (where c1 can be an arbitrary expression as long as it does not contain a reference to x1) can be substituted with (Ks1), where s1 is the result of performing any necessary substitutions on c1.
  • (x1>(x2>x1)) can be substituted with K.
  • (x1>(x2>e1)) (where e1 is an arbitrary expression that is depends on but is not x1) can be substituted by performing substitutions on (x2>e1) resulting in s2 and then performing substitutions on (x1>s2).
  • (x1>c1(e2)) (where c1 can be an arbitrary expression as long as it does not contain a reference to x1) can be substituted with (s1(s2)), where s1 and s2 are the result of performing any necessary substitutions on c1 and (x1>e2).
  • (x1>e1(e2)) can be substituted with (S(s1)(s2)), where s1 and s2 are the result of performing any necessary substitutions on (x1>e1) and (x1>e2).

Naturally function application associates left-to-right, so in the case of (x1>c1(c2)(e3)(c4)) this needs to be substituted using the last rule as if it was (x1>(c1(c2)(e2))(c4)), which requires generating substitutions for (x1>c1(c2)(e2)) and (x1>c4), but then (x1>c1(c2)(e2)) can use the penultimate rule, turning into c1(c2)(x1>e2), with (x1>e2) still needing to be substituted.

Given an input expression of lambda calculus, please convert it to SKI-notation. You must use the above substitutions at a minimum, but you can perform additional reductions if you wish, so for instance if you use a string-based notation then you may simplify ((S)(K)) to (SK) or even SK depending on context.

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

\$\endgroup\$
5
  • \$\begingroup\$ I'm a little unclear as to whether we can output any SKI expression so long as it is equivalent to the input and you are just laying out an example algo or whether we have to output the result of that algo (plus optional substitutions). \$\endgroup\$
    – Wheat Wizard Mod
    May 29 at 11:23
  • \$\begingroup\$ I think also a definition of what it means to be equivalent between the two systems and within the systems themselves would be helpful. \$\endgroup\$
    – Wheat Wizard Mod
    May 29 at 11:24
  • \$\begingroup\$ @WheatWizard You have to show that you do no worse than the example aglo. In theory the post will have enough examples so that you just need to be able to match or beat all of the examples. \$\endgroup\$
    – Neil
    May 29 at 12:44
  • \$\begingroup\$ I'll have to work on that definition because I don't want to accidentally be too restrictive or indeed lenient. \$\endgroup\$
    – Neil
    May 29 at 12:48
  • \$\begingroup\$ Closely related: unrestricted conversion challenge, metagolf version \$\endgroup\$
    – Bubbler
    Jun 7 at 7:01
0
\$\begingroup\$

Conic Sections (simplified)


Given the equation of a non-parabolic conic section, output its characteristics.


Spec

This assumes prior knowledge of hyperbolas and ellipses, as well as their characteristics. (This includes circles; they are a special case of the ellipse with eccentricity of zero.)

Input

A non-parabolic (to simplify things) conic section given in the standard equation form. To simplify things further (because the main point is not to perform linear algebra magic) there will be no xy term. This is an example of a valid equation:

x^2+6x+y^2-8y+15=0 // string form
[1,6,1,-8,15]      // array form

These are not:

x^3+5x^2+7x+4=0 // because the degree of the equation is 2
x^2+5xy+y^2-4=0 // because there should be no `xy` term
x^2+3x+7=0      // because there should be `x` and `y` terms.

Note that the conic section can also be taken as an array as shown above. If so, please specify the order of the array; I am flexible when it comes to this format, As long as there are 5 elements with nonexistent terms represented by zero (like no y term in x^2+5x+y^2+14=0) and that the terms they represent are x^2 x y^2 y c where c is a constant. The equation will always be <expression> = 0.

Output

Output should be the type of section, center, horizontal radius, vertical radius, foci and eccentricity (in whatever desired order). This can be output as a string or an array as long as it is clear. A valid output for x^2+6x+y^2+8y+16=0 (or its array equivalent) would be:

["ellipse", [-3, -4], 3, 3, [[-3, -4]], 0]

or

ellipse
-3 -4
3
3
-3 -4
0

or similar.

(no need to output "circle" because it is a special case of the ellipse)

Meta:

  • duplicate?
  • should I edit in info on conic sections?
  • should I create the characteristics tag?
\$\endgroup\$
2
  • \$\begingroup\$ One issue with the x^2+5xy+y^2-4=0: It is a valid conic section (hyperbola). \$\endgroup\$ May 30 at 21:01
  • \$\begingroup\$ @fasterthanlight I know, but we are not considering xy terms in this challenge. \$\endgroup\$
    – user100690
    May 31 at 8:01
0
\$\begingroup\$

Is this a trivial brainfuck NOP?

Brainfuck is Turing-complete*, which means determining the behaviour of an arbitrary program is undecidable. However, we can get arbitrarily close to this if we limit ourselves to detecting a limited subset of programs.

For this challenge, we will detect a limited subset of NOPs - that is, snippets of programs which have no effect including on the brainfuck interpreter's state - which I have declared "trivial NOPs".

Here is a recursive definition of a trivial NOP:

  • the empty string is a trivial NOP
  • snippets consisting only of characters outside the set []<>+-,. (that is, characters which are ignored in Brainfuck), are trivial NOPs
  • the concatenation of two trivial NOPs is also a trivial NOP
  • snippets of any of the following forms, where X is another trivial NOP, are also trivial NOPs:
    • +X-
    • -X+
    • <X>
    • >X<

We will assume brainfuck has unlimited tape length. Overflow also does not need to be considered, because if a cell rolls over due to a +, a - can then underflow it back and it will still be a NOP.

Any snippet containing , or . is not a NOP because it performs impure I/O, and any snippet containing [ or ] is undecidable, so must be assumed to be not a NOP.

Your task is to, given a brainfuck snippet as a string or list of characters, determine whether it is a trivial NOP, and output two distinct values corresponding to true and false.

Test-cases

Truthy

(empty string)
a
+-
<>-+
<!>
+<-+>-
<<<+++a+--++>><-+>>

Falsey

<+>+<->-             (note: even though this is a NOP, it's not a trivial NOP)
+<-+>-[]
,+<-+>-
<.>

Rules


Meta

  • Can you think of any other classes of brainfuck snippet that are simple to detect as doing nothing?
  • Is this a duplicate?
  • Is this clear enough?
  • Any other feedback?
\$\endgroup\$
3
0
\$\begingroup\$

Back to the sandbox.

Challenge will be initially done on TBD, although I may do it again after that sometimes. Also, programs will be launched with a launcher shell script.

Background/story

You are a programmer who has been requested to create a program that wipes out other similar programs. However, you need to have your program fight others to see how it does in the field.

Requirements

You are to make a program that can run on a Debian 10 VM. If the language you use is not available by default on Debian 10, you must provide instructions or a link to the compiler.

Formatting

Submissions are to be formatted as follows:

# Name, Language

Description

```
Code
```

More description and explanation

Link to compiler download and/or download instructions for Debian 10 GNU/Linux (also add compiler options to use when compiling.)

Requirements for being included

Your answer must have at least one upvote. You also need to include an emergency off switch and test the switch to be included, and provide the source code and have tested it on a Debian 10 VM. Also, its growth rate must be hypothetically sub-exponential (this is to stop excessively laggy answers. If you don't know what growth rate to go with, go with linear (\$O(x)\$) or Polynomial (\$O(x^2)\$,\$O(x^3)\$,etc.).

Victory

To win, you need to be the last process of the bunch, by killing other processes through a system call. If you kill important processes that result in the VM breaking, that round is ignored for scoring. A total of 100 rounds will be run, and you get a point if you are the last "malicious" process around. If there's a tie, the tiers will be put in a tiebreaker.

Pseudocode for submission example

I don't know much about coding yet, so here's some pseudocode (feel free to convert it to a real programming language and mark it as such:

Example, Pseudocode

This program simply kills processes with the word "MalicousProcess", unless it's "MalicousProcess-Example"

def Example () {
  while (system(ps aux | grep "MalicousProcess") != null) {
    for each process != "MalicousProcess-Example" {
      kill process
    }
  }
  return 0

fictitious compiler

Please provide more recommendations.

\$\endgroup\$
12
  • \$\begingroup\$ "This is to stop joke answers, suicidal answers (sorry EmoWolf), and malware if it shows up." I doubt it will stop joke answers (including EmoWolf). \$\endgroup\$
    – Wezl
    May 25 at 21:17
  • \$\begingroup\$ Well, then it may stop bad joke answers and malware (like :(){ :|:& };: ). Also, I sure hope stack exchange doesn't run all inputs through bash. \$\endgroup\$
    – 4D4850
    May 25 at 21:25
  • 2
    \$\begingroup\$ @4D4850 Don't worry about malware; we follow riules here :p \$\endgroup\$ May 25 at 22:30
  • \$\begingroup\$ Ok Redwolf. I'll remove the justification part, but still keep the policy. \$\endgroup\$
    – 4D4850
    May 26 at 11:50
  • \$\begingroup\$ Also, this is a little off topic, but why's your comment the only one that appears when you don't expand the comments? \$\endgroup\$
    – 4D4850
    May 26 at 12:14
  • \$\begingroup\$ @4D4850 Redwolf's comment has been upvoted, so it is viewed by stack exchange as the most important/helpful/funny comment, and the others are less important \$\endgroup\$
    – Wezl
    May 26 at 14:16
  • \$\begingroup\$ Anyway, can any of you provide any more recommendations, or do you think it's ready to be posted? \$\endgroup\$
    – 4D4850
    May 26 at 18:01
  • \$\begingroup\$ Disallowing a rate of O(e^x) doesn't disallow all exponential solutions. Is that intended? If not you should probably require it tl be subexponential \$\endgroup\$ May 28 at 6:05
  • \$\begingroup\$ Thanks for pointing that out. I'll make that change now. \$\endgroup\$
    – 4D4850
    May 28 at 14:44
  • \$\begingroup\$ Provided no recommendations are made by Sunday 9:00 Central time, I'll post this question on the main site. \$\endgroup\$
    – 4D4850
    May 28 at 23:37
  • \$\begingroup\$ Ok, I'm posting the question. \$\endgroup\$
    – 4D4850
    May 31 at 13:11
  • \$\begingroup\$ Nevermind. Back to the Sandbox. \$\endgroup\$
    – 4D4850
    May 31 at 15:51
0
\$\begingroup\$

Demonstrate some easier abstract algebra

From my related challenge, Demonstrate some advanced abstract algebra

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

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

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

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

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

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

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

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

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

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


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

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

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

Each of these 3 programs will:

  • take two integers, in any reasonable format and method

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

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

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

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

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


Meta

\$\endgroup\$
5
  • \$\begingroup\$ There exists no anti-distributive surjection when \$S=\mathbb Z\$. Maybe there exists one when \$S=(\mathbb Z/2\mathbb Z)^k\$, but I haven't found one yet. \$\endgroup\$ Jun 16 at 9:12
  • \$\begingroup\$ I would remove the constraint that the operator be a surjection, and just impose that it be non-constant. If you convince yourself that such an anti-distributive operator exists, you could post the challenge, but I would change it to "write 3-9 programs, such that each property is verified by (at least) one program". That would be an incentive to have programs which verify several properties at once, but make it more manageable. \$\endgroup\$ Jun 16 at 9:18
  • \$\begingroup\$ Also, there are problems with your "anti-associativity": if b = 1, then (a*b)*c = a*(b*c). \$\endgroup\$
    – anatolyg
    Jun 16 at 10:43
  • \$\begingroup\$ @RobinRyder If \$ S=(\mathbb Z / 2\mathbb Z)^k\$, aren't anti-distributive operators the same as distributive operators? \$\endgroup\$
    – Nitrodon
    Jun 16 at 14:21
  • \$\begingroup\$ @Nitrodon Yes, of course you are right. \$\endgroup\$ Jun 16 at 15:03
0
\$\begingroup\$

Pickleball Doubles Scoring

\$\endgroup\$
4
  • \$\begingroup\$ Welcome to Code Golf, and thanks for using the Sandbox! I've fixed some formatting for you - on our site, you have to use \$ instead of $ for MathJax. At the moment, this challenge is missing an objective scoring criterion - I'm guessing you want code-golf? \$\endgroup\$
    – pxeger
    May 31 at 7:17
  • \$\begingroup\$ @pxeger: Thank you. Yes, I was thinking code golf. Would it be better to take the END state as [0,0,0] so that the format of all inputs is the same? Is there a place to get the standard verbiage for code golf? \$\endgroup\$ May 31 at 13:43
  • \$\begingroup\$ Yes, I think [0, 0, 0] is a good idea - but I think the best option is to allow answers to choose a value to represent the END state depending on their language (in some it may be easier to use null, or take a string or something...). Re verbiage, there's Standard definitions of terms within specifications, but you can also just look at the various policy posts for more information. If you have specific questions, you can always ask in our chatroom, The Nineteenth Byte. \$\endgroup\$
    – pxeger
    May 31 at 14:51
  • \$\begingroup\$ I've edited this down to a stub now that it's been posted to save space \$\endgroup\$ Jun 1 at 15:15
0
\$\begingroup\$

What sequence?

Stack Exchange distinguishes users by a unique id that can be found in their their profile URL.

You can find your id by navigating to https://codegolf.stackexchange.com/users/current and finding the number after the /users/ bit in the resulting URL. For example, my profile URL is https://codegolf.stackexchange.com/users/99744/alex-bries, so my user ID is 99744

You can use this id to look up the number sequence of the last submitter on OEIS:

The On-Line Encyclopedia of Integer Sequences® (OEIS) is an online database containing integer sequences. These sequences are identified via an id starting with an A followed by 6 numbers. You can search for an id with the "id:" key on the search bar of OEIS.
id:A99744 will display sequence A099744 followed by all other sequences that mention it.

Your job is to write a program that, when given a number n, produces the nth number in sequence of the submitter above.

Rules

  • Each language may only be used once.
  • You may not post 2 answers in a row
  • Every next submision may only be max 10 bytes higher than the previous one
  • The champion is the last valid submission on june 14th

golfing is not a requirement but it is in your favour

\$\endgroup\$
5
  • \$\begingroup\$ Different OEIS sequences have vastly different complexity. I'm pretty sure my user id is solvable in 6 bytes of Jelly, which can break the whole game depending on the next user's id. \$\endgroup\$
    – Bubbler
    Jun 1 at 11:32
  • \$\begingroup\$ @Bubbler I like the idea of different complexities, but not sure how to prevent the colossal differences. Personally i like it, but if you have ny suggestions for how to improve, please let me know. \$\endgroup\$
    – Alex bries
    Jun 1 at 11:47
  • \$\begingroup\$ I don't think this works as a [answer-chaining] challenge, but it could work as just a regular code-golf challenge (i.e. write a program which takes an integer n and outputs the nth term of the sequence with your id). It's unlikely to be fair, but its better than [answer-chaining]. I've also done a bit of editing, feel free to change anything I did \$\endgroup\$ Jun 1 at 15:08
  • \$\begingroup\$ Yes, that's probably the better desicion, but after thinking about it more, its unlikely to be very fun, so ill leave it (for now). Thanks for the the constructive feedback guys. \$\endgroup\$
    – Alex bries
    Jun 1 at 20:04
  • \$\begingroup\$ Using user IDs is inherently unfair. \$\endgroup\$
    – qwr
    Jun 2 at 14:47
0
\$\begingroup\$

Write a program that mimics the output of the command env (with no arguments).

The program should print (to standard output, or closest equivalent) a list of all current environment variables, with each variable on it's own line. The name and content of a variable should be separated by an equal sign.

Here's a grammar:

line ::= name "=" contents
output ::= (line "\n")*

No using the env command, or any 1 command that completes the challenge on its own.

shortest code wins.

\$\endgroup\$
14
  • \$\begingroup\$ Can we use built-ins the get environment variables? \$\endgroup\$
    – Adám
    Nov 15 '20 at 15:50
  • \$\begingroup\$ @Adám If you mean "Can we use built-ins to get the environment variables", yes, I don't see a reason you couldn't, and otherwise it might be impossible to get them in some languages. \$\endgroup\$
    – binarycat
    Nov 15 '20 at 15:54
  • 1
    \$\begingroup\$ And I assume (ba|k|c|…)sh cannot participate? \$\endgroup\$
    – Adám
    Nov 15 '20 at 15:56
  • \$\begingroup\$ @Adám If they can find a way to get a list of environment variables without using env, I guess they could, unless I'm missing some other command that produces the same output. There's a lot of languages that can't complete the challenge (mostly esoteric ones), but they aren't technically banned. \$\endgroup\$
    – binarycat
    Nov 15 '20 at 23:28
  • \$\begingroup\$ How about set on Windows/DOS? \$\endgroup\$
    – Adám
    Nov 16 '20 at 8:07
  • \$\begingroup\$ @Adám I have no experience with windows, does it have similar functionality to env? if so then no. \$\endgroup\$
    – binarycat
    Jun 3 at 16:46
  • \$\begingroup\$ It is indeed. Note: As a beginner challenge writer (you've never posted a challenge before), you should avoid do X without Y challenges. \$\endgroup\$
    – Adám
    Jun 3 at 17:28
  • \$\begingroup\$ @Adám isn't "don't use a builtin that does exactly what you need" a standard loophole? \$\endgroup\$
    – binarycat
    Jun 3 at 19:32
  • \$\begingroup\$ No, not at all. \$\endgroup\$
    – Adám
    Jun 3 at 19:32
  • \$\begingroup\$ @Adám Huh, I guess you're right. I just don't want to have the challenge solved before it's even posted, with the 3 character bash program env. Does trying to prevent 1 cheeky answer really put you in the same category as forbidding something random? \$\endgroup\$
    – binarycat
    Jun 3 at 20:50
  • \$\begingroup\$ Does it really bother you that much that someone will post env or set as a solution? You're under no obligation to upvote or accept such an answer. \$\endgroup\$
    – Adám
    Jun 3 at 20:54
  • \$\begingroup\$ @Adám here's something you linked me: "A commonly used non-observable requirement is to avoid using a library function that solves the entire challenge. I believe these rules are OK. Like other non-observable requirements, one can find borderline cases of whether a built-in function solves all of the task or not, but the benefits of higher-quality answers outweigh the costs." this certainly seems to apply here. \$\endgroup\$
    – binarycat
    Jun 3 at 20:59
  • \$\begingroup\$ @Adám I've never posted anything because whenever I post a question to the sandbox all I get is reasons why my question is bad. And putting any additional rules whatsoever gets the same "no X without Y" response. Then people don't elaborate on anything. \$\endgroup\$
    – binarycat
    Jun 3 at 21:10
  • \$\begingroup\$ Don't be dismayed. We've all been there. Writing challenges is notoriously hard, especially for beginners. If you look at my account, I have lots of well-received challenges, but dig in, and you'll find that many of my early ones were flawed. I highly recommend answering a lot more existing challenges, before you attempt at writing your own. And when you do, decide on a simple unique task (this is hard, as a lot of obvious tasks have already been posted), with no special rules; just vanilla code golf. \$\endgroup\$
    – Adám
    Jun 3 at 21:14
0
\$\begingroup\$

Optimize imnotdeadfish

\$\endgroup\$
2
  • \$\begingroup\$ Deadfish (at least, the original one) used i and d intead of + and -. Is there a reason you chose to use them? \$\endgroup\$
    – pxeger
    Jun 3 at 10:45
  • \$\begingroup\$ @pxeger originally for the language I was going to have an input command so I swapped i and d for + and -, and I guess I never added them back. \$\endgroup\$
    – Underslash
    Jun 3 at 16:02
0
\$\begingroup\$

branch golfing: sort a list by number of prime factors

Specifically, first sorted by total number of prime factors, with any ties broken by number of unique prime factors (remove this?), then broken by size. The list will never contain 1.

Test cases:

[4,5,6,9] -> [5,4,9,6]
[10,11,12] -> [11,10,12]
[360 200 12] -> [12 200 360]
[63 64 65] -> [65 63 64]
\$\endgroup\$
13
  • \$\begingroup\$ Please avoid challenges involving the prime numbers. \$\endgroup\$
    – Adám
    Jun 3 at 20:42
  • \$\begingroup\$ Please avoid challenges where the scoring criteria relies on non-observable program requirements. \$\endgroup\$
    – Adám
    Jun 3 at 20:46
  • 1
    \$\begingroup\$ Please avoid making assumptions about language features. \$\endgroup\$
    – Adám
    Jun 3 at 20:47
  • 1
    \$\begingroup\$ Also, thank you for using the sandbox! :-) \$\endgroup\$
    – Adám
    Jun 3 at 20:47
  • \$\begingroup\$ @Adám Do you have any example of something where it isn't clear whether or not it is a conditional branch? Or any advice that isn't essentially just "this is bad"? \$\endgroup\$
    – binarycat
    Jun 3 at 20:56
  • \$\begingroup\$ Maybe. Are there any branches in this APL solution? The code means: the input ⊇⍨ permuted ∘⍋ according to a grade using prime factors for each , followed by ⍥≢ when counted, ∘∪ the unique ¨ for each, prime factors for each. Or in other words: Reorder according to a grading of prime factor count and then unique prime factor count. \$\endgroup\$
    – Adám
    Jun 3 at 21:10
  • \$\begingroup\$ From your description, sounds like a loop construct, which I intended to include, but I could clarify. \$\endgroup\$
    – binarycat
    Jun 3 at 21:21
  • \$\begingroup\$ If you count loop constructs, would you also count APL's + as a loop? It automatically loops over its arguments: Try it online! Besides, you state that "Builtin functions are presumed to be branchless." \$\endgroup\$
    – Adám
    Jun 3 at 21:24
  • \$\begingroup\$ No. It loops its own code, but can't be used to perform arbitrary code in a loop. \$\endgroup\$
    – binarycat
    Jun 3 at 21:28
  • \$\begingroup\$ My usage of was exactly parallel to any usage of +. They both auto-loop. However, ¨ is in fact an explicit loop. I could probably code around that too, though. An example would be extracting the odd numbers from a list. In many languages, you'd use a loop with a branch, but in APL you'd write List/⍨2|List which means L where 2 divides the list ['s elements; that's implicit]. Looping? Branching? Anyway, you get the idea. It'll be next to impossible to determine for every language (there are lots!) what exactly constitutes an explicit loop, or for that sake a branch. \$\endgroup\$
    – Adám
    Jun 3 at 21:32
  • \$\begingroup\$ @Adám so they are analogous to functions that take 2 arrays as input and provide one as output? that doesn't seem like a branch to me. "I could probably code around that too, though." Yeah that's kinda the point of the challenge. \$\endgroup\$
    – binarycat
    Jun 3 at 21:41
  • \$\begingroup\$ @Adám "but in APL you'd write List/⍨2|List which means L where 2 divides the list ['s elements; that's implicit]." this explanation isn't very clear. this seems like this construct (or pair of constructs, which you are confusingly explaining as one) is some kind of predicate filter/map? \$\endgroup\$
    – binarycat
    Jun 3 at 22:01
  • \$\begingroup\$ Let us continue this discussion in chat. \$\endgroup\$
    – Adám
    Jun 3 at 22:01
0
\$\begingroup\$

Random Prisoner's Trilemma - Python 3 KOTH

\$\endgroup\$
0
\$\begingroup\$

Yet another digit insertion problem

\$\endgroup\$
0
\$\begingroup\$

How much faster than qsort can you achieve?

Task

Sort 1 million 32 bit integers as quickly as possible. Your code must create 1 million random non-negative integers in the range 0 to 999999 and sort them. Only the sorting should be timed.

Score

I will run your code and compare the timings to my default C code using qsort. That is:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdint.h>


int cmpfunc (const void * a, const void * b) {
   return ( *(int*)a - *(int*)b );
}

int main(void) {
    int sz;
    srand(time(NULL));
    printf("Enter the size of array::");
    scanf("%d",&sz);
    uint32_t *arr = malloc (sz * sizeof(uint32_t));
    int i;
    for(i=0;i<sz;i++)
        arr[i]=rand()%1000000; 
    clock_t begin = clock();
    qsort(arr, sz, sizeof(int), cmpfunc);
    clock_t end = clock();
    double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
    printf("%f seconds\n", time_spent);
    printf("%d\n", arr[10]);
    return 0;
}

Your score will be the timing for my C code divided by the timing for your code. I will run both on my PC. This means I will need clear instructions for how to compile and run your code on linux.

This means you will have to provide timing code in your solution so I am not timing the creation of the data.

Current timings

On my PC:

  • The sample C code takes 0.11 seconds.
  • Python sorted takes 0.24 seconds.

Notes

My code prints out one of the elements of the sorted array. This is to stop the opimizing compiler from removing the sorting code completely. You will have to do the same.

Builins are allowed. As ever this is a challenge per language so if your Python answer is faster than any other Python answer you have won that mini-competition.

\$\endgroup\$
8
  • 1
    \$\begingroup\$ Obligatory question - builtins, yea or nay? \$\endgroup\$ Jun 7 at 14:31
  • \$\begingroup\$ That's probably the wisest choice - probably want to edit the post to say that. \$\endgroup\$ Jun 7 at 14:35
  • \$\begingroup\$ I strongly disagree with banning builtins. If any builtin is faster, either the builtin is interesting enough to be posted, or the challenge is boring. Banning builtins won't improve the latter, but it will hurt the former \$\endgroup\$ Jun 7 at 14:37
  • \$\begingroup\$ @cairdcoinheringaahing I can go with that. \$\endgroup\$
    – Anush
    Jun 7 at 14:37
  • \$\begingroup\$ @StackMeter I deleted my comment as bultins are going to be allowed. \$\endgroup\$
    – Anush
    Jun 7 at 14:39
  • \$\begingroup\$ I would argue that "only time sorting should be timed" is a bit unclear. Consider my code is: int counts[MAX],i,j,k,sort(*a,size){for(i=0;i<size;++i)++counts[a[i]];for(i=k=0;i<MAX;counts[i++]=0)for(j=counts[i];j--;)a[k++]=i;} (maybe some typos here, not tested) the initialize of counts array is required by sort. But C compiler will help me do so even begin main() entry and would probably not timed by my code. \$\endgroup\$
    – tsh
    Jun 8 at 6:12
  • \$\begingroup\$ @tsh what would you suggest? \$\endgroup\$
    – Anush
    Jun 8 at 7:08
  • \$\begingroup\$ Things to consider: 1) 32-bit signed or unsigned integers? Do I have a choice? 2) 32-bit (or any finite-sized) integers can be sorted using non-comparison sort algorithms, such as radix sort (which is asymptotically faster than any kind of comparison-based sort). Would you allow the answers to exploit this or not? \$\endgroup\$
    – Bubbler
    Jun 14 at 1:24
0
\$\begingroup\$

Does the regex match the string?

Despite having over 80 challenges tagged , we don't have one to simply verify if a regex matches a string.

Given a regular expression \$R\$ and a string \$S\$, output one of two distinct and consistent values to indicate whether the regex matches the string.

You may assume:

  • The input and output will be in any convenient methods
  • The regular expression may be in any regex flavour that existed before this question was posted
  • \$R\$ will always be well formed, and will not error, regardless of the string matched against it
  • \$R\$ may be either a regular expression object (such as Javascript's RegExp, Python's re.compile etc.) or a plain string object.
  • The output will either be:
    • 2 distinct, consistent values, such as 1/0, true/false, "Hello"/5
    • A truthy value and a falsey value, not necessarily consistent values

This is , so the shortest code in bytes wins.


Meta

\$\endgroup\$
2
  • \$\begingroup\$ Changed the formatting of the "distinct, consistent values" section because I kept interpreting 1/0 as something to do with division by zero \$\endgroup\$
    – pxeger
    Jun 8 at 18:52
  • \$\begingroup\$ I think the problem with a challenge like this is that there are only two types of solutions: generally low-effort calls to the built-in that does the task, and extremely tedious implementations of the built-in. For example, if I wanted to implement PCRE I would also need to handle the callout feature, which requires writing a whole API. You might be better off requiring a specific subset of behaviours to try to encourage answers that aren't just a built-in. \$\endgroup\$ Jun 9 at 17:28
0
\$\begingroup\$

Smallest number of actions to change reputation by [x] points

8: Upvote + downvote

75: Bounty + upvote + accepted answer

Draft

\$\endgroup\$
2
  • \$\begingroup\$ Potentially a dupe of this. Stoopid. \$\endgroup\$
    – lyxal
    Jun 9 at 4:55
  • \$\begingroup\$ @lyxal This one's about positive or negative rep gains though, so it's more general \$\endgroup\$ Jun 9 at 4:57
0
\$\begingroup\$

Finite Composition of Univariate Functions

Problem

Given $$\mathbb{F}:=\{f_i \mid f_i : \mathbb{R} \to \mathbb{R}\}$$ the set of all the real maps, and the n-ary function composition

\begin{gather} c_o(f_i) = f_1∘f_2∘f_3⋯∘f_n\\[8pt] ∀f_i ∈ \mathbb{F},~~~1≤i≤n∈\mathbb{N} \end{gather}

write a program which receives n functions of a real variable as the input and computes the in-order function composition, product of all the entered functions as a string.

As standard, x is the name of the parameter variable in each function. Any other literal is taken as a constant.

Examples

Example 1

input

number of functions: 3
f_1 = x + 1
f_2 = x - 10
f_3 = x^2 + 2

output

c_o(f_1,f_2,f_3) = ((x^2 + 2) - 10) + 1

Example 2

input

number of functions: 4
f_1 = 27x - sin(e)
f_2 = -7(x^2 + 1)^(1/2) 4y^3 + 21
f_3 = x^(3/2) + 4
f_4 = ln(z + 1)
(z, y, e are assumed as constants in the reals)

output

c_o(f_1,f_2,f_3,f_4) = 27(-7(((ln(z + 1))^(3/2) + 4)^2 + 1)^(1/2) 4y^3 + 21) - sin(e)
\$\endgroup\$
6
  • \$\begingroup\$ Any suggestion, improvement? please let me know. Thanks. \$\endgroup\$ Jun 8 at 20:08
  • 1
    \$\begingroup\$ Thanks for using the sandbox! This seems like an interesting idea, but currently it is too underspecified to be clear. For example, the test cases imply that the input to each function is represented by x, but you haven't stated anywhere that we can assume that. Consider also a slight change to one of your cases, where one function has -y(x^2 + 1). Is y a function or a constant? There are too many specifics, so hopefully the general idea gets across that you must specify how you are representing the functions very precisely for this to work. \$\endgroup\$ Jun 9 at 17:41
  • \$\begingroup\$ @FryAmTheEggman Thank you. I have edited it so that there's no ambiguity. \$\endgroup\$ Jun 9 at 18:19
  • \$\begingroup\$ That is a good change, but doesn't address the ambiguity in separating functions like sin from the product of constants. It also isn't clear to me whether x^2 - 7 is a valid solution for the first example. These have some overlap in that they (probably?) won't affect a solution that just substitutes the functions appropriately, but will affect any symbolic parser based approach. \$\endgroup\$ Jun 9 at 19:53
  • \$\begingroup\$ @FryAmTheEggman oh, yes, it's valid but it isn't required to go to such depths as to simplify symbolic expressions, that would be a problem on its own. I didn't specify so in the challenge and as for sin or those functions, since the problem to be addressed is the composition of functions any expression different from x should be "disregarded". I don't intend to make further computations with the output string. \$\endgroup\$ Jun 9 at 21:45
  • \$\begingroup\$ What I'm trying to communicate is that your "point" for writing the challenge doesn't really matter - in some language it probably would be better to use a parser. If you just want to say "no other assumptions about the form of the function can be made" that is fine - but you have to actually say it, otherwise your challenge is unclear because I won't be able to tell if a solution is valid or not. \$\endgroup\$ Jun 9 at 23:13
0
\$\begingroup\$

Interpret Unreadable


Introduction

Unreadable is a programming language designed to be – as the name states – unreadable (in most fonts, anyway). Instructions are ' followed by a run of "s. So while in a code block, '""'""'""" looks just fine, it looks like '""'""'""" outside of one - it is extremely difficult to tell what is going on.

The instructions are prefix, and are as so:

Instruction Arity Behavior
'" Unary Print; output the character with codepoint x, and return x.
'"" Unary Increment; return x+1.
'""" Nilary Unit; return 1.
'"""" Binary Both; evaluate and disregard x, and return the result of evaluating y.
'""""" Binary While; keep evaluating y until x is 0. Return the last result of evaluating y.
'"""""" Binary Set; set the xth variable to y, and return y.
'""""""" Unary Get; return the value of the xth value. Undefined is 0.
'"""""""" Unary Decrement; return x-1.
'""""""""" Ternary If-else; if x is 0, return z. Otherwise, return y.
'"""""""""" Nilary Input; return the codepoint of the next character of input. EOF is -1.

For example, a cat program would be:

'"""""'""'""""""'"""'""""""""""'"'"""""""'"""
while(++ setvar(1 = input())) {print(getvar(1))}

Or, in its unreadable glory: '"""""'""'""""""'"""'""""""""""'"'"""""""'"""

Challenge

Given a valid Unreadable program and its input separated by any non-' or " character, interpret the program as Unreadable code.

Test cases (uses ! as the separation character, I/O is represented in CP-437)

Input -> Output
'"'"""!this input does nothing lol -> ☺
'"""""'""'""""""'"""'""""""""""'"'"""""""'"""!This is a cat so I can output whatever I want. blah blah blah... -> This is a cat so I can output whatever I want. blah blah blah...
'""""'""""'""""""'"""'""'""'""'""'""'""'""'""'""'"""'"""""'"""""""'"""'""""'""""""'""'"""'""'""'""'""'""'""'""'"""""""'""'"""'""""'""""""'""'""'"""'""'""'""'""'""'""'""'""'""'""'"""""""'""'""'"""'""""'""""""'""'""'""'"""'""'""'""'""'""'""'""'""'""'""'""'"""""""'""'""'""'"""'""""'""""""'""'""'""'""'"""'""'""'""'""'"""""""'""'""'""'""'"""'""""'""""""'""'""'""'""'""'"""'""'""'""'"""""""'""'""'""'""'""'"""'""""'""""""'""'""'""'""'""'""'"""'""'""'""'""'""'""'""'""'""'""'""'""'"""""""'""'""'""'""'""'""'"""'""""'""""""'""'""'""'""'""'""'""'"""'""'"""""""'""'""'""'""'""'""'""'"""'""""""'"""'""""""""'"""""""'"""'""""'"'""'""'"""""""'""'"""'""""'"'""'"""""""'""'""'"""'""""'"'""""""""'""""""""'"""""""'""'""'""'"""'""""'"'""""""""'""""""""'"""""""'""'""'""'"""'""""'"'""'"""""""'""'""'""'"""'""""'"'""'""'""'""'"""""""'""'""'""'""'"""'""""'"'""'""'"""""""'""'""'""'""'""'"""'""""'"'""""""""'"""""""'""'""'""'""'""'""'"""'""""'"'""'"""""""'""'""'""'"""'""""'"'""'""'""'""'"""""""'""'""'""'"""'""""'"'""""""""'""""""""'"""""""'""'""'""'"""'""""'"'"""""""'""'""'"""'""""'"'""'""'""'"""""""'""'""'""'""'""'"""'"'"""""""'""'""'""'""'""'""'""'""" -> Hello, world!
'""""""'"""'""'""""""""""'""""""'""'"""'""""""""""'"'"""""'""""""'"""'""""""""'"""""""'"""'""""""'""'"""'""'"""""""'""'"""!A! -> b

Rules

  • Standard loopholes are forbidden.
  • A single trailing newline is allowed in the output, and no other trailing whitespace.
  • If possible, please link to an online interpreter (e.g. TIO) to run your program on.
  • Please explain your answer. This is not necessary, but it makes it easier for others to understand.
  • Languages newer than the question are allowed. This means you could create your own language where it would be trivial to do this, but don't expect any upvotes.
  • This is , so shortest code in bytes wins!
  • A +100 rep bounty will be given to the first Unreadable answer.
\$\endgroup\$
2
  • \$\begingroup\$ This doesn't seem like a particularly interesting challenge to me, to be honest. Decoding the program into commands is fairly trivial (split by ' and find lengths of each), in both golfing and practical languages, and the actual interpreting part of the challenge is basically the same as any other language. \$\endgroup\$ Jun 10 at 1:20
  • \$\begingroup\$ @RedwolfPrograms The interesting part is that commands are prefix, so it might take some thought to make an interpreter. \$\endgroup\$
    – Makonede
    Jun 10 at 1:55
1
101 102
103
104 105
116

You must log in to answer this question.

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