58
7
You may remember in first or second grade using expanded form to learn about place value of numbers. It's easier to explain with an example, so consider the number 123
. In expanded form it is represented as 100 + 20 + 3
, which helps a young mind visualize place value. It is reminiscent of how you say it: one hundred (plus) twenty (plus) three.
We can extend this past the units place with decimals: 2.718 => 2 + 0.7 + 0.01 + 0.008
Your challenge is to write a program or function that takes a positive floating point number or zero (assume it is as large or precise as your language can handle; it will not be in scientific notation) or string and prints/returns it in expanded form as explained above.
You need neither spaces between the +
's nor the zero before the decimal point, so the example above could be 2+.7+.01+.008
. Values that would be equal to zero must be omitted (101.01 => 100 + 1 + 0.01
) unless the input is zero (see below).
Values should not have more than one leading zero before the decimal point or any trailing zeroes after it (no-no's: 0060, 0000.2, 30., 30.000, .0400
). The input will conform to this too.
Since first-graders have short attention spans, your code will have to be as short as possible.
Test cases
0 => 0
6 => 6
0.99 => 0.9 + 0.09
24601 => 20000 + 4000 + 600 + 1
6.283 => 6 + 0.2 + 0.08 + 0.003
9000000.0000009 => 9000000 + 0.0000009
22+1 for "Since first-graders have short attention spans, your code will have to be as short as possible." – Downgoat – 2016-01-09T01:42:00.363
Can the input be string? – Akangka – 2016-01-09T02:09:45.280
@ChristianIrwan Yes, it says so above, but I guess it's confusingly worded – NinjaBearMonkey – 2016-01-09T02:13:06.247
2
@Doᴡɴɢᴏᴀᴛ glad to see the meme is still running.
– cat – 2016-01-09T04:52:19.9134It would have be funny to do it in the same way we (french) count, to see people strugle with the case of 97(4*20+10+7) ^^ – Katenkyo – 2016-01-18T12:14:19.090
CJam outputs
0.000009
as0.000009
but0.0000009
as9e-7
, and arguably it's too precise for CJam. Is that allowed? – jimmy23013 – 2016-01-18T13:34:41.2932@jimmy23013 Yes, as long as it works in theory. – NinjaBearMonkey – 2016-01-18T14:07:13.910
Could the input have trailing zeros? E.g.
15.20
or1.0
. – randomra – 2016-01-18T22:15:22.557@randomra No, the input will conform to the same restrictions as the output. – NinjaBearMonkey – 2016-01-18T22:18:24.927
@Katenkyo That would be horrible/hilarious! Perhaps it could be the seed of another puzzle? – Ogaday – 2016-01-21T10:08:08.737
@NBZ How does that give 197? – Thrax – 2016-01-21T13:07:29.497
1@Ogaday I don't know, it's only some edge cases. Maybe NBZ way would be better, but still, don't if it would really be interesting – Katenkyo – 2016-01-22T13:12:29.790
@Thrax Edit typo: Much worse in Danish: 197 = 100 + 7 + ((5 + 4) / 2) × 20.
– Adám – 2016-01-22T16:07:40.970