Write an interpreter for XKCD's broken language
- Is this a good idea?
- Is the point system good?
- If so, are the point values good? I based them off of how difficult I imaged each part to be.
Randall Munroe, author of the webcomic XKCD, posted a comic a while back about a joke programming language and how it gets types confused.
Here's the comic in question
Your job is to write an interpreter for a slightly modified version of this language.
- If your language supports it, it must be a live interpreter where all input lines start with
[#]> (where # is the line number) and all outputs must start with
- If your language does not support it, you may substitute this with reading all the lines on the input and printing out each output on its own line, still starting with
=>. Remember to keep track of line numbers.
- All commands must be usable in any order and with any parameters (within reason)
- Each command you implement correctly is worth the listed point value. See scoring to find your final score.
Integers plus numeric strings (1 point)
Given an int and a string containing a number (in that order) calculate the numeric sum of them, then return the result as a string with quotes surrounding it.
> 2 + "2"
> 6 + "13"
Numeric strings plus lists (1 point)
Given a numeric string and a list, append the numeric value to the list and return the list as a string.
> "3" + 
> "7" + [1,4]
Dividing by zero (1 point)
Any number divided by zero returns a NaN object. This does not have quotes around it.
NaN plus integer (2 points)
Given a NaN object and an integer, pretend it's a string and increment the leading character, carrying over if needed. The result does not have quotes.
You do not need to use ASCII for this, any character set works, as long as it includes at minimum the 52 uppercase and lowercase letters.
You will never have to add a result from this to another integer. (For example,
NaP+4 is invalid)
String plus string (1 point)
Given two strings, remove swap the first and last quotes with single quotes and return the result.
List of integers plus integer (3 points)
Given a list of integers (or an empty list) and another integer, return True or False based on whether the new integer matches the linear pattern.
It will follow this rule:
If the list has 0 or 1 items, return True regardless (everything matches).
Determine the pattern of the list by subtracting every item by the item right before it. If the pattern is inconsistent, return False regardless (nothing matches).
Otherwise, check if the new integer matches the pattern. If it does, return True. If it doesn't, return False.
> [1, 2, 3, 4] + 5
> [5, 7, 9, 11] + 13
> [6, 5, 4, 3] + 2
> [2, 4, 6, 8] + 9
> [1, 2, 1, 4] + 5
Range(a, b) (1 point)
With the Range function given two numbers, return a list of numbers from a to b.
> Range(1, 5)
=> [1, 2, 3, 4, 5]
> Range(3, 6)
=> [3, 4, 5, 6]
> Range(6, 4)
=> [6, 5, 4]
Plus integer (1 point)
Given simply "+" and an integer, return the line number plus the integer.
Single digit plus single digit (2 points)
Given two single digit integers
b, replace all future instances of
a in the output with
(a+b)%10 (where % means modulo, essentially wrap around if the result is more than 1 digit). Return Done. This effect is superficial and has no effect on math. This only affects output, not input.
> Range(1, 6)
=> [1, 2, 5, 4, 5, 6]
> Range(1, 6)
=> [3, 2, 5, 4, 5, 6]
Floor (1 point)
Draw an ascii art floor with the number at the bottom. The specifications are 3 lines of only pipes, then a line with a pipe, 3 underscores, the number, and 3 underscores.
Add up the points you've earned. Your score is now calculated as
(15-points) * bytecount. Lower is better.
- You do not have to account for mixing functions. For example,
Range(Floor(2.3)) is invalid.
- Whitespace may be added or removed as is convenient.
- Double quotes or single quotes may be used as convenient (as long as you use the opposite for "String plus string")
- Any brackets may be used for lists, as long as it's consistent.
- A few commands were omitted from the original comic because I couldn't figure out a way to make it well specified.