Skip to main content
Fix invalid outputs since 0 is invalid input!
Source Link
Olivier Grégoire
  • 14.4k
  • 3
  • 32
  • 56

This challenge is based on this Stackoverflow question.

With a positive number as input, output it as the sum of each digit multiplied by its power-of-10 representation.

Input

A number, as an integer, a string or a list of digits/characters.

  • The number will be strictly positive.
  • If you accept the number as a string or list, it will not start with a 0.

Output

A string representing a sum of each relevant base-10 digits, each multiplied by its respective base-10 power. A sum is represented as a + b. You may use up to one space around each side of the + sign if you want. The operands are listed in descending order.

  • 0 can never be a valid operand.
  • The + sign (surrounded or not by spaces) may not be the leading or trailing part.

Examples

Input       Output
12          10 + 2
         or 10+2
         or 10 +2
         or 10+ 2
9           9
123         100 + 20 + 3
10          10
101         100 + 1

Invalid outputs

0           0
2           1 + 1
10          10 + 0
1           0 + 1
12          + 10 + 2
12          10 + 2 +
12          2 + 10

This is code-golf so the shortest code in bytes wins!

This challenge is based on this Stackoverflow question.

With a positive number as input, output it as the sum of each digit multiplied by its power-of-10 representation.

Input

A number, as an integer, a string or a list of digits/characters.

  • The number will be strictly positive.
  • If you accept the number as a string or list, it will not start with a 0.

Output

A string representing a sum of each relevant base-10 digits, each multiplied by its respective base-10 power. A sum is represented as a + b. You may use up to one space around each side of the + sign if you want. The operands are listed in descending order.

  • 0 can never be a valid operand.
  • The + sign (surrounded or not by spaces) may not be the leading or trailing part.

Examples

Input       Output
12          10 + 2
         or 10+2
         or 10 +2
         or 10+ 2
9           9
123         100 + 20 + 3
10          10
101         100 + 1

Invalid outputs

0           0
2           1 + 1
10          10 + 0
1           0 + 1
12          + 10 + 2
12          10 + 2 +
12          2 + 10

This is code-golf so the shortest code in bytes wins!

This challenge is based on this Stackoverflow question.

With a positive number as input, output it as the sum of each digit multiplied by its power-of-10 representation.

Input

A number, as an integer, a string or a list of digits/characters.

  • The number will be strictly positive.
  • If you accept the number as a string or list, it will not start with a 0.

Output

A string representing a sum of each relevant base-10 digits, each multiplied by its respective base-10 power. A sum is represented as a + b. You may use up to one space around each side of the + sign if you want. The operands are listed in descending order.

  • 0 can never be a valid operand.
  • The + sign (surrounded or not by spaces) may not be the leading or trailing part.

Examples

Input       Output
12          10 + 2
         or 10+2
         or 10 +2
         or 10+ 2
9           9
123         100 + 20 + 3
10          10
101         100 + 1

Invalid outputs

2           1 + 1
10          10 + 0
1           0 + 1
12          + 10 + 2
12          10 + 2 +
12          2 + 10

This is code-golf so the shortest code in bytes wins!

Tweeted twitter.com/StackCodeGolf/status/1038940376902467584
Source Link
Olivier Grégoire
  • 14.4k
  • 3
  • 32
  • 56

Expand some number

This challenge is based on this Stackoverflow question.

With a positive number as input, output it as the sum of each digit multiplied by its power-of-10 representation.

Input

A number, as an integer, a string or a list of digits/characters.

  • The number will be strictly positive.
  • If you accept the number as a string or list, it will not start with a 0.

Output

A string representing a sum of each relevant base-10 digits, each multiplied by its respective base-10 power. A sum is represented as a + b. You may use up to one space around each side of the + sign if you want. The operands are listed in descending order.

  • 0 can never be a valid operand.
  • The + sign (surrounded or not by spaces) may not be the leading or trailing part.

Examples

Input       Output
12          10 + 2
         or 10+2
         or 10 +2
         or 10+ 2
9           9
123         100 + 20 + 3
10          10
101         100 + 1

Invalid outputs

0           0
2           1 + 1
10          10 + 0
1           0 + 1
12          + 10 + 2
12          10 + 2 +
12          2 + 10

This is code-golf so the shortest code in bytes wins!