484
\$\begingroup\$

What is the Sandbox?

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

To post to the Sandbox, scroll to the bottom of this page or click on the "Add Proposal" link below, 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. 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, replace the post here with a link to the challenge and delete it.

See the Sandbox FAQ for more information on how to use the Sandbox.

The Sandbox works best if you sort posts by "active".

Add Proposal

Search the Sandbox

Browse your pending proposals

Get the Sandbox Viewer to view the sandbox more easily

To add an inline tag to a proposal use shortcut link syntax with a prefix: [tag:king-of-the-hill]

| |
\$\endgroup\$

2916 Answers 2916

1
80 81
82
83 84
98
0
\$\begingroup\$

Distant Programs

Create a program that, when run, prints "Do you still love me?" with or without a newline at the end.

Your score is the Levenshtein distance to the closest non-erroring program (the non-erroring program doesn't have to do anything, although it might), and you want the largest score. Ties are broken by whichever code is shorter, in bytes.

Your program can read it's own source code, through the filesystem or otherwise. If your program must have a specific name, the length of that name should be included in your byte count.

Notes:

  • Compiler warnings are not counted as errors for this challenge.
  • An erroring program can output, frobricate, do anything as long as it eventually errors.

Notes for the sandbox:

  • Is the specification clear?
  • Is it possible to create a solution that can simply be repeated to get any arbitrary score? If so it would pretty much ruin the challenge as it is, and I think it should be possible but I can't get it to work.
  • What tags would this use?
| |
\$\endgroup\$
  • \$\begingroup\$ I think it should be possible to get arbitrary scores using quining techniques and cryptographic hash verification. \$\endgroup\$ – Peter Taylor Aug 20 '19 at 15:41
  • 1
    \$\begingroup\$ In addition to what Peter said, I think there is a clarity problem in defining precisely what counts as an error. \$\endgroup\$ – FryAmTheEggman Aug 20 '19 at 19:33
  • \$\begingroup\$ @FryAmTheEggman I was going top copy my definition of "error" from codegolf.stackexchange.com/questions/63433 but to my surprise there isn't one. Honestly I'm not sure how exactly to define "error". \$\endgroup\$ – Shelvacu Aug 23 '19 at 23:03
0
\$\begingroup\$

Get the number of upvotes of your own answer

Write a piece of code which makes a request to codegolf.stackexchange.com and prints the, up to date, number of upvotes to the specific answer in which you've posted that piece of code.

As a test case I'll post a (poor!) answer below (obviously only in the real question).

This is just a random idea I had when reading some other code golfing challenges, and I thought I'll post it here to see if it will fly.

| |
\$\endgroup\$
  • \$\begingroup\$ Standard procedure(although weird, it is inevitable): Post a placeholder answer, copy the URL, and then make a request to that URL. (Although how can I access the number of votes?) \$\endgroup\$ – user85052 Aug 20 '19 at 13:59
  • \$\begingroup\$ Yes, it would inevitably involve editing your answer (unless you embed a unique string in your answer just to find it...?). I admittedly have not tested this yet. I’ll have a play in python and make sure it’s not too hard to extract the information about the number of upvotes... \$\endgroup\$ – MarcinKonowalczyk Aug 20 '19 at 14:08
  • \$\begingroup\$ I recognise this would be a longer than usual challenge for core golf, but I thought exactly that would maybe make it interesting. \$\endgroup\$ – MarcinKonowalczyk Aug 20 '19 at 14:12
  • \$\begingroup\$ Can you define "core golf"? \$\endgroup\$ – user85052 Aug 20 '19 at 14:18
  • 6
    \$\begingroup\$ I'm afraid it's already done before 3 years ago, so it would be closed as a duplicated. \$\endgroup\$ – Kevin Cruijssen Aug 20 '19 at 14:21
  • \$\begingroup\$ Thank you for using the Sandbox and finding out it was a duplicate rather than posting it on main first. \$\endgroup\$ – MilkyWay90 Aug 20 '19 at 19:19
  • \$\begingroup\$ No worries. I came up with it all by myself so I’m still happy. ;) Also, @A__ “core golf” was, of course, a typo. \$\endgroup\$ – MarcinKonowalczyk Aug 20 '19 at 20:29
0
\$\begingroup\$

Approaching the Graham's number

Write a program that could theoretically output an integer with the minimum absolute difference to the Graham's number. Your code is cracked if a robber writes a program to output the Graham's number exactly, in the same language, with the edit distance to your code under a specific limit.

Builtins for the Graham's number is disallowed.

Details to be added, if I think this idea actually works.


Problems:

How to set the limit?

  1. Fixed number of bytes.
  2. Fixed % of the code. Does the boilerplate count?
  3. Set by the cop. But how to fix the winning criterion?

Do the cops need to know a crack to make the submission valid?

| |
\$\endgroup\$
  • \$\begingroup\$ What does "could theoretically output an integer with the minimum absolute difference..." mean? Would a program that outputs "Hello, World!" in a loop be valid, assuming enough time and radiation? Would a program that outputs all odd numbers be valid? Do we just have to output any number? \$\endgroup\$ – my pronoun is monicareinstate Sep 6 '19 at 17:18
  • \$\begingroup\$ @someone "Output an integer" means to output a string matching /-?[0-9]+/ exactly, or any equivalents in other allowed formats. No hello world or multiple numbers. I'm not sure why you would think that. \$\endgroup\$ – jimmy23013 Sep 6 '19 at 17:29
0
\$\begingroup\$

"Condense" String of Text

You will be given a string. You will be "condensing" it by combining the bits of characters into one.

Rules

  • The condensation works as follows:
    1. You are given a string with 8-bit characters, such as Hèl¹ò Wôrld, hex 48 e8 6c b9 f2 20 57 f4 72 6c 64
    2. Combines bits into 16-bit groups, from right to left. Above becomes hex 48 e86c b9f2 2057 f472 6c64, or H槲⁗汤.
  • You may assume that all characters in string range from 0x00 to 0xff.
  • Your resulting string would be encoded in UTF-16.
    • You may also assume that the resulting string won't contain characters from 0xd800 to 0xdfff, or the input containing any of ØÙÚÛÜÝÞß at even index in 0-index, or odd index in 1-index.
  • Standard loopholes apply.
  • This is code-golf so shortest code wins.

Examples

Input raw: abacaba
Input hex: 61 62 61 63 61 62 61
Outpt raw: a扡捡扡
Outpt hex: 61 6261 6361 6261
Input raw: Example.
Input hex: 45 78 61 6d 70 6c 65 2e
Outpt raw: 䕸慭灬攮
Outpt hex: 4578 616d 706c 652e
Input raw: ÿ!0ÿMÿEÿSÿSÿAÿGÿE0ÿCÿAÿN0ÿAÿPÿPÿEÿAÿR0ÿTÿOÿOÿ
Input hex: ff 21 30 00 ff 4d ff 45 ff 53 ff 53 ff 41 ff 47 ff 45 30 00 ff 43 ff 41 ff 4e 30 00 ff 41 ff 50 ff 50 ff 45 ff 41 ff 52 30 00 ff 54 ff 4f ff 4f ff 0e
Outpt raw: A message can appear too.
Outpt hex: ff21 3000 ff4d ff45 ff53 ff53 ff41 ff47 ff45 3000 ff43 ff41 ff4e 3000 ff41 ff50 ff50 ff45 ff41 ff52 3000 ff54 ff4f ff4f ff0e
Note that there are NULs in the input.
| |
\$\endgroup\$
  • 1
    \$\begingroup\$ You might want to specify an encoding for the 16-bit encoding. For example, if you use UTF-16, then the values D800-DFFF are not valid characters \$\endgroup\$ – ar4093 Aug 23 '19 at 18:21
  • \$\begingroup\$ This is essentially translation between encoding types, since the input bytes will be identical to the output bytes \$\endgroup\$ – Jo King Aug 25 '19 at 12:14
0
\$\begingroup\$

Break it up and put it back together again

Note: This is , so read carefully and make sure your answer is valid before posting.

Write a program/function that takes a list of integers greater than 1 and returns a list of positive integers where each number from the original list has been split in two. For example:

[10,11,12] -> [5,5,4,7,6,6]

Each pair of numbers in the output add up to the corresponding number in the input. The algorithm used to split it up doesn't matter (for example a valid output could be [1,9,1,10,1,11]), as long as it is deterministic.

However, when fed its own byte values and the output is turned back into bytes, the resulting program should do the opposite, taking a list of integers and summing each pair. For example, if the program abcde splits each number into halves, with the leftover going to the second number:

"abcde" -> [97, 98, 99, 100, 101]
-> [48, 49, 49, 49, 49, 50, 50, 50, 50, 51] -> "0111122223"

The program 0111122223 should then be able to take the list [48, 49, 49, 49, 49, 50, 50, 50, 50, 51] and return the original [97, 98, 99, 100, 101]

Notes/Rules

  • Output must be deterministic
  • Output can't be a list of lists (no list of pairs)
  • The second program may assume that the length of the input is even (so you don't need to account for a leftover element)
  • Submissions can be functions, programs or different for either part
  • Remember, output must be a list of positive integers.
    • You won't get 1 in the input of the first program
  • The programs have to be in the same language
  • Input can be a string or list of characters instead
| |
\$\endgroup\$
  • \$\begingroup\$ Knowing that low (<32) and high (>127) bytes in most languages are useless or cause errors, it will be difficult to use any lowercase letters in the second program, which might be a problem for case sensitive languages. Braces are nearly impossible without using the high bit, '{' and '}' are 123 and 125, and the maximum ascii value is 126. Not necessarily deal breakers, but important to know. \$\endgroup\$ – Hiatsu Aug 23 '19 at 2:45
  • \$\begingroup\$ Can the two programs be in different languages? \$\endgroup\$ – Hiatsu Aug 23 '19 at 21:05
  • \$\begingroup\$ @Hiatsu I knew I had forgotten to specify something. No, they can't \$\endgroup\$ – Jo King Aug 24 '19 at 4:33
0
\$\begingroup\$

Golfed User-Pinging

In this task, given a user's display name(e.g. Display Name) from which the first space has been removed, use the API to find a user it could be and output their display name up to the first space. Both input and output must have an @ prefixed to them.

The input will not contain spaces in it, so you have to search for the user before doing this.

If the user does not exist, do anything else other than outputting the golfed user ping. (That includes outputting to stderr.)

Rules

  • This is a contest; the shortest answer wins.
  • No standard loopholes allowed. (In fact, hard-coding the answers will annoy other users, as you pinged all of them.)
  • The space (U+0020) is the word separator.
  • Input will not contain display names containing 2 or more spaces.
  • In addition to golfing the code, the answers also have to use as few calls as possible. (Due to the rate limiting of the API)
  • The input will always be case-insensitive and are restricted to printable ASCII.

    Examples

@Dennis -> @Dennis
@MartinEnder -> @Martin
@Cowsquack -> @Cows
@JB -> @J
@tsh -> @tsh
@cairdcoinheringaahing -> @caird


Sandbox

  • Is it a duplicate?
  • Do you understand everything here?
  • Any suggestions for test cases?
  • Most importantly, can you cheat over this challenge by using an algorithm not requiring network access?

| |
\$\endgroup\$
  • 2
    \$\begingroup\$ Pings don't work like this. Maybe you can instead phrase the challenge as "given a CGCC comment mention (@username), reduce it to the first word of the CGCC username it refers to". Also, internet is a better tag than networking for here. Finally, what happens when the the ping/mention can refer to more than one user? \$\endgroup\$ – Erik the Outgolfer Aug 23 '19 at 18:48
  • 2
    \$\begingroup\$ This is underspecified. It needs an unambiguous way of determining the word separation. \$\endgroup\$ – Peter Taylor Aug 24 '19 at 6:19
  • 2
    \$\begingroup\$ 1. I overlooked that this was supposed to be a stack-exchange-api question. That should be made explicit. 2. Since it's an API question, it would be best to use the same terminology as the API: i.e. display name instead of username. 3. If space (U+0020) is the word separator, make that explicit. It seems the TL;DR would be "Given a user's display name from which the first space has been removed, use the API to find a user it could be and output their display name up to the first space. Both input and output must have an @ prefixed to them." \$\endgroup\$ – Peter Taylor Aug 24 '19 at 7:20
  • 3
    \$\begingroup\$ 4. Making potentially 30+ calls to the API per input is going to make this hard to test without running foul of the rate limiting, so I would suggest explicitly requiring the answers to use as few calls as possible. 5. What about case? If the match should be case-insensitive, either the input should be restricted to ASCII or the way StackExchange handles case conversions should be explicitly documented. The former option seems preferable, because otherwise it randomly boosts languages with default behaviour which matches and penalises those which don't. \$\endgroup\$ – Peter Taylor Aug 24 '19 at 7:23
0
\$\begingroup\$

Self-fibonaciing prophecy

Where L is the length in bytes of your source code
and n=(L+2)^2

Your program or function should output Fib(n)

Your program or function should take no input, and output according to the usual code golf rules

This seems like a very simple task, but the (L+2)^2 part is designed to prevent single digit outputs which could be hard coded in many languages, and ensure the required output will usually be longer than the source code, so it should produce some interesting mathematical answers

Question for meta - Is (L+2)^2 suitable for this?
Another possibility I have considered is (L^2)+8 which would grow a bit slower but still achieve the goal of preventing trivial solutions
Having a look at this again, I think the sequence still grows too quickly. Maybe n=ceil(L^1.5)+18 would be more reasonable

Another question for meta - Should I include a link to a source of Fibonaci numbers, or is it safe to assume readers know what this is and can find it for themselves?

| |
\$\endgroup\$
  • 2
    \$\begingroup\$ It probably isn't exactly necessary to explain the Fibonacci sequence, but I think it would be better if you did. You should explicitly state what you consider to be valid initial points (i.e. is Fib(0), Fib(1) = 0, 1 acceptable? I'm not sure about the scaling sequence, but I do think your idea for preventing too many trivial answers is a good one. You might consider just doing L+100 since that should probably make hardcoding inefficient but make calculation much easier. \$\endgroup\$ – FryAmTheEggman Aug 27 '19 at 14:54
  • 1
    \$\begingroup\$ The point of (L+2)^2 or whatever other function seems to be to push answers towards actually implementing a general-purpose Fibonacci function, but that makes the question a dupe of the general-purpose Fibonacci question. I don't see how this can be rescued: either it's a dupe, or it's possible to "cheat". \$\endgroup\$ – Peter Taylor Aug 27 '19 at 19:36
  • 1
    \$\begingroup\$ @Peter Taylor Here Fib(n) is a number that depends only on the program length but not any other input. It is unnecessary to implement a general purpose Fibonoacci function at all. You can just print a Fibonoacci number and tune the length of the program to fit the requirement. \$\endgroup\$ – Joel Aug 28 '19 at 1:14
  • \$\begingroup\$ @JoKing print(34) is 7 bytes, which would require to output a 9 digit number not a 2 digit number. If you simply change the 34 for the required 9 digit number, then your solution becomes 14 bytes, so would then require to output a 15 digit number rather than 9, and so on. The output is always longer than your source code, hence trival answers are by design, not possible \$\endgroup\$ – Darren H Aug 28 '19 at 8:32
  • \$\begingroup\$ @FryAmTheEggman the L+100 idea seems promising, but I fear it would then open up to trival answers for high L, indeed JoKing's point would then run true for L>33. I feel the function needs some kind of exponent to eliminate that problem \$\endgroup\$ – Darren H Aug 28 '19 at 8:38
  • \$\begingroup\$ Ah oops, I missed that part and thought it was just Fib(length). Still, it's then going to be either base compression for golfier languages, or just indexing into the fibonacci list at the correct index \$\endgroup\$ – Jo King Aug 28 '19 at 9:24
  • \$\begingroup\$ @Joel, I said "push towards" not "absolutely require". The key points are: (1) if it's competitive to tweak an answer from the general-purpose Fibonacci question, this is a dupe; (2) if it isn't then setting n=(L+2)^2 would seem not to have done its job. \$\endgroup\$ – Peter Taylor Aug 28 '19 at 9:46
  • \$\begingroup\$ @Peter Taylor I see your point. However, since the problem does not require to output the entire Fibonacci sequence, it is allowed to use any function to generate a single Fibonacci number that fits the requirement. There might be a non-trivial solution that is shorter than a general-purpose Fibonacci function implementation. \$\endgroup\$ – Joel Aug 28 '19 at 13:10
  • \$\begingroup\$ I'm mostly curious about the calculation of n, I want to design it in such a way that it prevents trival answers, as my goal with this challenge is to see some interesting calculations. Trivial answers for very low L or very high L will be possible with a poorly chosen formula. This might even be a question for math.se \$\endgroup\$ – Darren H Aug 30 '19 at 12:39
  • \$\begingroup\$ After some further research and experimentation, I'm considering using n=(L+2)*5 this is the slowest growing formula I have found so far that fits the requirements \$\endgroup\$ – Darren H Aug 30 '19 at 12:52
0
\$\begingroup\$

Compare Multiratios

EDIT: No doesn't make any sense, I need to think about it again.

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

Smallest self-hosting golfing language

Implement a golfing language that is implemented in itself. Formally speaking: The original compiler O (an executable file assumed to be implemented in other language X), when given its own source code S (in golfing language G), must produce the identical executable file O. Your task is to create G by implementing O.

Language X might be an ELF binary, x86 assembly, Python, LLVM IR, etc.

Neither your language implementation S or executable file O may use functions that have the same functionality as exec or eval. That is, you may not use any built-in code evaluation of your compiler O. Standard Quine loopholes, such as reading your own file, downloading your file from the internet, and holding the program data in the filename, are not allowed.

The executable file O of the compiler must not be embedded in any shape or form in the source code S of the compiler. Additionally, the source code S of the compiler must be strictly shorter than the executable file O.

Your code must not be dependent on any external program or internet resource other than language X: if your language is dependent, include the size of the external dependencies in your submissions. Your program gets a standard POSIX system base for free, plus GCC 8 and LLVM 10: you do not have to include the size of this in your submission. For example, if you submitted a Bash Quine that just invoked the 05AB1E interpreter via the command line, you would have to include both the size of the the 05AB1E implementation, as well as the size of the Elixir implementation (since 05AB1E is implemented in Elixir and your base language X isn’t Elixir) in your bytecount.

In addition, standard golfing loopholes apply.

Requirements

Answer at least 10 other challenges on this website, beating at least 50% of the existing answers in byte size.

  • Only challenges that already have 4 answers are allowed
  • Your own other answers to the challenges do not count
  • Challenges newer than this post are allowed, as well as answering your own challenges; provided they already have 4 answers from other answerers
  • Link to your challenge answers for verification

Base language X restrictions

Language X must have an implementation, in other words we must be able to actually run your compiler O with source code S and get compiler O as output.

Language X must have already existed before this post was created.

You are encouraged to share the techniques of how you created your compiler and how you came up with your golfing language.

Scoring

$$S*0.98^{(n-10)}$$ where n is the number of challenges where you beat at least 50% of the other answers, not including your own.


Feedback

How can you prevent someone submit a zero bytes answer and claim: if source is empty, it output the interpreter, otherwise output compiled python code. – tsh Aug 24 at 4:42

| |
\$\endgroup\$
  • 1
    \$\begingroup\$ I don't think your requirements are a good way to handle making people use a real language. It doesn't do much to prevent abuse and is extremely tedious to perform and to verify. I'm sorry I don't have an alternative, but I think that may be a red flag that this won't work very well on this site. \$\endgroup\$ – FryAmTheEggman Sep 3 '19 at 3:49
  • \$\begingroup\$ Is there a reason languages created after this is posted are not eligible? \$\endgroup\$ – trichoplax Sep 3 '19 at 11:42
  • \$\begingroup\$ It may be worth explicitly stating that lowest wins in the scoring section. \$\endgroup\$ – trichoplax Sep 3 '19 at 11:43
0
\$\begingroup\$

DRAFT: Remove vertical comments

Vertical commnets...

        /> push back with    result.cows_used.push_back(possiblecow);
        the new cow added </ cows_used.push_back(result.cows_used);

Traditional comments...

        /* push back with */ result.cows_used.push_back(possiblecow);
     /* the new cow added */ cows_used.push_back(result.cows_used);


        result.cows_used.push_back(possiblecow);
        cows_used.push_back(result.cows_used);

Annotated, everything with . is considered a comment:

        />.................. result.cows_used.push_back(possiblecow);
        ..................</ cows_used.push_back(result.cows_used);

What is both between the start row of /= and the end row of =/ and between the start column of /= and the end column of =/ is considered a comment.

        /> push back with       result.cows_used.push_back(possiblecow);
           the new cow added </ cows_used.push_back(result.cows_used);

Todo

  • Add testcases
| |
\$\endgroup\$
  • \$\begingroup\$ Can vertical comments be nested or overlapped? \$\endgroup\$ – Hiatsu Sep 4 '19 at 18:03
  • \$\begingroup\$ Will this language have string literal? \$\endgroup\$ – tsh Sep 5 '19 at 5:23
0
\$\begingroup\$

Breaking the Wordbuilder (canned)

Dupe of Let's Play Countdown! ... oh well. Thanks for the feedback everyone.

| |
\$\endgroup\$
  • \$\begingroup\$ Does the dictionary have to be encoded in submissions, or may they take it as input? \$\endgroup\$ – Unrelated String Aug 22 '19 at 9:24
  • \$\begingroup\$ Take it as input. Assume unix-words is a local file. \$\endgroup\$ – roblogic Aug 22 '19 at 9:26
  • 1
    \$\begingroup\$ similar question.. Solve an Anagram, this one is possibly a bit easier \$\endgroup\$ – roblogic Aug 22 '19 at 15:35
  • \$\begingroup\$ can languages without file io take unix-words as input? \$\endgroup\$ – SuperStormer Aug 22 '19 at 16:33
  • \$\begingroup\$ @SuperStormer, sure thing \$\endgroup\$ – roblogic Aug 22 '19 at 16:44
  • \$\begingroup\$ So the two differences between this and that are that the input is always precisely five characters long and output words can leave out one or two of them. I'm not really sure if this is a duplicate, but it's close \$\endgroup\$ – Unrelated String Aug 22 '19 at 18:23
  • 2
    \$\begingroup\$ The line about the standard loopholes seems to be a bit misleading - the standard loopholes do not ban built-ins that solve the problem. They primarily ban abusive an uninteresting answers like print("an anagram solver"). I'm not sure exactly what you want, but it certainly isn't to allow all loopholes. Unrelatedly, the other challenge included multi-word solutions, so I doubt that answers to either can be trivially used for the other without being highly uncompetitive. \$\endgroup\$ – FryAmTheEggman Aug 22 '19 at 18:46
  • \$\begingroup\$ Thanks @FryAmTheEggman, restored the anti-loophole rule. \$\endgroup\$ – roblogic Aug 22 '19 at 18:51
  • 1
    \$\begingroup\$ A closer previous question is Let's play countdown. IMO the change from 9 input letters to 5 is completely trivial, and the change in the filtering is still sufficiently trivial to count as a dupe. \$\endgroup\$ – Peter Taylor Aug 23 '19 at 7:37
  • \$\begingroup\$ @Peter Taylor, you are correct! I'll take this down 😭 \$\endgroup\$ – roblogic Aug 27 '19 at 3:29
0
\$\begingroup\$

I see your BIDMAS and raise you a BADMIS

Challenge

Given a set of numbers with operators between them: "5 + 4 * 9 / 3 - 8", return all the possible results of the expression for every permutation of the order of basic operations: [/, *, +, -].

Rules

  • Standard loopholes forbidden
  • I/O
    • Input must be ordered with infix operations, but however that is easiest (string or array)
    • Output must be all of the possible results of the expression, no specified format or order, no repeats
  • All of the inputs are valid (e.g. do not need to deal with "7 / 3 + *"
  • Operators are all left-associative so "20 / 4 / 2" = "(20 / 4) / 2"
  • This is Code Golf so fewest number of bytes wins

Test Cases (With explanation)

  • "2 + 3 * 4" = [14, 20]
    • 2 + (3 * 4) ⟶ 2 + (12) ⟶ 14
    • (2 + 3) * 4 ⟶ (5) * 4 ⟶ 20
  • "18 / 3 * 2 - 1" = [11, 6, 2, 6]
    • ((18 / 3) * 2) - 1 ⟶ ((6) * 2) - 1 ⟶ (12) - 1 ⟶ 11
    • ((18 / 3) * (2 - 1) ⟶ (6) * (1) ⟶ 6
    • (18 / (3 * 2)) - 1 ⟶ (18 / (6)) - 1 ⟶ (3) - 1 ⟶ 2
    • 18 / (3 * (2 - 1)) ⟶ 18 / (3 * (1)) ⟶ 6

Test Cases (Without explanation)

  • "45 / 8 + 19 / 45 * 3" = [5.765740740740741, 0.01234567901234568, 0.11111111111111113]
  • "2 + 6 * 7 * 2 + 6 / 4" = [154,113.5,88]
| |
\$\endgroup\$
  • 1
    \$\begingroup\$ I'd recommend removing the bonus part, since people aren't going to do more than they have to, especially if it's code-golf (which I'm assuming this is, though you haven't specified it) \$\endgroup\$ – Jo King Sep 5 '19 at 0:03
  • \$\begingroup\$ An I/O question: would providing 45 / 8 as 45.0 / 8.0 to the program be allowed? I suggest allowing it. This frees users working in languages which parse integers as integer types by default to work on the problem you ask, instead of an ancillary problem. \$\endgroup\$ – GammaFunction Sep 8 '19 at 6:22
  • \$\begingroup\$ @GammaFunction Yes that would be fine \$\endgroup\$ – Freddie R Sep 8 '19 at 22:02
0
\$\begingroup\$

The Rattlin' Bog

There's a relatively famous Irish Folk song called The Rattlin' Bog, a type of cumulative song.

Basically, this is a song that alternates between a chorus and an ever-expanding verse

The chorus is as follows:

Ho, ro, the rattlin' bog
The bog down in the valley-o
Ho, ro, the rattlin' bog
The bog down in the valley-o

Each verse starts with
And on that [x] there was a [y]
A rare [y], a rattlin' [y]

and ends with
In the bog down in the valley-o

Where x is the word from the previous round, and y is the word for the current round. In between these two is the cumulative part. It starts with:
The [z] in the bog

Each new round adds
The [y] in the [x] and
To its predecessor.

Challenge

Given a list of strings, character arrays, or whatever reasonable equivalent collection of string or your language equivalent thereof, Your job is to print the lyrics to the song, alternating between the chorus and each verse, starting and ending with the chorus. The chorus and verse must be separated by a pair of newlines.

For the sake of brevity, you may write the chorus once, preceded by "[Chorus]" then write "[Chorus]" in place. I'm also omitting any sort of final verse with unique text.

Example:

Input: [House, Roof, Nest] 
Output:
[Chorus] 
Ho, ro, the rattlin' bog 
The bog down in the valley-o 
Ho, ro, the rattlin' bog 
The bog down in the valley-o  

And on that bog there was a House
A rare House, a rattlin' House
The House in the bog
In the bog down in the valley-o

[Chorus]

And on that House there was a Roof
A rare Roof, a rattlin' Roof
The Roof in the House and the House in the bog
In the bog down in the valley-o

[Chorus]

And on that Roof there was a Nest
A rare Nest, a rattlin' Nest
The Nest in the Roof and the Roof in the House and the House in the bog
In the bog down in the valley-o

[Chorus]
| |
\$\endgroup\$
  • \$\begingroup\$ I'm terrible at formatting, so if anyone wants to tell me the best formatting for this or edit and reformat it, that's fine. Of course, feedback is also appreciated to make this more readable or easier to understand. Also if/when I put it up on main golf, i'll add the standard disclaimers about abusing loopholes and least bytes wins, etc. \$\endgroup\$ – Andrew Baumher Sep 5 '19 at 16:27
  • 4
    \$\begingroup\$ I'm not sure that adding input is enough to make a difference between this and something like There was an Old Lady or There's a hole in the bottom of the sea... \$\endgroup\$ – AdmBorkBork Sep 5 '19 at 17:53
0
\$\begingroup\$

Sliding window minimum

The task is, given a number array of length n and a positive integer k, to compute the smallest values in all its overlapping consecutive subarrays of length k in the order they occur.

The complexity of your program should be no more than linear with respect to n with a constant added.

Input

A positive integer array of length n and a positive integer k (k <= n)

Output

A positive integer array with n - k + 1 elements, where the element at position i equals the least number in the subarray starting at i and ending at i + k - 1 inclusive.

Examples

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

TODO

Sandbox stuff

  • Is it acceptable to combine code-golf with restricted-complexity?
  • Is the complexity restriction clear enough?
  • Is it a good idea to allow calculating the maximum instead of the mininum?
  • Is the grammar correct enough?
  • Has this been posted before?
  • Do any languages have built-ins for this?
| |
\$\endgroup\$
  • \$\begingroup\$ Close to a dupe. This one adds the minimum operation; you don't need to modify the programs in that question too much in order to post submissions for this challenge. \$\endgroup\$ – user85052 Sep 7 '19 at 14:08
  • \$\begingroup\$ @A_ I'm perfectly certain it's not, that one doesn't have [restricted-complexity]. \$\endgroup\$ – my pronoun is monicareinstate Sep 7 '19 at 14:12
  • \$\begingroup\$ This functionality is needed in statistical analysis so I think many statistics / data analysis oriented languages or libraries would have a built-in for it. For example, this is the built-in in the R language. \$\endgroup\$ – Joel Sep 7 '19 at 15:24
  • \$\begingroup\$ Meanwhile, the complexity of the function depends also on k. So "linear with respect to n with a constant added" might not be accurate enough without mentioning k. It would be better to say sth. like "linear with respect to n for each fixed value of k". \$\endgroup\$ – Joel Sep 7 '19 at 15:28
  • \$\begingroup\$ @Joel Are you sure? I think I can compute it with a deque in O(n + k), and since k <= n that is linear with respect to n. \$\endgroup\$ – my pronoun is monicareinstate Sep 7 '19 at 15:31
  • \$\begingroup\$ A basic deque-based approach needs O(k) time to calculate or updating the minimum for each window in the worst case so the overall worse-case complexity would be O(n + k(n - k)) which is the same as O(n + kn). If you use a priority queue the complexity can be driven down to O(n+nlogk) or maybe O(n) only for some very advanced implementations. How do you implement that in O(n + k)? \$\endgroup\$ – Joel Sep 7 '19 at 15:53
  • \$\begingroup\$ @Joel reference O(n) implementation by me \$\endgroup\$ – my pronoun is monicareinstate Sep 7 '19 at 16:07
  • \$\begingroup\$ @someone OK. That algorithm is O(n). So you may emphasize in the description to say "The complexity of your program should be O(n), i.e. linear with respect to n for every possible k <= n", in case anyone wonders about whether an O(n + nlogk) implementation is accepted. \$\endgroup\$ – Joel Sep 7 '19 at 16:39
  • \$\begingroup\$ @Joel I'd argue O(n log k) is not really linear with respect to n (since that equals O(n log n)) \$\endgroup\$ – my pronoun is monicareinstate Sep 8 '19 at 6:04
  • \$\begingroup\$ @someone O(nlogk) is not linear with respect to n but it is also different from O(nlogn) because it really depends on the input k. If k has another constraint itself (e.g. an upper bound that is better than O(n)), O(nlogk) could be better than O(nlogn). That is why I suggested you clearly mention about the range of k in the complexity description to avoid any possible confusion, as I showed in my comment. \$\endgroup\$ – Joel Sep 8 '19 at 6:26
0
\$\begingroup\$

Random point on a sphere

Posted here

| |
\$\endgroup\$
  • \$\begingroup\$ Must we use a specific distribution (e.g. uniform or normal) or is the "bias" acceptable? \$\endgroup\$ – Erik the Outgolfer Aug 23 '19 at 10:11
  • \$\begingroup\$ @EriktheOutgolfer My intention was to require a strictly uniform distribution. I do realize that it is unreasonable to verify this for each and every submission, so I have not made up my mind about that yet. Any suggestions? \$\endgroup\$ – Jitse Aug 23 '19 at 10:18
  • \$\begingroup\$ I'm in favor of just using the community default for randomness, that is, if every possible output (up to precision limits) has a non-zero probability of being returned, the submission is valid. \$\endgroup\$ – Erik the Outgolfer Aug 23 '19 at 10:24
  • \$\begingroup\$ @EriktheOutgolfer That sounds like a decent compromise. I'll update the challenge. \$\endgroup\$ – Jitse Aug 23 '19 at 10:43
  • \$\begingroup\$ Hm... another thing I noticed is that you have 6 tags, you can't have more than 5. \$\endgroup\$ – Erik the Outgolfer Aug 23 '19 at 11:13
  • \$\begingroup\$ @EriktheOutgolfer I was unaware of that. I'm removing number-theory, since it is the least fitting. \$\endgroup\$ – Jitse Aug 23 '19 at 11:20
  • \$\begingroup\$ Also, thanks for your feedback! \$\endgroup\$ – Jitse Aug 23 '19 at 11:20
  • 6
    \$\begingroup\$ Pick three uniform random numbers and normalise? I think it would be a more interesting question with the requirement that if the random number generation were substituted with true RNG from the reals then the distribution would be uniform. That opens up a variety of approaches. \$\endgroup\$ – Peter Taylor Aug 24 '19 at 6:29
  • \$\begingroup\$ @PeterTaylor That was my original intention and I agree that it would definitely be more interesting. However, I thought it might be tricky to verify the uniformity of the distribution for each submission. Your reference could help with that, though. \$\endgroup\$ – Jitse Aug 24 '19 at 9:52
  • \$\begingroup\$ I seem to remember a similar challrnge about uniformly generating random numbers that add up to the input, but I can't find it. Not that this is a duplicate, just related \$\endgroup\$ – Jo King Aug 25 '19 at 12:17
  • \$\begingroup\$ The challenge as it stands is impossible. Since there are more real numbers than finite binary strings you cannot represent real numbers in the output of a program. It is going to be impossible to create a program that has a chance of outputting all the real numbers on the surface of a sphere. You likely need to restrict the output domain in some way. \$\endgroup\$ – Ad Hoc Garf Hunter Aug 25 '19 at 14:27
  • 1
    \$\begingroup\$ @JoKing perhaps this? \$\endgroup\$ – Giuseppe Aug 27 '19 at 14:14
  • 1
    \$\begingroup\$ @PeterTaylor Since your comment has received quite some upvotes, I have reverted the challenge back to requiring a uniform distribution. I included some examples of how to achieve this. Do you think it is clear enough like this? \$\endgroup\$ – Jitse Aug 28 '19 at 8:21
  • 1
    \$\begingroup\$ I think it's preferable to talk about being "theoretically uniform": to achieve actual uniformity "up to the precision limits of your language" would probably require using intermediate values of greater precision. Finding the right wording for questions which use floating point numbers is hard! \$\endgroup\$ – Peter Taylor Aug 28 '19 at 8:30
  • 1
    \$\begingroup\$ In your second and third remarks, it would be good to point out that the random numbers in these invalid schemes are uniform. As shown in the fourth remark, both schemes are valid with specific non-uniform distributions. \$\endgroup\$ – Nitrodon Aug 29 '19 at 21:28
0
\$\begingroup\$

Consequence Golf

As specified by this xkcd, Consequence Golf is a game involving golfing and bombs. The most obvious consequence of bombs: damage to the course.

A Consequence Golf course consists of the union of several circular regions. The tee is located at (0, 0). On each turn, you hit the ball bomb in a direction at some power. The bomb will travel in that direction for a distance equal to the power of the shot, unless it hits the edge of the course, in which case it will explode immediately. When a bomb explodes, it adds to the course a new circle centered on the point of contact with a radius equal to the remaining power. The next shot starts at the last location of the bomb.

Input

  • A list of circles, consisting of x/y coordinates and a radius for each circle, taken in any convenient format. If you must define a custom data type to store this information (e.g. struct c{float x,y,r};), you do not need to count that added boilerplate, nonproductive storage code in your byte count. Any code that does something besides specify storage must be counted.
  • A list of strokes, consisting of an angle in radians or degrees, and a power, again in any convenient format.

Output

  • The location of the bomb after the final stroke.

Additional Rules

  • The course will always contain at least one circle
  • The course will always contain the tee at (0, 0)
  • If there are zero strokes, you must return (0, 0)
  • The power of a stroke will never be zero
  • "Any convenient format" means a list of lists, list of C structs, a list for each input (x, y, radius, power, angle), or similar. You may not take information that is not as it is specified. For example, you may not take the circles as a series of equations.
  • This is consequence code golf, so shortest code in bytes wins.

Test cases

TBD...

Questions for META:

  • Is the description of how to play Consequence Golf clear enough?
  • Is the description of the input clear enough? This is my main concern.
  • Is allowing boilerplate storage code outside of the byte count an issue?
  • I am considering allowing fixed point approximations with at least 4 bits of precision for languages without floating point (and languages with, if it somehow makes things shorter). Would this cause any unintended issues?
| |
\$\endgroup\$
  • \$\begingroup\$ The overview talks about the bomb hitting walls, but the input section doesn't include anything about the walls. \$\endgroup\$ – Peter Taylor Sep 9 '19 at 8:27
0
\$\begingroup\$

Posted to PPCG

| |
\$\endgroup\$
  • \$\begingroup\$ When I first posted this to PPCG, @PeterTaylor pointed out that although outputs to a file are allowed for King of the Hill challenges, it is preferable to use STDIN/STDOUT. But the fundamental problem is that programs need a standard venue, hidden from each other but not from the Arbiter, in order to Exchange. \$\endgroup\$ – Purple P Sep 9 '19 at 16:02
  • \$\begingroup\$ If you fail to challenge an assassination, how do you format losing two cards? \$\endgroup\$ – Hiatsu Sep 9 '19 at 16:07
  • \$\begingroup\$ @Hiatsu Assuming you mean "incorrectly challenge" or "get caught falsely blocking with a Contessa", you don't need to. You write the card you lost from the failed challenge, then the Arbiter detects that the move was an Assassination and you are eliminated. But your wording could be interpreted another way: I noticed in your answer to the original challenge that you always challenged or blocked Assassinations. You don't have to do that. You can surrender one of your cards right away. \$\endgroup\$ – Purple P Sep 9 '19 at 16:12
  • \$\begingroup\$ As long as each program has its own STDIN/STDOUT (using player=subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE); player.stdin.write("I\n"); response=player.stdout.read(1); or similar), you can copy across communication from the other player but restrict exchanges to the relevant player. \$\endgroup\$ – Hiatsu Sep 9 '19 at 16:16
  • \$\begingroup\$ Also, if you are willing to translate the Arbiter into Java, you can use this to arrange games. \$\endgroup\$ – Hiatsu Sep 9 '19 at 16:18
  • \$\begingroup\$ @Hiatsu Done! ...Well, kind of. I gave up and posted the original question. See my explanation in the comments here. \$\endgroup\$ – Purple P Sep 10 '19 at 0:09
0
\$\begingroup\$

Encode MC6000 instructions

In MC6000, these are registers list:

acc  general
dat  general
p0   simple pin
p1   simple pin
x0   XBus pin
x1   XBus pin
x2   XBus pin
x3   XBus pin
null pseudo

where general means reading from it returns the last value written to it, XBus pin means reading from or writing to it have side effect(so teq x0 x1 doesn't equal to teq x1 x0) that nothing else can take its place, simple pin means reading from it also writes zero to it(so mov 0 p0 equals to mov p0 null), writing anything to it(including the writing while reading) have side effect, and writing anything larger than 100 behaves same as writing 100, as well as writing anything smaller than 0 same as writing 0(so mov -4 p0 equals to mov 0 p0). Writing anything into null has no effect and reading from null returns 0(so mov 9 null equals to nop).

The instruction set is:

nop            Do nothing
mov R/I R      operand2:=operand1
jmp L          Jump to the specified line(can't be replaced)
slp R/I        Sleep for the number of time units specified by the operand.
slx XP         Sleep until XP avaliable(can't be replaced)
add R/I        acc:=f(acc+operand)
sub R/I        acc:=f(acc-operand)
mul R/I        acc:=f(acc*operand)
not            acc:=100(if acc was 0) or 0(otherwise)
dgt R/I        acc:=(acc/10^operand)mod 10(if operand in 0,1,2) or 0(otherwise)
dst R/I R/I    acc:=[abs(acc-[(acc/10^operand1)mod 10]*10^operand1)+abs(operand2 mod 10)*10^operand1]
               *sgn(operand2*2+1) (if operand1 in 0,1,2) or acc(otherwise)
teq R/I R/I    sym:='+'(if operand1 equals to operand2) or '-'(otherwise)
tgt R/I R/I    sym:='+'(if operand1 is larger than operand2) or '-'(otherwise)
tlt R/I R/I    sym:='+'(if operand1 is smaller than operand2) or '-'(otherwise)
tcp R/I R/I    sym:='+'(if operand1 is larger than operand2),
               '-'(if operand1 is smaller than operand2) or '*'(otherwise)
gen SP R/I R/I Output 100 and 0 to the simple pin for the latter two operands, inclusive

Here R/I means a register or an integer between -999 and 999, inclusive; R means a register, L means an integer between 0 and 13, XP means an XBus pin, and SP means a simple pin. f(x) is defined as min(999,max(-999,x)). Sleeping for zero or negative time units sleep for time as long as a nop(so nop=slp 0=slp -3).

Before an instruction, symbol +, - and @ can exist, meaning that the instruction is executed only if sym is +, - and @, inclusive. (@ doesn't work like this in fact but that doesn't affect this problem) Whether the instruction is executed matters even if it's a no operand(so + nop doesn't equal to - nop, but + nop equals to + tgt 1 0).After an instruction, comment start with # can exist, which can be ignored. Spaces work as separator, and can be added or removed as long as words are separated in the same way(continious spaces, spaces at both side).


Now, write a program A that, reads an instruction and output an integer; and write a program B that reads the output of program A and output an equal or same instruction fed into program A.

Your score will be the sum of length of program A, length of program B, and difference between the maximum and minimum of output of A. Lowest score in every language win.

Using MetaGolfScript is allowed. Only write MetaGolfScript in language area and show the exact version you use in code area to make it leaderboard friendly.

| |
\$\endgroup\$
  • 1
    \$\begingroup\$ Why do you allow MetaGolfScript? Does it add anything to the challenge apart from a boring winner answer? \$\endgroup\$ – wastl Sep 9 '19 at 13:47
  • \$\begingroup\$ @wastl Pure (difference between the maximum and minimum of output of A) worth optimize \$\endgroup\$ – l4m2 Sep 10 '19 at 4:39
0
\$\begingroup\$

Tiling string

(inspired by the tiling pattern effect tipic of the texturing of landscapes. This question I would like to ask is not exactly the same thing but close to it, or at least it was the idea)

Given a string S check if it has some repeating tile pattern and prove it providing at least one repeating pattern.

A substring tile pattern is a pattern of any form which repeats inside the string where one or more(alpha case insensitive) chars forms a fixed structure that can contain also others generic chars(all printable ascii table set)

the output will be false,0,f or any meaningful negative expression if no tiling occours. true,1,... plus at least one repeating tile pattern instead.

for example :

 they were obsessed by the sessions
             s#ss          s#ss
 outputs : true s#ss



look at that truck
      t$t  t$t
outputs: 1,t$t


- case insensitive. 
- only letters matters as tile pattern.   
- if two letters are separated by space or every non letter char they still form a pattern.  
- overlaps doesnt make tiling effect.
- a pattern must contain half or more structure chars and must be len>1 to be considered, and must be repeated at least two times.

example

"Hello world!"=> false or 0 or f or no

"aa"=>false.
aaa=>false. "aaaa"=> true(aa)
"aabbabab"=>true:ab,ba or 1 ab ba. or yes[ba,ab] or...any reasonable format

This is code-golf so shortest answer wins

| |
\$\endgroup\$
  • \$\begingroup\$ Can you give an example of a tile with non-structure characters? \$\endgroup\$ – Hiatsu Sep 14 '19 at 21:51
  • \$\begingroup\$ Hi!ho!bushy! => h#! Here h and ! Form a pattern of 2 fixed symbols and one ?symbol between. But it's not valid as ! is not an alpha character. The same applies to spaces or , or . for example.. \$\endgroup\$ – AZTECCO Sep 15 '19 at 8:06
0
\$\begingroup\$

Posted

| |
\$\endgroup\$
  • \$\begingroup\$ An explanation of how to form rectangular prisms out of factors would probably help. It seems like you're asking for the output to be every unordered multiset of three natural numbers the product of which is the input, in which case I'm not sure where the connection to geometry is, except in that each set can describe the dimensions of a rectangular prism among a host of other things. I guess because the volume of each prism is equal to the input? \$\endgroup\$ – Unrelated String Sep 14 '19 at 5:54
  • \$\begingroup\$ Why 1000 as the upper limit? I could understand 256 to cater for languages whose native type is the 8-bit integer, or \$2^{31}-1\$ for languages whose native type is the 32-bit signed integer, but 1000 seems completely arbitrary and I can't see any benefit to having the limit. \$\endgroup\$ – Peter Taylor Sep 14 '19 at 8:33
  • \$\begingroup\$ @PeterTaylor, I just thought 1000 was a nice number, but I've made is \$2^{31} - 1\$ now. \$\endgroup\$ – Lyxal Sep 14 '19 at 8:54
  • \$\begingroup\$ @UnrelatedString, see the updated post \$\endgroup\$ – Lyxal Sep 14 '19 at 8:54
0
\$\begingroup\$

Observability and controllability

Background

Note: if you like, you can skip this part, and simply implement the suggested algorithm.

When looking at dynamic systems, there exist the notions of observability and controllability. They are the answers to the questions

  • Observability: can we know all the internal states of a system by looking only at the given outputs?
  • Controllability: can we influence all the internal states of a system by changing only the inputs?

For the sake of this question, we will only look at LTI (linear time-invariant) systems, either in continuous or discrete time. During the rest of the question, I will only present the case of a state-space representation of the system and suggest using the observability- or controllability Gramian, but you may use any other algorithm to decide the controllability and observability of the given LTI system.

Input

You are given an LTI system, $$ \mathbf{\dot{x}}(t) = A\mathbf{x}(t) + B\mathbf{u}(t)\\\mathbf{y}(t)=C\mathbf{x}(t)$$ where \$A\$, \$B\$ and \$C\$ are matrices containing only integers. The dimensions of these matrices are \$\dim{A}=n \times n\$, \$\dim{B} = n \times p \$ and \$\dim{C} = q \times n\$ with \$n\$, \$p\$ and \$q\$ positive integers.

A suggested input method would be to simply input these matrices, but you may also take input as function objects, string representations of differential equations, or transfer functions.

Output

You must output two thruthy/falsy values, one indicating whether the system is controllable, and one indicating whether the system is observable. Order does not matter but must always be the same. Alternatively, you may have four distinct outputs (e.g., the integers 0, 1, 2, 3) each corresponding to a possible combination of controllability/observability.

Suggested algorithm

To decide on observability and controllability, you can calculate the so-called observability Gramian (a square matrix), $$\begin{bmatrix}C & CA & CA^2 & ... & CA^{(n-1)}\end{bmatrix}^T$$ and controllability Gramian $$\begin{bmatrix}B & AB & A^2B & ... & A^{(n-1)}B\end{bmatrix}$$ The system is observable iff the observability Gramian is invertible. The system is controllable iff the controllability Gramian is invertible. There are many ways to check if a matrix is invertible; an obvious way would be to check if the determinant equals zero.

Testcases

Coming soon, because I want to include worked examples.

| |
\$\endgroup\$
  • \$\begingroup\$ I'm not sure requiring tests for both is particularly meaningful, since an LTI system \$ (A, \cdot ,C) \$ is observable iff the LTI system \$ ( A^{T}, C^{T}, \cdot ) \$ is controllable. Otherwise I think this is fine (though for some reason I thought it had already been asked, but I didn't find a dupe). \$\endgroup\$ – FryAmTheEggman Sep 15 '19 at 18:54
  • \$\begingroup\$ @FryAmEggman True but I think almost-repetition can make golfing challenges interesting because there's so many ways to do that. \$\endgroup\$ – Sanchises Sep 15 '19 at 20:10
  • \$\begingroup\$ @FryAmTheEggman Thanks for the edits, by the way. \$\endgroup\$ – Sanchises Sep 15 '19 at 20:11
0
\$\begingroup\$

Posted

| |
\$\endgroup\$
  • \$\begingroup\$ So: If input ends with ? and has , return text from last , until before ? Else, if input ends with ? return No Else, return Yes. Right? \$\endgroup\$ – Adám Sep 15 '19 at 9:20
  • \$\begingroup\$ That's correct. \$\endgroup\$ – user85052 Sep 15 '19 at 10:55
  • \$\begingroup\$ Then maybe just say so instead of over-complicating it? \$\endgroup\$ – Adám Sep 15 '19 at 11:36
0
\$\begingroup\$

Smallest put pixel function

Task: write in any language which can generate images e.g. JavaScript, the smallest "put pixel" function. You can initialise screen inside that function or in separate code. The put-pixel function should consume 6 arguments: x,y,r,g,b,a where x,y pixel coordinates and r,g,b,a are color values (a is for alpha - transparency - you need blend new color with old color). Provide also example which use that function a draw something.

| |
\$\endgroup\$
  • 2
    \$\begingroup\$ I think the way this is worded will likely make it too broad or unclear. I think it would work better if you essentially required that this function be implemented to solve a more normal golf task that you could have test cases for. For example, "given vectors x,y,r,g,b,a produce an image where each pixel xi,yi has colour (ri,gi,bi,ai) and the rest are black". That would usually make submissions do as you say, but avoid ambiguities like "function" and "arguments" which may not exist precisely in languages people want to use. \$\endgroup\$ – FryAmTheEggman Sep 19 '19 at 19:45
0
\$\begingroup\$

Shuffling Sequence

Consider an operation \$f_n\$ that takes a length-\$2n\$ list and performs a perfect shuffle on it, reversing the second half. For example, \$f_4 [abcdABCD] = [aDbCcBdA]\$.

Under composition, \$f_n\$ generates a cyclic group. That is, there exists some minimal positive integer \$m\$ such that \$(f_n)^m\$ (which means \$f_n\$ composed \$m\$ times with itself) is an identical map, leaving its argument alone. Here's an example for ya:

$$f_2 [abAB] = [aBbA]$$ $$f_2 [aBbA] = [aABb]$$ $$f_2 [aABb] = [abAB]$$

$$f_2(f_2(f_2 [abAB])) = (f_2)^3 [abAB] = [abAB]$$

Since \$(f_2)^3\$ is the identity, we say that \$f_2\$ has order 3.

Your task is to determine the order of \$f_n\$. You've got a few choices for how to do this:

  • Take as input \$n\$ and provide as output the order of \$f_n\$
  • Take as input \$n\$ and provide as output the order of each \$f_k\$ for \$k < n\$ or for \$k \le n\$ (your choice)
  • Output these orders indefinitely, beginning with that of \$f_0\$ or \$f_1\$ (your choice)

For reference, the first 77 terms (beginning from \$n = 1\$; you may optionally return \$1\$ for \$n = 0\$) are provided below. This is OEIS A294673.

1,3,5,4,9,11,9,5,12,12,7,23,8,20,29,6,33,35,20,39,41,28,12,36,15,51,53,36,44,24,20,7,65,36,69,60,42,15,20,52,81,83,9,60,89,60,40,95,12,99,84,66,105,28,18,37,113,30,92,119,81,36,25,8,36,131,22,135,20,30,47,60,48,116,132,100,51,155

Standard IO and loophole rules apply. This is , so the shortest program (in bytes) in each language wins! Happy golfing!

| |
\$\endgroup\$
  • \$\begingroup\$ You may want to add a brief explanation that the exponentiation you use is on the group (i.e. repeated application of \$ f_{n} \$) and not the one most people are used to. I'd guess the average reader of this site doesn't know what a group is, but I may be being overcautious since it wouldn't really make much sense otherwise. Maybe run it by some people who don't know what groups are, if you can. \$\endgroup\$ – FryAmTheEggman Sep 19 '19 at 18:48
  • \$\begingroup\$ @FryAmTheEggman How's that? \$\endgroup\$ – Khuldraeseth na'Barya Sep 19 '19 at 18:55
  • \$\begingroup\$ I don't understand what you are asking, but maybe I rambled too much to be clear? Anyway, my point was that I think the challenge is good but it might come off as inaccessible to users with less maths background. I suggested asking other people (maybe someone you know or people in chat) about if the wording is confusing to them. \$\endgroup\$ – FryAmTheEggman Sep 19 '19 at 19:15
  • \$\begingroup\$ @FryAmTheEggman My mistake. What I meant was "Updated. What do you think now?" Dang ambiguity. \$\endgroup\$ – Khuldraeseth na'Barya Sep 19 '19 at 19:46
  • \$\begingroup\$ Haha, I didn't see the edit because I didn't scroll up, my bad. That definitely looks better, though I'm not exactly the target of my own comment. \$\endgroup\$ – FryAmTheEggman Sep 19 '19 at 19:49
0
\$\begingroup\$

Golf Golf!

This is my first challenge, so please be gentle! The challenge is to write a program that will output the correct score for a layout in the card game "Golf."

The card game Golf has many variations. The house rules I use follow the standard rules for Six-Card Golf given by Pagat, with one slight difference.

Each player has a 2x3 layout of cards. By the end of each round all cards are turned face up and scored as follows:

  • Each ace counts 1 point.
  • Each two counts minus two points.
  • Each numeral card from 3 to 10 scores face value.
  • Each Jack or Queen scores 10 points.
  • Each King scores zero points.
  • A pair of equal cards in the same column scores zero points for the column (even if the equal cards are twos).
  • Three equal cards in the same row scores zero points for the row (even if the equal cards are twos).

Input

The input can be a string or array of any kind.

Output

An integer representing the score of the Golf hand.

Examples

These use the notation A23456789TJQK.

Layout
AK3
J23

Score
7
-----------------------    
Layout
25Q
25J

Score
20
-----------------------        
Layout
T82
T8A

Score
-1
-----------------------        
Layout
QQQ
234

Score
5
-----------------------        
Layout
TJQ
QTJ

Score
60

This is code golf, so the shortest answer in bytes wins!

| |
\$\endgroup\$
  • \$\begingroup\$ I'd recommend allowing submissions to take the layout as a list of numbers with a reasonably convention. The point of this challenge appears to be in analysing the layout and applying the rules, not in performing string parsing. Otherwise this looks good to me. \$\endgroup\$ – FryAmTheEggman Sep 22 '19 at 20:07
0
\$\begingroup\$

Reorder a matrix, twice

You are given a square \$n \times n\$ matrix \$A\$, and a list (or vector) \$u\$ of length \$n\$ containing the numbers \$1\$ through \$n\$ (or \$0\$ through \$n-1\$). Your task is to reorder the columns and rows of the matrix \$A\$ according to the order specified in \$u\$.

That is, you will construct a matrix \$B\$ where the \$(i,j)\$-th element is the \$(u(i),u(j))\$-th element of \$A\$. You should also output the inverse of this action; that is, the (i,j)-th element of \$A\$ will end up at position \$(u(i),u(j))\$ in a new matrix \$C\$.

For example, given $$A = \begin{bmatrix} 11 &12& 13 \\ 21& 22& 23 \\ 31& 32& 33 \end{bmatrix},\quad u=\begin{bmatrix}3 & 1 & 2\end{bmatrix}$$

the output should be $$B = \begin{bmatrix}33 & 31 & 32 \\ 13 & 11 & 12 \\ 23 & 21 & 22 \end{bmatrix},\quad C= \begin{bmatrix}22 & 23 & 21 \\32 & 33 & 31 \\ 12 & 13 & 11 \end{bmatrix}$$

You may take input and output through any of the default I/O methods. You do not have to specify which matrix is \$B\$ or \$C\$, as long as you output both. You may assume \$A\$ only contains positive integers. You must support matrices up to at least size \$64 \times 64\$.

Example

===== Input =====
A =
 35     1     6    26    19    24
  3    32     7    21    23    25
 31     9     2    22    27    20
  8    28    33    17    10    15
 30     5    34    12    14    16
  4    36    29    13    18    11
u=
  3 5 6 1 4 2

==== Output =====
B = 
  2    27    20    31    22     9
 34    14    16    30    12     5
 29    18    11     4    13    36
  6    19    24    35    26     1
 33    10    15     8    17    28
  7    23    25     3    21    32
C = 
 17    15     8    10    28    33
 13    11     4    18    36    29
 26    24    35    19     1     6
 12    16    30    14     5    34
 21    25     3    23    32     7
 22    20    31    27     9     2
| |
\$\endgroup\$
0
\$\begingroup\$

No, you don't need deep learning: AlphaGo edition

To counter the industry fad of tackling the most trivial problems with deep learning, I propose that a proof of concept be created to demonstrate that simple database queries are in fact often sufficient. To wit, I propose to demonstrate that a database query can in fact be used to do what AlphaGo does and even do it better.

The proof of concept will consist of two parts:

  • A Script to create a script to create a (...) query that, given the state of a game of Go, will retrieve a move that will result in eventual victory.
  • A Script to create a script to create a (...) script to initialize the database.

This is a challenge so the least total bytes wins. Some rules for the challenge:

  1. You may use whatever database lets you golf best
  2. There is no limit for how many recursions of running the script generated by the script are required
    1. This includes directly submitting the query and (if the universe's hard drive capacity somehow permits) the init data
  3. The final init script may also be an executable-ish that connects to the database backend to populate it
  4. You may either pass the game state as an argument to the query or store the state in the database (storing the state in the DB doesn't count toward your score)
  5. Handicap games don't need to be accounted for.

The rules of Go are, paraphrased:

  • The game is played by placing black and white stones on a 19x19 grid
  • The aim of the game is to score points by surrounding open space with your stones (The edges of the board count as surrounding)
  • Black plays first
  • A stone is part of a group if it is orthogonally connected to a member of the group
  • The empty spaces orthogonally adjacent to a group are called liberties
  • A group without liberties is considered captured and is removed from the board and added to the capturing player's score
  • A group that can not escape capture is considered dead, and remains on the board but will count as captured for scoring purposes
  • Suiciding your own group by filling its last liberty is not allowed
  • Ko rule: If a single stone is captured so that capturing the capturing stone will return the board to the immediately previous state, such second capture is illegal on the turn after the first capture.
  • The game ends when both players pass, indicating that they can't improve their score by playing another stone.

| |
\$\endgroup\$
  • 1
    \$\begingroup\$ As it stands there doesn't seem to be any requirements on making actually good moves. I'm not convinced you can fix that well without making this into a king-of-the-hill which will be a lot more work for you. Further, I suspect databases that make any actually good moves will need to be so large that you may struggle to see meaningful results. \$\endgroup\$ – FryAmTheEggman Sep 21 '19 at 19:07
  • \$\begingroup\$ So it looks like you want us to write a solver for go that's optimal but likely extremely slow? I suspect this will come down to implementing the rules of Go then writing a simple recursive evaluation function, and that the Go rules will take most of the work. I'm confused where the database and scripts come in. \$\endgroup\$ – xnor Sep 22 '19 at 5:30
  • \$\begingroup\$ @xnor I thought this up when I read a rather ranty blog post (which I can't seem to find anymore) about how one doesn't need deep learning, with the sole supporting argument being that data retrieval can be done with database queries. \$\endgroup\$ – HAEM Sep 22 '19 at 8:55
  • \$\begingroup\$ @FryAmTheEggman This is a bit of a joke challenge (see above comment for details) \$\endgroup\$ – HAEM Sep 22 '19 at 8:55
  • \$\begingroup\$ Come to think of it, I could probably leave out generating the query and leave it at just the database initialization. \$\endgroup\$ – HAEM Sep 22 '19 at 8:59
0
\$\begingroup\$

Where am I?

Given an input folder structure (i.e. a tree data structure with named nodes), and a pointer to a directory (a specific node) within that tree structure, the program should output the full path to the directory the pointer is pointing at (in linux format, so /var/log for example), without ever inspecting the name of the current node or its ancestors; only its descendants.

The rules of the challenge are the equivalent of:

"you can cd and ls but not pwd" if this was being run on a live linux files system

but I feel I should expand on that a bit regarding how it works for a tree:

  • The method starts with the pointer, and it can ls to see what is contained within this directory (i.e. view the immediate child nodes in the tree structure, but not its own name)
  • the method can also cd dirname into any directory (i.e. move the pointer to any immediate child in the tree from the current node), and cd .. to go to a previous directory (i.e. move the pointer to the immediate previous child in the tree)
    • cd .. leaves you in the same directory (node), if there is no parent
    • cd invalidDirNameleaves you in the same directory (node), if there is no immediate child with that name
  • Any directory can contain any number of other directories, or none.
  • You are forbidden to do the equivalent of cd /

Inputs

A tree-structure representing a file system and a pointer to a node in that tree

Assumptions 1. There are no duplicate folder names in the same parent folder (so ls can't return folder, folder; for example) - but there may be other folders with the same name elsewhere in the tree structure; in which case it doesn't matter which your program outputs (as it has no way of knowing which is correct) 2. ...Therefore, it may not be possible to determine which folder you started in - in which case a best-guess is fine (see examples 3, 4) 3. Symlinks and cyclical trees are not a part of this challenge - it will always be a simple parent-child tree structure - so you are always guaranteed to find the root by doing cd .. enough times.

Output

The path that the pointer was pointing at, as a string or char array (or equivalent for your language).

Scoring

scoring is used, so shortest bytes wins.

Example 1

input map: /var/ftp/in, /var/ftp/out/child, /var/etc/test/child/etc, /var/etc/test/c2/etc2, /var/etc/test/c3

input pointer points at: c2

sample working:

  1. ls -> "etc2"
  2. cd .., ls -> "child","c2", "c3" - three options, need to find which is correct
  3. cd child, ls -> "etc2" - this doesn't match 1, so go back
  4. cd .., cd c2, ls -> "etc2" - this is correct, so I know my path ends with "/c2"
  5. cd .., cd .., ls -> "test" - only one folder, so I know my path ends with "/test/c2"
  6. cd .., ls -> "ftp", "etc" - two options, need to find which is correct
  7. cd ftp, ls -> "in", "out" - incorrect, so go back
  8. cd .. -> there was only two options, so "etc" must be correct. my path ends with "/etc/test/c2"
  9. cd .., ls -> "var" - only one folder, so path ends with "/var/etc/test/c2"
  10. cd .., ls -> "var" - same option, so assume we're at root.
  11. Output "/var/etc/test/c2"

Example 2

input map: /app/data/dir/in

input pointer points at: /app/data

Output: "/app/data"

Example 3

input map: /var, /app, /test

input pointer points at: /app

Output: "/var", "/app" or "/test" are all valid outputs.

Note that there is no way for the program to differentiate between the three folders; and so any of the three are a valid answer for the purposes of this application.

Example 4

input map: /var/etc/test, /app/etc/two

input pointer points at: /app/etc

Output: "/app/etc"

Questions

  1. Does the spec make sense?
| |
\$\endgroup\$
  • \$\begingroup\$ Nice idea for a challenge, but: I would remove the free cd and ls helpers for it only creates problems, you can (partially) solve the issue of using system built-ins by just allowing the directory-structure as input and not a directory-node (there will still be one or two solutions like eval mkdir -p $1;eval $2;pwd) but they are not really interesting and need to create the directories which is a penalty). I would also remove the explanations of examples 2-4, one suffices and more makes the post hard to read. \$\endgroup\$ – ბიმო Dec 13 '18 at 22:24
  • \$\begingroup\$ Thanks for your comments! I think I'll remove the cd/ls stuff and the file system input, like you suggest. What would you recommend for the examples 2-4 then - just show the input and output? Or just show the working but not the text explanations? Should I include the result of "ls" each time also in that case? \$\endgroup\$ – simonalexander2005 Dec 14 '18 at 9:49
  • \$\begingroup\$ Just input and output should suffice imo. Maybe you want to exchange 1 and 2 such that there is the more thorough explanation of 2 and 1 just i/o. \$\endgroup\$ – ბიმო Dec 14 '18 at 10:26
  • \$\begingroup\$ done :) Thanks for your input. How does it look now? \$\endgroup\$ – simonalexander2005 Dec 14 '18 at 13:16
0
\$\begingroup\$

Title: The Liquorice All-sort

I have just invented a new sorting algorithm, which I would like you to implement in the smallest number of bytes... The Liquorice All-sort

Given that the individual sweets are (or were) named: chips, rocks, Buttons[sic], nuggets, plugs, twists, spogs (https://en.wikipedia.org/wiki/Liquorice_allsorts), we can say that:

A bag (multiset) of Liquorice allsorts is comprised of 0..n of each item chip, rock, Button, nugget, plug, twist, spog, plus a bonus of 1..2 Bertie.

Given an input of an arbitrary array characters (A-z, 0-9, space), and a bag of Liquorice allsorts, sort as follows:

For each input character, from last to first:
    Is the bag empty?
           Yes: Exit the sort
    Else, Is the input equal to a letter (case-sensitive) that is contained in the name of one or more of the sweets in the bag?
       Yes: The number of items it matches is the number of positions it moves up the list (can't move past the top of the list).
            Take a bite out of each of the used sweets, by removing the letter we used. 
            Have we used all the letters in any item?
               Yes: Remove that item from the bag
    Else: move to the bottom of the list.

Inputs

two inputs - the numbers to sort, and the list of sweet names. Both are guaranteed to be non-empty bags (sets that can contain duplicates; or an equivalent in your language), and the bags of sweets contains a minimum of one Bertie.

Outputs

The sorted numbers

Examples

  1. {Bertie},{B} -> {B}
  2. {Bertie},{X,B} -> {B,X}
  3. {chip, rock, Button, nugget, plug, twist, spog, Bertie}, {A,B,C,k,e,r,w} -> {B,C,k,e,r,w,A} -> {B,C,k,e,r,w,A} -> {B,k,e,r,w,A,C} -> {k,B,e,r,w,A,C} -> {e,k,B,r,w,A,C} -> {e,r,k,B,w,A,C} -> {e,r,k,w,B,A,C}
  4. {Bertie, rock}, {r,o,c,k,r,o,c,e} -> {r,o,c,k,r,o,c,e} -> {o,r,c,k,r,o,c,e} -> {o,c,r,k,r,o,c,e} -> {o,c,k,r,r,o,c,e} -> (remove rock from the bag) -> {o,c,k,r,o,c,e,r} -> {o,c,k,r,c,e,r,o} -> {o,c,k,r,e,r,o,c} -> {o,c,k,e,r,r,o,c}

Sandbox Questions

I really like the idea of doing something with this (I like the wordplay of bag, etc.); but I don't know if the algorithm is just too arbitrary. Any thoughts or alternatives would be appreciated!

| |
\$\endgroup\$
  • \$\begingroup\$ Absolutely not. (although you're encouraged to post your sandbox challenges when they're ready) \$\endgroup\$ – user202729 Apr 10 '18 at 14:56
  • 1
    \$\begingroup\$ You only have 3 challenges in the Sandbox at the moment; some people have north of 30 - You don't have to worry. \$\endgroup\$ – caird coinheringaahing Apr 10 '18 at 15:43
0
\$\begingroup\$

Title - Fix the note

Given a musical note letter (A to G), followed by any number of accidentals (either 0..n # or 0..n b, not a combination of both); output a representation of the same note with the least possible number of accidentals.

A # means "shift the note one semitone up", and a b means "shift the note one semitone down".

All valid output notes are as follows (in order, then loops):

A A#/Bb B C C#/Db D D#/Eb E F F#/Gb G G#/Ab

So a sharp would move to the right along that list (and loop); and a flat would move you to the left.

Examples

  • A -> A
  • B# -> C
  • Fb -> E
  • A##### -> D
  • Dbbb -> B
  • Gbbbbbbbbbbbb -> G
  • Bb -> Bb
  • F# -> F#
  • E## -> F#
  • E# -> F

You can assume that the length of the input won't overflow your language.

Where the input has sharps, the output can also only contain 0..1 sharps; and vice-versa for flats.

This is , so shortest bytes wins. Standard rules apply

| |
\$\endgroup\$
  • \$\begingroup\$ These accidentals aren't even correct. There's no such thing as E sharp sharp. \$\endgroup\$ – S.S. Anne Sep 25 '19 at 20:42
  • \$\begingroup\$ Also, should E sharp be simplified to F? Is this bass, treble, or tenor? \$\endgroup\$ – S.S. Anne Sep 25 '19 at 20:43
  • 1
    \$\begingroup\$ @JL2210 sure there is - any note can be represented as accidentals of another note, in theory. I know that in practise you wouldn't do so. \$\endgroup\$ – simonalexander2005 Sep 26 '19 at 8:26
  • \$\begingroup\$ E# should be simplified to F, yes, because the aim is to reduce the number of accidentals where possible \$\endgroup\$ – simonalexander2005 Sep 26 '19 at 8:27
  • 1
    \$\begingroup\$ I'm not sure what you mean by "is this bass, treble or tenor?" - The instrument shouldn't matter for the purposes of this - this is purely theoretical (they're notes, not chords, if that helps) \$\endgroup\$ – simonalexander2005 Sep 26 '19 at 8:28
1
80 81
82
83 84
98

You must log in to answer this question.

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