-6
\$\begingroup\$

Number Splitting

Instructions

Oh no, my numbers have stuck together! Luckily, we can split them apart with code.

Your code should be a function that takes an integer input from stdin or as an argument. You must split the number into single digits, keeping the order and possible duplicates of these digits. The output should be an array and it may be returned from the function or from stdout. Other forms of output may be discussed. The array elements may be a string or number.

Examples

345 -> [3, 4, 5]
12218 -> [1, 2, 2, 1, 8]

Scoring

This is code golf, so shortest code wins.

\$\endgroup\$
3
  • 8
    \$\begingroup\$ As you don't have an a negative number in the examples, will the input always be zero or greater? \$\endgroup\$ – ErikF Aug 23 '20 at 0:56
  • 6
    \$\begingroup\$ Please, put your questions in the sandbox before posting them. Your submission is bascially something like this, but with lesser steps. Please check for similiar questions before posting, as well. \$\endgroup\$ – Razetime Aug 23 '20 at 4:47
  • 8
    \$\begingroup\$ Firstly, it is generally discouraged to accept answers here as that discourages further answers. Secondly, even if you decided to accept an answer, you should at least wait for several weeks, which is significantly longer than 1 hour. Thirdly, the JS answer is not the shortest, why accepting it? \$\endgroup\$ – null Aug 23 '20 at 8:38

13 Answers 13

2
\$\begingroup\$

JavaScript, 12 bytes

-3 bytes if we can take input as a string.

n=>[...n+""]

Try it online!

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Absolutely destroyed me. Congratulations. \$\endgroup\$ – Pinrui Huang Aug 22 '20 at 23:46
  • 5
    \$\begingroup\$ Firstly, it is generally discouraged to accept answers here as that discourages further answers. Secondly, even if you decided to accept an answer, you should at least wait for several weeks, which is significantly longer than 1 hour. It seems ridiculous to accept a 12 byte answer when there's a 4 byte answer posted only a few hours later. \$\endgroup\$ – null Aug 23 '20 at 8:37
4
\$\begingroup\$

Japt, 1 byte

Trivial challenges get trivial solutions.

ì

Try it

\$\endgroup\$
1
  • \$\begingroup\$ Trivial challenges meant for noobs like me to try to get into this thing. \$\endgroup\$ – Pinrui Huang Aug 22 '20 at 23:31
3
\$\begingroup\$

Wolfram Language (Mathematica), 13 bytes

IntegerDigits

Try it online!

\$\endgroup\$
8
  • \$\begingroup\$ A surprising builtin. I'm impressed. \$\endgroup\$ – Pinrui Huang Aug 22 '20 at 23:06
  • \$\begingroup\$ @PinruiHuang I'm impressed too!! how did you think of such an unexpected challenge? \$\endgroup\$ – ZaMoC Aug 22 '20 at 23:09
  • \$\begingroup\$ I thought there weren't enough basic challenges on this site and I'm trying to help newbies like me get into golfing. I snatched it from here: adriann.github.io/programming_problems.html \$\endgroup\$ – Pinrui Huang Aug 22 '20 at 23:10
  • \$\begingroup\$ @PinruiHuang are you impressed by my answer then? \$\endgroup\$ – ZaMoC Aug 22 '20 at 23:13
  • 2
    \$\begingroup\$ Haaaave you met Mathematica?! codegolf.stackexchange.com/a/71680/58974 \$\endgroup\$ – Shaggy Aug 23 '20 at 1:11
2
\$\begingroup\$

Retina 0.8.2, 4 bytes

\B
,

Try it online! Explanation: Simply inserts a , between each pair of digits. Alternatively, if a newline-delimited list is acceptable, then for 3 bytes:

!`.

Try it online! Explanation: Simply lists each digit separately. Alternatively, to produce the example output, then for 13 bytes:

\B
, 
^
[
$
]

Try it online! Link includes test cases. Alternatively, this can be done in Retina 1 for 12 bytes:

[[|", "]]L`.

Try it online! Link includes test cases.

\$\endgroup\$
2
\$\begingroup\$

Arn, 1 byte

Mmm. I have absolutely no idea how I found this...

\

Try it!

Explained

  _  The current input (implicit)
 \   Map/Fold by this:
_    The current item of the input, overloaded by \

     Thus, Map/Fold yields a list of the items of the input string.

Arn, 2 bytes

<i

Try it!

Explained

Unpacked: :!"

_      Input indicator (implicit)
 :!    Split by
   "   The empty string
    "  Trailing quote mark (implicit)
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Try it! <- fixed the link for you. I can't believe that works too XD \$\endgroup\$ – ZippyMagician Aug 26 '20 at 16:14
1
\$\begingroup\$

APL+WIN, 4 bytes

Prompts for integer:

⍎¨⍕⎕

Try it online! Courtesy of Dyalog Classic

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

Assembly (MIPS, SPIM), 106 bytes

Outputs the digits space-separated.

.data
.text
main:li $v0,12
syscall
move $a0,$v0
beq $a0,10,e
li $v0,11
syscall
li $a0,32
syscall
b main
e:

Try it online!

Assembly (MIPS, SPIM), 160 bytes

This outputs in a format looking like a list of digits.

.data
.text
main:li $a0,91
li $v0,11
syscall
l:li $v0,12
syscall
move $a0,$v0
beq $a0,10,e
li $v0,11
syscall
li $a0,44
syscall
b l
e:li $a0,93
li $v0,11
syscall

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Javascript(ES6), 38 bytes

Just to kick this off.

a=(w)=>{return w.toString().split("")}

Output:

345 -> ["3", "4", "5"] 12218 -> ["1", "2", "2", "1", "8"]

\$\endgroup\$
3
  • \$\begingroup\$ Wow I got outgolfed so hard so fast that I am literally confused. \$\endgroup\$ – Pinrui Huang Aug 22 '20 at 23:39
  • 1
    \$\begingroup\$ You don't need to count the a= and you can remove the parentheses around the w, the return & the curly braces. \$\endgroup\$ – Shaggy Aug 23 '20 at 0:26
  • \$\begingroup\$ Oh, and split works without and argument and you can cast w to a string with w+"", but you'll need to wrap it in parentheses. \$\endgroup\$ – Shaggy Aug 23 '20 at 0:28
0
\$\begingroup\$

Python 2, 18 bytes

lambda x:list(`x`)

Python 3, 21 bytes

lambda x:list(str(x))
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Great. Can you provide examples of output and put a place where we can run it online? \$\endgroup\$ – Pinrui Huang Aug 22 '20 at 23:28
  • \$\begingroup\$ If you're allowed to take input as a string, you can have list for 4 bytes. \$\endgroup\$ – pxeger Aug 23 '20 at 8:21
0
\$\begingroup\$

Python 3: 28 20 bytes

Try it Online - lambda function

Try it Online - normal function def

Tried it with lambda function (20 bytes) instead of normal function definition

x=lambda n:[*str(n)]

My earlier response with normal def function - 28 bytes

def x(n):*x,=str(n);return x

Calling the function as follows:

print(x(345))
print(x(12218))

Output:

['3', '4', '5']
['1', '2', '2', '1', '8']
\$\endgroup\$
2
  • 1
    \$\begingroup\$ For the lambda function, you are allowed to keep the "x=" part in the header saving 2 bytes: tio.run/##K6gsycjPM/5foWCrEPM/… \$\endgroup\$ – Manish Kundu Aug 23 '20 at 8:47
  • \$\begingroup\$ Thank You for the tip. I also realized that `` is not working in python 3. It is giving me errors. The tips said i could use `` as an option. That would reduce 3 more \$\endgroup\$ – Joe Ferndz Aug 23 '20 at 15:49
0
\$\begingroup\$

C (gcc), 36 bytes

Returns a string representation of the integer.

f(i){int*s;asprintf(&s,"%d",i);i=s;}

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ We were asked to return a digit array, not a string. \$\endgroup\$ – Shaggy Aug 23 '20 at 14:56
  • \$\begingroup\$ Suggest *s;f(i){ instead of f(i){int*s; \$\endgroup\$ – ceilingcat Aug 24 '20 at 7:23
0
\$\begingroup\$

Jelly, 1 byte

Another trivial solution.

D

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Pyth, 3 bytes

jQT

Try it online!

Explanation: Takes the integer input (Q) and converts it into base 10 (T) and returns the individual digits in a list.


Pyth, 3 bytes

cz1

Try it online!

Unlike the above one, this one takes input as a string and returns an array of the characters of the string. Still 3 bytes. :<)

\$\endgroup\$

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