Sum of multiples of powers of ten of an integer

2

Write a function to produce, for a non-negative integer, a string containing a sum of terms according to the following:

3 -> "3"

805 -> "800 + 5"

700390 -> "700000 + 300 + 90"

0 -> "0"

13 -> "10 + 3"

200 -> "200"

Any input other than a non-negative integer can be ignored. Zero terms must be skipped unless the input is 0. Terms must go from largest to smallest. The plus signs must have only a single space on either side.

This is code golf; fewest number of bytes wins.

Ray Toal

Posted 2019-07-07T02:02:14.010

Reputation: 589

Question was closed 2019-07-07T02:52:34.440

The edge case of an input of zero seems awkward and yet easily avoidable with "for a positive integer...". – Jonathan Allan – 2019-07-07T02:11:53.313

Thank you so much for detecting the inconsistency. Regarding the +, I'd like to see how people can golf the join, so it was intentional. I say "nonnegative" instead of "positive" on purpose as well. Nonnegative includes 0 which will indeed make the code longer and IMHO more of a challenge to golf. It's true that a list of positive integers (filtered to remove zeros) is way easier, but I did add the two complications as a challenge. I don't use this site too often so if these kinds of complications are frowned upon please let me know for sure. Thanks. – Ray Toal – 2019-07-07T02:16:16.307

1...not really frowned upon, just seems like the core of the challenge on its own would make for a better question. Are the single spaces required or optional? – Jonathan Allan – 2019-07-07T02:21:15.137

Required. Though I think I have an idea of how to ask next time. :) Cheers :) – Ray Toal – 2019-07-07T02:26:58.657

Flexibile output format is generally preferred – Luis Mendo – 2019-07-07T02:30:08.753

1Thanks good to know. For next time. – Ray Toal – 2019-07-07T02:39:35.017

Subset of this question. Enough so, that I would consider this a dupe

– Jo King – 2019-07-07T02:47:34.600

Didn't see that. Should I delete? Okay if anyone wants to close it. – Ray Toal – 2019-07-07T02:49:04.843

Answers

2

Jelly, 14 bytes

DḊƬḌINḟ0ȯ0j”+K

A full program which prints the result

Try it online!

How?

DḊƬḌINḟ0ȯ0j”+K - Main Link: integer, n       e.g. 805
D              - to base ten                      [8,0,5]
  Ƭ            - collect until a fixed point:
 Ḋ             -   dequeue                        [[8,0,5],[0,5],[5],[]]
   Ḍ           - from base ten (vectorises)       [ 805,    5,    5,  0]
    I          - incremental differences          [    -800,   0,   -5 ]
     N         - negate                           [     800,   0,    5 ]
      ḟ0       - filter discard zeros             [     800,         5 ]
        ȯ0     - OR zero (replacing an empty list with 0)
          j”+  - join with '+' characters         [     800,  '+',   5 ]
             K - join with space characters       [800,' ','+',' ',5]
               - implicit, smashing print         "800 + 5"

Jonathan Allan

Posted 2019-07-07T02:02:14.010

Reputation: 67 804

1

APL (Dyalog Unicode), 31 bytesSBCS

Anonymous tacit prefix function, taking the number as a string.

(1↓∘∊1@1∘×⊆⌽∘⍳∘≢('+',10⊥↑)¨⊢)⍎¨

Try it online!

⍎¨ execute each character (this gives the digits as numbers)

() apply the following anonymous tacit prefix function to that:

 …()¨⊢ apply the below function between each element and the corresponding element of…

 the reversal
 of
 the ɩndices (1…N)
 of
 the tally of digits

   take that many elements, padding with zeros (as there's only ever one)

  10⊥ evaluate that in base ten

  '+', prepend a plus

 … partition it beginning a new segment where indicated by:

1@1 a one at the first element
 of
× the sign (0 for 0; 1 for all other)

1↓ drop the first
 of
 the ϵnlisted (flattened) data

Adám

Posted 2019-07-07T02:02:14.010

Reputation: 37 779

1

Perl 5 -p, 53 bytes

$&&&push@a,$&.y//0/cr while s/.//;$"=" + ";$_="@a"||0

Try it online!

Xcali

Posted 2019-07-07T02:02:14.010

Reputation: 7 671