567
\$\begingroup\$

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

Sandbox FAQ

Posting

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

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

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

Discussion

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

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

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

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

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

Other

Search the sandbox / Browse your pending proposals

The sandbox works best if you sort posts by active.

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

\$\endgroup\$
0

4469 Answers 4469

1
54 55
56
57 58
149
1
\$\begingroup\$

Tweetable Video

This challenge is inspired by the Encode Images into Tweets challenge, extending the idea to now attempt to compress video into a series of "tweet" sized messages where the number of tweets == number frames of the video.

The competition is to compress 450 frames of video into 450 (or less) tweets of 140 charactes ( 114.97 bytes per tweet, total max 51737 bytes).

Original frames as Video, as PNG images [Sandbox note: Higher quality version will be uploaded soon]

Reference solution frames as Video [Sandbox note: Reference solution will be expanded once challenge changes stabilized]

Rules:

These rules are the based from rules in Encode Images into Tweets modified to accommodate multiple frames that will yield the resultant video.

  1. You must write a program that will communicate via std in/out based on the communication protocol defined below, ultimately producing a series of tweets that will, when fed back to your program, produce image frames of the video.
  2. The text created for each 'Tweet' by the program must be at most 140 characters long and must only contain characters whose code points are in the range of 32-126, inclusive.
  3. The source video will be 450 frames of Big Buck Bunny. Frames can be downloaded here
  4. Frames will be width 320 pixels and height 240 pixels @ 24 bit RGB, each channel having 8 bit precision.
  5. Your program should be able to compress other video sources at same resolution.
  6. Your program can use external libraries and files, but cannot require an internet connection or a connection to other computers.
  7. The decoding process cannot access or contain the original images in any way. (Clarification use of corpus images is allowed as long as these images do not contain direct or manipulated original image frames)
  8. Use of video codecs or existing libraries that perform video compression are not allowed. This is to encourage experimentation and originality rather than simply calling libraries. Note that use of static image compressors/ libraries are allowed.
  9. Standard Loopholes are forbidden.

Communication Protocol

  1. All communication is via std in/out.
  2. The controller program will launch your program with no command line arguments.
  3. The controller will either initiate a one of the following sequences commanding to your program and expecting results.

Note the sequences are described below in the following format:

<Comms direction from controller perspective>, <seq number>: <Transmitted Data> - <Information> 

For Encoding of the frames:

Output, 1: ENCODE\n - Indicating that the controller will be sending frames for your program to compress.
Output, 2: <number of frames>\n - String indicating number of frames. i.e. 450
Output, 3: <number of bytes for image data>\n - String indicating number of bytes of the current image data. e.g. 32145
Output, 4: <image data> - sequence of bytes of length defined in output line 3. The is the image data. The image data is a 24 bit image in PNG format
Repeat from output seq number 3 for the number of frames in output seq number 2.
Input, 1: <Tweet>\n - encoded image data. upto 140 characters, whose code points are in the range of 32-126, inclusive. A zero sized tweet will indicate end of all tweets.
Repeat from input line 1 for 1 to up to the number of frames in output seq number 2. Note a zero sized tweet will indicate end of all tweets.
Output, 5: END\n - Indicating that all processing has finished and your program should quit gracefully.

For Decoding of the frames:

Output, 1: DECODE\n - Indicating that the controller will be sending tweets for your program to decompress.
Output, 2: <number of tweets>\n - String indicating number of tweets. i.e. 450
Output, 3: <Tweet>\n - encoded image data. upto 140 characters, whose code points are in the range of 32-126
Input, 1: <number image frames decoded>\n - String indicating number of image frames that have been decoded as a result of the provided tweet. If 0 then no images have been decoded for this tweet.
Input, 2: <number of bytes for image data>\n - String indicating number of bytes of the current image data. e.g. 32145.
Input, 3: <image data> - sequence of bytes of length defined in input line 2. The is the image data. The image data is a 24 bit image in PNG format. 
Repeat from input seq number 2 for the number of frames in input seq number 1.
Repeat from output seq number 3 for the number of tweets in output seq number 2.
Output, 4: END\n - Indicating that all processing has finished and your program should quit gracefully.
  1. Your program will be restart between the encoding and decoding phases.

Scoring

As an attempt to model the human visual system in a simplistic manner, JPEG quality levels are used as an analogue. Each frame will be compressed to a JPEG using quality levels 0.01 to 1.0 in 0.01 increments. The PSNR of these JPEG compressed versions are calculated and added into a data structure. Additionally, each frame will be resized by half and the JPEG encoding and PSNR calculations performed and added to the data structure. The resizing and calculating will repeat until a single pixel is left.

The result of which will be a scoring continuum of visual 'quality' from low (single color frame) to High (near-identical to original).

Thus, scoring will be objective by means of the following formula:

JPEG_QUALITY_LEVEL_PSNR = a pre-computed data multi dimensional structure consisting of the
                          PSNR for incremental JPEG quality levels for each image frame of
                          the video. The entries into this structure for each frame is such
                          that higher indecies are deemed to be of more similar to the
                          original.

Score = SUM of (index of closest matching entry in JPEG_QUALITY_LEVEL_PSNR for given
                original frame and given PSNR of decoded frame)

Controller

[Sandbox note: Controller is to be created]

Refrence Solution

[Sandbox note: Reference is to be created once question stabilizes]

Sandbox Questions

Ok, so this is my first question ever posed to codegolf and I am very keen to get feedback on how to better present the challenge.

Some areas where I would like specific feed back is the objective scoring mechanism... i purposely avoided direct PSNR as the score as it is very limited for scoring video quality, being biased toward blurry images, however other algorithms are too complex or not defined. So as a compromise I am using JPEG compression as an analogue for the Human Visual System. Effectively matching a given solutions output frame to original frame PSNR to a JPEG compressed, at a given quality level) version's PSNR and using that match's index in sorted JPEG quality order as input for the score.

Is the protocol clear enough? if not what would be a better way to present it?

\$\endgroup\$
4
  • \$\begingroup\$ "I introduced it so that solutions that take all the frames then perform processing over all the frames would be at a clear advantage over those solutions than work over a much shorter frame period." I'm not seeing it. If I process all of the frames then the first frame will probably use a lot of tweets and the subsequent ones fewer, so I'll be penalised for the first frame. \$\endgroup\$ Sep 21, 2016 at 9:25
  • \$\begingroup\$ @PeterTaylor Indeed, that is the point i am ?trying? to make, In the fantasy world where video by tweets is a feature desired by users, having to wait until all the tweets are received before playing the video would be very annoying. So this part of the scoring is to give those solutions that return frames quicker in response to provided tweet(s) an advantage. I am thinking that it may be not that "interesting" or "fun" to have this modifier. I think i shall remove it unless some people think it has some worth. \$\endgroup\$
    – Moogie
    Sep 21, 2016 at 11:23
  • \$\begingroup\$ The point I was trying to make is that the feature of the scoring system seems to me to penalise the approach which you say it's trying to promote. \$\endgroup\$ Sep 21, 2016 at 11:28
  • \$\begingroup\$ oh i apologize, i think i misunderstood. Yes, in the example you have given it will penalize that solution, however it would be less penalized than a solution that waited for all tweets, but more penalized than a solution that immediately gives an image frame after each tweet. i.e. the fantasy user would have to wait longer than if the solution produced images after each tweet. Hmm, given the confusion i will remove the modifer as it will be difficult to tune the "negative" effect as to not completely favour immediate frames vs better quality frames. \$\endgroup\$
    – Moogie
    Sep 21, 2016 at 11:42
1
\$\begingroup\$

Interpret MSF-!

Because there aren't enough "Interpret X" challenges.

MiniStringFuck (or MSF-) is a brainfuck derivate made by @ErikTheGolfer, with two commands, operating on one cell:

InstructionAction                                                                               
+               Increment the memory cell. If it reaches 256, wrap to 0.
.                Output the character in the cell.                                      

or a bad-looking table:

Instruction, Action
+, Increment the memory cell. If it reaches 256, wrap to 0.
., Output the character in the cell.

Task

Your task is to, given a string of MSF- code, containing only printable ASCII characters, output what it would print.

The string may have other characters than + and .. Treat them as NOPs

Example

++++++++++++++++++++++++++++++++++++++++++++++++.+.+.+.+.+.+.+.+.+.
++++++++++++++++++++++++++++++++++++++++++++++++.                    Set cell to 48 (0) and output
                                                 +.+.+.+.+.+.+.+.+.  Keep adding and outputting until 57 (9)

Test Cases

+++++++++++++++++++++++++++++++++.+.+. -> !"#
++++++++++++++++++++++++++++++++++++++++++++++++.+.+.+.+.+.+.+.+.+. -> 0123456789
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+. -> ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ -> (no output)
\$\endgroup\$
3
  • \$\begingroup\$ Will the input string ever contain other characters? If so, should they be treated as NOP/comments? \$\endgroup\$
    – Geobits
    Sep 22, 2016 at 14:16
  • \$\begingroup\$ @Geobits Yes and yes. \$\endgroup\$
    – acrolith
    Sep 22, 2016 at 15:39
  • 1
    \$\begingroup\$ This is basically "Parse unary numbers", and as such I think it's a duplicate of codegolf.stackexchange.com/q/76756/194 \$\endgroup\$ Sep 22, 2016 at 16:20
1
\$\begingroup\$

Interleave your code with the input

Given an input string, interleave this string with the source code of your solution.

For example, if your code is abcd and the input is 1234, then your program must output 1a2b3c4d.

If your code is shorter than the input, then it must loop back to the beginning of your code. For example, if your code is abcd and the input is 123456, then your program must output 1a2b3c4d5a6b.

If your code is longer than the input, then only part of your code will be interleaved with the input. For example, if your code is abcd and the input is 12, then your program must output 1a2b.

Inputs and outputs

  • The input string will only contain characters which are valid string characters in your language.

  • The input may be taken through STDIN, as a function argument, or anything similar.

  • The output may be printed to STDOUT, returned from a function, or anything similar.

Scoring

This is , so the shortest answer in bytes wins.

\$\endgroup\$
4
  • \$\begingroup\$ I'm not even sure that this is possible. \$\endgroup\$
    – Fatalize
    Sep 28, 2016 at 12:01
  • 2
    \$\begingroup\$ Why would it not be possible? The only potential problem I see is being closed as a dupe for being a straightforward generalised quine. \$\endgroup\$ Sep 28, 2016 at 12:05
  • \$\begingroup\$ @PeterTaylor I don't understand how being a "straightforward generalised quine" makes it a dupe (of what?). I don't see how this is more straightforward than say this challenge nor do I think it is a dupe simply because source code is involved. \$\endgroup\$
    – Fatalize
    Sep 28, 2016 at 12:09
  • 2
    \$\begingroup\$ meta.codegolf.stackexchange.com/a/8595/194 \$\endgroup\$ Sep 28, 2016 at 13:28
1
\$\begingroup\$

Priority to the right

Your task is to regulate traffic on a crossroads. There are 4 roads coming from north, east, south and west.

The input is a string representing the upcoming traffic on each road. For example, NNNWS indicates there is a total of 5 cars: three at the north, one at the west and one at the south. The order has no importance here, NNNWS is equivalent to WNNSN.

You must output the order in which the cars should go, using the priority to the right rule: cars coming from the south must let cars coming from east go first, east gives way to north, north gives way to west and west gives way to south.

For example, with the input NNNWS, the south car should go first, then the west car, then the 3 north cars. The output should then be SWNNN.

There are some indecidable cases, for example NS or NNWSE : you should then output the string stuck.

Test cases

N => N
NW => WN
NWS => SWN
SNW => SWN
SSSSS => SSSSS
ENNNNES => NNNNEES
NS => stuck
NNWSE => stuck
\$\endgroup\$
1
\$\begingroup\$

golf.ko - Kernel module golf

A Linux-specific challenge.

Implement a Linux kernel module which printks Hello world upon loading, in some compiled language.


Source file, linker scripts and command lines, Makefile content (if any) are all counted towards the byte score.

You may expect kernel version to be 4.1 or 4.4.

The kernel is assumed to be running. It's built, not cleaned source code is assumed to reside in /usr/src/linux. In addition you may expect the system to be Debian and to have working DKMS.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Maybe can be a catalog. \$\endgroup\$
    – Vi.
    Sep 30, 2016 at 13:18
  • \$\begingroup\$ What does it mean for a kernel module to "print"? \$\endgroup\$ Sep 30, 2016 at 18:00
  • \$\begingroup\$ Issue behaviour similar to printk kernel API function in C. Output message to logs, to console, to /proc/kmsg. Shall definition what exactly is a printk be included? Obviously user is expected to call the function inside kernel, not re-implement printk. \$\endgroup\$
    – Vi.
    Oct 2, 2016 at 18:29
  • 1
    \$\begingroup\$ I see: "printks" just looks like a typo, it isn't really one. That could be clearer if you wrote "printks", and clearer still if you link to a man page or other documentation. But if you're requiring use of that library, you can pretty much rule out answers in languages other than C and maybe Rust (since what other languages will have bindings for it?), and I wouldn't be surprised if there's nothing interesting left to golf and you just get one answer. \$\endgroup\$ Oct 3, 2016 at 11:49
  • \$\begingroup\$ @PeterTaylor, Maybe just exclude printk requirement and require just some code derived from posted source code to be executed? Or just the endless loop (it should be doable in almost any language, is API-agnostic and is observable)? \$\endgroup\$
    – Vi.
    Oct 3, 2016 at 23:32
1
\$\begingroup\$

Random Quine

(something related to languages?)

You are to write N programs in N langauges (at least 2) that satify the following properties:

  1. For each program P, the program P must produce an output O that is equal to a random program Q that you wrote, including P.
  2. No two programs P and Q may be equal.

Let's say you have two programs, ABCD in language Bar and 1234 in language Baz. Then, ABCD must produce each of ABCD and 1234 with nonzero (but not necessarily equal) probability. Similarly, 1234 must output ABCD or 1234 with nonzero chance. Note that the chances do not have to be same for each program.

\$\endgroup\$
2
  • \$\begingroup\$ Higher N being winning doesn't seem to be viable on it's own as I think this could be done essentially forever. Will it be something like total length of all Ps divided by N? \$\endgroup\$ Oct 5, 2016 at 22:35
  • \$\begingroup\$ 1. On clarity: it's not clear what the requirements are for which programs each P must produce with non-zero probability. All of them? At least two different ones? ("At least one" would satisfy the stated properties, but not the explanation in the example). 2. On boringness: the programs can just be identical apart from an integer or string literal which they ignore when executing and replace at random when outputting. So a generalised quine of half of the random challenges would meet spec. \$\endgroup\$ Oct 6, 2016 at 7:28
1
\$\begingroup\$

Introduction

The challenge is to determine if two complex lines intersect.

Challenge

Given two lines made up of multiple straight line segments determine if the lines cross at any point.

Input can be in the format of you choice that takes a series of [x,y] coordinates. All coordinates will be integers (positive or negative)

Output will be truthy if the lines cross and falsey if they don't cross.

There will be no coincidental points and no segment of one line will terminate at a point touching the other line (there will be a clean cross of line segments if the case is truthy).

This is code golf so shortest answer wins

Example Input and Output

For the examples formats are given as

Line 1: [[X1,Y1],[X2,Y2]...,[Xn,Yn]] Line 2: [[X1,Y1],[X2,Y2]...,[Xn,Yn]]

Examples

Line 1: [[0,0],[1,1]] Line 2: [[0,1],[1,0]] = TRUE

Line 1: [[2,2],[1,0],[0,2]] Line 2: [[0,1],[1,1]] = TRUE

Line 1: [[1,3],[2,9],[3,6],[4,7],[5,4],[6,1]] Line 2: [[2,6],[3,5],[5,2]] = FALSE

Line 1: [[0,6],[3,8],[5,5],[8,5]] Line 2: [[0,4],[3,7],[5,4],[7,6]] = TRUE

Line 1: [[2,2],[3,2]] Line 2: [[1,1],[10,4]] = FALSE

enter image description here

Please let me know your thoughts, first puzzle.

\$\endgroup\$
3
  • \$\begingroup\$ What about the case where one of the line segments "touches" another. That is: one of the end points of a line is on the other line. Would that be considered "crossing" or not? \$\endgroup\$
    – DarthRubik
    Oct 6, 2016 at 23:49
  • \$\begingroup\$ @DarthRubik tried to clarify, don't need to handle that case. \$\endgroup\$
    – gtwebb
    Oct 6, 2016 at 23:55
  • \$\begingroup\$ All of the test cases have x increasing strictly monotonically. Is this guaranteed, or could line segments be vertical or even loop back? If you don't guarantee that input will avoid those situations, please add test cases to cover them. \$\endgroup\$ Oct 9, 2016 at 16:11
1
\$\begingroup\$

Raytracing/Raycasting

Here is a 256*256 heightmap. Your challenge is to render it, in minecraft-style blocks, using some form of raytracing.

enter image description here

Fundamental Definitions And Clarifications

These are all straightforward and fairly obvious but I'm being explicit so that what follows is (ideally) unambiguous.

  • Let each pixel represent 1x1 metre, for ease of thought.
  • This image is a top-down view. The bottom-left pixel of the image in screen space is at [0,0,0] in world space. The bottom-right pixel of the image in screen space is at [255,0,0] (i.e. the next full tile to the right would be anchored at [256,0,0]). The Z-axis increases away from us in world space so the top-left pixel is at [0,0,255] and the top-right-pixel is at [255,0,255].
  • As you are looking down to the floor at the moment, you have positive height and the vector from your eye to the image points in the direction of [0,-1,0], so the camera which corresponds to the above image has a world-space coordinate of something like [128,300,128].
  • It shouldn't really matter, but let's take the red channel as holding the height. A red channel value of foo puts that pixel at y=foo. The tallest possible pixel would have world coordinate [x,255,z] with 0<=x,z<256.

Aesthetic Definitions

  • Each pixel effectively represents a tower some number of 1x1x1 solid cubes. The edges of these blocks should be rendered sharply at 90-degree angles. If you were to convert this scene to triangles, each normal would be along one of the major axes. You can think of blocks having end-caps on their top (which is the floor, to us), and walls are extruded between neighbouring blocks as required. There are no holes in the rendered surface. You can assume all renderings will be from above (extra implied geometry below the floor doesn't matter).
  • As well as the heightmap, the world contains one camera and one light source. The light casts hard shadows, so every rendered pixel will require two ray casts. Light falloff is inverse-square. You may optionally gamma-correct the result.
  • Pixels on a surface which receive no light should be black. Pixels of rays which do not hit any surface should be magenta: rgb=(255,0,255).

Input Parameters

  • The camera is defined by position P=[px,py,pz], a T=[tx,ty,tz] target point, and a field of view FOV. It renders WIDTH x HEIGHT images such that the target point appears in the centre.
  • The camera is orientated such that it yaws, then pitches, but does not roll.
  • The light is at position L=[lx,ly,lz] and casts light uniformly in all directions.
  • You solution must be posted with reference image rendered with P=[80,256,0], T=[128,50,200], L=[256,300,128], WIDTH=400, HEIGHT=300 and FOV=45 degrees.
  • (As reference images emerge I might tweak these parameters.)

Free Parameters

  • The light can be any colour and brightness you like but the colour and brightness must be inputs to the program.
  • Likewise, the colour/material of the surface may be anything you like (except magenta, which is for revealing holes) but these colour/material parameters must be an adjustable input to the program.
  • Use any lighting/shader model you like.
  • Use fog or other effects if you like. Different submissions are likely to vary considerably in size, so there's room to play a bit.

Criteria

  • You must post code which can be run as-is, with the specified reference parameters and all your free parameters included.
  • You can take the heightmap image as hardcoded in a representation of your choice and this does not count towards your golf score. You must post the code which converts an image to your preferred format. This script should either run using common Linux command-line tools, or over an online service. This script should not assume that heightmaps are square.
  • The output of raw pixel data can be in any format you like (e.g. [[r,g,b],...] BUT you must provide a second script, not counted towards your golf score, which turns your program's raw output into some standard image format such as PNG. This script should either run using common Linux command-line tools, or over an online service.
  • You must post the final image produced by this method, as well as the code which produces the output which produces the image.

In a nutshell, the above specifies that your output is reproducible, with different parameters and heightmap images as desired.

The winner will be chosen a few weeks from now, as the shortest code which conforms to the above aesthetic specifications (e.g. shadows, no holes, etc) and which gives end-to-end reproducible results.

\$\endgroup\$
4
  • \$\begingroup\$ Maybe include a sample output? \$\endgroup\$
    – Steven H.
    Oct 7, 2016 at 18:10
  • \$\begingroup\$ My own attempt at this is part of a larger, more complex project, it'll be a while :-) In the meantime I was curious if others would find a simplified version of the problem a bit more fun. \$\endgroup\$
    – spraff
    Oct 7, 2016 at 19:42
  • \$\begingroup\$ Must run, so will expand on this later, if not within 48 hours then ping me. There are some subtleties with respect to offsets (are blocks centred?) The stuff about fog etc is wasted space, because in code-golf no-one will spend bytes on it, so remove it to KISS. I strongly advise before posting to get a test output which two independent programs agree on. \$\endgroup\$ Oct 8, 2016 at 8:07
  • \$\begingroup\$ 1. The subtlety I promised to expand on: does "The bottom-left pixel of the image in screen space is at [0,0,0] in world space" mean that the pixel corresponds to the voxel whose coordinates range from (0,0,0) to (1,1,1) or the voxel whose coordinates range from (-0.5, -0.5, -0.5) to (0.5, 0.5, 0.5)? 2. Also, what does "The camera is orientated such that it yaws, then pitches, but does not roll" mean? Perhaps it could be better expressed by giving the initial up, forward, right vectors and then saying around which axes it rotates. \$\endgroup\$ Oct 9, 2016 at 16:09
1
\$\begingroup\$

First 100 prime numbers in Wentelx87

Wentel consists of, at the basic level, a memory pointer and an instruction pointer. Instructions are stored loaded into memory. The execution pointer moves to the right after executing the instruction at that memory pointer.

The program is loaded into rightmost slot of memory. For example, if your program is 0010 0000 0101 0100, then the virtual machine will look like this when started:

0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0010 0000 0101 0100 (memory)
                                                            ^                   (execution pointer)
^                                                                               (memory pointer)

(from README)


Wentel documentation examples, thorough and clear explanation here.

Wentel is language that is very hard to program in. Unlike brain**k, functions and GOTOs can be implemented. It emulates real machine code/assembly much better than brainf**k.

Wentel is a simple language.

  1. instructions are held in the rightmost cells, one cell = one instruction
  2. execution pointer starts out on first instruction
  3. execution pointer moves right
  4. the same cells that hold memory hold instructions.
  5. these are the opcodes:

0000 - no-op

0001 - if the current address's value is equal to the value stored by the adress defined by the next word, move the execution pointer to the address pointed to the next word after the word after the current address.

0010 - move memory pointer to address specified by next word.

0011 - deposit the current address of the execution pointer into the current memory address

0100 - increment current memory address

0101 - decrement current memory address

0110 - move memory pointer left

0111 - move memory pointer right

  1. and here are the specialized opcodes for output.

1000 - output 0

1001 - output 1


That was a boiling down of Wentel. Please read the full docs. Now for the actual challenge. Output the first 100 prime numbers, separated by spaces, in Wentel.

This is , so the shortest answer in bits wins.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ If you want to encourage an answer to an existing question in a new language, the way to do it is with a bounty on the existing question. If you're worried about losing your rep because no answers will be posted, see meta.codegolf.stackexchange.com/q/5243/194 \$\endgroup\$ Oct 10, 2016 at 8:33
1
\$\begingroup\$

A simple but fun code golf challenge!

Given positive integer n

create a 2 dimensional matrix where each location is filled with it's xy index (starting from the top left).

For example:

Your grid should look like: input: 2

00 10
01 11

input: 3

00 10 20
01 11 21
02 12 22

Once the grid is created, randomly fill each index. This can be with an 'x' or any other way to denote a spot has been filled

A complete simulation looks like:

input: 3

x x x
x x x
x x x

O O O
O O O
O O O

You can use whatever character you want to fill in each location.

Extra points if you use a GUI and use black squares to fill it in

You should only fill a spot in once so the number of times you fill should be n^2. (So no randomly filling spots until they are all filled you have to check!)

You can refresh the grid however you want. i.e print the grid, make a new line, print again... You can use a timer. However you want to do it to illustrate each step. You only need to refresh when you are filling not when you are populating the grid.

An example simulation looks like this:

input: 2

00 10
01 11

randomed 01

00 10
XX 11

randomed 11

00 10
XX XX

randomed 11
re-roll
randomed 01
re-roll
randomed 00

XX 10
XX XX

randomed 10

XX XX
XX XX

Complete

Do not display your randomed numbers, the output should tell me which were randomed.

Have fun!

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

Just like magic


Nowadays, the word "abracadabra" is used mostly in connection with magic tricks. The original purpose of the word had more to do with superstition, and some doctors used it to make lethal diseases go away by writing it in a triangle1, like so:

A B R A C A D A B R A
 A B R A C A D A B R
  A B R A C A D A B
   A B R A C A D A
    A B R A C A D
     A B R A C A
      A B R A C
       A B R A
        A B R
         A B
          A

Your task is to write a program or function that outputs/returns the exact text shown above in as few bytes as possible. The output may contain trailing spaces on each line and/or trailing newlines. This is , so the shortest code in bytes wins.

1. https://en.wikipedia.org/wiki/Abracadabra


Sandbox questions

  • Is it too simple?
  • Is it a duplicate?
  • Any better name ideas?
\$\endgroup\$
4
  • \$\begingroup\$ "It's magic, you know! Never believe it's not so!" \$\endgroup\$ Oct 11, 2016 at 20:32
  • \$\begingroup\$ @VTCAKAVSMoACE Haha :) though I'm not sure how many people would get the reference. I had to google the lyrics, but the song sounds very familiar. I wonder where I've heard it before... \$\endgroup\$ Oct 11, 2016 at 20:39
  • \$\begingroup\$ Well, it's the same formatting than Number Triangles, but the task is clearly different. I suppose you could also call it "A Kind of Magic", although the Queen/Highlander reference might be a bit far-fetched. \$\endgroup\$
    – Arnauld
    Oct 11, 2016 at 23:04
  • \$\begingroup\$ The closest I can think of as a dupe is codegolf.stackexchange.com/q/65300/8478, but it seems likely that it's shorter to generate each line from the previous instead of generating a string of all prefixes first and the laying it out in a triangle. \$\endgroup\$ Oct 12, 2016 at 12:38
1
\$\begingroup\$

Ungolf batch

This question wants you to golf batch programms. In Batch you can use

set x=averylongbatchcommand
%x% arg1

which expands to

set x=averylongbatchcommand
averylongbatchcommand arg1

Your task is it to expand these statements.
As you can also use set for arithmetic purposes like this:

set x = 0
set /a x= %x%+1

you should ignore the statements where the pattern with set /a is applied.
There will never be something like

set s=SET
%s% s=%s% a

where the variable is assigned using itself as command. But you have to handle things like

b:
set a =echo
%a% Hello World!
set a=goto
%a% b

Scoring

This is so the answer with the fewest bytes wins.

Testcases

set s=set
%s% e=echo
%e% I will have to use this exact thing very often because
%e% it absolutely wouldn't be shorter if i just left out these set statements

Expands to:

set s=set
set e=echo
echo I will have to use this exact thing very often because
echo it absolutely wouldn't be shorter if i just left out these set statements

set s=set
%s% i=0
set g=goto
:a
if %i%==15 (
%g% e
)
%s% /a i=%i%+1
%g% a
:e

Expands to:

set s=set
set i=0
:a
if %i%==15(
set e
)
set /a i=%i%+1
goto a
e:

set s=set
%s% s=%s% a=

Mustn't be supported.

\$\endgroup\$
2
  • \$\begingroup\$ Too bad the question mentioned has no answers... \$\endgroup\$ Oct 18, 2016 at 0:44
  • \$\begingroup\$ I will probably post It tomorrow, if there isn't any critic. \$\endgroup\$ Oct 18, 2016 at 12:46
1
\$\begingroup\$

Convert Birkana to Hexademical

Introduction

Birkana is a not-very-well-known notation for hexadecimal using rune-like symbols. The general structure of a Birkana rune is like this:

|\     <-- 0x1
|/     <-- 0x2
|\     <-- 0x4
|/     <-- 0x8

The slash or backslash is present if its base number (0x1, 0x2, 0x4, or 0x8) is present in the number, or not otherwise. The pipe is always at the left-hand side.

For example, here's the Birkana rune for 0xD (= 0x1 + 0x4 + 0x8):

|\
|
|\
|/

Challenge

Given a Birkana rune as input, write a program or function to output the corresponding hexadecimal digit.

You may choose to output either lowercase (a-f) or uppercase (A-F) hex digits, but they cannot be mixed.

You only need to support the 16 hexadecimal digits and their corresponding Birkana runes.

Test cases

Input
Output

|
|
|
|
0

|\
|
|
|
1

|
|/
|
|
2

|\
|/
|
|
3

|
|
|\
|
4

|\
|
|\
|
5

|
|/
|\
|
6

|\
|/
|\
|
7

|
|
|
|/
8

|\
|
|
|/
9

|
|/
|
|/
A

|\
|/
|
|/
B

|
|
|\
|/
C

|\
|
|\
|/
D

|
|/
|\
|/
E

|\
|/
|\
|/
F

Scoring

This is , so the shortest answer (in bytes) wins.

\$\endgroup\$
1
  • \$\begingroup\$ Is this just binary displayed vertically using space/nothing for zero and a slash for a one? I guess the fact that the slashes alternate could just be ignored, treating each position as either empty or full, which would still give the correct output? If so, is it acceptable to output a hex digit for invalid inputs? This is likely to make a difference to the approaches used. \$\endgroup\$ Oct 18, 2016 at 12:03
1
\$\begingroup\$

Find the least squares regression line

Background

I'm taking AP stats now, so I figured I'd bring the joy to you all here.

The form of an LSRL (least squares regression line) is ŷ = a + bx.

Input

Your job is to find the least squares regression line for two data sets (lists), X and Y. X and Y will be the same size with at least 2 elements. They will contain positive or negative floats. Input can be taken in any reasonable format.

Output

The values a and b in the least squares regression line (LSRL) for the data in X and Y, where a is the y-intercept and b is the slope of the LSRL. This page describes a way to calculate this line.

This is , so shortest code in bytes wins!

Also, built-ins are allowed!

Examples

[1.0, 2.0, 3.0, 4.0] [12.0, 15.0, 16.0, 17.0] -> A: 11.0 B: 1.6
[1.5, 1.5, 51.0, -15.0] [2.0, 1109.0, 0.003, 2.0] -> A: 314.92 B: -3.7609
[1.0, 2.0] [1.0, 2.0] -> A: 0.0 B: 1.0
[-15.0, -13.0, -163.0, -12.25, -14.41, 100000.0, -630.0, -135.0] [-142531.0, -12.0, 153.0, -135.0, 135.0, 16.0, 21.0, 0.0] -> A: -20285.0 B: 0.20121
[3.0, 5.0, 7.0, 9.0, 11.0, 13.0] [2.0, 4.0, 6.0, 8.0, 10.0, 12.0] -> A: -1.0 B: 1.0
\$\endgroup\$
5
  • \$\begingroup\$ 1. Use the full name in the title, because the title will be shown in contexts where the body isn't. 2. What kind of numbers will X and Y contain? The test cases use signed integers, but can answers assume that all inputs will be integers, or that all inputs will be floating point numbers? 3. In some contexts, a quadratic can validly be described as a least squares regression line. It would be better to move the definition which makes it clear that it's a linear polynomial to the introduction, before the section on input, rather than having it in the section on output. \$\endgroup\$ Oct 22, 2016 at 20:55
  • \$\begingroup\$ 4. There's more than one way to calculate the gradient, and the method described is not the best one. I would suggest removing that section entirely and having a link to en.wikipedia.org/wiki/Simple_linear_regression in the introduction. \$\endgroup\$ Oct 22, 2016 at 20:56
  • \$\begingroup\$ @PeterTaylor, I have edited the question. Is it better now? \$\endgroup\$
    – Daniel
    Oct 22, 2016 at 21:26
  • \$\begingroup\$ Every math language has a builtin for this so it's just solve(x,y). Even without the built-in linear solver, this is just the pseudoinverse of (1,x) multiplied with y. \$\endgroup\$
    – Angs
    Oct 23, 2016 at 6:04
  • \$\begingroup\$ I disagree with Peter - for the sake of self-containedness, you should include at least one method of computing the LSRL (and keep the link in case people want to see more methods). \$\endgroup\$
    – user45941
    Oct 24, 2016 at 6:50
1
\$\begingroup\$

2D Poor Man's Plotting!



Introduction

Surely you've also come across the problem where you have a multi-dimensional function at hand and want to visualize it (it's just such an everyday problem!). So we need a (programmatic) solution to this. A memorable solution, thus obviously the shortest solution is best!

Specification

Input

Your (primary) input will be a function that maps two floats onto another float.

Additionally there are six other parameters (floats), which you may take explicitely or hard-code:

  • StartingValueX: 0
  • StartingValueY: 0
  • EndingValueX: 10
  • EndingValueY: 10
  • StepSizeX: 1
  • StepSizeY: 1

You may take these parameters using your preferred method of input, but please document it (especially how the function should be passed).

Output

The output is a formatted string or a printed output (whatever pleases you).

What to do?

The introduction already hinted it:
Given a function that takes two floats as inputs, iterate it two-dimensionally and print the resulting values. So the top left entry is the result of the evaluation at (StartValueX,StartValueY) and the bottom right entry is the evaluation of (EndValueX,EndValueY) and then you form basically a table from this by incrementing the StartValue by the respective StepSize for each iteration.

Formatting guidelines:
Between each line may be one (or more) empty lines (eg end your lines with \n\n in C).
There must be at least one white space between every output number.
All numbers with the same x-coordinate must be aligned respectively to their start.

Potential Corner Cases

You may always assume a certain function prototype is used.
If EndValue-StartValue is not an exact multiple of StepSize (which may happen because we're dealing with floats here) then you shall compute for all StartValue + k * StepSize smaller than EndValue as well as for EndValue.
You may always assume the computation results (of the function) are actually representable in your language.
You may not assume that the function is a simple mathematical function (e.g. any valid function adhering to the above conditions in your language must work).
Trailing newlines and white spaces are allowed.

Who wins?

This is code-golf so the shortest answer in bytes wins!
Standard rules apply of course.

Test-cases

Default parameters, f(x,y)=x+y
Output:

0  1  2  3  4  5  6  7  8  9  10
1  2  3  4  5  6  7  8  9  10 11
2  3  4  5  6  7  8  9  10 11 12
3  4  5  6  7  8  9  10 11 12 13
4  5  6  7  8  9  10 11 12 13 14
5  6  7  8  9  10 11 12 13 14 15
6  7  8  9  10 11 12 13 14 15 16
7  8  9  10 11 12 13 14 15 16 17
8  9  10 11 12 13 14 15 16 17 18
9  10 11 12 13 14 15 16 17 18 19
10 11 12 13 14 15 16 17 18 19 20
\$\endgroup\$
1
\$\begingroup\$

[Lang1] vs [Lang2] ... vs [LangN]: Battle of the golfers!

and other tags for the challenge questions, which may be a lot.

Note: I haven't figured out all the kinks out yet, all I have is a general idea that, with your help, can be put properly into an awesome challenge (I hope). The parts where my ideas are not solid yet have Notes attached to them that must be read.

Team

In the midst of the nation of SE, there lies a golf field for the community of golfers. These golfers play day and night using specialised clubs (languages) to complete difficult and challenging holes (challenges). Now it is time to see the best of these golf clubs in the game!

Note 1: First of all, this challenge is going to be language-specific. I haven't decided the languages yet, but right now I'm thinking around the lines of Java, C, C++, C# since these languages are very well known, unlike the other esoteric languages.

You, the golfer, are to choose one golf club out from these [Lang1], [Lang2], ..., [LangN]. You will team up with other players using the same golf club as you.

Note 2: Now there is supposed to be a table where the players can edit into this challenge (once I post it) to join a team. After ~1 week from the beginning of the sign-up, people will start competing in the challenge.

This will be a sort of golf relay, where every player must only complete 1 hole, no more, no less. But of course, each player may help teammates to complete the holes using the minimum number of shots (bytes) as possible. All golfers, of every team, will be playing using the same playing field, in this case UTF-8.

The Challenge

There will be a list of challenges [see Note 3] to be completed by the team. Each team member must complete 1 challenge only. After a fixed amount of time (see Note 3), all team members must have complete their assigned challenges (see Note 4). I will sum of all the bytes used by each team to figure out the winning team (see Note 5)!

Note 3: Either I will provide all the challenges, or each team member may give 1 challenge to the challenge pool. Challenges can range from anything like quines and "Hello, World" challenges, to more complicated ones, like solving advanced mathematical (pardon me) mumbo jumbo and outputting complex sequences. Every team completes all of these challenges (see Note 4).
The time that I have decided, so far, is to give the entire team 3 weeks (subject to change) to complete their challenges.

Note 4: Each team member complete 1 challenge. However, if the team sizes are different, the team with the greatest number of members will have to complete all of the challenges. The other teams only have to complete as many challenges as members, so that there will be one for every member.

Note 5: The scoring will be the number of bytes used by each team, summed up, and then divided by the number of challenges completed.

Answering

Here is the format to each answer:

#Language, challenge-number, byte-count in UTF-8

for example:

#Java, #34, <s>463</s> <s>444</s>  <s>442</s> bytes

Summary

To sum it up, here is the process that this challenge goes through:

  1. People who want to play must choose one team to be on, if they want to play.
  2. After registration is over, the games begin: each player is to choose only one challenge and give a golfed answer. (Other players of the same team are allowed to help teammates)
  3. After some time, when all the challenges are attempted (one for every person, if people haven't answered in this time, they won't be counted towards the challenge), I will calculate the score of each team (using the total bytes used).
  4. Leaderboard is announced and the winning team walks away with all the glory!

Comment any ways this challenge can be improved. My general idea is to implement a team game, instead of the standard free-for-all.

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

King of the Hill: Moon Rockets

This is a challenge where you code bots to compete in an arena. In this case, you code a rocket flying over the Moon. Your goal is to shoot down enemy rockets, while not running out of fuel and crashing into the Moon.

Environment

Unlike many KOTH challenges the playing field is continuous, rather than discrete. You are provided with variables for your position, velocity, and the direction you are facing. Each turn, you are given a list of all the rockets you can "see" (all rockets less than 10 units away). You return a turning speed, thrust and whether you want to fire missiles.

Arena

The arena wraps left-right. There is a floor; if you crash into it going faster than 4 m/s, you will explode. There is no ceiling, but you do have a limit of 10 fuel units. This can be refueled by landing on the floor going less than 4 m/s.

Missiles

Every turn, you have the option to fire missiles. Missiles go in the direction you are pointing, at a constant velocity 5 m/s faster than you were when you fired. If they come within a 1m radius of another rocket, they explode and destroy it.

Gravity

Gravity is a constant force of 1 downward. With a max thrust of 2, you can accelerate at an equal rate in the opposite direction.

Pastebin for current controller code: http://pastebin.com/FNyjrJfr
No tournament code is in place yet.

\$\endgroup\$
10
  • \$\begingroup\$ Possibly relevant: Spacewar KotH \$\endgroup\$ Oct 25, 2016 at 20:11
  • \$\begingroup\$ The controller seems to set the step size at 0.5s. Does that give enough time to take evasive action at all, given the missile speed and sight range? (Even discounting step size, with max thrust of 2, it seems like it's going to be hard to outmaneuver something going possibly several times faster) \$\endgroup\$
    – Geobits
    Oct 25, 2016 at 20:11
  • \$\begingroup\$ Additionally related to step size, how are collisions handled? If I'm a meter away from you and fire a missile that goes 7m/s, can it miss by overshooting in that half second? \$\endgroup\$
    – Geobits
    Oct 25, 2016 at 20:14
  • \$\begingroup\$ Can spaceships collide with each other, or only the surface? \$\endgroup\$
    – Geobits
    Oct 25, 2016 at 20:15
  • \$\begingroup\$ 1. Continuous presumably means floats, which means possible disagreements over the results (because e.g. Java doesn't strictly apply the IEEE-754 spec unless you tell it to). 2. The spec should say at the very least what information is available to the bots. Does the "list of all the rockets you can "see"" include velocity, angular velocity, etc. or just position? \$\endgroup\$ Oct 25, 2016 at 20:24
  • \$\begingroup\$ I assume one win condition is "last rocket flying", but what about stalemates? Is there a "last turn", since it seems like a couple pacifists could land/refuel forever? \$\endgroup\$
    – Geobits
    Oct 25, 2016 at 20:27
  • \$\begingroup\$ @Geobits - the collisions are handled point-by-point, so yes, you could miss someone right in front of you if you have a high closing speed. I think evasion is possible as long as you have at least 1 step of warning. \$\endgroup\$
    – Skyler
    Oct 26, 2016 at 13:10
  • \$\begingroup\$ @Geobits Spaceships can't collide in the current code. If they could, what should define a "collision"? Currently they are modeled as point objects. \$\endgroup\$
    – Skyler
    Oct 26, 2016 at 13:11
  • \$\begingroup\$ @PeterTaylor they can access velocity etc. as well as position. I think this might be required as if a rocket is moving fast you need to lead it with the missile. \$\endgroup\$
    – Skyler
    Oct 26, 2016 at 13:12
  • \$\begingroup\$ @Geobits I could add a condition that limits the total turns. Does 50 turns seem reasonable? \$\endgroup\$
    – Skyler
    Oct 26, 2016 at 13:13
1
\$\begingroup\$

C Compiler

Implement a basic C Compiler.

You do not need to implement the libraries. Compiler would output an object file containing a function, that would be linked with some libraries using GNU binutils ld and run.

No function calls, unions, structs, casts will be used in the test cases.

Test case:

int test(int i, int j) {
  int x[10][20];
  int s = i + j;
  i = 0;
  j = 0;
  for(i = 0; i < 10; i++)
    while (j < 20)
    {
      j += 1;
      x[i][j] = i * j;
    }
  for(i = 0; i < 10; i++)
    while (j < 20) {
      s += x[i][j];
    }
  return s;
}

\$\endgroup\$
1
  • 2
    \$\begingroup\$ What does need to be implemented? A question should ideally be self-contained, but if there's too much information to fit in the post then it should contain links to the relevant specifications. \$\endgroup\$ Oct 26, 2016 at 10:08
1
\$\begingroup\$

HamSort a list of strings

Given a list of strings, sort them in a manner such that the Hamming distance between each is minimal.

The Hamming distance between two strings of equal length is the number of positions where the corresponding characters do not match. For example, the Hamming distance between apple and ample is 1 since only the second characters of each do not match.

apple
ample
 *

Rules

  • This is so the shortest code wins.
  • Each string in the input list will have the same length and will consist of only the letters in the English alphabet. You may assume all to be either uppercase or lowercase.
  • If there are multiple possible solutions, you may output any number of them.

Test Cases

<to be created>
\$\endgroup\$
5
  • 2
    \$\begingroup\$ The Hamming between consecutive pairs? If so, what is to be minimized? The sum of the Hamming distances? Their maximum? \$\endgroup\$
    – xnor
    Oct 26, 2016 at 7:32
  • \$\begingroup\$ What is (for this challenge) the hamming distance of strings without equal length? Or will the list contain only strings of the same length? \$\endgroup\$
    – Laikoni
    Oct 26, 2016 at 8:06
  • \$\begingroup\$ @xnor It's still a work-in-progress. I'd like to have the problem be minimizing the Hamming distance between the consecutive pairs, but that problem might be hard enough that the only golfy solution would be generating all permutations and testing each pair. Then a length n input would have a length n-1 Hamming distance array where each value is minimal. If you have better ideas that could still capture the spirit of this, feel free to suggest them. \$\endgroup\$
    – miles
    Oct 26, 2016 at 8:33
  • \$\begingroup\$ @Laikoni Yes, Hamming distance is only defined between strings with equal length and one of the rules was that the input array would only contain strings with equal length. \$\endgroup\$
    – miles
    Oct 26, 2016 at 8:34
  • \$\begingroup\$ I think it's NP hard to find the path that minimizes total Hamming distance by reduction to Ham-Path. So, I wouldn't expect anything better than brute force there. \$\endgroup\$
    – xnor
    Oct 26, 2016 at 9:00
1
\$\begingroup\$

How fast (slow) is your language?

This is a question both to test your coding skills but also to speed benchmark your favorite language. The code snippet will make a table of the fastest code per language. It is related to previous [challenge]1 so you may find some of the code there a useful starting point.

The challenge is to write the fastest code possible for computing the permanent of a matrix whose entries are floating point in the range -1 to 1.

The permanent of an n-by-n matrix A = (ai,j) is defined as

enter image description here

Here S_n represents the set of all permutations of [1, n].

As an example (from the wiki):

enter image description here

In this question matrices are all square and will only have the floating point values in the range -1 to 1 (excluding both ends). I will construct all tests in such a way that the permanent is also in the range -1 to 1.

Examples

Input:

[[-0.34389946 -0.811193  ]
 [ 0.62778788 -0.09918764]]

Permanent:

-0.47514655471

Input:

[[-0.34389946 -0.811193  ]
[ 0.4783581  -0.56636227]]

Permanent:

-0.19326906099

The task

You should write code that, given an n by n matrix, outputs its permanent.

As the output will never be bigger than 1 in absolute value you do not have to worry about overflow. However you do have to worry about numerical accuracy. All answers should be correct up to 8 decimal places.

Languages and libraries

You can use any available language and libraries you like but no pre-existing function to compute the permanent.

More to be added..Work in progress

\$\endgroup\$
4
  • \$\begingroup\$ "All answers should be correct up to 8 decimal places." Are you sure this is possible? I suspect that finding the permanent has similar problems of ill-conditioning to finding the determinant. \$\endgroup\$ Oct 28, 2016 at 11:37
  • \$\begingroup\$ So this is your posted permanent challenge but with floats? I don't see what that's so different as to warrant another challenge. \$\endgroup\$
    – xnor
    Oct 29, 2016 at 6:12
  • \$\begingroup\$ @xnor The only differences are a) it avoids all the big int stuff that turned out to be crucial in the last challenge and b) it will have a code snippet and will be designed to encourage answers in lots of languages. Not just fast ones. The idea is to show how slow (fast) your language is and it can serve as a fun sort of language competition/benchmark. \$\endgroup\$
    – user9206
    Oct 29, 2016 at 7:07
  • \$\begingroup\$ @PeterTaylor That's a very interesting point. I am not sure of the answer but I was going to choose random orthogonal matrices (I need to change and add examples) so maybe ill-conditioning will be very rare? \$\endgroup\$
    – user9206
    Oct 29, 2016 at 7:08
1
\$\begingroup\$

Directional Brainf*ck Compiler/Interpreter

Challenge: Write a Brainf*ck compiler with 4 extra commands.

L Set the direction to go left
R Set the direction to go right
D Set the direction to go down
U Set the direction to go up

The original commands (altered):

+ Increment the cell one
- Decrement the cell one
[ Start a loop, if the cell is 0 then go to the next ]
] end a loop if the current cell is 0. Else go back to the last [ read and continue from there (in the direction you were going last time it was read.
. output the byte at the data pointer.
, accept one byte of input, storing its value in the byte at the data pointer.
> point to the cell to the right
< point to the cell to the left

Compiler/Interpreter

In this case the compiler must either convert the code to another language or be an interpreter and run the code.

This is basically a directional version of brainf*ck. Directions should work the same way they do in ><>.

The pointer should start at the tape's beginning. It must be infinite in the right direction. Left infinite is optional (if you add it -10 to the score). Upon end of code output (in any reasonable form) the current values on all the stacks. If a ] is hit before a [ output a warning (with a message of your choosing). If a , is met at the end of the code you can do with it as you please. The program will terminate at the end of the file.

Standard loopholes apply.

\$\endgroup\$
14
  • 1
    \$\begingroup\$ So, what does ] do \$\endgroup\$
    – feersum
    Oct 23, 2016 at 19:05
  • \$\begingroup\$ Taken from Wikipedia: if the byte at the data pointer is nonzero, then instead of moving the instruction pointer forward to the next command, jump it back to the command after the matching [ command. @feersum \$\endgroup\$
    – XiKuuKy
    Oct 24, 2016 at 0:51
  • 1
    \$\begingroup\$ You don't need to tell me what it does in brainfuck. I'm asking you, what does it do in this language you're defining here? \$\endgroup\$
    – feersum
    Oct 24, 2016 at 1:02
  • \$\begingroup\$ @feersum Oh okay. If the byte at the data pointer isn't 0 then instead of going to the next command. Go back to the last [ found. Should I add that to the challenge description? \$\endgroup\$
    – XiKuuKy
    Oct 24, 2016 at 1:39
  • \$\begingroup\$ That description doesn't accord with the normal interpretation, which goes to a matching bracket, rather than the last one found. And it still doesn't clarify how it will interact with the two-dimensional aspect. In particular, what direction will the instruction pointer travel after jumping to a [? \$\endgroup\$
    – feersum
    Oct 24, 2016 at 2:01
  • 1
    \$\begingroup\$ @feersum it should go in the same direction it was going last time it was parsed I guess. I thought the last [ parsed would be better since I can't imagine matching brackets in 2D language. \$\endgroup\$
    – XiKuuKy
    Oct 24, 2016 at 2:06
  • \$\begingroup\$ You need to specify the behavior in the challenge specification. Personally I don't see an issue with jumping back to the command after the last [ encountered, continuing in the same direction. 1D programs in this extension that didn't use any of LRDU would be compatible with regular brainfuck with that behavior (assuming no mismatched braces). \$\endgroup\$
    – user45941
    Oct 24, 2016 at 6:47
  • \$\begingroup\$ Also, what does [ do if the cell is 0.... \$\endgroup\$
    – feersum
    Oct 24, 2016 at 19:03
  • \$\begingroup\$ @feersum Jump to the next ] in the current direction, presumably. That needs to be specified too, though. \$\endgroup\$
    – user45941
    Oct 24, 2016 at 21:09
  • \$\begingroup\$ @trichoplax Done. \$\endgroup\$
    – XiKuuKy
    Oct 25, 2016 at 3:43
  • \$\begingroup\$ Since tags can't be added to an answer, you can use [tag:code-golf] to show the tags you are going to use, so it won't be necessary in the title. \$\endgroup\$ Oct 25, 2016 at 9:17
  • \$\begingroup\$ If it's going to be a compiler/interpreter, it may attract more competitors if that is included in the title. \$\endgroup\$ Oct 25, 2016 at 9:18
  • \$\begingroup\$ Is this ready to post? \$\endgroup\$
    – XiKuuKy
    Oct 28, 2016 at 14:36
  • 1
    \$\begingroup\$ It's missing a lot of info. (1) The position and direction of instrucitons pointer at the beginning. (2) What happens upon reaching an edge of teh code. (3) What happens if ] is hit before any [. (4) The usual BF-specific stuff: (a) behavior of , on EOF (b) Integer range of cells (c) whether tape should be infinite in one direction, both, or something else. (5) How does the program terminate, if at all. \$\endgroup\$
    – feersum
    Oct 29, 2016 at 2:52
1
\$\begingroup\$

Modular Reverse Recursion Sequence

Given any number n > 0, let r represent the reverse of the number n. Iterate until the final result is zero, passing the result of each iteration back into the function using recursion or a methodology of your choice by performing the below operation:

  • If r > n for that iteration the result is r % n.
  • If n > r for that iteration the result is n % r.
  • If n % r = 0 or r % n = 0, you terminate iteration.

Take the intermediate result of each execution and store them in an array for the final answer.

Lets walk through an example where n=32452345.

54325423 % 32452345 = 21873078 # r > n, uses r % n
87037812 % 21873078 = 21418578 # r > n, uses r % n
87581412 % 21418578 = 1907100  # r > n, uses r % n
1907100 % 17091 = 9999         # n > r, uses n % r
9999 % 9999 = 0                # r % n = n % r = 0, terminated

Result: [21873078, 21418578, 1907100, 9999]     

Another example n=12345678:

87654321 % 12345678 = 1234575 # r > n, uses r % n
5754321 % 1234575 = 816021    # r > n, uses r % n
816021 % 120618 = 92313       # n > r, uses n % r
92313 % 31329 = 29655         # n > r, uses n % r
55692 % 29655 = 26037         # r > n, uses r % n
73062 % 26037 = 20988         # r > n, uses r % n
88902 % 20988 = 4950          # r > n, uses r % n
4950 % 594 = 198              # n > r, uses n % r
891 % 198 = 99                # r > n, uses r % n
99 % 99 = 0                   # r % n = n % r = 0, terminated

Result: [1234575, 816021, 92313, 29655, 26037, 20988, 4950, 198, 99]

A final example n=11000:

11000 % 11 = 0 # n % r = 0, terminated

Result: []
\$\endgroup\$
2
  • 1
    \$\begingroup\$ If you have to include 0 as the final element, it might be better to say "if n = 0, terminate", since terminating when n=r doesn't really include 0, because the final modulus (e.g. 9999%9999) was never done. \$\endgroup\$
    – Geobits
    Nov 1, 2016 at 18:22
  • \$\begingroup\$ @Geobits Edited to remove 0 from the list of final results, nice catch. That was the only part I did manually. \$\endgroup\$ Nov 1, 2016 at 18:33
1
\$\begingroup\$

Simplified maze-solver

A spin-off from this question Textual maze solver

Write a program, that will take an bunch of 0's and 1's as it's input (see below), and a half-solved maze as it's output. The program should take 1's as walls and 0's as corridors. The output should should show 99's as walls and a number as the distance from the start of the maze. The maze should have no real entrance/exit; instead, the entrance is the square in the 2nd column of the 2nd row.

Examples (allowed cases):

Input               Output

1 1 1 1 1 1         99 99 99 99 99 99
1 0 0 1 0 1         99 00 01 99 05 99
1 1 0 0 0 1   -->   99 99 02 03 04 99
1 0 1 1 0 1         99 10 99 99 05 99
1 0 0 0 0 1         99 09 08 07 06 99
1 1 1 1 1 1         99 99 99 99 99 99 

{{1, 1, 1, 1, 1, 1, 1, 1, 1},         99 99 99 99 99 99 99 99 99
 {1, 0, 0, 0, 1, 0, 0, 0, 1},         99 0  1  2  99 16 17 18 99
 {1, 1, 1, 0, 1, 0, 1, 0, 1},         99 99 99 3  99 15 99 19 99
 {1, 0, 0, 0, 1, 0, 1, 0, 1},   -->   99 6  5  4  99 14 99 20 99
 {1, 0, 1, 1, 1, 0, 1, 0, 1},         99 7  99 99 99 13 99 21 99
 {1, 0, 0, 0, 0, 0, 1, 0, 1},         99 8  9  10 11 12 99 22 99
 {1, 1, 1, 1, 1, 1, 1, 1, 1}}         99 99 99 99 99 99 99 99 99

Examples (not allowed outputs and inputs):

11111 10101 10101 10001 11111  --> 9999999999 9900990699 9901990599 9902030499 9999999999

Notes:

  • as seen above, the input and output must be bunch of numbers separated by whitespaces, commas, full stops, newlines, parenthesis (and so on), and mustn't be in a single row

  • you can output 01 instead of 1

  • cases where the distance is over 99 don't need to be covered

  • the program doesn't need to solve the maze, just to find the distance from the start

  • two fields connected diagonally aren't next to each other

  • it's , so the shortest answer in bytes wins!


\$\endgroup\$
4
  • \$\begingroup\$ it makes more sense to show walls as ## or || etc, instead of an integer \$\endgroup\$
    – FlipTack
    Nov 5, 2016 at 21:56
  • \$\begingroup\$ The restrictions on the format on the output and inputs don't add anything to the challenge (imo) : the interesting part is solving the maze. \$\endgroup\$
    – Dada
    Nov 6, 2016 at 10:56
  • \$\begingroup\$ @Dada They allow the program to be shorter, at least in most languages I know. \$\endgroup\$
    – user60199
    Nov 6, 2016 at 13:55
  • \$\begingroup\$ @Flp.Tkc See the comment above. \$\endgroup\$
    – user60199
    Nov 6, 2016 at 13:56
1
\$\begingroup\$

Defs need to fix up layout

[Very much a WIP]

Maze battle

In this challenge, bots will team up, navigate around a maze and try to destroy the enemy team's artifact, and protect their own.

A maze might look like this. Note that the bots will receive numbers rather than ascii art, which represent their vision, and the objects they see.

+---------+-+---+-------+++-+-+---+-+-------+-+-----+
|         | |   |    #  | | | |   | |       | |     |
+-+ +-+-+ + + + +-+-+ +-+ + + + + + + + +-+-+ +-+ + |
|   |         |         |     | |   | | |   |     | |
| +-+-+ + +-+ +-+ + + +-+ + +-+ +-+ +-+ + +-+ +-+ + |
|       | |   |   | |     | |   |         | |   |   |
| +-+-+-+ +-+-+-+ + + + + +-+ +-+-+ +-+ + + + +-+ + |
|   |     |       |   | |   |   |     | | |       | |
| + + +-+-+ +-+ +-+ +-+ +-+ + +-+ + + + + + +-+ + + |
| |       |                 | |   | | |     |   | | |
| + + + +-+-+-+-+ +-+-+-+ + + +-+ +-+ + +-+-+-+ +-+-+
| | | |     |   | | |     |     |   | | |           |
| +-+ +-+ + +-+ +-+ +-+-+ +-+ + + + + + +-+-+ +-+ + |
|   | |   | |           | |   | | | |   | | |     | |
| +-+ + +-+ +-+ + + + +-+-+-+ +-+ +-+-+ + + + +-+ + |
|     | |       | | |   | | | | | |         |   | | |
| + + +-+ + + + + +-+ +-+ + +-+ + +-+ +-+-+ +-+ +-+-+
| | |   | | | | |   |   |       | |   | |       |   |
| +-+ + + +-+ +-+-+-+ +-+ +-+-+ + + +-+ + +-+-+ +-+ |
|   | |   |         |       |   |       |   |       |
+-+ +-+-+ +-+ +-+ +-+ + +-+ + + + +-+ +-+ +-+ + +-+-+
|   |   |   |   |   | | |     |     |   | |   |     |
| + +-+ +-+ + +-+-+-+ + + + +-+-+-+ +-+ + + +-+ +-+ |
| |       |   |   |     | |         | |   | |       |
| +-+-+ + +-+ +-+ + +-+-+-+ + + + +-+ +-+ +-+-+ +-+ |
| |   | | | |       | |   | | | | |   |     |     | |
+-+-+ + +-+ +-+-+-+-+ +-+ +-+ +-+ + +-+-+ +-+ +-+ + |
| | | | |   |           |     | |     |       | | | |
| + + +-+-+ + +-+ +-+-+ + +-+-+ +-+ +-+-+-+ +-+ +-+-+
|   |     | | |             |   | |   | |     |     |
+-+ +-+-+ + + + +-+ +-+-+-+ +-+ + +-+ + +-+ +-+ +-+-+
|   | |   |   |   | |     | |   |     |   |       | |
| +-+ + +-+-+ + +-+ + + +-+-+ + + + + + +-+-+ + + + |
|       | |           |       |   | | | |     | |   |
| +-+-+ + +-+ + +-+-+ + + +-+ + +-+-+ + +-+ + + +-+ |
|   | |   |         | | | |   |     |   | | | | |   |
+-+ + + + + + + +-+ + +-+-+-+-+ +-+-+ +-+ +-+-+ + +-+
|       |   | | |           |     | | |   | |       |
+-+-+-+ + +-+-+-+-+-+ + +-+-+ +-+ + +-+ + + + +-+ + |
|     |   | |         |     | |     |   |       | | |
+-+ +-+ +-+ +-+ + +-+ +-+-+ + + +-+ +-+ +-+ +-+ +-+-+
|     | | |     |   |         | |           |   |   |
+-+ +-+-+ + +-+-+ +-+-+ +-+-+ +-+ +-+-+-+-+ + +-+ + |
|               | |   |                 |   | |   | |
+-+-+ +-+-+-+ + + + + +-+-+-+-+-+-+ +-+ + + +-+ +-+ |
|       |   | | |   |   |           | | | |       | |
| +-+-+ + + +-+ +-+ + + +-+-+ +-+ + + + +-+-+-+-+ + |
| |       | | | |   | |       |   |     |           |
+-+-+ + +-+-+ + + +-+ +-+ +-+-+ + +-+ + + +-+-+ +-+-+
| |   |     |   | | |   |       |     | |     |     |
| + +-+ + + + + + + + + + + + + +-+-+-+ +-+-+ +-+-+ |
|       | | | |    #  |   | | | |       |     |     |
+-------+-+-+-+-------+---+-+-+-+-------+-----+-----+

Bots receive information about their current environment. They will receive a 8 lines of numbers, representing their view in each of the directions. vision is blocked by walls, and walls only, so at the end of the line of sight is a wall.

Numbers represent the following things:

  • 0 represents a space with nothing in it.
  • 1 represents a friendly bot
  • 2 represents an enemy bot
  • 3 represents your team's artifact
  • 4 represents the enemy team's artifact

To destroy the enemy's artifact, you have to shoot it a bunch. heaps. So, you probably want to bring that artifact to your team, or your team's side, so that you can shoot it more.

When you die, you respawn after 5 turns.

At the start of the round, bots spawn into the first third of the maze on their side. when respawning, they also spawn into this area.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Nice idea! Would be better if it was Java :D \$\endgroup\$
    – user41805
    Nov 6, 2016 at 10:20
  • 1
    \$\begingroup\$ @KritixiLithos: No \$\endgroup\$ Nov 8, 2016 at 23:28
1
\$\begingroup\$

Format an XML file

Your task is to parse an XML snippet (not necessarily an entire file) and format it. I am explicitly making this simple to encourage extremely golfed code.

The formatter should transform XML like this:

<?xml version="1.0" encoding="UTF-8"?>
<body>
    <div class="community-option">
        <input id="communitymode" name="communitymode" type="checkbox"/>
        <label for="communitymode" title="Marking an answer community wiki encourages others to edit it by lowering the reputation barrier required to edit. However, you will not gain any upvote reputation from it. This cannot be undone.">community wiki</label>
        <a href="www.hoehel.be"><i>My inner web developer hates me</i></a>
        <b>BOLD</b>
    </div>
    <iframe/>
</body>

into formatted text like this:

?xml
body
    div
        input
        label
        a
            i
        b
    iframe

Rules

The main element does not get a indent, every layer deeper does get an extra indent. An indent is either a tab or 4 spaces.

When there are multiple root elements, they simply are added below eachother with no indentation. The ?xml element is not different from any other element.

The XML will have a space after each element name, so if there is a CDATA element, it will be formatted as <![CDATA[ (the data)]]> and will need to be formatted as ![CDATA[ at the correct indentation.

Also, the XML will not include > and < except for opening and closing elements.

\$\endgroup\$
4
  • \$\begingroup\$ In XML, a right angle bracket > is allowed in an attribute. So perhaps include that as a test case, or explicitly state that this will not happen. Also, what qualifies as an indent? One space? Four spaces? A tab character \t? \$\endgroup\$
    – Sanchises
    Nov 8, 2016 at 11:31
  • \$\begingroup\$ 1. The example is invalid because (a) it doesn't have a single root element; (b) the input element isn't closed. 2. This isn't really pretty printing XML. It's pretty printing the element structure of XML. 3. There are a whole bunch of important test cases which are missing. CDATA. Namespaces defined at the root element. Namespaces defined on non-root elements. Elements expanded from entities. There are bound to be some others if you look at the spec. \$\endgroup\$ Nov 8, 2016 at 17:08
  • \$\begingroup\$ @sanchises Reworked the question to avoid problems there. \$\endgroup\$
    – vrwim
    Nov 8, 2016 at 21:11
  • \$\begingroup\$ @PeterTaylor (a) I'll include that it can be a snippet. (b) Didn't notice it, took some HTML from stackoverflow source code, don't blame me :) 2. Renamed to "formatting" the XML 3. Added workaround for the special cases I could find. \$\endgroup\$
    – vrwim
    Nov 8, 2016 at 21:15
1
\$\begingroup\$

Build a learning 20Q bot

Twenty Questions is a verbal game where an 'asker' asks you a series of yes or no questions about an object that you're thinking of. If the 'asker' gets the object correct after twenty questions, then the 'asker' wins the game. If the asker does not guess the question right after 20 questions, then the 'thinker' wins.

Input:

Your program needs to take 4 different inputs, non simultaneously. Those three inputs are:

  • A category (valid categories are Animal, Food, Action, Person/Character, Thing)
  • A yes / no input from the user to answer the questions.
  • A question input to 'teach the bot', and an object input to show the bot what was being described.

Output:

Your program needs to play a game of 20 questions, wherein it follows this structure:

  • Ask a category like so: "Select a category (Animal, Food, Action, Person/Character, Thing):"
  • Ask questions based on the category.
  • Make a guess of the object like so: "Is it a "[guess]"?".
  • IF the guess was wrong, ask what the object was, and inquire the difference between the guess and the object described.
  • your program must stop at 20 questions and make a guess. it doesn't have to be random.
  • your program must progressively 'learn' as it is played. It must be able to add new questions and objects to guess.

Examples:

⇨Select a category (Animal, Food, Action, Person/Character, Thing):  
⇦Food  
⇨Is it  ?  
⇦No  
⇨What is it?  
⇦Marmite  
⇨What is the difference between  and Marmite?   
⇦It has a saying 'You either love it or hate it'  
⇨  
⇨Select a category (Animal, Food, Action, Person/Character, Thing):  
⇦Food  
⇨Does It has a saying 'You either love it or hate it'?   
⇦No  
⇨Is it Marmite?  
⇦No  
⇨What is it?  
⇦A Carrot  
⇨What is the difference between Marmite and A Carrot?  
⇦It has lots of vitamin C   
⇨  
⇨Select a category (Animal, Food, Action, Person/Character, Thing):  
⇦Food  
⇨Does It has a saying 'You either love it or hate it'?   
⇦No  
⇨Does it It has lots of vitamin C?  
⇦Yes  
⇨Is it a Carrot?  
⇦No  
⇨What is it?  
⇦A Yam  
⇨ What's the difference between a Carrot and A Yam?  
⇦It grows underground.  
⇨  
⇨Select a category (Animal, Food, Action, Person/Character, Thing):  
⇦Food  
⇨Does It has a saying 'You either love it or hate it'?   
Yes  
⇨Is it Marmite?  
Yes  
⇨Yay, I Win!  
⇨  
⇨Select a category (Animal, Food, Action, Person/Character, Thing): 

Etc.

\$\endgroup\$
2
  • \$\begingroup\$ Pass2: How Can I improve this? I would like some critiques before I post. \$\endgroup\$
    – tuskiomi
    Nov 16, 2016 at 19:51
  • \$\begingroup\$ Possible duplicate: codegolf.stackexchange.com/q/57171/32686 \$\endgroup\$
    – Blue
    Feb 10, 2017 at 15:14
1
\$\begingroup\$

Produce a quine of length n

Write a program that, given a number n, produces a quine in your language of length n. If your language cannot have a quine of that length, it does not need to be generated.

Standard loopholes are forbidden.

The quines that are generated must be proper quines.

Remember, this is , so the code with the smallest number of bytes wins.

\$\endgroup\$
7
  • \$\begingroup\$ Is there a minimum/maximum n? Plenty of languages don't have a 1 or 2 length quine, for example. \$\endgroup\$
    – Geobits
    Nov 17, 2016 at 20:28
  • \$\begingroup\$ @Geobits How botu now? \$\endgroup\$
    – Oliver Ni
    Nov 18, 2016 at 0:03
  • \$\begingroup\$ Seems reasonable. I'm not sure how to prove it for some languages, but I'll leave that to answerers :) \$\endgroup\$
    – Geobits
    Nov 18, 2016 at 0:13
  • \$\begingroup\$ The ! in the title made me think that the quine had to have length n factorial, whereas the text says n. You should remove the ! in the title. \$\endgroup\$
    – Fatalize
    Nov 18, 2016 at 9:11
  • \$\begingroup\$ @Fatalize Okay. Done. \$\endgroup\$
    – Oliver Ni
    Nov 18, 2016 at 16:09
  • \$\begingroup\$ Regarding what @Geobits said, with the current wording I'm definitely worried since answerers would need to prove that they've got the minimum length quine covered. \$\endgroup\$
    – Sp3000
    Nov 19, 2016 at 5:54
  • \$\begingroup\$ Sp3000's concern is definitely valid. It would mean that you can only answer in languages where the shortest known quine has been proven to be optimal. Also, I'm not sure it's terribly interesting in most languages, because you can usually just insert spaces or no-ops into one or two places and the code remains a quine. \$\endgroup\$ Nov 21, 2016 at 15:23
1
\$\begingroup\$

Rearrange the Words!

Given a string of text containing some words, which are separated by spaces, determine the best arrangement to fit it in the shortest number of pages.

If a word is shorter than the length of a page:

  • If the word fits on the latest page, it stays on that page
  • If the word does not fit, it goes to the next page.

If a word is longer than the length of a page, it stays on that page and overflows to the next page.

Words have one character between them if they are on the same page.

For example, if the length of the page was 10, and my words are hello my name is oliver ni!!, it would fit like this:

hello my
name is
oliver
ni!!       # 4 pages

However, one of the ways to rearrange it can get a shorter number of pages:

hello name
oliver is
my ni!!    # 3 pages

So the shortest way in this case would be 3 pages.

Remember, this is , so the code with the fewest bytes wins.

\$\endgroup\$
5
  • \$\begingroup\$ 1) Does the greedy algorithm give an optimal solution? If not, there should be test cases specifically for it. 2) A few test cases would be good, covering edge cases such as everything fitting on one line 3) Can the length of the page be shorter than the longest word? Is that valid input? 4) Is the output the arrangement or the number of pages? \$\endgroup\$
    – Sp3000
    Nov 21, 2016 at 1:15
  • \$\begingroup\$ This problem is well-known to be NP-complete. As such, the greedy algorithm can't possibly be optimal. I imagine that code-golf solutions will just brute-force the solution, given that (for an NP-complete problem) it's very hard to objectively enforce a faster-than-brute-force algorithm. \$\endgroup\$
    – user62131
    Nov 21, 2016 at 1:19
  • 1
    \$\begingroup\$ An example where the greedy algorithm fails: word lengths 10, 6, 5, 3, 2, 2, page length 14 (this assumes that spaces have already been "factored in to" the word and page lengths by adding 1 to each). The greedy algorithm gives (10, 3), (6, 5, 2), (2). The best answer is (10, 2, 2), (6, 5, 3). \$\endgroup\$
    – user62131
    Nov 21, 2016 at 2:04
  • \$\begingroup\$ @ais523 I'm pretty sure the greedy algorithm will give an optimal solution. \$\endgroup\$
    – Oliver Ni
    Nov 21, 2016 at 5:28
  • \$\begingroup\$ @Oliver ais523's example is actually 9, 5, 4, 2, 1, 1 with page length 13. The greedy algorithm gives (9 + space + 2), (5 + space + 4 + space + 1), (1), but the optimal solution is (9 + space + 1 + space + 1), (5 + space + 4 + space + 2). \$\endgroup\$ Nov 21, 2016 at 15:45
1
\$\begingroup\$

Change the type of a variable without introducing any new identifiers while preserving a value resulting from the conversion of the original one

Consider the variable which type you should change is declared/defined inside a function. Another thing to consider is that there wouldn't be any other identifiers manually defined/included before your function and the variable inside it (this includes referring to standard header/modules too). Also you aren't allowed to change the value or meaning of any existing identifiers (if any) except a after your code is finished, compared to before it started executing. (Of-course this doesn't forbids you to actually change them but after your code is finished they must be in the same state as before it was entered in, except a of-course)

As identifiers I mean reserved names of functions, variables and etc.

Example in C:

void f() //no identifiers defined/included prior
    //the function can't have any parameters

{ //inside function block

    int a = 67; //assign some constant value here

    //... your code here

    //no new identifiers introduced
    //and no existing one have changed meaning (or value) after this point
    //except 'a' of-course

    a ; //here 'a' must have different static type
        //with a value resulting from the conversion
        //of the one stored previously in 'a'


    //You are allowed to include libraries/modules (identifiers) here in
    //order to show us the value and type of 'a'
    //The unused statement above is used only as valid syntax containing 'a'
}

Of-course the above snippet is just a sample. In case of using actually C you are allowed to add all other kind of stuff (like additional block scopes, etc). The only important thing to target is that after you have declared some variable in your function you have to write some magic with the requirements given.

This question have intended solution in C. I'm not sure how applicable is it for other languages though because it depends on the concept of having some names (identifiers) referring to certain entities. Also it needs types and object values defined too.

The initial and target type of a aren't specified. Also if the identifier a of the variable is not allowed, you can use one that is.

\$\endgroup\$
3
  • \$\begingroup\$ I think I understand now your purpose better. This is a programming-puzzle, and we certainly allow them. For that purpose, I think its a good idea to limit it to C. That said, you should be very explicit about where they are allowed to put code and what code they can write. (You have it in your comments, but I'd add it in the post) \$\endgroup\$ Nov 21, 2016 at 16:33
  • \$\begingroup\$ @NathanMerrill Thanks for the feedback! Yeah - you are probably right to limit this question as C only. \$\endgroup\$ Nov 21, 2016 at 16:49
  • \$\begingroup\$ Is any C standard allowed? E.g. I have a solution which only works with C99. \$\endgroup\$ Nov 21, 2016 at 22:05
1
\$\begingroup\$

Pluralize Words

(DISCLAIMER: In this challenge, several exceptions and rules are not accounted for. Many plurals are based on sound and not spelling, and still others have no set guidelines. Please do not use this as an actual set of rules for making plurals.)

A lot of questions here require using different Algebras. ... Algebra? Algebrae?

OK, that's it. We're going to get to the bottom of this.

Your challenge is to write a program that accepts a word as input and outputs the plural (in standard English rules).

Rules on making plurals

  • Words ending in s, x, z, ch, sh, o, ex, or ix have es added to them. (For example, box -> boxes)
  • Words ending in y preceded by a consonant have the y removed and ies added to the end. For example, cherry -> cherries. (Words like day with a vowel before the y are treated as normal.)
  • Words ending in f or fe are treated as normal.
  • Words ending in us have the ending changed to i. (For example, alumnus -> alumni)
  • Words ending in is have the ending changed to es. (For example, oasis -> oases)
  • Words ending in um are changed to a. (For example, medium -> media)
  • Words ending in a are changed to ae. (For example, antenna -> antennae)
  • All other words have s added to the end.

Several rules have been omitted for simplicity:

  • You do not have to account for irregular plurals like sheep. If sheep was inputted, you would return sheeps.
  • The o/f/fe/ex/ix rules have lots of exceptions. knives, matrices, etc. I can't find any pattern, however, so these are simplified to one rule.
  • chateau -> chateaux, man -> men, child -> children, foot -> feet, and mouse -> mice have been left out due to ambiguity. You can replace these with chateaus, mans, childs, foots, and mouses, respectively.
  • Words are counted based on spelling, not sound. TimmyD mentioned in the Sandbox:

    For example, Czech (as in, a person from the Czech Republic) goes to Czechs ("checks") since the ch is pronounced like ck.

    Here you would output Czeches (as it is spelled with a ch).

Input

A single word, like challenge. The input will only contain letters a-z.

Output

The pluralized word, based on the rules above, like challenges. You may have a trailing newline.

Output may be case-insensitive.

Test Cases

algebra
algebrae

shoe
shoes

fax
faxes

datum
data

bunny
bunnies

qlwlsh
qlwlshes

Meta Questions

UPDATE: I've answered these questions myself. Do you guys think it's ready for posting?

  • Is this a duplicate? (No, other similar challenges have different scoring)
  • Are there any special cases I left out? (Probably but they'd over-complicate things)
  • When researching this I discovered there are a LOT of little rules. I've omitted some here, but is this too complex to be a feasible challenge? (No)
    • In addition, have I left out too many rules? (Adding any more would make it too complex)
\$\endgroup\$
7
  • \$\begingroup\$ The problem is that English handles its pluralization (and a/an distinction) based on the vocal pronunciation, and not by the letters themselves. For example, Czech (as in, a person from the Czech Republic) goes to Czechs ("checks") since the ch is pronounced like ck. I think the simplifications you have here are OK, so long as you're very explicit that there are a lot of exceptions that aren't accounted for. \$\endgroup\$ Aug 5, 2016 at 15:13
  • \$\begingroup\$ Also, relevant chat discussion \$\endgroup\$ Aug 5, 2016 at 15:21
  • \$\begingroup\$ @TimmyD Interesting. I'll double-check the edits I made to make sure those strange cases don't slip through. \$\endgroup\$ Aug 5, 2016 at 15:26
  • \$\begingroup\$ I think the f rule could be left in, since it's pretty consistent that f and fe become ves. I can only think of a few exceptions to that rule. A better title might be "Pluralize Words" (since pluralize is actually a word). I think if you just need to follow the rules and not worry about exceptions, it wouldn't be too complex. \$\endgroup\$ Aug 5, 2016 at 15:30
  • \$\begingroup\$ Also possible duplicate of this and this. However, I prefer this one to the others since this explicitly states the rules your program should follow. \$\endgroup\$ Aug 5, 2016 at 15:31
  • \$\begingroup\$ @BusinessCat Those are different, scoring is based on the number of wrong words there. Here, this is a code-golf for shortest code that properly prints plurals (with some exceptions.) \$\endgroup\$ Aug 5, 2016 at 15:47
  • \$\begingroup\$ My answer to "Is this too complex to be a feasible challenge?" is "Yes". Natural language processing in general (and especially where the language is English) is a better fit for challenges which score by performance against a large suite of manually curated test cases than pure code-golf. \$\endgroup\$ Nov 23, 2016 at 15:54
1
54 55
56
57 58
149

You must log in to answer this question.

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