# Listify a number

Given a real number, convert it to a list of lists, with the negative sign (if any) becoming an empty list, the integer part becoming a list of digits, and the fractional part (if any) becoming a list of digits. The digits must be actual numbers, not strings.

### Examples

0[[0]]

123[[1,2,3]]

-123[[],[1,2,3]]

123.45[[1,2,3],[4,5]]

0.45[[0],[4,5]]

-0.45[[],[0],[4,5]]

-123.45[[],[1,2,3],[4,5]]

• are the digits in the array allowed to be single-char strings? – dzaima Jul 27 '17 at 14:02
• @dzaima No. I'll add that. – Adám Jul 27 '17 at 14:03
• can the number be taken as a string? – Uriel Jul 27 '17 at 14:04
• @Uriel If you get input from STDIN then yes (there is no difference). If as argument, no, I think. Whatever Meta consensus is. – Adám Jul 27 '17 at 14:06
• @Quelklef Yes, in fact. It was the result of a long design discussion you can see here about the l (listify) command of totallyhuman's WIP language, Intrnt. – Adám Jul 27 '17 at 21:18

# C#, 60 66 bytes

using System.Linq;s=>s.Split('-','.').Select(p=>p.Select(c=>c-48))


Try it online!

• There are no visible outer brackets. Are your results actually lists? – Adám Jul 27 '17 at 14:05
• @Adám Yeah (IEnumerable<char[]>) it's just the printing code doesn't add them in. – TheLethalCoder Jul 27 '17 at 14:18
• @TheLethalCoder The requirements state that the required return type is a list of digits as numbers, not strings or chars which display those numbers. – Kamil Drakari Jul 27 '17 at 14:59
• @KamilDrakari Oh didn't see the spec had been updated... – TheLethalCoder Jul 27 '17 at 15:00
• @KamilDrakari Fixed, thanks for letting me know about the update. – TheLethalCoder Jul 27 '17 at 15:03

# JavaScript (ES6), 334443 41 bytes

Takes input as a string. Sacrificed 11 10 bytes converting the elements in the output to numbers after the challenge spec was updated.

s=>s.split(/\D/).map(a=>[...a].map(eval))

• Saved a byte thanks to Arnauld's suggestion of using eval.

## Test it

console.log((
s=>s.split(/\D/).map(a=>[...a].map(eval))
)("-123.45"))

## Explanation

s=>


Anonymous function taking the string as an argument via parameter s.
"-123.45"

s.split(/\D/)


Use RegEx to split the string to an array on all non-digit characters - i.e., - and .
["","123","45"]

.map(a=>)


Map over the array, passing each string to a function via parameter a.

[...a]


Split to an array of individual character strings.
[[],["1","2","3"],["4","5"]]

.map(eval)


Map over the subarray and eval each string, which converts it to an integer.
[[],[1,2,3],[4,5]]

• 2D array of strings? How does the JSON look? – Adám Jul 27 '17 at 14:04
• Digits must be numbers. – Adám Jul 27 '17 at 14:07
• @Adám: Updated, at a cost of 11 bytes. – Shaggy Jul 27 '17 at 14:12

# Python 2, 58 bytes

lambda x:[map(int,i)for i inx.replace(*'-.').split('.')]


Try it online!

# Jelly,  13  10 bytes

ŒṘµ<”/œpV€


A monadic link taking a number and returning the resulting list of lists of numbers.

Try it online! (the footer just prints the python representation to show all the actual lists)
...or see the test suite.

### How?

ŒṘµ<”/œpV€ - Link: number
ŒṘ         - Python representation (yields a string representation of the number)
µ        - monadic chain separation (call that s)
”/     - literal '/' character
<       - less than? (vectorises) ('.' and '-' are, digit characters are not)
œp   - partition s at truthy indexes of the resulting list discarding the borders
V€ - evaluate €ach (list of characters) as Jelly code (vectorises)

• And again Jelly is better than 05AB1E! – Erik the Outgolfer Jul 27 '17 at 14:54

# 05AB1E, 12 11 bytes

Thanks to Riley for saving a byte. Code:

'-'.:'.¡εSï


Uses the 05AB1E encoding. Try it online!

Explanation:

'-'.:            # Replace "-" by "."
'.¡         # Split on "."
ε        # Apply to each element..
S       #   Split into a list of characters
ï      #   Convert back to int

• Do you technically need the ï? – Magic Octopus Urn Jul 27 '17 at 14:42
• @MagicOctopusUrn OP said The digits must be actual numbers, not strings. – dzaima Jul 27 '17 at 14:55
• That's a shame, I was thinking: „-.S€¡ for 6, but if they have to be ints... unsure. – Magic Octopus Urn Jul 27 '17 at 16:22

# Python 2, 59 bytes

lambda a:[[]]*(a<0)+[map(int,n)for n inabs(a).split('.')]


Try it online!

-5 bytes from Felipe Nardi Batista

• in your third output [[[1, 2, 3], [4, 5]]] there is an extra outer list – Felipe Nardi Batista Jul 27 '17 at 14:12
• fixed and still uses 64 bytes link – Felipe Nardi Batista Jul 27 '17 at 14:14
• you can get to 59 bytes if you change your outer map with list comprehension: link – Felipe Nardi Batista Jul 27 '17 at 14:22

# Actually, 23 bytes

'.@;)A$s⌠♂≈⌡M[[]]+@s~@t  Try it online! Explanation: '.@;)A$s⌠♂≈⌡M[[]]+@s~@t
'.                       push "."
@;)                    make a copy of the input and move it to the bottom of the stack
A$s absolute value of input, stringify, split on periods ⌠♂≈⌡M convert integer and fractional parts to lists of digits [[]]+ prepend an empty list @s~ bitwise negation of sign of input (1 -> -2, 0 -> -1, -1 -> 0) @t elements in the list starting at that 0-based index (drops the leading empty list if the input was positive)  # SOGL V0.12, 11 bytes Ζ-.ŗ .Θ⌡č¹r  Try it Here! Outputs to the top of the stack (because SOGL converts it to a multiline string because it is made for ascii-art). To view the result, look in the console after  r@10: (the outer brackets are the stack arrays) or just append οø∑ after the code Ζ-.ŗ replace "-" with "." .Θ split on "."s ⌡ for each č chop into characters (casts to strings :/) ¹ wrap in array (this + for each is like map()) r reverse types, vectorizing  # Japt (v2.0a0), 1210 8 bytes Takes input as a string. q\D ®¬®n  Test it (-Q flag for visualisation purposes only.) • 2 bytes saved thanks to Justin. • 2 bytes saved thanks to ETH. ## Explanation Implicit input of string U. q\D  Use RegEx to split (q) to an array on all non-digit characters. ®  Map over the array. ¬  Split each string to an array of individual characters. ®  Map over the array. n  Convert to integer. • Couldn't you split on \D, since the only non-digits will be . and -? – Justin Mariner Jul 27 '17 at 17:48 • The best part is, you don't even need the /s :-) – ETHproductions Jul 28 '17 at 23:27 • @ETHproductions: Now, that's pretty sweet. I thought I might have been able to omit the closing / by going multiline. I never considered trying to omit both of them. – Shaggy Jul 29 '17 at 12:02 # Octave, 54 bytes @(x)cellfun(@(c){c-48},strsplit(num2str(x),{'-' '.'}))  Anonymous function that takes a number as input and produces a cell array of numeric vectors. Try it online! ### Explanation @(x)cellfun(@(c){c-48},strsplit(num2str(x),{'-' '.'})) @(x) % Function with input x num2str(x) % Convert x to string strsplit( ,{'-' '.'}) % Split at '-' or '.'. Gives a % cell array of substrings cellfun( , ) % To each substring apply % the following function @(c){c-48} % Subtract 48 from each char % and pack into a cell  # C (gcc), 170164152146 144 bytes Should be able to golf this down a bit... #define P printf #define V *v[1] main(c,v)char**v;{for(V^45?P("[[%c",V++):P("[[],[%c",V++,V++);V;V^46?P(",%c",V++):P("],[%c",V++,V++));P("]]");}  Try it online! • I think you can save a few bytes by doing #define P printf( and then omitting the parenthesis in the P calls – Cyoce Aug 7 '17 at 17:57 # Jelly, 12 bytes ŒṘ⁾-.yṣ”.V€€  Try it online! • Yeah I tried that too, currently I've only got 13 (I'll post that but think it must be able to be shorter) 0.45 returns [[],[4,5] instead of [[0],[4,5]] (it collides with -45) – Jonathan Allan Jul 27 '17 at 14:45 • @JonathanAllan Oh yeah indeed...not sure where I got that? Oh wait, it's Ṿ! Fixing...ok fixed. Who would expect that Jelly parses the 0 and .45 of 0.45 separately...and have it as Ṿ output too. – Erik the Outgolfer Jul 27 '17 at 14:48 # Actually, 16 bytes $'.'-(Æ'.@s⌠♂≈⌡M


Try it online!

Explanation:

$'.'-(Æ'.@s⌠♂≈⌡M Implicit eval'd input$                Convert to str
'.              Push '.'
'-            Push '-'
(           Rotate stack left
Æ          Pop a, b, c; push c.replace(b, a)
'.        Push '.'
@       Pop a, b; push b, a (swap)
⌠♂≈⌡   Push function ♂≈
♂       Map
≈        Convert to int
M  Map

• Outgolfed the creator, nice. – Zacharý Jul 27 '17 at 15:35
• @Zacharý It's just a matter of algorithm choice. – Erik the Outgolfer Jul 27 '17 at 15:35
• And it's Mego you beat, not Dennis of course :) – Zacharý Jul 27 '17 at 15:36
• @Zacharý Well, twice in this challenge to be fair... – Erik the Outgolfer Jul 27 '17 at 15:36

## R, 5147 72 bytes

x=RG::s(strtoi(s(gsub('-','.',scan()),on='\\.')))
x[is.na(x)]=list(NULL)


I'm loving the RG library.

Had to add 26 bytes to make sure the empty list was actually empty.

               gsub('-','.',scan())             # replace - with . in input; also converts to string
s(                    ,on='\\.')   # split string on '.'
strtoi(                                )  # convert to numeric
RG::s(                                        ) # convert to lists of digits

x[is.na(x)]=list(NULL)                      # converts list of NA to empty list


Example output:

> x=RG::s(strtoi(s(gsub('-','.',-123.45),on='\\.')))
> x[is.na(x)]=list(NULL)
> x
[[1]]
NULL

[[2]]
[1] 1 2 3

[[3]]
[1] 4 5

• well, there's numeric(0) for an empty numeric list or list() or NULL? – Giuseppe Jul 27 '17 at 20:11
• I tacked on a fix to make it NULL but now I'm sure there's a golfier way to do it. Maybe I'll come back to it. – BLT Jul 27 '17 at 20:29

# Perl 5, 56 54 + 1 (-p) = 55 bytes

$_="[[$_]]";s/\D\K\./0./;s/\d(?=\d)/$&,/g;s/-|\./],[/g  Try it online! Saved two bytes due to Dom reminding me about$&

Explanation:

$_="[[$_]]";        # Add opening and closing to ends of strings
s/\D\K\./0./;       # handle the case of .45 or -.45 by inserting 0 before
# the decimal.  Otherwise, .45 & 45 would be ambiguous.
s/\d(?=\d)/$&,/g; # Put a comma between numbers. s/-|\./],[/g # Turn - and . into separators between lists  • Had a little play with this and found breaking it down to be a bit shorter. Not sure if you can combine it based on mine to shrink yours down? 😊 Try it online! – Dom Hastings Jul 27 '17 at 20:44 • Used$& from yours. I think the two are basically equivalent now, except that mine handles the ".45" vs. "45" case. – Xcali Jul 27 '17 at 22:21
• Ahhh, yeah, missed that one! – Dom Hastings Jul 28 '17 at 7:37

# Perl 6, 23 bytes

+«*.split(/\D/)».comb


Test it

## Expanded

+«\            # numify each of the following (possibly in parallel)
*\             # WhateverCode lambda (this is the input)
.split(/\D/)\  # split on non-digits ( . and - )
».comb         # split each of those into individual characters


Note that …».comb is a higher precedence than +«…

# Python 2, 59 bytes

lambda x:(x<0)*[[]]+[map(int,i)for i inabs(x).split('.')]


Try it online!

# CJam, 13 bytes

r'-'.er'./::~


Try it online!

# Pyth, 12 bytes

sMMcXz\-\.\.


Try it here.

# Perl 6, 22 bytes

{m:g/^\d*|\d+/».comb}


Try it online!

The elements in the returned list are strings, but as is normal for Perl, they can be used as numbers and will be converted implicitly; they are "actual numbers" for all practical purposes. To force them to be numbers in their most immediate representation, one need only prefix the contents of the code block with "+«" for three more bytes.

• The challenge specifically states “The digits must be actual numbers, not strings.”, so the +« should probably be included. – Brad Gilbert b2gills Jul 28 '17 at 3:12

# RUBY, 75 bytes

->(x){(x<0?[[]]:[])+x.abs.to_s.split('.').map{|y|y.chars.map{|z|z.to_i}}}


Try it online!

• Can you remove all that whitespace? – TheLethalCoder Jul 28 '17 at 12:18
• Yep, you're right, done! – Gregory Jul 28 '17 at 12:23
• The same with some syntax tweaks: Try it online! (BTW, if you use Header and Footer on TIO the size displayed on the right side of Code will be the actual size of the solution.) – manatwork Jul 28 '17 at 12:32
• Fixed, thanks for all the infos. – Gregory Jul 28 '17 at 12:35
• You can swap .map{z|z.to_i} for .map &:to_i – Cyoce Aug 10 '17 at 0:54