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

2914 Answers 2914

1
87 88
89
90 91
98
-1
\$\begingroup\$

Win 2048

Win 2048. You can decide where to summon block of 2(no 4), and how you move. The output would be [place, place, move, place, move, ..., place, move](finally you reach 2048).

Shortest code win.

SN: Similar to Play a perfect game of 2048 ?

| |
\$\endgroup\$
  • 3
    \$\begingroup\$ (in case somebody don't understand the challenge: the program should take no input, and output any sequence of moves in 2048 game such that the tile 2048 is generated. As said in the challenge, the new block must be [2] (not [4]) and the program can determine where the block appears) \$\endgroup\$ – user202729 Nov 15 '18 at 14:31
-1
\$\begingroup\$

Coprimes upto N (Performance Edition)

There is this question: Coprimes up to N

Most of the answers there take way to long for larger numbers.

So, your task is to find all the coprimes of a number n (gcd(n,coprime)==1) up to nas fast as possible.

Scoring

Score=Largest n for which your code will output in less than 10 seconds on my laptop. Provide instructions to run it on my machine.

Highest score wins.

I will put the specs of my pc, anything else needed?

| |
\$\endgroup\$
  • 2
    \$\begingroup\$ I think this is not interesting. The output (φ(n) numbers) is too large in comparison with the time complexity of a reasonably-competitive algorithm (I'd expect algorithms to take n log n time at worst), so printing output is going to be the bottleneck. \$\endgroup\$ – user202729 Nov 23 '18 at 11:54
  • \$\begingroup\$ What if I make it to print the number of coprimes instead? @user202729 \$\endgroup\$ – Vedant Kandoi Nov 23 '18 at 12:08
  • \$\begingroup\$ That is number of primes @user202729. This is co-primes of n. \$\endgroup\$ – Vedant Kandoi Nov 23 '18 at 12:24
  • \$\begingroup\$ Still, very similar to codegolf.stackexchange.com/questions/26739/… . \$\endgroup\$ – user202729 Nov 23 '18 at 13:03
-1
\$\begingroup\$

A Quine that Grows!

Challenge

Create a quine that, when run, outputs itself but copied larger in the next one. The output should be able to be run, and get larger each time the output is run. The output must consist only of characters from the original quine!

EX:

abc //original
abcabc //output

or

abc //original
aabcc //output

What not to do

abc //original
abcgef //output

abc //original
abcoooooooooooo //output

An example I created

Try it Online! It replicates pretty fast if I do say so myself!

Points

This challenge is meant to be a codegolf, but also emphasize on how fast it replicates. So perhaps something like the speed at which it gets bigger divided by the number of bytes.

I really don't want loopholes like just repetitively adding characters to a section of the code, making infinite loops, and things of the like.

Any input on how to make this a good challenge? I'm open to suggestions!!!

| |
\$\endgroup\$
  • 1
    \$\begingroup\$ Related, related (probably a dupe) \$\endgroup\$ – Jo King Feb 10 '19 at 10:25
  • \$\begingroup\$ @JoKing What about a polyglot that gets bigger? It runs, making another program that runs and outputs a bigger version of the original, and vice versa. \$\endgroup\$ – KrystosTheOverlord Feb 10 '19 at 16:02
  • \$\begingroup\$ where's the polyglot part come from? otherwise that sounds like the second one \$\endgroup\$ – Jo King Feb 10 '19 at 22:04
  • \$\begingroup\$ @JoKing The output has to be in a different programming language, and then create a larger version of the original, then this larger original makes a larger of second program etc... Also to prevent easy loopholes, no using program languages that are derivatives of eachother \$\endgroup\$ – KrystosTheOverlord Feb 10 '19 at 22:32
-1
\$\begingroup\$

Just idea. Not sure what to do exactly.

Evolutionary Golf

Make simple (not golfed at all at first) code for (some program) with language (something).

Now, change a little bit (maximum 3 byte) of code to make it shorter.

Altered code must work properly (this is how evolution work).

(Maybe here will be starting code).

Sandbox

First. What program would be best? For example, 'Hello World' program is not proper, because it is too short, and can't golf that much.

Second. What language would be best? Esolang like BF? Or something like C?

| |
\$\endgroup\$
  • \$\begingroup\$ If I understand the challenge idea correctly you will post an ungolfed answer that does something in some (probably verbose) language (i.e. FizzBuzz in C#). And then answers after that should have a Levensteihn distance change \$c\$ where \$1\leq c\leq3\$ (at least 1 delete) that does the same thing (in any language). And the shortest answer that's at the end of the answer chain wins if no other answers are posted within 2-3 weeks (which is usually the case with answer-chaining challenges)? Or do you mean that anyone can post an ungolfed program, and others using the same language chain it? \$\endgroup\$ – Kevin Cruijssen Jun 26 '19 at 9:04
  • \$\begingroup\$ @KevinCruijssen I'm thinking of both. This is just brief outline, so everything can be changed. \$\endgroup\$ – LegenDUST Jun 26 '19 at 12:10
  • \$\begingroup\$ this is just game of nim with extra steps \$\endgroup\$ – Kenzie Aug 2 '19 at 17:51
-1
\$\begingroup\$

Print all the commands

META Just a rought idea, needs to be worked out.

Write a program that prints all the keywords and commands that are available in your langauge when you do not import/add anything

Details

  • require full program or standard code-golf?
| |
\$\endgroup\$
  • 2
    \$\begingroup\$ Understanding that this is a rough idea, what happens in languages without commands corresponding to single tokens e.g. ///? \$\endgroup\$ – lirtosiast Jul 18 '19 at 15:49
  • 7
    \$\begingroup\$ It is my opinion that this sort of challenge will likely never be clear. \$\endgroup\$ – Ad Hoc Garf Hunter Jul 18 '19 at 16:08
  • \$\begingroup\$ I'd imagine for /// you'd output all valid non-text characters, so \ /. \$\endgroup\$ – bigyihsuan Jul 18 '19 at 17:08
  • 1
    \$\begingroup\$ Another thing is that language version would be specified. For example, Python 2 has print as a keyword, while Python 3 has print() instead. \$\endgroup\$ – bigyihsuan Jul 18 '19 at 17:10
  • \$\begingroup\$ Pretty sure this (or something similar enough to be a dupe) has been done before. Lemme see if I can find it .... \$\endgroup\$ – Shaggy Jul 21 '19 at 21:11
  • \$\begingroup\$ Found it! codegolf.stackexchange.com/q/162384/58974 \$\endgroup\$ – Shaggy Jul 21 '19 at 21:13
  • \$\begingroup\$ One way to do this might be to make it language specific? While that isn't usually popular, outputting all of, say, Python's commands in most languages besides python is a dupe of the Rickroll challenge. However, in python itself that isn't the best approach. I can't say how well reveived it'd be, but you could try "output these MATLAB commands in MATLAB" and see what people think. \$\endgroup\$ – FryAmTheEggman Jul 23 '19 at 19:45
-1
\$\begingroup\$

Here's a challenge I'd like post because I'm curious to see what people will come up with. It's a bit of an anti-code-golf question because the code should look normal.

Is it clear what the constraints? Did I miss anything?


Introduction

Write a piece of unsuspicious code that does the following:

Let's say you've written a parser that parses it's input line by line and somewhere in your code is

find_string(line, "[start]", "[end]") // returns string between [start] and [end]

This program, when given it's own source code plain text as input, will match that line (twice actually); but we don't want that. It should still parse what it was designed for but not match any line of it's own source code.

Rules

  • It's preferred that your code makes it obvious that one of it's intended uses is to parse (and not match) itself. This is so anyone 'refactoring' the code will not accidentally undo the trick that made it work.
  • Your source code as input will be reduced to a single line.
  • Your program should be able to handle large input (~10mb) and perform reasonably (for your chosen language).
  • Your program does not need to parse the input line by line but that just seems like something reasonable code would do.
  • Points are awarded for code that looks like a normal parser and contains as little assumptions about the input as possible. Bonus points for solutions that contain the start and end delimiters in that order.

Easy solutions are to swap the start and end token arguments or to pre-treat the tokens in some way but that would look suspicious. Someone will come along and refactor your code and break the 'trick'.

I'm interested in reasonable solutions because this is a reduction of a real life problem.

Example Input and Output

  • Input lines may or may not contain [start] or [end], only return it in the output when it occurs in a pair.
  • Input lines will never contain more than one pair of [start] and [end] tokens.
  • Input lines may contain additional content before [start] or after [end]
  • Your source code plain text will be inserted at a random line in the input.

Input #1:

[start]hello[end]
<<your source code plain text>>
dont output this[start]world[end]

Output #1:

hello world

Input #2:

lorem[start]i solemnly[end]
[start]false start
[start]swear[end]
ope, sorry just passing through
this is not the[end]
[start]that i'm up to[end]ipsum
[start]no[end]dolor
<<your source code plain text>>
[start]good[end]

Output #2:

i solemnly swear that i'm up to no good
| |
\$\endgroup\$
  • \$\begingroup\$ What is the objective winning criterion? \$\endgroup\$ – Unrelated String Aug 9 '19 at 5:16
  • 1
    \$\begingroup\$ This could be a good challenge if it was just 'Write a program that takes a line of input and returns the concatenation of anything between [start] and [end] on each line, otherwise an empty string', with the restriction that if it was fed itself, it wouldn't return anything \$\endgroup\$ – Jo King Aug 9 '19 at 6:05
  • \$\begingroup\$ I'm a little confused about what Bonus points for solutions that contain the start and end delimiters in that order. means, since I thought the point was that we're not allowed to have that? \$\endgroup\$ – Jo King Aug 9 '19 at 6:07
  • 2
    \$\begingroup\$ "unsuspicious code" will raise red flags in the minds of a number of old-timers, for historical reasons which I won't explain in detail. What I will say is that unsuspicious is subjective, and we insist on objective criteria. In terms of actual reasonable solutions to the real world problem: don't use magic strings. If "[start]" and "[end]" are both constants and defined on separate lines, the problem is averted, and anyone who refactors to inline them deserves all the bugs that causes. \$\endgroup\$ – Peter Taylor Aug 9 '19 at 10:45
-1
\$\begingroup\$

Title: Transposition

** The challenge **

Given a set of notes (as a string, or a list, or any other reasonable input - but as letters and accidentals, not a numerical equivalent), the key those notes are in, and a target key; output the notes transposed into the new key. Some of the notes may not exist in the scale for the given key (e.g Eb in the key of C).

** Inputs **

The complete set of input notes for this challenge will use the English naming convention, and so are as follows:

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

where "b" represents a flattened note (down one semitone per b), and # represents a sharpened note (up one semitone per #). Theoretically all notes can be extended further with more #s and bs; but for the purposes of this program that won't happen beyond what is already given.

** What is transposing? **

Transposing a song involves "moving" the song into a different key, by finding the equivalent note of the scale in that key.

We will assume Major scales for the purposes of this challenge.

** Scales **

The scales for this challenge are officially as follows:

  • C: C, D, E, F, G, A, B, C
  • C#: C#, D#, E#, F#, G#, A#, B#, C#
  • Db: Db, Eb, F, Gb, Ab, Bb, C, Db
  • D: D, E, F#, G, A, B, C#, D
  • D#: D#, E#, F##, G#, A#, B#, C##, D#
  • Eb:Eb, F, G, Ab, Bb, C, D, Eb
  • E: E, F#, G#, A, B, C#, D#, E
  • F: F, G, A, Bb, C, D, E, F
  • F#: F#, G#, A#, B, C#, D#, E#, F#
  • Gb: Gb, Ab, Bb, Cb, Db, Eb, F, Gb
  • G: G, A, B, C, D, E, F#, G
  • G#: G#, A#, B#, C#, D#, E#, F##, G#
  • Ab: Ab, Bb, C, Db, Eb, F, G, Ab
  • A: A, B, C#, D, E, F#, G#, A
  • A#: A#, B#, C##, D#, E#, F##, G##, A#
  • Bb: Bb, C, D, Eb, F, G, A, Bb
  • B: B, C#, D#, E, F#, G#, A#, B

For simplicity, we can assume that both notes in the pairs A#/Bb, C#/Db, D#/Eb, F#/Gb, G#/Ab, C##/D, F##/G, G##/A are enharmonically equivalent (i.e. interchangeable - although they're not, always).

For scales with double-sharps, I will accept the enharmonic equivalents as an alternative implementation:

  • D#: D#, E#, G, G#, A#, B#, D, D#
  • G#: G#, A#, B#, C#, D#, E#, G, G#
  • A#: A#, B#, D, D#, E#, G, A, A#

but for all other notes in the scale, they must match. If the note isn't in the scale, either can be used.

e.g.

  • F in the key of C should transpose to F# in the key of C#, and not to Gb, because that option in the pair is explicitly in the scale
  • but D in the key of C# could transpose to either C# or Db in the key of C, because it's an incidental anyway and so there's no easy rule to determine which it should be.

BONUS feel-good points *: normally it's # if you're going up, and a b if you're going down - feel free to implement this if you want!

For double-sharps (e.g F##) in all cases, It's OK if the program "resolves" these (e.g.to G in that case) even if they are in the scale; but again, some BONUS feel-good points * if you keep the double-sharps in.

Examples

  • CDEFGABC in C to A -> ABC#DEF#G#A
  • C# in C to A -> A# OR Bb
  • ABCDEFGBAF#Bb in Bb to Gb -> FGAbBbCDbEbGFDGb
  • CCGGAAAAGFFEEDDCGGGFFEEEDCGGGFFFFEEED in C to G# -> G#G#D#D#E#E#E#E#D#C#C#B#B#A#A#G#D#D#D#C#C#B#B#B#A#G#D#D#D#C#C#C#C#B#B#B#A#

Websites like http://www.logue.net/xp/ can be used to test your answers to other inputs

* Bonus feel-good points don't get you anything extra, unless someone can come up with a quantifiable difference that it should make to the score?

| |
\$\endgroup\$
  • \$\begingroup\$ This is kind of similar but doesn't use scales and has a different set of chords, so I think this is effectively different? \$\endgroup\$ – FryAmTheEggman Sep 24 '19 at 18:47
  • \$\begingroup\$ Yes, I agree it's similar but doing a different thing to me (they're using chords, and so have the extra text to worry about; but I'm doing notes, like sheet music; and so you have accidentals to worry about) \$\endgroup\$ – simonalexander2005 Sep 25 '19 at 8:48
-1
\$\begingroup\$

Interpret Unneccesary (Not quite)

Unneccesary is a joke language created by Keymaker. The source is unneccesary, and if given, it will error out.

Your task here is similar. If there is input, your program should error out. If the input is empty, your program should do nothing and terminate.

| |
\$\endgroup\$
  • 1
    \$\begingroup\$ What does it mean to error out? \$\endgroup\$ – Beefster Oct 24 '19 at 19:14
  • \$\begingroup\$ @Beefster throw a runtime error... Or? \$\endgroup\$ – HighlyRadioactive Dec 1 '19 at 10:15
  • \$\begingroup\$ What if I'm using a language that does not have terminal errors such as Bash? \$\endgroup\$ – Beefster Dec 2 '19 at 19:34
  • \$\begingroup\$ @Beefster Are there any specifications for, something like error quine? \$\endgroup\$ – HighlyRadioactive Dec 21 '19 at 10:54
-1
\$\begingroup\$

Mirror, Mirror, on the wall. Who's the fairest of them all?

Well, you know it's Snow White, and the evil Queen is at it again. Will Snow White be saved? Will she fall asleep once again? Will the Prince find her?

Challenge:

Given an arbitrary number (>= 2) of possibly duplicated hexadecimal color values (ranging from #000000 to #FFFFFF) and paired strings, calculate the following:

  • If #FF0800 (Candy apple red) appears in the input, return "Return to Sleeping Death"
  • If #000000 appears in the input, return "Saved by Grumpy"
  • If #A98AC7 or #111111 appears in the input, return "Saved by Happy"
  • If #21E88E or #222222 appears in the input, return "Saved by Sleepy"
  • If #32DCD5 or #333333 appears in the input, return "Saved by Bashful"
  • If #43D11C or #444444 appears in the input, return "Saved by Sneezy"
  • If #54C563 or #555555 appears in the input, return "Saved by Dopey"
  • If #65B9AA or #666666 appears in the input, return "Saved by Doc"
  • If #76ADF1 or #777777 appears in the input, return "Saved by the Seven Dwarfs"
  • If #FFFAFA (Snow) appears in the input, return "Saved by Love's first kiss"
  • If an F variant appears in the input, return "Press F to pay respects to Snow White"
    • An F variant is any number that contains at least one F in its hexadecimal form, and is otherwise all 0s (e.g. #0FF0F0, #FFFFFF, #00000F, #F00F00)
  • If multiple of the preceding occur, return the "fairest" answer. The "fairest" answer is calculated as follows:
    • For all N occurrences of special color values, choose the (N-1)/2-th (truncating division) occurrence. The associated special output is the "fairest" answer.

"Appears in the input" here refers to only the hexadecimal color values, and not to the paired strings.

  • If none of the preceding occur, return the "fairest" answer. The "fairest" answer is calculated as follows:
    • Take the hexadecimal color value at the end of input values, write it down, and exclude that single color-string pair from consideration as the "fairest" answer
    • Show its binary form to the mirror, computing a reflection of only the last 24 (#FFFFFF is the mask) bits.
    • Choose the hexadecimal color with least Hamming distance from the reflection. If there are multiple (N) such colors, choose the middle ((N-1)/2-th, truncating division) instance of the color. The "fairest" answer is the associated string for the color.

Inputs:

A sequence of hexadecimal color values and String values separated by a space. The input may also be read as two separate sequences of hexadecimal color values and String values, or a single sequence of 2-tuples (either (hexValue, stringValue) or (stringValue, hexValue) is permissible, as long as the ordering is consistent across all 2-tuples). Input order matters - for each index, the corresponding element in the supply of color values is "associated" with the corresponding element in the supply of String values, and duplicates can affect the "fairest" answer. The effect is something like Function(List(HexColorValue),List(AssociatedStrings)) -> "fairest" answer. Hexadecimal color values may be represented as either (your choice of) a String "#"+6 digits, or 6 digits alone, as long as the representation is consistent across all color values.

Here's an example input:

76ADF1 Return to Sleeping Death
2FE84E Return whence ye came!

Here's another example input:

2FE84E Return to Sender
4FFAFC Return of the Obra Dinn
2FE84E Return to the house immediately, young lady!
2FE84E Return to Sleeping Death
2FE84E Return of the Jedi

Here's the third example input:

2FE84E Return to Sender
4FFAFC Return of the Obra Dinn
2FE84E Return to the house immediately, young lady!
2FE84E Return to Sleeping Death
7217F8 Return of the King

Here's the final sample input:

F4A52F Eating hearts and livers
F4A52F Eating apples
F4A52F Eating porridge
F4A52F Eating candy houses
F4A52F A Modest Proposal

Outputs:

The "fairest" answer as computed by the specified logic. For example, on the first sample input, the "fairest" answer would be Saved by the Seven Dwarfs, due to the special hex color 76ADF1 appearing within the input.

In the second sample, there are no special inputs. First, we take "2FE84E Return of the Jedi", which has value #2FE84E. In binary, this is:

001011111110100001001110

We take the reflection from the mirror, getting:

011100100001011111110100

We compare it against 2FE84E (001011111110100001001110) and 4FFAFC (010011111111101011111100), which have Hamming distances of 18 and 12 from the reflection, respectively. Since #4FFAFC has the uniquely lowest Hamming distance from the reflection, the "fairest" answer is Return of the Obra Dinn.

In the third sample input, there are no special inputs. First, we take "7217F8 Return of the King", which has value #7217F8. In binary, this is:

011100100001011111111000

We take the reflection from the mirror, getting:

000111111110100001001110

We compare it against 2FE84E (001011111110100001001110) and 4FFAFC (010011111111101011111100), which have Hamming distances of 2 and 8 from the reflection, respectively. All 3 instances of hexadecimal color value #2FE84E have minimum Hamming distance from the reflection, so we take the (3-1)/2=1th instance (0-indexed) of #2FE84E. Therefore, the "fairest" answer is Return to the house immediately, young lady!.

In the last sample input, there are no special inputs. First, we take "F4A52F A Modest Proposal", which has value #F4A52F. In binary, this is:

1111010011001100101111

We take the reflection from the mirror, getting:

1111010011001100101111

We compare it against F4A52F (1111010011001100101111), which has Hamming distance 0 from the reflection. All instances of hexadecimal color value #F4A52F have minimum Hamming distance from the reflection. There are FOUR instances of #F4A52F, because we always exclude the last hexadecimal color instance from evaluation. Therefore, we take the (4-1)/2=1th instance (0-indexed) of #F4A52F, and the "fairest" answer is Eating apples. If you don't exclude the last value from consideration, you actually get the (5-1)/2=2th instance of #F4A52F (Eating porridge), which is wrong.

Rules:

  • No standard loopholes
  • Input/output taken via standard input/output methods.
  • The output must be exactly equal to the "fairest" answer

Scoring:

This is code golf, so shortest program wins.

Posted~ you can see it here

| |
\$\endgroup\$
  • \$\begingroup\$ Going to need tag suggestions :) \$\endgroup\$ – Avi Sep 30 '19 at 20:57
  • \$\begingroup\$ Can each entry be taken as a tuple, i.e. ("#FFFFFF","Return the Slab")? Can the label part also have a hex number in it or are we guaranteed it wont? Rules has the # but the examples do not, is either form fine? Can we get a worked example of a list containing multiple matching entries? \$\endgroup\$ – Veskah Oct 1 '19 at 12:33
  • \$\begingroup\$ @Veskah You can take tuples as input. You can choose whether to keep # in your input hex colors or not, as long as you keep it the same for every single input (no sneaky stuff like putting a # before the correct answer every time). I've added more sample inputs/outputs with explanation. \$\endgroup\$ – Avi Oct 1 '19 at 14:28
  • \$\begingroup\$ -1: This has way too many hardcoded input/output mappings. This challenge is more about encoding those than solving a problem. \$\endgroup\$ – Beefster Oct 24 '19 at 19:10
-1
\$\begingroup\$

CMC: Cross-Multiplication Calculator

In this task you should create a Cross-Multiplication Calculator.


Cross-multiplication is a way of factoring an algebraic expression. This is the expression form that this way can solve:

$$x^2 + ax + b$$

\$a\$ and \$b\$ are constants here, and \$x\$ is a variable.

Anyway, as far as I can tell, this expression form is only solvable in this method.

Anyway, how do I do Cross-Multiplication? (TODO)

You first take the number \$b\$ and factor this number into integral factors.

Okay. We are using the expression \$x^2 + 8x + 16\$ as an example.

(Although 16 is not a prime) let us assume that 16 only has 2 possible factors:

  • \$-1 \times -16\$ (because \$-x \times -x = x^2\$)
  • \$1 \times 16\$ (Obviously this is 16)
  • And the above 2 with the factors reversed.

Now you sum these possible two factors and check this against the number \$a\$.

  • Check 1. So \$-1 + (-16) = -17\$. And unfortunately -17 is not 8, we proceed to the next check.
  • Check 2. So \$1 + 16 = 17\$. And unfortunately 17 is not 8, we proceed to the next check. There are no checks left.

Did I make a mistake? Of course, I need to change the factors.

  • \$-2 \times -8\$ (because \$-x \times -x = x^2\$)
  • \$2 \times 8\$ (Obviously this is 16)
  • And the above 2 with the factors reversed.

We sum those values, and they are -10 and 10 respectively. So I should change the factors to another value:

  • \$-4 \times -4\$ (because \$-x \times -x = x\$)
  • \$4 \times 4\$ (Obviously this is 16)
  • And the above 2 with the factors reversed.

Finally! \$4+4 = 8\$, and here is the factorization:

$$(x+4)(x+4)$$

Now you will probably realize why I desperately need a program to automate this.

Test cases

You can assume that the input is always valid. You do not have to specify the variables, only the numbers. Therefore the expression

$$x^2 + ax + b$$

is converted into:

$$+a \, +b$$

The expected output is not:

$$(x+\alpha)(x+\beta)$$

but:

$$+\alpha \, +\beta$$

a, b => α, β
8, 16 => 4, 4
-5, -24 => 3, -8

Scoring

This is , so shortest answer in bytes wins.

Meta

  • Is this clear enough?
  • I haven't found a duplicate, but anything? (Although unlikely, I found nothing by searching "Cross Multiplication".)
  • Tags are code-golf, string and interpreter. Anything else?
  • Any further feedback?
| |
\$\endgroup\$
  • 1
    \$\begingroup\$ I've edited your post to use MathJax for the mathematical formula/workings. In addition, I've edited out the rather strict input/output format (leading + etc.) as it's generally recommended to allow the most natural output format. Feel free to revert these changes if you dislike them. Also, your tags bullet point in the Meta section appears to be different to the tags in the title? \$\endgroup\$ – caird coinheringaahing Oct 21 '19 at 17:48
  • \$\begingroup\$ Finally, I'd vote to close this as a duplicate of this or this challenge (as it is a subset of both) \$\endgroup\$ – caird coinheringaahing Oct 21 '19 at 17:51
-1
\$\begingroup\$

Return the maximum value in the final List

In this challenge, start with an array initialized to zeros with indices starting at 1 and a series of operations to perform on segments of the list. Each operation will consist of a starting and ending index within the array, and a number to add to each element within that range.

Determine the maximum value in the final array.

For example, start with an array of 5 elements: list = [0, 0, 0, 0, 0]. The variables a and b represent the starting and ending indices, inclusive. Another variable, k, is the addend. The first element is at index 1.

a    b    k             list

               [  0,  0,  0,  0,  0]

1    2   10    [ 10, 10,  0,  0,  0]

2    4    5    [ 10, 15,  5,  5,  0]

3    5   12    [ 10, 15, 17, 17, 12]

The maximum value in the resultant array is 17. That is the value to be determined.

Function description

The function must return a long integer that denotes the largest value in the array after all operations have been performed.

listMax has the following parameters:

n: an integer, the size of the initial array.
operations: a 2D integer array where each element contains an operation.

Constraints

\$3 ≤ n ≤ 10^7\$

\$1 ≤ o ≤ 2 × 10^{5}\$

\$1 ≤ a ≤ b ≤ n\$

\$0 ≤ k ≤ 10^{9}\$

Input Format

Allowed inputs are STDIN, function argument, System argument, file input, etc.

inputs are optional and both 0- and 1-based indexing is allowed.

Sample Case 0

Sample Input 0

5

3

3

1 2 100

2 5 100

3 4 100

Sample Output 0

200

Explanation 0

Perform the following sequence of o = 3 operations on list = [0, 0, 0, 0, 0]:

  1. Add k = 100 to every element in the inclusive range [1, 2], resulting in list = [100, 100, 0, 0, 0].

  2. Add k = 100 to every element in the inclusive range [2, 5], resulting in list = [100, 200, 100, 100, 100].

  3. Add k = 100 to every element in the inclusive range [3, 4], resulting in list = [100, 200, 200, 200, 100].

Return the maximum value in the final list, 200, as the answer.

Sample Case 1

Sample Input 1

4

3

3

2 3 603

1 1 286

4 4 882

Sample Output 1

882

Explanation 1

Perform the following sequence of o = 3 operations on list = [0, 0, 0, 0]:

  1. Add k = 603 to every element in the inclusive range [2, 3], resulting in list = [0, 603, 603, 0].

  2. Add k = 286 to every element in the inclusive range [1, 1], resulting in list = [286, 603, 603, 0].

  3. Add k = 882 to every element in the inclusive range [4, 4], resulting in list = [286, 603, 603, 882].

Return the maximum value in the final list, 882, as the answer.

References:

| |
\$\endgroup\$
  • \$\begingroup\$ Thanks for using the sandbox! This challenge looks a bit like one taken from another site, so I'd urge you to either put a reference or change some of the wording. Notably, the strict input format isn't a good idea, particularly input that conveys no information (i.e. the 3 after o). I strongly recommend changing the input to match what is usually done for challenges on this site: allowing the information in any convenient format. \$\endgroup\$ – FryAmTheEggman Nov 6 '19 at 16:48
  • \$\begingroup\$ @FryAmTheEggman Please take a look and let me know if there are any changes need to be considered. \$\endgroup\$ – Pluviophile Nov 7 '19 at 6:28
  • 1
    \$\begingroup\$ Thank you for adding a reference! I would recommend also including a link, if you can. I would also like to reiterate my suggestion to change the input format to be more lenient. \$\endgroup\$ – FryAmTheEggman Nov 7 '19 at 18:25
  • \$\begingroup\$ Hi Krishna, welcome and thanks for using the sandbox! I would agree with FryAmTheEggman regarding allowing "any convenient format" for the input, as is usual on this site. So for your example, f(5,[[1,2,100],[2,5,100],[3,4,100]]) would be a valid option. \$\endgroup\$ – Chas Brown Nov 7 '19 at 22:50
  • \$\begingroup\$ @FryAmTheEggman Added reference Links, and inputs are optional. \$\endgroup\$ – Pluviophile Nov 8 '19 at 10:36
  • \$\begingroup\$ @ChasBrown Hi, Thank you for your time & the Suggestion. Could you please elaborate on the Convenient input format. And do I need to change the sample test cases as well? or Only "Input Format" \$\endgroup\$ – Pluviophile Nov 8 '19 at 10:42
  • 1
    \$\begingroup\$ Thanks for adding the link! I've looked through the Terms of Service for hackerrank, and I couldn't figure out whether basically reusing challenges was allowed. I think it would be good if you explained why you were sure it is ok to post (this is the first time I've seen a hackerrank question posted here). The changes you made to input are good, and you can leave the test cases. However, I recommend re-wording the input part so that you don't have "first line," etc., in them anymore. \$\endgroup\$ – FryAmTheEggman Nov 8 '19 at 17:23
-1
\$\begingroup\$

Parse a regex

Grep is a wonderful tool. It can find stuff in files, it can help you spell stuff correctly (grep 'whatever' /usr/share/dict/words or wherever that file is), and it can even test if something is a prime number!

However, the first version was implemented back in the golden age, when FORTRAN was respected, Pascal was the language for beginners, and object orientation was just starting out in on its great adventure.

One could argue that modern developers have nowhere near that much talent or skill, what with their flashy "IDEs" and "frameworks". If they would be asked to implement something similar, they would just jump at the nearest library or cloud thingimabob and say "Done!".

At least, that is what some would say.

Prove them wrong! Golf grep!

Parse a regular expression without calling any built-in functions or operators explicitly meant for this.

input:

Basically the same as a simple grep: a regular expression as a command line parameter, followed by an optional filename or a dash. If the filename is not present, or it is a dash, read for stdin.

This is the recommended way to do it, but if you can write an adapter (eg: post stuff to a php form for your program via a shell script), then that is OK as well. The adapter does not contribute to your score.

output

Lines that match the regular expression.

Notes:

The regex dialect is PCRE (perl compatible). Files use unix line terminators if it is relevant.

| |
\$\endgroup\$
  • \$\begingroup\$ Closely related, but not quite a duplicate. \$\endgroup\$ – AdmBorkBork Nov 18 '19 at 20:42
  • \$\begingroup\$ Why the downvote? \$\endgroup\$ – Mark Gardner Nov 20 '19 at 8:16
  • \$\begingroup\$ You've likely been downvoted because you "ban built-in functions or operations explicitly meant for this." Consider this post for a lengthy discussion of why this has fallen out of favour. Beyond this being trivial besides parsing regular expressions, it also doesn't actually describe what a regex is or what it means to be PCRE. Challenges need to be self contained! I think your bet is to make a different matching language yourself and ask us to implement grep but with that language instead of regex. \$\endgroup\$ – FryAmTheEggman Nov 20 '19 at 19:09
-1
\$\begingroup\$

Predict the state of a Minecraft inventory after click events

Minecraft does inventory management over the network by sending packets describing the clicks that a player does. If you're caching these events, it can be non-trivial to predict what state the inventory is in after the clicks

Challenge

Take an inventory of 9 slots, each with an item and a type. Assume all items can stack up to 64 and that if a slot would be "overfilled" that the cursor will continue holding onto the items. Then, take a list of the slot index, button, and mode variables for the clicks to be done (mode and button are defined at https://wiki.vg/Protocol#Click_Window). Output the inventory afterwards.

Restrictions/Rules

You may input and output the inventory in any reasonable format. You may take click input in any reasonable format. You may ignore Mode==2, as the player inventory is not implemented correctly enough for this. You may ignore Mode==3 because this is a survival player You may ignore Mode==5 where Button==8, 9, or 10 for the same reason as Mode 3. Dropping the item is a delete. Your player won't pick it back up or anything silly like that. You may assume that input will have valid counts Don't use standard loopholes

Examples

Input:

[["diamond",64],[],[],[],[],[],[],[],[]]

[
 [0, 0, 0]
 [0, 0, 1]
]

Output

[[],["diamond",64],[],[],[],[],[],[],[]]

Input:

[["diamond",64],["dirt", 64],[],[],[],[],[],[],[]]

[
 [0, 0, 0]
 [0, 0, 1]
 [0, 0, 2]
]

Output

[[],["diamond",64],["dirt",64],[],[],[],[],[],[]]

Input:

[["diamond",64],[],[],[],[],[],[],[],[]]

[
 [0, 0, 0],
 [-999, 4, 4],
 [0, 4, 5],
 [1, 4, 5],
 [2, 4, 5],
 [3, 4, 5],
 [4, 4, 5],
 [5, 4, 5],
 [6, 4, 5],
 [7, 4, 5],
 [8, 4, 5],
 [-999, 4, 6],
 [8, 0, 0]
]

Output

[["diamond",56],["diamond",1],["diamond",1],["diamond",1],["diamond",1],["diamond",1],["diamond",1],["diamond",1],["diamond",1]]

Meta

I have no clue what I'm doing writing a question.

Tagged code golf

Critique goals:

  • Improve testcases
  • Improve description of problem
  • Determine if the problem is too complex
| |
\$\endgroup\$
  • 1
    \$\begingroup\$ Challenges are meant to be self-contained. While information where the idea/process comes from can be nice, everything needed to solve the challenge should be in the description. This means you should write down what click does what, for all the people who don't remember what Minecraft clicks do by heart. \$\endgroup\$ – AlienAtSystem Nov 22 '19 at 6:43
-1
\$\begingroup\$

A very-very old (maybe early 2000s) problem:

Print out a decimal number \$n\$ such as \$n^2\$ ends with \$n\$ with maximal length your program can compute in 60 seconds

In other words it's needed to find some long enough \$n\$ such as \$10^{\lfloor\log_{10}n\rfloor+1}|(n^2-n)\$.
A hint may be that an \$n\$ ending with \$5\$ is more easy to compute than an \$n\$ ending with \$6\$.

code-challenge

| |
\$\endgroup\$
  • \$\begingroup\$ How can this be king-of-the-hill? Do you mean code-challenge? And what stops us from hardcoding some extremely large number? \$\endgroup\$ – Jo King Nov 21 '19 at 23:36
  • \$\begingroup\$ king-of-the-hill needs interaction between submissions. I don't see any here \$\endgroup\$ – Jo King Nov 22 '19 at 1:43
  • \$\begingroup\$ @JoKing the problem becomes very simple with modular arithmetic: got 205k digits for free with ~len(n) time for each step imgur.com/ExPdwMb , so there's no need for hardcoding and it's not much interesting. ) \$\endgroup\$ – Alexey Burdin Nov 22 '19 at 14:11
-1
\$\begingroup\$

JavaScript: Free for All

This is a very experimental idea of mine: given a function which is provided a single function as an argument, try to run that function the most times possible in a browser environment while competing against other bots.

Bot submissions

Each bot consists of a function. This function takes a scoring function as input. Each bot has a state consisting of three values:

  • score: Number indicating score, winning criterion
  • locked: Boolean which, when true, prevents further score increases
  • calls: Number of times scoring function called in last 100ms (?), will set locked to true for the remainder of the round if it exceeds a certain value

The scoring function increments score and calls, as long as locked is not true.

Restrictions

If any of these restrictions are violated, a bot will have locked set to true.

No bot or bot-defined function may:

  • Run longer than 5ms
  • Attempt to modify the window location (location.href, location.assign, etc.)
  • Attempt to connect to the internet (AJAX, WebSockets, etc.)
  • Create web workers
  • Affect hardware (sound, microphone, camera, USB, gamepads, etc.)
  • Download files
  • Leave an impact which cannot be fixed by reloading the page

Notes

This is almost certainly a very bad idea on an assortment of levels. If you have any suggestions of restrictions or ways to make the challenge more interesting, be sure to comment.

I'm considering some sort of system to determine which bot runs first that adds to the strategy, and interesting attack angles for other bots.

To prevent this from becoming a "read the last answer and exactly cancel out its strategy" type thing, I'm open to any suggestions.

| |
\$\endgroup\$
-1
\$\begingroup\$

Given a digit as an English word, output its numerical value.

For example, given the input one, you should output 1 (optionally with a trailing newline).

Your program should cover all the following cases:

zero  => 0
one   => 1
two   => 2
three => 3
four  => 4
five  => 5
six   => 6
seven => 7
eight => 8
nine  => 9

| |
\$\endgroup\$
-1
\$\begingroup\$

Introduction

This challenge was inspired by the 24 Game.

In the 24 Game, you are given 4 numbers and are asked to make 24 using addition, subtraction, multiplication, division, and parentheses. So...

What is the biggest number you can make given 4 numbers using the above operations?

Challenge

For four given inputs a, b, c, d, output the biggest number you can get using addition, subtraction, multiplication, division, and parentheses.

This is code-golf, so the shortest answer wins.

Example Input and Output

  Input  -->  Output  -->   Explanation
 1,3,2,4 -->    36    --> (1 + 2) × 3 × 4
 5,5,5,5 -->   625    -->  5 × 5 × 5 × 5
 9,2,3,1 -->    81    --> (1 + 2) × 3 × 9
 

Please give feedback on this challenge and correct me if my outputs are wrong. Should I change it to the smallest number?

| |
\$\endgroup\$
  • \$\begingroup\$ Related. \$\endgroup\$ – FlipTack Dec 30 '19 at 23:19
  • 1
    \$\begingroup\$ Also the subtraction and division are surely obsolete for the challenge? \$\endgroup\$ – FlipTack Dec 30 '19 at 23:21
  • \$\begingroup\$ Will positive number divide zero yield Infinity as what IEEE 754 does? \$\endgroup\$ – tsh Dec 31 '19 at 3:37
  • 1
    \$\begingroup\$ Shouldn't (1+2)x3x4 greater than 1+2x3x4? \$\endgroup\$ – tsh Dec 31 '19 at 3:37
  • \$\begingroup\$ @FlipTack Probably but maybe not in some circumstances. \$\endgroup\$ – Yousername Dec 31 '19 at 22:18
  • \$\begingroup\$ @tsh No, infinity will not count as the solution. Thank you, that is true that (1+2)x3x4 is greater. \$\endgroup\$ – Yousername Dec 31 '19 at 22:19
  • \$\begingroup\$ Does order matter? From the input it seems the order matters, i.e. we are not supposed to change the order of the input. So, for 1,3,2,4, the answer is 32, rather than 36. \$\endgroup\$ – Element118 Jan 1 at 5:25
  • \$\begingroup\$ @Element118 No, order does not matter, those were just the random numbers that came from my head. \$\endgroup\$ – Yousername Jan 4 at 21:11
-1
\$\begingroup\$

How many ACus do I have?

Posted to main

| |
\$\endgroup\$
  • \$\begingroup\$ I'm not sure this counts as a dupe, but what it seems to be is n=floor(days_between(input, date(1,1,2020)) / 7); return n*(n-1)/2, which doesn't seem terribly interesting to golf. (Also just fyi, the 01 you used in your dates in your script is actually an octal literal i.e. 010 is 8) \$\endgroup\$ – FryAmTheEggman Jan 7 at 21:33
  • \$\begingroup\$ Thanks for the feedback. I have corrected the script. Not sure how the extra 0's managed to slip in! I'll leave the challenge here for a couple more days to see if there are any more comments. \$\endgroup\$ – ElPedro Jan 8 at 7:43
  • \$\begingroup\$ @ElPedro You need to wait longer. At least a month or two, but a few months is really good. \$\endgroup\$ – S.S. Anne Jan 11 at 21:25
  • \$\begingroup\$ I'm sorry and no personal offence intended but I find it a bit strange that a member of 3 months is telling a member of over 4 years with lot's of experience and over 5000 rep how to use the sandbox and the main site. Maybe I am simply getting too old for this community. \$\endgroup\$ – ElPedro Jan 11 at 21:36
  • \$\begingroup\$ And besides which, none of that alters my opinion that downvotes without the downvoter giving a reason are not any help to anyone. If you think differently then please feel free to give me a good reason. I am happy to listen and learn. \$\endgroup\$ – ElPedro Jan 11 at 21:40
-1
\$\begingroup\$

Make a Decompiler Bomb

Similar to the Make a Compiler Bomb challenge, but backwards.

The goal is to create the a 1KiB (1024 bytes) or smaller bytecode file that creates the largest output when decompiled.

Constraints

  • A binary is either an x86 binary (in the form of an ELF file, PE file (.dll/.exe), or Mach-O binary) or a virtual bytecode file (e.g. Python .pyc, Java .class, .NET CLR, etc.)

  • The decompiler can be any public (preferably free) decompiler of your choice. (e.g Snowman/Hex-Rays for x86 binarys, CFR/Fernflower/etc. for java, dotPeek for .NET, uncompyle6 for Python, etc.)

  • A decompiler is any tool that takes a binary and attempts to reconstruct human readable source code from it.

  • The largest output byte count wins, with the smallest input size as a tie-breaker

  • The binary must be executable, and print "Hello World!"

  • The decompiled code must be syntactically correct

| |
\$\endgroup\$
  • 4
    \$\begingroup\$ I think you probably want to specify what a "decompiler" is, since really any file is "binary" and anything that takes that and produces some valid code probably arguable counts as a decompiler. Further, I think you might be better served by limiting the binary size, like the original challenge, as if someone finds a way that adding \$n\$ bytes adds more than \$n^{2}\$ bytes to the output they would achieve an arbitrarily large score. \$\endgroup\$ – FryAmTheEggman Jan 17 at 20:06
  • \$\begingroup\$ @FryAmTheEggman I put in a basic explanation and made the scoring based on largest output rather than a formula. Explaining a decompiler is tricky though, I'll think about that more and maybe edit for it later. \$\endgroup\$ – famous1622 Jan 21 at 14:15
  • \$\begingroup\$ I think the scoring change you made is good, only that kb is a tad ambiguous between being 1000 or 1024, and that it seems a tad large (but neither of those is critical and the second is just my opinion). Thinking about what to do with the problem of defining a decompiler, I realised it was probably a good idea to require that the resulting decompiled code does something. Maybe requiring that the decompiled code is a hello world variant or something will limit some problems like "this program converts to Unary source code". \$\endgroup\$ – FryAmTheEggman Jan 21 at 16:27
  • \$\begingroup\$ @FryAmTheEggman Made it so the decompiled code must have correct syntax, and made the size smaller, was going to post my java example but I just realized I have to make it fit in the new restrictions so... \$\endgroup\$ – famous1622 Jan 21 at 16:46
-1
\$\begingroup\$

I delete the input, you delete the source code

This is a new twist on the long running series on CGCC.

Your task, if you accept it, is to write a program/function that outputs/returns the contents of an input file. The tricky part is that if I delete the input file, your program must delete itself.

Rules

  • The source code file and the input file should be in the same directory.

  • The input file and source file can be named anything at all. I.e. The file names are your choice.

  • The contents of the input file will be restricted to printable ASCII.

  • The input file and the source file must be deletable.

  • This is code-golf, so the shortest (original) code in each language wins!

  • Default Loopholes apply.

Example

If your program is jspwjxnlow8229 and the input file exists, the program must print the contents of the file. If the file doesn't exist, the program must delete itself.

Feedback

In regards to file manipulation, have I specified the rules enough?

| |
\$\endgroup\$
  • \$\begingroup\$ What about languages in which programs don't live in files but rather in binary blobs? Is it enough for the program to delete itself from the binary blob? \$\endgroup\$ – Adám Jan 22 at 9:30
  • \$\begingroup\$ Can the program and/or source file also be named anything at all? \$\endgroup\$ – Adám Jan 22 at 9:32
  • \$\begingroup\$ @Adam, forgive me for not knowing, but what's a binary blob? \$\endgroup\$ – Lyxal Jan 22 at 9:32
  • 1
    \$\begingroup\$ It doesn't matter what a binary blob is. I just wanted you to be aware than not all languages use the same model. \$\endgroup\$ – Adám Jan 22 at 9:33
  • \$\begingroup\$ @Adam sure. I'll add a part about that to the challenge \$\endgroup\$ – Lyxal Jan 22 at 9:34
  • \$\begingroup\$ Parts of this feel a bit unclear. Can the submissions know the file names in advance? (If not then the name feels a bit odd, isn't it really write a cat program that deletes itself if the input name doesn't correspond to an existing file? They aren't really tied together in that case) Similarly, why mention the recycling bin? It isn't present on many systems, and behaves differently on those that do have one (most programmatic deletions will require more work to send the file to the temporary "are you sure" location). \$\endgroup\$ – FryAmTheEggman Jan 22 at 17:06
-1
\$\begingroup\$

How healthy are my children?

As anyone who has twins will know, it can be hard to keep track of which child fed / was changed, and when.

That's why I've devised a system using OneNote on my phone. It's quick and easy to use.

Each entry (line) uses the following structure (note: I'm not a regex expert and the expression is more permissive than I want - see words for detail):

(ddMMyyyy )?HHmm ((1|2|B) (💧|💩|🤱){1,3}){1,3}

Or, in words:

  1. For the first entry on or after midnight each day only, each line starts with the date.
  2. The next component is always the time, hour and minute in 24 hour clock format
  3. Next is a child identifier character - 1 or 2; or B if what follows applies to both children. All subsequent emoticons apply to the identified child, until a new child identifier is found or a newline. There is guaranteed to be at least one child identifier in a record.
  4. Next comes any or all of the three emoticons (maximum one of each) representing a wet nappy (💧), a dirty nappy (💩) or a feed (🤱)
  5. Repeat from step 3. until done. BUT - each emoticon will only appear once per child - so if it appears in B then it won't appear in either 1 or 2; and if it appears in either 1 or 2 it won't appear in B. It won't appear in both 1 and 2 (because then it would be in B instead). The regex doesn't show this subtlety.

Some other notes:

  • Breast-Feeding (feed) emoticon 🤱 is codepoint U+1F931
  • Droplet (wet) emoticon 💧 is codepoint U+1F4A7
  • Pile of Poo (dirty) emoticon 💩 is codepoint U+1F4A9
  • All items in the string are space-separated
  • I would actually use the initials of my children's names, rather than 1 and 2 - but for the challenge I went with the numbers instead.

Example

02022020 0005 1 💩 B 💧 🤱
0230 2 💧 🤱
0250 1 💧 💩 🤱
0330 2 🤱
0400 1 🤱
0700 B 💧 🤱
0900 2 🤱
1000 2 💧 🤱
1020 1 💧 🤱
1220 1 🤱
1420 B 💧 1 💩 2 🤱
1440 1 🤱
1600 2 💧 💩
1700 1 💧
1745 B 🤱
2100 B 💧 🤱 1 💩
2350 2 🤱 B 💩 1 💧
03022020 0015 1 🤱 B 💧
0500 1 💧 🤱
0830 1 💧 🤱
0900 2 💧 🤱
1115 B 💧 1 💩
1215 B 🤱
1330 B 🤱
1400 2 💧 💩

The Challenge

Given the raw data, input as a single string or array of entries, containing data such as the above example, output a summary of:

  • number of feeds, wet and dirty nappies, per baby, over the past 24 hours

"The past 24 hours" can be either based on system time, or the current time can be passed as an extra input.

The output format is up to you, as long as it:

a) is consistent across all runs of the program
b) shows the information required

some example outputs for the above inputs, with a current time of 14:30 on 3rd of February 2020 (hand-calculated, sorry if they're not right!):

Baby 1 had 6 wet nappies, 2 dirty nappies, and fed 8 times Baby 2 had 6 wet nappies, 3 dirty nappies and fed 6 times

{{6,6},{2,3},{8,6}}

{6,2,8},{6,3,6}

etc.

This is so lowest bytes wins. Usual exclusions apply.

| |
\$\endgroup\$
  • \$\begingroup\$ If a language can't deal with unicode input, should I allow the whole codepoint string substituted in its place? \$\endgroup\$ – simonalexander2005 Feb 14 at 11:59
  • \$\begingroup\$ Should I be more explicit with the output format? \$\endgroup\$ – simonalexander2005 Feb 14 at 12:00
  • \$\begingroup\$ Can someone help me make the regex more tight? \$\endgroup\$ – simonalexander2005 Feb 14 at 12:05
-1
\$\begingroup\$

I want to ask a combined popularity / objectively scored question. Something like:

Take a string as input. Match the string to a famous painting such as "Mona Lisa" and render a cartoon version of it. 1 point per painting, 1 point per upvote. Voting closes on XX/YY/ZZZZ.

I want to reward people for including more possibilities (there will be a fixed upper limit, unlike with paintings). I also want to reward people for the quality of their renderings. The cartoon paintings should be recognisable as versions of the real paintings.

Is this a good scoring system? If not, what would be better?

| |
\$\endgroup\$
  • 1
    \$\begingroup\$ This is somewhat vague. Can you be more precise? \$\endgroup\$ – Don Thousand Feb 17 at 15:38
-1
\$\begingroup\$

Implement PSL(2,3)

Since challenge to implement Galois field already have been many, I'm writing a challenge involving a group of Lie type!

Objective

Implement the multiplication and inversion in \$\text{PSL}(2,3)\$.

The ring \$\mathbb{Z}_3\$

The ring \$\mathbb{Z}_3\$ is the set \$\{0,1,2\}\$ with addition, negation, subtraction, and multiplication defined as modular arithmetic:

  • Addition is the usual addition with the result moduloed by 3;

  • Negation, subtraction, and multiplication are also analogously defined.

Reciprocal and division is also well-defined, but that's just another detail.

The group \$\text{SL}(2,3)\$

The multiplicative group \$\text{SL}(2,3)\$ is the set of 2-by-2 matrices whose entries are members of \$\mathbb{Z}_3\$ and the determinant is \$1\$. Note that the determinant is calculated using modular arithmetic. Matrix multiplication and matrix inversion is defined as:

  • Matrix multiplication is the usual matrix multiplication, where addition and multiplication of the entries are modular;

  • Matrix inversion of \$\begin{pmatrix} a & b \\ c & d \end{pmatrix}\$ is \$\begin{pmatrix} d & -b \\ -c & a \end{pmatrix}\$. This exploits that the determinant is \$1\$.

As a consequence, the elements of \$\text{SL}(2,3)\$ are:

$$\begin{pmatrix} 0 & 1 \\ 2 & 0\end{pmatrix}, \begin{pmatrix} 0 & 1 \\ 2 & 1\end{pmatrix}, \begin{pmatrix} 0 & 1 \\ 2 & 2\end{pmatrix}, \begin{pmatrix} 0 & 2 \\ 1 & 0\end{pmatrix}, \begin{pmatrix} 0 & 2 \\ 1 & 1\end{pmatrix}, \begin{pmatrix} 0 & 2 \\ 1 & 2\end{pmatrix}, \begin{pmatrix} 1 & 0 \\ 0 & 1\end{pmatrix}, \begin{pmatrix} 1 & 0 \\ 1 & 1\end{pmatrix}, \begin{pmatrix} 1 & 0 \\ 2 & 1\end{pmatrix}, \begin{pmatrix} 1 & 1 \\ 0 & 1\end{pmatrix}, \begin{pmatrix} 1 & 1 \\ 1 & 2\end{pmatrix}, \begin{pmatrix} 1 & 1 \\ 2 & 0\end{pmatrix}, \begin{pmatrix} 1 & 2 \\ 0 & 1\end{pmatrix}, \begin{pmatrix} 1 & 2 \\ 1 & 0\end{pmatrix}, \begin{pmatrix} 1 & 2 \\ 2 & 2\end{pmatrix}, \begin{pmatrix} 2 & 0 \\ 0 & 2\end{pmatrix}, \begin{pmatrix} 2 & 0 \\ 1 & 2\end{pmatrix}, \begin{pmatrix} 2 & 0 \\ 2 & 2\end{pmatrix}, \begin{pmatrix} 2 & 1 \\ 0 & 2\end{pmatrix}, \begin{pmatrix} 2 & 1 \\ 1 & 1\end{pmatrix}, \begin{pmatrix} 2 & 1 \\ 2 & 0\end{pmatrix}, \begin{pmatrix} 2 & 2 \\ 0 & 2\end{pmatrix}, \begin{pmatrix} 2 & 2 \\ 1 & 0\end{pmatrix}, \begin{pmatrix} 2 & 2 \\ 2 & 1\end{pmatrix} $$

The factor group \$\text{PSL}(2,3)\$

\$\text{PSL}(2,3)\$ is defined as cosets of \$\text{SL}(2,3)\$ by \$\{\begin{pmatrix} 1 & 0 \\ 0 & 1\end{pmatrix}, \begin{pmatrix} 2 & 0 \\ 0 & 2\end{pmatrix}\}\$. That is, elementwise multiplications of \$\{\begin{pmatrix} 1 & 0 \\ 0 & 1\end{pmatrix}, \begin{pmatrix} 2 & 0 \\ 0 & 2\end{pmatrix}\}\$. They are: $$ \{\begin{pmatrix} 0 & 1 \\ 2 & 0\end{pmatrix}, \begin{pmatrix} 0 & 2 \\ 1 & 0\end{pmatrix}\}, \{\begin{pmatrix} 0 & 1 \\ 2 & 1\end{pmatrix}, \begin{pmatrix} 0 & 2 \\ 1 & 2\end{pmatrix}\}, \{\begin{pmatrix} 0 & 1 \\ 2 & 2\end{pmatrix}, \begin{pmatrix} 0 & 2 \\ 1 & 1\end{pmatrix}\}, \{\begin{pmatrix} 1 & 0 \\ 0 & 1\end{pmatrix}, \begin{pmatrix} 2 & 0 \\ 0 & 2\end{pmatrix}\}, \{\begin{pmatrix} 1 & 0 \\ 1 & 1\end{pmatrix}, \begin{pmatrix} 2 & 0 \\ 2 & 2\end{pmatrix}\}, \{\begin{pmatrix} 1 & 0 \\ 2 & 1\end{pmatrix}, \begin{pmatrix} 2 & 0 \\ 1 & 2\end{pmatrix}\}, \{\begin{pmatrix} 1 & 1 \\ 0 & 1\end{pmatrix}, \begin{pmatrix} 2 & 2 \\ 0 & 2\end{pmatrix}\}, \{\begin{pmatrix} 1 & 1 \\ 1 & 2\end{pmatrix}, \begin{pmatrix} 2 & 2 \\ 2 & 1\end{pmatrix}\}, \{\begin{pmatrix} 1 & 1 \\ 2 & 0\end{pmatrix}, \begin{pmatrix} 2 & 2 \\ 1 & 0\end{pmatrix}\}, \{\begin{pmatrix} 1 & 2 \\ 0 & 1\end{pmatrix}, \begin{pmatrix} 2 & 1 \\ 0 & 2\end{pmatrix}\}, \{\begin{pmatrix} 1 & 2 \\ 1 & 0\end{pmatrix}, \begin{pmatrix} 2 & 1 \\ 2 & 0\end{pmatrix}\}, \{\begin{pmatrix} 1 & 2 \\ 2 & 2\end{pmatrix}, \begin{pmatrix} 2 & 1 \\ 1 & 1\end{pmatrix}\}, $$

You pick an element of each coset as representives, and don't care about the rest.

Multiplication/inversion of such representives is defined as multiplication/inversion in \$\text{SL}(2,3)\$, then taking the representive of the coset the multiplication/inversion is in. For example, \$\begin{pmatrix} 0 & 1 \\ 2 & 0\end{pmatrix}^2 = \begin{pmatrix} 1 & 0 \\ 0 & 1\end{pmatrix}\$, if \$\begin{pmatrix} 0 & 1 \\ 2 & 0\end{pmatrix}\$ and \$\begin{pmatrix} 1 & 0 \\ 0 & 1\end{pmatrix}\$ are representives.

Examples

Picking the left elements as representives of the cosets above: $$ \begin{pmatrix} 0 & 1 \\ 2 & 0\end{pmatrix}^2 = \begin{pmatrix} 1 & 0 \\ 0 & 1\end{pmatrix}, \begin{pmatrix} 0 & 1 \\ 2 & 1\end{pmatrix}^2 = \begin{pmatrix} 1 & 2 \\ 1 & 0\end{pmatrix}, \begin{pmatrix} 1 & 0 \\ 0 & 1\end{pmatrix}\begin{pmatrix} 1 & 1 \\ 1 & 2\end{pmatrix} = \begin{pmatrix} 1 & 1 \\ 1 & 2\end{pmatrix}, \begin{pmatrix} 1 & 2 \\ 2 & 2\end{pmatrix}\begin{pmatrix} 1 & 1 \\ 1 & 2\end{pmatrix} = \begin{pmatrix} 0 & 1 \\ 2 & 0\end{pmatrix}, \begin{pmatrix} 1 & 2 \\ 2 & 2\end{pmatrix}^{-1} = \begin{pmatrix} 1 & 2 \\ 2 & 2\end{pmatrix} $$ To be more specific about the method of evaluation: $$ \begin{pmatrix} 0 & 1 \\ 2 & 1\end{pmatrix}\begin{pmatrix} 0 & 1 \\ 2 & 2\end{pmatrix} ≡ \begin{pmatrix} 2 & 2 \\ 2 & 4\end{pmatrix} ≡ \begin{pmatrix} 2 & 2 \\ 2 & 1\end{pmatrix} ≡ \begin{pmatrix} 1 & 1 \\ 1 & 2\end{pmatrix}, \\ \begin{pmatrix} 1 & 1 \\ 1 & 2\end{pmatrix}^{-1} ≡ \begin{pmatrix} 2 & -1 \\ -1 & 1\end{pmatrix} ≡ \begin{pmatrix} 2 & 2 \\ 2 & 1\end{pmatrix} ≡ \begin{pmatrix} 1 & 1 \\ 1 & 2\end{pmatrix} $$ The steps of this algorithm is:

  1. Do usual matrix multiplication/inversion;
  2. Modulo the entries by 3;
  3. Take the representive of the coset.

Though you can make any possible algorithm.

Rules

  • Input type and format doesn't matter, but it must be a container of integers. In C, int[2][2] and int[4] are valid examples. This restriction prevents abusing the fact that \$\text{PSL}(2,3) \cong A_4\$.
  • Output type and format doesn't matter either, but it must be the same as the input type and format.
  • Invalid inputs fall in don't care situation.
  • Multiplication and inversion may be in separate codes. In this case, the score is the sum of their lengths in bytes.
  • Since this is a code-golf, the code with least score wins.
| |
\$\endgroup\$
  • \$\begingroup\$ This challenge appears to heavily rely on restricted-source and thus to me does not seem too viable. \$\endgroup\$ – Jonathan Frech Feb 25 at 0:58
  • 1
    \$\begingroup\$ @JonathanFrech Do you think I should lift the restriction on input type? Otherwise, "You pick an element of each coset as representives, and don't care about the rest" and "Invalid inputs fall in don't care situation" should be enough. \$\endgroup\$ – Dannyu NDos Feb 25 at 3:38
-1
\$\begingroup\$

Very rough outline of a challenge that rewards short programs that take a long time.

Probably a no input and output can be anything except an error challenge.

Is there a nice way to exclude things like sleep(), wait() etc?

Would be required for 1 person to run all the programs for the timing to be fair.

Thinking that answers would include loops, recursion, testing of complex criteria.

| |
\$\endgroup\$
  • 1
    \$\begingroup\$ I think you would have to post your challenge as a popularity contest ... a slippery slope indeed. \$\endgroup\$ – Jonathan Frech Mar 15 at 23:43
-1
\$\begingroup\$

Design a Stack With Increment Operation

Design a stack that supports the following operations.

Implement the CustomStack class:

  • CustomStack(int maxSize) Initializes the object with maxSize which is the maximum number of elements in the stack or do nothing if the stack reached the maxSize.
  • void push(int x) Adds x to the top of the stack if the stack hasn't reached the maxSize.
  • int pop() Pops and returns the top of stack or -1 if the stack is empty.
  • void inc(int k, int val) Increments the bottom k elements of the stack by val. If there are less than k elements in the stack, just increment all the elements in the stack.

Test Case:

Input

["CustomStack","push","push","pop","push","push","push","increment","increment","pop","pop","pop","pop"]
[[3],[1],[2],[],[2],[3],[4],[5,100],[2,100],[],[],[],[]]


Output

[null,null,null,2,null,null,null,null,null,103,202,201,-1]

Explanation


CustomStack customStack = new CustomStack(3); // Stack is Empty []
customStack.push(1);                          // stack becomes [1]
customStack.push(2);                          // stack becomes [1, 2]
customStack.pop();                            // return 2 --> Return top of the stack 2, stack becomes [1]
customStack.push(2);                          // stack becomes [1, 2]
customStack.push(3);                          // stack becomes [1, 2, 3]
customStack.push(4);                          // stack still [1, 2, 3], Don't add another elements as size is 4
customStack.increment(5, 100);                // stack becomes [101, 102, 103]
customStack.increment(2, 100);                // stack becomes [201, 202, 103]
customStack.pop();                            // return 103 --> Return top of the stack 103, stack becomes [201, 202]
customStack.pop();                            // return 202 --> Return top of the stack 102, stack becomes [201]
customStack.pop();                            // return 201 --> Return top of the stack 101, stack becomes []
customStack.pop();                            // return -1 --> Stack is empty return -1.

This is code-golf so shortest submission in bytes wins! If you liked this challenge, consider upvoting it... And happy golfing!

| |
\$\endgroup\$
  • 1
    \$\begingroup\$ This would be limited to Java and C++. I could almost guarantee that this will be downvoted and closed as unclear if posted. \$\endgroup\$ – S.S. Anne Mar 15 at 15:45
  • \$\begingroup\$ I agree with @S.S.Anne; terms like class, void and even the concept of operations are not existent in a majority of languages. \$\endgroup\$ – Jonathan Frech Mar 15 at 23:35
  • 1
    \$\begingroup\$ One possibility of altering the challenge may be to supply a finite number of stack instructions as a series of text commands and ask how the stack looks at the end ... Kind of asking to implement an interpreter for a basic stack-based language. \$\endgroup\$ – Jonathan Frech Mar 15 at 23:36
-1
\$\begingroup\$

Find the longest common subsequence

Given two integer arrays. Find the longest common subsequence

Test Case:

Input:

a=[1,5,2,6,3,7]
b=[5,6,7,1,2,3]

Output:

Return
[1,2,3] or [5,6,7]

This is code-golf so shortest submission in bytes wins! If you liked this challenge, consider upvoting it... And happy golfing!

| |
\$\endgroup\$
  • \$\begingroup\$ Similar to this challenge. \$\endgroup\$ – Jonathan Frech Mar 15 at 23:29
  • \$\begingroup\$ @JonathanFrech Yeah Similar, But not Same. Am I supposed to post the challenge?? \$\endgroup\$ – Pluviophile Mar 17 at 15:52
  • \$\begingroup\$ Posting is up to you. I would not, since I think it would be righteously closed as a duplicate. \$\endgroup\$ – Jonathan Frech Mar 17 at 16:27
-1
\$\begingroup\$

The (All But) Quine challenge

Like a quine challenge, but the opposite. Print everything except source code

Challenge

Write a program, which takes no input, and outputs all the strings of printable characters which are the same length as the source code of the program, except the source code of the program.

Scoring

The shortest program (per language) to accomplish the above task, wins

| |
\$\endgroup\$
  • \$\begingroup\$ By 'printable characters' you mean printable ASCII characters (code-points [32,126])? What if I use a language that don't contain any ASCII characters in its source code? \$\endgroup\$ – Kevin Cruijssen Mar 20 at 13:38
  • \$\begingroup\$ If your program is in ASCII, then you would need to print all of the ASCII characters, if the program is in Unicode, then you would have to print all of the Unicode characters \$\endgroup\$ – Benji Mar 20 at 14:40
  • \$\begingroup\$ @Benji Would you consider a Python 3 source file as being "in Unicode"? \$\endgroup\$ – Jonathan Frech Mar 22 at 17:49
  • \$\begingroup\$ If there are any Unicode characters that are used in the file, then no. If Unicode characters are used, then I would consider it to be Unicode \$\endgroup\$ – Benji Mar 23 at 14:47
-1
\$\begingroup\$

Maximum Performance of a Team

There are \$n\$ engineers numbered from 1 to \$n\$ and two arrays: speed and efficiency, where \$speed[i]\$ and \$efficiency[i]\$ represent the speed and efficiency for the i-th engineer respectively. Return the maximum performance of a team composed of at most \$k\$ engineers, since the answer can be a huge number, return this modulo \$10^9 + 7.\$

The performance of a team is the sum of their engineers' speeds multiplied by the minimum efficiency among their engineers.

Test Case 1:

Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 2

Output: 60

Explanation:

We have the maximum performance of the team by selecting engineer 2 (with speed=10 and efficiency=4) and engineer 5 (with speed=5 and efficiency=7). That is, performance = \$(10 + 5) * min(4, 7) = 60.\$

Test Case 2:

Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 3

Output: 68

Explanation:

This is the same example as the first but k = 3. We can select engineer 1, engineer 2 and engineer 5 to get the maximum performance of the team. That is, performance = \$(2 + 10 + 5) * min(5, 4, 7) = 68.\$

Test Case 3:

Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 4

Output: 72

Please feel free to add more test cases.

This is code-golf so shortest submission in bytes wins! If you liked this challenge, consider upvoting it... And happy golfing!

| |
\$\endgroup\$
  • 2
    \$\begingroup\$ Since this is a challenge from another site, I don't think you'd be allowed to post it here without express permission. \$\endgroup\$ – xnor Mar 19 at 16:38
-1
\$\begingroup\$

Numbers by index

Challenge

Print the numbers:

0
1
22
333
4444
55555
666666
7777777
88888888
999999999

In that order.

I/O

Takes no input. The numbers can have any delimiters desired (or none). Example outputs:

0122333444455555666666777777788888888999999999

[0,1,22,333,4444,55555,666666,7777777,88888888,999999999]

etc....

Code Example

This is an un-golfed example that may perhaps act as algorithm guide (or maybe not):

Turing Machine Code, 553 bytes

0 * 0 r K
K * _ r 1
1 * 1 r L
L * _ r 2
2 * 2 r a
a * 2 r M
M * _ r 3
3 * 3 r b
b * 3 r c
c * 3 r N
N * _ r 4
4 * 4 r d
d * 4 r e
e * 4 r f
f * 4 r O
O * _ r 5
5 * 5 r g
g * 5 r h
h * 5 r i 
i * 5 r j
j * 5 r P
P * _ r 6
6 * 6 r k
k * 6 r l
l * 6 r m
m * 6 r n
n * 6 r o
o * 6 r Q
Q * _ r 7
7 * 7 r p
p * 7 r q
q * 7 r r
r * 7 r s
s * 7 r t
t * 7 r u
u * 7 r R
R * _ r 8
8 * 8 r v
v * 8 r w
w * 8 r x
x * 8 r y
y * 8 r z
z * 8 r A
A * 8 r B
B * 8 r S
S * _ r 9
9 * 9 r C
C * 9 r D
D * 9 r E
E * 9 r F
F * 9 r G
G * 9 r H
H * 9 r I
I * 9 r J
J * 9 r halt

Try it online!

This prints out the numbers with a space delimiter:

0 1 22 333 4444 55555 666666 7777777 88888888 999999999

Challenge Type

, so shortest answer in bytes (by language) wins.

Edit: Link to the related challenge. Curiously, there is one answer on there where if it was by index, and the zero was included, it would be shorter.

| |
\$\endgroup\$
  • \$\begingroup\$ Nice challenge! Are preceding & trailing whitespace (during string output) permitted? \$\endgroup\$ – user92069 Mar 11 at 3:22
  • \$\begingroup\$ I think it should also be kolgomorov-complexity. \$\endgroup\$ – PkmnQ Mar 11 at 5:19
  • 6
    \$\begingroup\$ I don't like how the zero breaks the pattern of having the digit N appear N times -- it seems like an exceptional edge case. I think it would be better for 0 not to appear, so the numbers would just start from 1. \$\endgroup\$ – xnor Mar 11 at 7:06
  • 1
    \$\begingroup\$ @xnor, I knew you wouldn't like it (and probably a few other won't as well) That's on purpose. It just seems a little too easy otherwise. \$\endgroup\$ – ouflak Mar 11 at 7:28
  • \$\begingroup\$ @a'_', Yes. @ PkmnQ, Noted. \$\endgroup\$ – ouflak Mar 11 at 7:29
  • \$\begingroup\$ I don't really see how adding a fixed zero to the beginning makes the challenge harder in almost any language. In some, the empty string will actually convert to zero, which makes it more natural but still fairly trivial. I feel like you probably either want to go with omitting the zero, or finding a different way to make the challenge more complicated. \$\endgroup\$ – FryAmTheEggman Mar 11 at 14:53
  • \$\begingroup\$ @FryAmTheEggman, How can you have a challenge about indexes and not have zero? That just seems wrong. \$\endgroup\$ – ouflak Mar 11 at 15:31
  • \$\begingroup\$ Most people start counting from one - only people who use computers a lot default to starting at zero. And separately, I think finding another way to incorporate it is better than leaving it out (I just also think both are better than having an unexplained outlier). \$\endgroup\$ – FryAmTheEggman Mar 11 at 15:40
  • \$\begingroup\$ @FryAmTheEggman, But it's not really unexplained is it? It's the index of the first number. \$\endgroup\$ – ouflak Mar 11 at 15:46
  • 1
    \$\begingroup\$ As per what xnor said, it doesn't match the pattern of the others. That behaviour is not explained. Comments aren't really for a discussion like this; if you disagree that is fine. I've just given feedback on how I think you could improve the challenge. \$\endgroup\$ – FryAmTheEggman Mar 11 at 16:15
  • \$\begingroup\$ I do want to make sure that everyone knows that I am VERY grateful for the feedback! I also appreciate having a Sandbox where we can have this discussion here instead of on the main site. \$\endgroup\$ – ouflak Mar 11 at 16:18
  • \$\begingroup\$ @FryAmTheEggman, It's not that I disagree. I kind of agree. I like that leading zero in there to represent the index location of the initial value. I considered this when I thought of the question, before posting it here. It breaks up a trivial loop sequence answer a bit, or maybe even inspires a clever solution that nobody considered. I'd like to keep it because it stands out like that, not despite that. \$\endgroup\$ – ouflak Mar 11 at 16:21
  • \$\begingroup\$ I posted an example implementation above. The zero wasn't a big deal. \$\endgroup\$ – ouflak Mar 11 at 16:31
  • 1
    \$\begingroup\$ My guess is that you're excited about the arithmetic expression 10**n/9*n or similar. But I don't think that's much more interesting than the obvious loops that removing the zero would allow unmodified. \$\endgroup\$ – xnor Mar 11 at 21:59
  • 1
    \$\begingroup\$ @ouflak I contrast, I am very excited about arithmetic expressions :) \$\endgroup\$ – xnor Mar 12 at 21:16
-1
\$\begingroup\$

A malbolge interpreter

The challenge today is to write a Malbolge interpreter.

Specification

Malbolge
'98, Ben Olmstead

I hereby relenquish any and all copyright on this language,
documentation, and interpreter; Malbolge is officially public domain.

------------------------------------------------------------------------

                                Malbolge
                           '98, Ben Olmstead

Introduction
^^^^^^^^^^^^

It was noticed that, in the field of esoteric programming languages,
there was a particular and surprising void: no programming language
known to the author was specifically designed to be difficult to program
in.

Certainly, there were languages which were difficult to write in, and
far more were difficult to read (see: Befunge, False, TWDL, RUBE...).
But even INTERCAL and BrainF***, the two kings of mental torment, were
designed with other goals: INTERCAL to have nothing in common with any
major programming language, and BrainF*** to be a very tiny, yet still
Turing-complete, language.

INTERCAL's constructs are certainly tortuous, but they are all too
flexible; you can, for instance, quite easily assign any number to a
variable with a single statement.

BrainF*** is lacking the flexibility which is INTERCAL's major weakness,
but it fails in that its constructs are far, far too intuitive.
Certainly, there are only 8 instructions, none of which take any
arguments--but it is quite easy to determine how to use those
instructions.  Subtract 8 from the current number?  With a simple
'--------' you are done!  This kind of simple answer was unacceptable to
the author.

Hence the author created Malbolge.  It borrows from machine, BrainF***,
and tri-INTERCAL, but put together in a unique way.  It was designed to
be difficult to use, and so it is.  It is designed to be
incomprehensible, and so it is.

So far, no Malbolge programs have been written.  Thus, we cannot give an
example.

"Malbolge" is the name of Dante's Eighth Circle of Hell, in which
practitioners of deception (seducers, flatterers, simonists, thieves,
hypocrites, and so on) spend eternity.


Environment
^^^^^^^^^^^

In many languages, the environment is easy to understand.  In Malbolge,
it is best to understand the runtime environment before you ever see a
command.

The environment is, roughly, that of a primitive trinary CPU.  Both code
and data share the same space (the machine's memory segment), and there
are three registers.  Machine words are ten trits (trinary digits) wide,
giving a maximum possible value of 59048 (all numbers are unsigned).
Memory space is exactly 59049 words long.

The three registers are A, C, and D.  A is the accumulator, used for
data manipulation.  A is implicitly set to the value written by all
write operations on memory.  (Standard I/O, a distinctly non-chip-level
feature, is done directly with the A register.)

C is the code pointer.  It is automatically incremented after each
instruction, and points the instruction being executed.

D is the data pointer.  It, too, is automatically incremented after each
instruction, but the location it points to is used for the data
manipulation commands.

All registers begin with the value 0.

When the interpreter loads the program, it ignores all whitespace.  If
it encounters anything that is not one of an instruction and is not
whitespace, it will give an error, otherwise it loads the file, one non-
whitespace character per cell, into memory.  Cells which are not
initialized are set by performing op on the previous two cells
repetitively.


Commands
^^^^^^^^

When the interpreter tries to execute a program, it first checks to
see if the current instruction is a graphical ASCII character (33
through 126).  If it is, it subtracts 33 from it, adds C to it, mods it
by 94, then uses the result as an index into the following table of 94
characters:

            +b(29e*j1VMEKLyC})8&m#~W>qxdRp0wkrUo[D7,XTcA"lI
            .v%{gJh4G\-=O@5`_3i<?Z';FNQuY]szf$!BS/|t:Pn6^Ha

It then checks it against the characters listed below, and performs an
appropriate action.

If the result is not one of the characters listed below, it is treated
as a nop.  If the original character is not graphic ASCII, the program
is immediately ended.

When the interpreter parses the input file, it checks each non-
whitespace character with the process above.  If any result is not one
of the eight characters below, the file will be rejected.

After the instruction is executed, 33 is subtracted from the instruction
at C, and the result is used as an index in the table below.  The new
character is then placed at C, and then C is incremented.

            5z]&gqtyfr$(we4{WP)H-Zn,[%\3dL+Q;>U!pJS72FhOA1C
            B6v^=I_0/8|jsb9m<.TVac`uY*MK'X~xDl}REokN:#?G"i@

j
  sets the data pointer to the value in the cell pointed to by the
  current data pointer.

i
  sets the code pointer to the value in the cell pointed to be the
  current data pointer.

*
  rotates the trinary value of the cell pointed to by D to the right 1.
  The least significant trit becomes the most significant trit, and all
  others move one position to the left.

p
  performs a tritwise "op" on the value pointed to by D with the
  contents of A.  The op (don't look for pattern, it's not there) is:

            | A trit:
    ________|_0__1__2_
          0 | 1  0  0
      *D  1 | 1  0  2
     trit 2 | 2  2  1



Di-trits:
    00 01 02 10 11 12 20 21 22

00  04 03 03 01 00 00 01 00 00
01  04 03 05 01 00 02 01 00 02
02  05 05 04 02 02 01 02 02 01
10  04 03 03 01 00 00 07 06 06
11  04 03 05 01 00 02 07 06 08
12  05 05 04 02 02 01 08 08 07
20  07 06 06 07 06 06 04 03 03
21  07 06 08 07 06 08 04 03 05
22  08 08 07 08 08 07 05 05 04

<
  reads an ASCII value from the stdin and converts it to Trinary, then
  stores it in A.  10 (line feed) is considered 'newline', and
  2222222222t (59048 dec.) is EOF.

/
  converts the value in A to ASCII and writes it to stdout.  Writing
  10 is a newline.

v
  indicates a full stop for the machine.

o
  does nothing, except increment C and D, as all other instructions do.


Turing-Completeness
^^^^^^^^^^^^^^^^^^^

Though I have not proven it, I _think_ Malbolge to be Turing-complete.
To be Turing-complete, there must be some data construct which can be
used to do any mathematical calculation.  I believe that using *p in
various clever ways on the tritwords can fulfill this requirement.

Turing-completeness also requires three code constructs: sequential
execution (which Malbolge obviously has), repetition (provided by the
i and, indirectly, j instructions), and conditional-execution (provided,
I believe, by self-modifying code and altering i destinations).

I do have my doubts, particularly about data constructs, but I *think*
this works...


Appendix: Trinary Conversion Table
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Trinary to ASCII to decimal to hex table, provided, strangely enough,
for the convenience of Malbolge programmers.

00000 NUL 000 00    01012   032 20    02101 @ 064 40    10120 ` 096 60
00001 SOH 001 01    01020 ! 033 21    02102 A 065 41    10121 a 097 61
00002 STX 002 02    01021 " 034 22    02110 B 066 42    10122 b 098 62
00010 ETX 003 03    01022 # 035 23    02111 C 067 43    10200 c 099 63
00011 EOT 004 04    01100 $ 036 24    02112 D 068 44    10201 d 100 64
00012 ENQ 005 05    01101 % 037 25    02120 E 069 45    10202 e 101 65
00020 ACK 006 06    01102 & 038 26    02121 F 070 46    10210 f 102 66
00021 BEL 007 07    01110 ' 039 27    02122 G 071 47    10211 g 103 67
00022 BS  008 08    01111 ( 040 28    02200 H 072 48    10212 h 104 68
00100 HT  009 09    01112 ) 041 29    02201 I 073 49    10220 i 105 69
00101 LF  010 0a    01120 * 042 2a    02202 J 074 4a    10221 j 106 6a
00102 VT  011 0b    01121 + 043 2b    02210 K 075 4b    10222 k 107 6b
00110 FF  012 0c    01122 , 044 2c    02211 L 076 4c    11000 l 108 6c
00111 CR  013 0d    01200 - 045 2d    02212 M 077 4d    11001 m 109 6d
00112 SO  014 0e    01201 . 046 2e    02220 N 078 4e    11002 n 110 6e
00120 SI  015 0f    01202 / 047 2f    02221 O 079 4f    11010 o 111 6f
00121 DLE 016 10    01210 0 048 30    02222 P 080 50    11011 p 112 70
00122 DC1 017 11    01211 1 049 31    10000 Q 081 51    11012 q 113 71
00200 DC2 018 12    01212 2 050 32    10001 R 082 52    11020 r 114 72
00201 DC3 019 13    01220 3 051 33    10002 S 083 53    11021 s 115 73
00202 DC4 020 14    01221 4 052 34    10010 T 084 54    11022 t 116 74
00210 NAK 021 15    01222 5 053 35    10011 U 085 55    11100 u 117 75
00211 SYN 022 16    02000 6 054 36    10012 V 086 56    11101 v 118 76
00212 ETB 023 17    02001 7 055 37    10020 W 087 57    11102 w 119 77
00220 CAN 024 18    02002 8 056 38    10021 X 088 58    11110 x 120 78
00221 EM  025 19    02010 9 057 39    10022 Y 089 59    11111 y 121 79
00222 SUB 026 1a    02011 : 058 3a    10100 Z 090 5a    11112 z 122 7a
01000 ESC 027 1b    02012 ; 059 3b    10101 [ 091 5b    11120 { 123 7b
01001 FS  028 1c    02020 < 060 3c    10102 \ 092 5c    11121 | 124 7c
01002 GS  029 1d    02021 = 061 3d    10110 ] 093 5d    11122 } 125 7d
01010 RS  030 1e    02022 > 062 3e    10111 ^ 094 5e    11200 ~ 126 7e
01011 US  031 1f    02100 ? 063 3f    10112 _ 095 5f

11202 128 80    12221 160 a0    21010 192 c0    22022 224 e0
11210 129 81    12222 161 a1    21011 193 c1    22100 225 e1
11211 130 82    20000 162 a2    21012 194 c2    22101 226 e2
11212 131 83    20001 163 a3    21020 195 c3    22102 227 e3
11220 132 84    20002 164 a4    21021 196 c4    22110 228 e4
11221 133 85    20010 165 a5    21022 197 c5    22111 229 e5
11222 134 86    20011 166 a6    21100 198 c6    22112 230 e6
12000 135 87    20012 167 a7    21101 199 c7    22120 231 e7
12001 136 88    20020 168 a8    21102 200 c8    22121 232 e8
12002 137 89    20021 169 a9    21110 201 c9    22122 233 e9
12010 138 8a    20022 170 aa    21111 202 ca    22200 234 ea
12011 139 8b    20100 171 ab    21112 203 cb    22201 235 eb
12012 140 8c    20101 172 ac    21120 204 cc    22202 236 ec
12020 141 8d    20102 173 ad    21121 205 cd    22210 237 ed
12021 142 8e    20110 174 ae    21122 206 ce    22211 238 ee
12022 143 8f    20111 175 af    21200 207 cf    22212 239 ef
12100 144 90    20112 176 b0    21201 208 d0    22220 240 f0
12101 145 91    20120 177 b1    21202 209 d1    22221 241 f1
12102 146 92    20121 178 b2    21210 210 d2    22222 242 f2
12110 147 93    20122 179 b3    21211 211 d3
12111 148 94    20200 180 b4    21212 212 d4
12112 149 95    20201 181 b5    21220 213 d5
12120 150 96    20202 182 b6    21221 214 d6
12121 151 97    20210 183 b7    21222 215 d7
12122 152 98    20211 184 b8    22000 216 d8
12200 153 99    20212 185 b9    22001 217 d9
12201 154 9a    20220 186 ba    22002 218 da
12202 155 9b    20221 187 bb    22010 219 db
12210 156 9c    20222 188 bc    22011 220 dc
12211 157 9d    21000 189 bd    22012 221 dd
12212 158 9e    21001 190 be    22020 222 de
12220 159 9f    21002 191 bf    22021 223 df

Notes

Note that the original specification has one quirk: after encountering an illegal instruction, the interpreter hangs. You may choose which behaviour do you want to implement (hang or exit).

I/O rules

The program or function has to take input in any reasonable way (for the program), and somehow supply the input and output features to the malbolge program (either by return value / parameter, tty or a file).

Sandbox stuff

note: I'm a bit unsure about the scoring criterion: would popularity-contest be good? I'm mostly looking for creative answers

| |
\$\endgroup\$
  • \$\begingroup\$ add interpreter ? \$\endgroup\$ – Wezl Apr 30 at 22:00
  • \$\begingroup\$ I think popularity-contest would be adding Do X creatively to an already relatively non-interesting challenge [unless if you somehow managed to create a Malbolge self-interpreter and that is the trick you're planning to use to win your own competition with it :) ] \$\endgroup\$ – my pronoun is monicareinstate May 1 at 2:15
  • \$\begingroup\$ hmm right; I'm not planning on submitting a malbolge interpreter on malbolge :P, I'd like to see what people can think of. Also I've been thinking about fastest-code contest that would possibly allow me to switch my tolling a bit :P \$\endgroup\$ – Szewczyk May 1 at 10:54
  • \$\begingroup\$ I'd probably participate if this was [fastest-code], but I doubt there are enough interesting optimizations here. \$\endgroup\$ – my pronoun is monicareinstate May 2 at 3:26
  • \$\begingroup\$ there is a lot of room for optimization, you just need to investigate the challenge a little bit further \$\endgroup\$ – Szewczyk May 2 at 17:46
1
87 88
89
90 91
98

You must log in to answer this question.

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