r/dailyprogrammer • u/den510 • Sep 06 '17
[2017-09-06] Challenge #330 [Intermediate] Check Writer
Description:
Given a dollar amount between 0.00 and 999,999.00, create a program that will provide a worded representation of a dollar amount on a check.
Input:
You will be given one line, the dollar amount as a float or integer. It can be as follows:
400120.0
400120.00
400120
Output:
This will be what you would write on a check for the dollar amount.
Four hundred thousand, one hundred twenty dollars and zero cents.
edit: There is no and between hundred and twenty, thank you /u/AllanBz
Challenge Inputs:
333.88
742388.15
919616.12
12.11
2.0
Challenge Outputs:
Three hundred thirty three dollars and eighty eight cents.
Seven hundred forty two thousand, three hundred eighty eight dollars and fifteen cents.
Nine hundred nineteen thousand, six hundred sixteen dollars and twelve cents.
Twelve dollars and eleven cents.
Two dollars and zero cents.
Bonus:
While I had a difficult time finding an official listing of the world's total wealth, many sources estimate it to be in the trillions of dollars. Extend this program to handle sums up to 999,999,999,999,999.99
Challenge Credit:
In part due to Dave Jones at Spokane Community College, one of the coolest programming instructors I ever had.
Notes:
This is my first submission to /r/dailyprogrammer, feedback is welcome.
edit: formatting
10
u/sirnamlik Sep 06 '17
In Java
Without the challenge.
7
u/Specter_Terrasbane Sep 06 '17
Python 2 with Bonus
Using the num2words library from PyPI
Testing
Output
3
u/AllanBz Sep 06 '17
I think it's supposed to be "one hundred twenty dollars", not "one hundred and twenty dollars". At least one of my math teachers drilled that into my head some twenty-five, thirty years ago.
5
u/lukz 2 0 Sep 06 '17
Z80 assembly
Eight-bit computers based on Z80 processor usually used CP/M as their operating system. This system fixed the address where the program was loaded in memory (0100h) and also addresses that the program could jump to and where CP/M subroutine implementations performing some specified function were. This provided some compatibility between different computers, so that program written for CP/M could run on any computer with a CP/M implementation.
This program uses only CP/M function with index number 9--write string. The string has to be terminated by $ character.
The program only supports numbers in range 0-9999 (due to complexity, assembly programs get very long easily) and ignores the cents.
Program size is 400 bytes. The program was saved as number.com and tested in a Z80 emulator.
Sample session:
Source:
2
u/Baelyk Sep 12 '17
Rust without Bonus
Also, shouldn't 333.88 be Three hundred thirty three dollars and eighty eight cents?
2
u/quantik64 Sep 14 '17 edited Sep 14 '17
Was quite proud of this one took me most of the morning (with challenge)
PYTHON WITH CHALLENGE
5
u/olzd Sep 06 '17
Common Lisp: kinda cheating, with bonus
Example:
1
u/nahuak Sep 06 '17 edited Sep 07 '17
Python 3 with bonus. Feedback will be highly appreciated.
Inputs:
Output:
1
u/[deleted] Sep 06 '17 edited Sep 06 '17
Ruby With (buggy) bonus
Monkey patching, anyone? :D Works with inputs up to 99 trillion... things start to get weird after that, and I'm not completely sure why.
Edit: Fixed some things. Hypothetically works up to 999 quadrillion, however starting at about 90 trillion or so the output for 'cents' starts to randomly and with increasing frequency become inaccurate... sometimes by one or two ('twenty three' instead of 'twenty four'), and sometimes by 4 or 5+, presumably because of the size of the numbers and .round? Not entirely sure. But I had a lot of fun working on this code.
challenge inputs/output (copy/pasted from irb):
1
u/chunes 1 2 Sep 06 '17
Factor (with bonus)
1
u/geigenmusikant Sep 06 '17
In Go
For anyone learning German. Code can be tested here: https://play.golang.org/p/i_yKH-IUs6
1
u/mn-haskell-guy 1 0 Sep 07 '17
Reminds of the "ITA Word Number Problem":
Presumably it was used as an interview question by ITA Software (before they were bought by Google.)
3
u/SpikeX Sep 06 '17 edited Sep 06 '17
PowerShell
First post on /r/dailyprogrammer!
With the bonus, however the .NET type
decimalcontains 17 digits of precision but only uses 15 of them (?) and adoubleonly contains 15-16 digits of precision, making the challenge more difficult (you'd have to resort to splitting it up into a string and I didn't go that route). This program attempts to convert it anyway, and will sometimes get it wrong when you get into the tens- and hundred-trillions.Also, this code is ridiculously messy (multiple uses of Regex to clean up spacing), and I really wish PowerShell had a ternary (
?:) operator, but it works!Tests:
3
u/gabyjunior 1 2 Sep 06 '17 edited Sep 06 '17
C with bonus, up to the maximum value a 64 bits unsigned integer can hold for the integral part + 99 cents.
Source code
Test input
Output
2
u/[deleted] Sep 06 '17 edited Sep 06 '17
PYTHON 2.7 - Upto 1000 trillions
Challenge inputs
Outputs
2
u/ironboy_ Sep 07 '17
JavaScript solution with bonus.
(Yes I know I could've just included a npm package like number-to-words and been done in seconds, but it's more fun to write you own logic.)
Outputs:
2
u/tuttomax Sep 08 '17
first challenge here:
C++ with bonus
2
u/Herpuzderpuz Sep 08 '17
Python 3.6 without using the num2words library
2
u/ChemiCalChems Sep 08 '17
C++ with bonus
+/u/CompileBot C++ --time --date
2
u/Hypersigils Sep 10 '17
In Java.
Here's mine. Takes the input as a harcoded String, although converting it would be simple enough. Handles the bonus/challenge.
}
1
u/popillol Sep 06 '17
Go / Golang Playground Link. Not my cleanest code, but it works with the bonus. It does not have a limitation on the size of the number because it splits the initial string into sets of 1 or 2 digits, and concatenates the string. To go beyond trillions, I'd only need to add a couple fields to the
suffixmap. Also added a couple inputs to test more edge cases.Code:
Output:
1
u/[deleted] Sep 06 '17
Python 3, with challenge:
Output:
1
u/cheers- Sep 06 '17 edited Sep 06 '17
Javascript (Node)
Edit: refactored it a bit
I've overdone it a bit :).
10^18 -1:999 Quadriliion, 999 trillion etc...0.99=>ninety-nine cents.0, 0.0 and 0.00returnszero dollarsCmd :
node verboseDollars 0.12 1 1001 1023.2 200100 123456789012345678Output:
Source
mappings.js it contains dictionary objects: https://pastebin.com/4NxnFMjU
verboseDollar.js
1
u/curtmack Sep 06 '17 edited Sep 06 '17
Common Lisp
Implements the bonus, and also handles some edge cases like "one thousand one" instead of "one thousand, one."
1
u/[deleted] Sep 06 '17
Haskell:
1
u/fingertoe11 Sep 06 '17
In Clojure
Tests:
There are better ways to test, but that works.
Didn't do the bonus, but maybe later.
1
u/_tpr_ Sep 07 '17
In Haskell. Still learning, so feedback is definitely welcome.
1
u/Scroph 0 0 Sep 07 '17
Lengthy D solution with bonus :
Input :
Output :
1
u/[deleted] Sep 07 '17
C++ with bonus up to decillions...
A bit long, but it's from the ground up not using any libraries and with exception handling in case of invalid input. Also works for numbers in the decillions (around 36 digits). Not very beautiful I admit, but it works well.
1
u/Vectorious Sep 08 '17 edited Sep 18 '25
reminiscent unique spoon grandiose imagine coherent flowery ink spotted melodic
This post was mass deleted and anonymized with Redact
1
u/octolanceae Sep 08 '17
Python3
Output: (with Bonus)
1
u/Escherize Sep 08 '17 edited Sep 08 '17
5 line solution, in Clojure
Hey thanks - I got a chance to check out cl-format (ripped off from common lisp)!
1
u/a_ctor Sep 08 '17
In C#
With bonus
Running the test numbers:
prints:
1
u/JakDrako Sep 08 '17
VB.Net with bonus.
Using the Humanizer library from Nuget. I decided to try and use the Pythony approach: "There's got to be a library for that..." and turns out, there is.
The only gotcha is the "and" in "one hundred and twenty", but looking at various discussions about that, it seems to vary by region... Worthwhile trade-off IMHO considering the small amount of code required.
Output:
1
u/jonsbrown Sep 08 '17
C#
Usage: result = NumberWords.Translate(n);
Max value (ulong): NumberToWords.Translate(18446744073709551615)
Challenge Output:
333.88: Three Hundred Thirty Three dollars and Eighty Eight cents
742388.15: Seven Hundred Forty Two Thousand Three Hundred Eighty Eight dollars and Fifteen cents
919616.12: Nine Hundred Nineteen Thousand Six Hundred Sixteen dollars and Twelve cents
12.11: Twelve dollars and Eleven cents
2.0: Two dollars and Zero cents
1
u/[deleted] Sep 09 '17
Python 3.6 with bonus: Upper limit seems to be determined by the number of power names in the english language as far as I can tell. I would like to hear any feedback you guys might have.
Sample input:
Sample output:
1
u/zookeeper_zeke Sep 15 '17 edited Sep 15 '17
Really nice problem, thanks OP and Dave Jones. Simple to explain but a few wrinkles to deal with when coding. I chose to code my solution in C and I made it a point to not buffer any output.
1
u/pie__flavor Sep 16 '17 edited Sep 16 '17
Rust
1
u/nequals30 Sep 16 '17
Python 3: This is basically the first thing I've written in Python, I would like some feedback.
1
u/atreideks Sep 24 '17
C++ solution, without bonus:
1
u/[deleted] Oct 07 '17 edited Oct 08 '17
Java - With Bonus
1
u/itachi_2017 Oct 08 '17 edited Oct 08 '17
C++ solution (Solves bonus as well) Kindly, let me know if any improvements can be made to it :)
Uses Recursion to break down the integral part into 3 digit parts and each part has <=3 digits. Their solutions can be produced from solutions to 2 digit numbers which are our base cases.
1
u/lghitman Nov 01 '17
In Kotlin
With the challenge, or some of it anyway, because I'm lazy, but you get the idea:
1
u/Raider_Scum Dec 14 '17
In Java With the challenge, but accepts amounts in Strings. I could not find a way to use doubles with accuracy. Even with BigDecimal or DecimalFormat, 999999999999999.99 would round up to 1000000000000000. Please let me know if there is a way around this because i searched for a good while.
Challenge:
1
u/zatoichi49 Feb 21 '18 edited Apr 17 '18
Method:
Create a dictionary of all the unique words that can be used to write the results. Reformat the input string and split into groups at each instance of ',' or '.'). Taking each group in turn, convert to written text and add to the result, making sure to account for any postfix text for the group (e.g. 'hundred and', 'zero cents' etc.).
Python 3: with Bonus
Output:
0
u/[deleted] Sep 06 '17
[deleted] — view deleted comment
0
u/[deleted] Sep 06 '17
[deleted] — view deleted comment