560
\$\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".

Get the Sandbox Viewer to view the sandbox more easily!

\$\endgroup\$
0

4201 Answers 4201

1
11 12
13
14 15
141
4
\$\begingroup\$

Universal Unicode Clock

\$\endgroup\$
4
  • \$\begingroup\$ How does this have to do with polyglot? \$\endgroup\$
    – Steffan
    Jun 11, 2022 at 20:09
  • \$\begingroup\$ @Steffan It's a program which runs on different versions of a library. So it's a sort of library polyglot. \$\endgroup\$
    – Wheat Wizard Mod
    Jun 11, 2022 at 20:10
  • \$\begingroup\$ If the Unicode handling library has an interface to determine which version of the Unicode database it uses, are we allowed to use that? \$\endgroup\$
    – pxeger
    Jun 12, 2022 at 15:21
  • \$\begingroup\$ @pxeger I'm not sure if there is really any point in disallowing it since it seems unlikely that it would get you many votes. \$\endgroup\$
    – Wheat Wizard Mod
    Jun 12, 2022 at 17:20
4
\$\begingroup\$

Extend a matrix in all directions

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Why is the second input another array, and not just the dimensions of the output array? \$\endgroup\$
    – pxeger
    Jun 14, 2022 at 16:00
  • \$\begingroup\$ @pxeger Good point \$\endgroup\$
    – emanresu A
    Jun 14, 2022 at 19:20
4
\$\begingroup\$

ro1000an nu1000era50 en100o501ng

\$\endgroup\$
1
  • 2
    \$\begingroup\$ I suggest adding a worked out example - the test cases were still confusing for me after reading the challenge body. \$\endgroup\$
    – pajonk
    Jun 25, 2022 at 13:48
4
\$\begingroup\$

Not-Roman-Numeral Addition

\$\endgroup\$
4
  • \$\begingroup\$ (from the test cases) How is 110015, ICIV, 94? IC is 99 + IV which is 4 should be 103. I'd suggest removing that test case though, because ICIV is not technically a valid roman numeral. \$\endgroup\$
    – Steffan
    Jun 26, 2022 at 3:06
  • \$\begingroup\$ The second-to-last test case is also wrong apparently: the second number is MMCCCXL which is 2340, not 2345, so the result is 3574 \$\endgroup\$
    – Steffan
    Jun 26, 2022 at 3:17
  • \$\begingroup\$ @Steffan fixed and fixed \$\endgroup\$
    – bigyihsuan
    Jun 26, 2022 at 6:53
  • \$\begingroup\$ this is basically roman numeral addition right? with translating before and after to numeric symbols \$\endgroup\$
    – qwr
    Jun 26, 2022 at 21:12
4
\$\begingroup\$

Word stays a word after taking away a letter

Posted here

\$\endgroup\$
2
  • \$\begingroup\$ Is it guaranteed that the input will contain a solution? \$\endgroup\$
    – pajonk
    Jul 1, 2022 at 12:36
  • \$\begingroup\$ @pajonk I think it's better to say no, thanks \$\endgroup\$
    – mathcat
    Jul 1, 2022 at 18:10
4
\$\begingroup\$

All Possible Ties in Tic-Tac-Toe

\$\endgroup\$
4
\$\begingroup\$

Next digit of rational number

\$\endgroup\$
4
  • \$\begingroup\$ A good test case might be 1.221122, which I believe should result in an output of 2 \$\endgroup\$
    – isaacg
    Jul 15, 2022 at 5:52
  • \$\begingroup\$ Good idea, added it \$\endgroup\$
    – Jiří
    Jul 15, 2022 at 10:08
  • \$\begingroup\$ Is the decimal separator important to the challenge? I feel like it might be better to just have no decimal separator \$\endgroup\$ Jul 16, 2022 at 4:19
  • \$\begingroup\$ @CommandMaster It isn't really that important, but I wanted the challenge make sense so this is why I kept it there. I expect that for most languages it will just be single replace, tr or something. \$\endgroup\$
    – Jiří
    Jul 16, 2022 at 14:36
4
\$\begingroup\$

Count the cells

\$\endgroup\$
4
\$\begingroup\$

Rearrange to a palindrome

Given a string, shuffle it so that it becomes a palindrome.

For example, adadbcc can be arranged into dacbcad, or dcabacd, acdbdca and more. Any of these (or all) is acceptable, and duplicates are allowed if outputting all. Something like abc cannot be shuffled into a palindrome, and you can assume it won't be inputted.

(if it helps) input will only contain lowercase letters.

Testcases

These show one possible solution.

nanas -> nasan
coconutnut -> conuttunoc
apotato -> atopota
manplancanalpanamaaaa -> amanaplanacanalpanama
canadadance -> canadedanac
\$\endgroup\$
0
4
\$\begingroup\$

Halve a string

Posted

\$\endgroup\$
2
  • \$\begingroup\$ If this was a decision problem, it would be easier to give test cases (because it would true/false instead of many different possible outputs) \$\endgroup\$
    – Fatalize
    Jul 19, 2022 at 12:46
  • \$\begingroup\$ I guess another possible task is to output the list of all halves of a given string. \$\endgroup\$
    – Fatalize
    Jul 19, 2022 at 14:20
4
\$\begingroup\$

Every \$ n \$th repeat

Given a list of positive integers, and another integer \$ n \$, output every \$ n \$th instance of each distinct item in the list, starting with the first, in the order they appear in the original list.

For example, with \$ n = 2 \$, we will output the first instance of each item, but not the second, but we will output the third, and so on.

If \$ n = 2 \$ and the list is 4 1 3 2 3 1 6 3 4 1 1, then:

  • 1 occurs four times, so only the first and third will be kept
  • 4 occurs twice, and only the first will be kept
  • 3 occurs three times; the first and third will be kept
  • 2 and 6 only occur once each, so their first and only occurrences will be kept

Therefore, the output is 4 1 3 2 6 3 1.

This is , so the shortest code in bytes wins.

Test cases

todo

Meta

  • Related (\$ n = 2 \$, and more open ended)
  • Is this a duplicate?
  • Is this clear enough?
\$\endgroup\$
4
\$\begingroup\$

Swap every two elements in the list every possible way

Posted

\$\endgroup\$
2
  • \$\begingroup\$ Welcome to the Code Golf SE! I have a few questions about your problem: 1) Should the output include the original order? Examples 2 and 3 do, but example 1 doesn't. 2) I might be mistaken, but isn't this equivalent to finding all permutations of the array? \$\endgroup\$ Jul 23, 2022 at 20:13
  • \$\begingroup\$ @Adam 1) The output should include the original order only if the original order can be reached by applying every possible pair swap in some order. For example 1, the only possible swap just swaps two elements, and so only one permutation can be reached. For example 3, there is no possible swap, so the array stays the same, and for example 2, there are a lot of different possible results that happen to include the original order. 2) You are mistaken in that case. It isn't all permutations, it's slightly more complicated than that. It's a little bit of a research problem. \$\endgroup\$
    – Pavgran
    Jul 25, 2022 at 12:33
4
\$\begingroup\$

IE's Extra Robust Color Parsing®

\$\endgroup\$
10
  • 1
    \$\begingroup\$ So #FFF (white in today's browsers) became #0F0F0F (very dark grey)‽ \$\endgroup\$
    – Adám
    Jul 25, 2022 at 15:02
  • \$\begingroup\$ When you say output as an "a RGB color", you should clarify whether you mean the text formatted as an RGB color string, or to graphically output the color itself \$\endgroup\$ Jul 25, 2022 at 15:48
  • \$\begingroup\$ @Adám So it would seem, I noticed that as well. \$\endgroup\$ Jul 26, 2022 at 20:39
  • \$\begingroup\$ @thejonymyster right, poor wording on my part. Fixed. \$\endgroup\$ Jul 26, 2022 at 20:39
  • \$\begingroup\$ Could you please add comments to less obvious steps in the worked out examples? (I don't get the AD0, AC0, 0E0 --> AD, AC, 0E, as the algorithm says to remove characters always from the front.) Also, I suggest adding some more test-cases (not necessarily worked out, but in a copy-friendly format). \$\endgroup\$
    – pajonk
    Jul 27, 2022 at 5:27
  • \$\begingroup\$ @pajonk I added some comments, planning to add some test cases later whan I find the time. \$\endgroup\$ Jul 27, 2022 at 9:57
  • \$\begingroup\$ Will IE recognize some special words like red, white? Or red is processed into #000e0d? \$\endgroup\$
    – tsh
    Jul 29, 2022 at 3:17
  • \$\begingroup\$ @tsh Good point, thanks! IE probably did respect them, but for the challenge, we'll process them. Updated the rules. \$\endgroup\$ Jul 29, 2022 at 7:05
  • \$\begingroup\$ I assume we just have to output the six resulting characters, and the leading # in the output is optional? \$\endgroup\$ Jul 29, 2022 at 7:44
  • \$\begingroup\$ Yes, I just used that to mark the results. \$\endgroup\$ Jul 29, 2022 at 9:29
4
\$\begingroup\$

Cryptic Multiplications

\$\endgroup\$
4
\$\begingroup\$

Anti-divisors of a number

\$\endgroup\$
4
\$\begingroup\$

An algorithm to find even sublime numbers

\$\endgroup\$
4
\$\begingroup\$

Count Futoshiki row solutions

\$\endgroup\$
4
\$\begingroup\$

Is it an alphadrome?

\$\endgroup\$
4
\$\begingroup\$

MD5 Hello, World!

Posted here

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Are there interesting ways of getting two files to have the same MD5 hash? I know there are some ways to add arbitrary text to files to give them the same hash, but it seems difficult to engineer the program itself to have the same hashes. \$\endgroup\$ Oct 13, 2022 at 14:16
  • \$\begingroup\$ @97.100.97.109, I’m not sure. It may be interesting to see if someone can come up with a solution that is a bit smarter than including a lot of unused data. \$\endgroup\$
    – jdt
    Oct 13, 2022 at 14:36
4
\$\begingroup\$

Count the shared substrings with 2 programs

\$\endgroup\$
1
  • \$\begingroup\$ incident, score 0, fermat quote \$\endgroup\$ Oct 19, 2022 at 12:34
4
\$\begingroup\$

Character Insertion on Letterboards

\$\endgroup\$
3
  • \$\begingroup\$ Can we assume it's a permutation of the string? \$\endgroup\$
    – emanresu A
    Oct 21, 2022 at 1:03
  • \$\begingroup\$ I think the example has a mistake, the second "B" and "A" are inserted from the right not the left. \$\endgroup\$ Oct 21, 2022 at 22:57
  • \$\begingroup\$ @emanresuA I'm not sure. What do you think? It doesn't make a difference to the two algorithms I've thought of. \$\endgroup\$
    – FlipTack
    Oct 22, 2022 at 14:13
4
\$\begingroup\$

"Prime" pyramid

\$\endgroup\$
4
\$\begingroup\$

Is it true? Ask Pip!

\$\endgroup\$
1
  • \$\begingroup\$ I think I'd stick to the decision-problem defaults and encourage to post non-trivial Pip solution; or post a community-wiki answer with that trivial solution to claim it. \$\endgroup\$
    – pajonk
    Dec 20, 2022 at 9:21
4
\$\begingroup\$

Transpose binary numbers

The input numbers [34, 7, 109] can be represented in binary as

0100010
0000111
1101101

(one number on each line; and padding with zeroes on the left so that all the rows are the same length)

Now transpose this array:

001
101
000
001
011
110
011

Then convert each row back to decimal to get the output:

[1, 5, 0, 1, 3, 6, 3]

This is the output.


todo: write a proper spec

Is this a duplicate?

Is this interesting?

Would this be more interesting if generalised to an input base b?

\$\endgroup\$
4
\$\begingroup\$

Is it a Shift matrix?

Posted here

\$\endgroup\$
2
  • \$\begingroup\$ Are other forms of input ok? Like a rectangular matrix if my language supports it? \$\endgroup\$
    – pajonk
    Jan 6 at 14:10
  • \$\begingroup\$ @pajonk oops yes thanks \$\endgroup\$
    – mathcat
    Jan 17 at 18:43
4
\$\begingroup\$

Salacious Bacon Tripod

\$\endgroup\$
5
  • \$\begingroup\$ So Bad Task (not saying it bad but this fits SBT) \$\endgroup\$
    – l4m2
    Jan 24 at 11:19
  • \$\begingroup\$ Do I understand correctly, that acronyms at the beginning and end of the text won't be replaced (as aren't surrounded by spaces)? \$\endgroup\$
    – pajonk
    Jan 24 at 13:23
  • \$\begingroup\$ @pajonk that was an unintended consequence of trying to make it so that words are "letters surrounded by a gap". I've fixed that now. \$\endgroup\$
    – lyxal
    Jan 24 at 13:27
  • 1
    \$\begingroup\$ Another thing, will the acronym consist only of distinct letters? \$\endgroup\$
    – pajonk
    Jan 24 at 13:42
  • \$\begingroup\$ @pajonk I've added that clarification to the challenge \$\endgroup\$
    – lyxal
    Jan 26 at 0:34
3
\$\begingroup\$

Metagolf: Catlike Piet

The goal of this is to write a catlike program, which would be executed (in a Unix environment, though you needn't stick to that) by the following:

yourprogram < file > output
piet output

where piet output writes the contents of file to stdout. That is, you're to generate a Piet program which prints the input to yourprogram.

One-liners

Straight line programs can be written in Piet... in straight lines. If you're willing to take a hit to your score, your output can take the form of a string of commands:

=  none (continue color block)
|  push
^  pop
+  add
-  subtract
*  multiply
/  divide
%  mod
~  not
>  greater
.  pointer
\  switch
:  duplicate
@  roll
$  input number
?  input character
#  output number
!  output character

which is trivial to convert to a Piet program with the following (partially golfed) Python code:

def P(s):
 h=v=0;l=len(s)+1;R="P3 %i 2 255 192 0 0 "%(l+2)
 C=[1,3,2,6,4,5];V=[0,192,192,255,0,255]
 for x in map("=|^+-*/%~>.,:@$?#!".find,s):
  C=C[x//3:]+C[:x//3];V=V[x%3*2:]+V[:x%3*2]
  for i in [1,2,4]:R+="%i "%V[(C[0]//i)%2]
 return R+"255 "*4+"0 0 "+"255 "*l*3+"255 0 0 "*2

The dimension of said program is (n+3) x 2 if there are n characters in the string.

Scoring

Your code will be judged on the maximum dimension of the images that it outputs.

  • Part 1: Take the maximum score taken over all ascii codes (that is, single-character inputs), discounting EOF.

  • Part 2: Take the score for the input "Hello. My name is Inigo Montoya. You killed my father. Prepare to die."

Your score is the product of the scores in part 1 and part 2.

Punishment: Double your score if you write one-liners as above (that is, if you don't output an image).

Bonus: If your program is written in Piet, take the square root of your score above.

\$\endgroup\$
4
  • 3
    \$\begingroup\$ It took me a while to understand the task as "Write a program taking INPUT which produces as output a piet program that takes no input but produces INPUT." I think it is a interesting and challenging, but it's reception will depend entirely on how many people are willing to learn/futz-around-in/deal-with piet. And I have no feel for how many that is. \$\endgroup\$ Jul 7, 2011 at 3:12
  • \$\begingroup\$ @dmckee; would it be better if I just used a reduced instruction set, and only ask for the instruction stream? I think this is still challenging with {push 1,duplicate,add,subtract,multiply,output}. Come to think of it, if I restrict to {push 1,duplicate,add,output}, there's a reduction to some awesome algorithms. \$\endgroup\$
    – boothby
    Jul 7, 2011 at 4:48
  • \$\begingroup\$ I did this in piet some time ago: craigoclock.blogspot.com/2011/05/metaprogramming-in-piet.html \$\endgroup\$
    – captncraig
    May 21, 2012 at 18:31
  • \$\begingroup\$ Hello! This looks like a good but abandoned meta post, would you be willing to offer it for adoption? (If you want to, you can still post to main.) \$\endgroup\$
    – user58826
    Jun 9, 2017 at 15:22
3
\$\begingroup\$

Count Syllables

The goal of this challenge is to write a program that can count the syllables in a word as accurately as possible.

Input

On STDIN, your program will receive a number X followed by X lines, each containing a single word. Simple enough. (Should there be a limit on the size of X?) The words will come from this list.

4
challenge
to
count
syllables

Output

Your output should be to STDOUT and have X lines. On each line should be the number of syllables counted in that word.

2
1
1
3

Scoring

To score you program, it will receive a long secret list of words to test. All programs will receive the same list of words. For each word, the number of syllables that your program got wrong will be added to the score of the program. If it output a 4 or a 2 when the word had 3 syllables, then one point will be added. If it said a 15 instead of a 3, then 12 points will be added to the score. The lower the score, the better.

For example, if for the above input your program output 3 2 2 2 (which would be produced by a program that counts strings of vowels), then the program would receive a score of 2.

Rules

Your program should not access any external files (such as the word list). Also, your program should be no more than 5,000 bytes long (is this a reasonable limit?).

The winner will be the person whose program has the lowest score, therefor the most accurate syllable counter. The deadline for submissions is [some time at least a month away].

Suggestions

I am open to all constructive criticism. Is 5,000 bytes a reasonable limit for the program size? How long should the official scoring test be? How long should the deadline be?

\$\endgroup\$
10
  • 12
    \$\begingroup\$ This has one major flaw: the output is subjective. How many syllables do these words have? Every; victory; hierarchy; desire; oil; hour; poem. The only real way I see to work around this is for you to produce a marked-up version of the word list. \$\endgroup\$ May 29, 2012 at 20:40
  • \$\begingroup\$ I was really worried about that, and I don't see a way around it. \$\endgroup\$
    – PhiNotPi
    May 29, 2012 at 20:42
  • 2
    \$\begingroup\$ I personally would love to see more language processing challenges. I agree with @PeterTaylor on the difficulty of some words. Perhaps taking a specific text(s) and identifying explicitly in the challenge which words will have how many syllables? \$\endgroup\$
    – Gaffi
    Jun 8, 2012 at 3:34
  • 2
    \$\begingroup\$ @PeterTaylor ...Or maybe you could filter ambiguous words out of the reference list? \$\endgroup\$
    – user16991
    Feb 8, 2015 at 1:19
  • 1
    \$\begingroup\$ What's the point of the first line of input? \$\endgroup\$
    – msh210
    Apr 27, 2016 at 20:05
  • 3
    \$\begingroup\$ If you provide a reference list, A hyphenated reference list, and hide a secret list which may or may not include members of the reference list, this would be a reasonable challenge \$\endgroup\$ Sep 17, 2016 at 0:05
  • \$\begingroup\$ Do you plan to post this? If not, I'd be happy to adopt it. (If you don't respond within two weeks, by community standards, I'm allowed to do so.) \$\endgroup\$
    – MD XF
    Aug 18, 2017 at 3:20
  • \$\begingroup\$ The example of inaccurate program that would score 2 - did you mean to output 3 1 1 2 rather than 3 2 2 2? \$\endgroup\$
    – Heimdall
    Nov 9, 2017 at 18:31
  • \$\begingroup\$ A reference list could be dynamic: potential contestants can ask for words of their choice to be added to the list. They won't know what's on the secret list but will try to make their programs as accurate as possible (according to your syllable count) so they should always be able to ask for specific words they are not sure about. Of course, you could make it in different language. In my language, Slovene, it's much clearer how many syllables words have. How about Solresol, haha! \$\endgroup\$
    – Heimdall
    Nov 9, 2017 at 18:38
  • \$\begingroup\$ I am going to adopt this if you don''t respond \$\endgroup\$
    – user63187
    Dec 20, 2017 at 16:48
3
\$\begingroup\$

Play Simple 2-Dimensional Minecraft

Recently I found this video of "HansLemurson" showing a computer that was built in minecraft, which runs minecraft. He is playing minecraft on a computer that was built in minecraft that is running on his computer. To be specific, it is a two dimensional version with an 8x8 grid of cells. There is gravity, block placement, and even jumping. It is worth noting that the computer is single purpose. The same person has built programmable computers, but making them single purpose allows the computer to be much smaller.

Details

The minecraft world is an 8x8 grid (one horizontal and one vertical dimension). The grid is comprised of either Xs (representing blocks) or empty spaces. The player is an X that is blinking on and off about once every second.

There are two modes in the game, controlled by a toggle switch. The first mode is movement. This is controlled by a WASD-like button arrangement. If the player chooses to move left/right/down, the computer checks to see if the space immediately in that direction is empty. If so, then the player moves into that space.

If the player chooses to move up, then the computer checks that the block underneath the player is solid. If so, then the player moves upward two units. Notice that this can propel the player into a solid block. If this happens, the player is obscured by the solid block, but can still move to an empty block next to him. When the player is inside on a solid block, the game continues as if the block isn't there, although the block is still there once the player leaves it.

After each move, the player falls down one unit if there is empty space there. This simulates gravity. This is also why moving up moves up two units, so that the gravity makes a net movement of up one unit. Gravity does not cause the player to fall all of the way to the ground, just one unit.

The second mode is block placement. In this mode, the same exact WASD buttons are used. Instead of moving the player, they toggle the state of the block in that direction. If the player presses "left" and there is a block there, then the block is destroyed. If there is not a block there, then a block is placed. Again after this move, the player is again subject to gravity. The blocks are not subject to falling.

Toggling the toggle switch does not count as a move, and does not invoke gravity.

The game board is a torus, so all actions (movement, block creation) can wrap around the board. The board does not scroll with the player. The player moves, and the blocks stay in the same place.

The challenge

You challenge is to write the shortest program that simulates this game. Your program should display and update the map correctly (with Xs as blocks, and with the blinking player). It should accept input from a button that toggles the state and four buttons for movement and actions. This is code golf.

There are imaginary bonus points for adding more features (block types, game size, etc) to your game.

Suggestions?

\$\endgroup\$
4
  • 4
    \$\begingroup\$ With more complicated challenges I find that it helps to do a reference implementation so that you have a very concrete idea of how much work is involved. Aside from that, I like it. \$\endgroup\$ Jun 3, 2012 at 20:11
  • \$\begingroup\$ Is the blink rate selected to fit with the ANSI escape sequence? Either way I would explicitly allow that, because it's the obvious way to do it on compatible terminals. \$\endgroup\$ Jun 5, 2012 at 7:14
  • \$\begingroup\$ The blink rate wasn't selected to be anything specific. I think that I will broaden the restriction. Maybe any blink rate between 3 blinks per second to 1 blink every 2 seconds. \$\endgroup\$
    – PhiNotPi
    Jun 5, 2012 at 20:21
  • \$\begingroup\$ @programmer5000 No, for two main reasons: First, challenges can go extended periods of time in the sandbox before they are posted and/or adopted. In the past I've posted challenges after not touching them for 4 years. Second, deleting this answer will not reduce lag, as deleted answers are still present, simply not visible. Users with sufficient rep will see all 4040 answers in the sandbox, and you will too once you earn the "view deleted answers" privilege. \$\endgroup\$
    – PhiNotPi
    Apr 13, 2017 at 18:15
3
\$\begingroup\$

Bad Voice Recognition Calculator

Overview:

Let's say you've decided to operate your computer using voice recognition software, but unfortunately you did a horrible job researching the various products out there and chose a package that does not recognize numbers as numerals, only words. (i.e. "one" (spoken) == "one" (typed), not "1".) Rather than spend more money to get another option, you decide to make do. Now you want to use the computer's calculator, but this poses a problem, since your machine doesn't know how to add "one plus one".

Objective:

Implement a basic calculator that will read in a string of the written-out equation, perform the correct calculations, then return the result in its text form. Your code should be as short as possible; this is code golf.

Rules/Constraints:

  • Input/output will be using your preferred method (STDIN, ARGV, etc.).
  • Your calculator must be able to handle input and output within the billions (non-inclusive) -1,000,000,000 < i < 1,000,000,000, but you may expand to more if you wish.
  • Decimal values and/or parts must be accepted (0 < i < 1) up to 3 places/digits.
    • When calculating answers, proper rounding must be used, so "three point one four one five nine two six" must be returned as "three point one four two".
  • Basic calculator functions required:
    • "Add"/"Plus"/"Sum"/"And" (+)
    • "Subtract"/"Minus"/"Remove" (-)
    • "Multiply"/"Times" (*)
    • "Divide"/"Divided"/"Divide by"/"Divided by" (/)
    • "Raise"/"Exponent"/"Power"/"To the power of" (^)
    • "<Base>Root"/"<Base>Radical" (√)
    • "Point"/"Decimal" (.)
    • "Pi" (π)
  • All strings in the list above must be accounted for in your code, capitalization does not matter.
  • Numbers may be presented as their full value ("one thousand") or by digit (one zero zero zero).
  • Negative numbers may be assigned using "Minus" or "Negative".
    • The string "Minus" bust be accounted for as an operator and identifier. (see example)
  • "And" is only acceptable as an operator, not as part of a named number.
    • "one hundred and one"
    • "one hundred one"
  • "a" or the absence of a number does not equate to any number; all numbers will be explicitly accounted for in the program input.
    • "a hundred" does not equate to "one hundred" and is not a valid input.
  • No more than 2 terms will be used.
    • "one plus one minus one" will not be implemented.
  • If an invalid input is supplied, your function/program should handle the error and exit gracefully with an error description.

Example I/O:

  • "one add one" --> "two"
  • "five thousand thirty four subtract ten thousand six hundred" --> "negative five thousand five hundred sixty six"
    • Alternatively: "five zero three four subtract one zero six zero zero"
  • "three root twenty seven" --> "three"
  • "ten minus minus ten" --> "twenty"
    • Alternatively: "ten subtract negative ten"

Sandbox Questions:

  1. Is this too basic/complicated? (I'm assuming some languages will handle this much more simply than the method I have in my head...)
  2. Does the title fit?
  3. Are there any constraints that should be added/lifted?
  4. Are any more examples needed for clarification?

Thanks for your input, guys!

\$\endgroup\$
6
  • \$\begingroup\$ Not everyone says numbers the same way. Does the parser have to treat the following as equivalent? "negative one hundred five", "minus one hundred five", "negative one hundred and five", "minus one hundred and five", "negative a hundred five", "negative a hundred and five", ...? \$\endgroup\$ Jun 15, 2012 at 15:12
  • \$\begingroup\$ @PeterTaylor I had had a similar thought re: operators. ("plus" versus "add", etc.) I think it would be more interesting to account for all, but given the wide variety of possible inputs, it may generally be better to limit the options to a specifically defined set (which I have yet to define). \$\endgroup\$
    – Gaffi
    Jun 15, 2012 at 15:18
  • \$\begingroup\$ @PeterTaylor I've added some of these details. Please let me know if there's anything unclear about them. \$\endgroup\$
    – Gaffi
    Jun 15, 2012 at 16:10
  • \$\begingroup\$ I don't spot any ambiguities in the parser. There is still an ambiguity relating to decimals, though. What precision should be used? Also, I notice now that there's no winning condition. Is this intended to be code-golf? (Ugh - tons of strings which will have to be hard-coded in most languages. I expect Perl has a suitable parser already in CPAN, though...) \$\endgroup\$ Jun 19, 2012 at 9:03
  • \$\begingroup\$ @PeterTaylor I don't know where I went... I've updated the spec. re: decimal places and objective. \$\endgroup\$
    – Gaffi
    Jun 29, 2012 at 13:24
  • 1
    \$\begingroup\$ @PeterTaylor metacpan.org/pod/Lingua::EN::Words2Nums \$\endgroup\$
    – msh210
    Apr 27, 2016 at 20:37
1
11 12
13
14 15
141

You must log in to answer this question.

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