2
\$\begingroup\$

This question already has an answer here:

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.

\$\endgroup\$

marked as duplicate by Jo King code-golf Jul 7 at 2:52

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • \$\begingroup\$ The edge case of an input of zero seems awkward and yet easily avoidable with "for a positive integer...". \$\endgroup\$ – Jonathan Allan Jul 7 at 2:11
  • \$\begingroup\$ 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. \$\endgroup\$ – Ray Toal Jul 7 at 2:16
  • 1
    \$\begingroup\$ ...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? \$\endgroup\$ – Jonathan Allan Jul 7 at 2:21
  • \$\begingroup\$ Required. Though I think I have an idea of how to ask next time. :) Cheers :) \$\endgroup\$ – Ray Toal Jul 7 at 2:26
  • 1
    \$\begingroup\$ Thanks good to know. For next time. \$\endgroup\$ – Ray Toal Jul 7 at 2:39
2
\$\begingroup\$

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"
\$\endgroup\$
1
\$\begingroup\$

Perl 5 -p, 53 bytes

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

Try it online!

\$\endgroup\$
1
\$\begingroup\$

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

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.