19
\$\begingroup\$

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]]

\$\endgroup\$
10
  • \$\begingroup\$ are the digits in the array allowed to be single-char strings? \$\endgroup\$ – dzaima Jul 27 '17 at 14:02
  • \$\begingroup\$ @dzaima No. I'll add that. \$\endgroup\$ – Adám Jul 27 '17 at 14:03
  • \$\begingroup\$ can the number be taken as a string? \$\endgroup\$ – Uriel Jul 27 '17 at 14:04
  • \$\begingroup\$ @Uriel If you get input from STDIN then yes (there is no difference). If as argument, no, I think. Whatever Meta consensus is. \$\endgroup\$ – Adám Jul 27 '17 at 14:06
  • 2
    \$\begingroup\$ @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. \$\endgroup\$ – Adám Jul 27 '17 at 21:18

21 Answers 21

7
\$\begingroup\$

C#, 60 66 bytes

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

Try it online!

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

JavaScript (ES6), 33 44 43 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]]

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

Python 2, 58 bytes

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

Try it online!

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

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)
\$\endgroup\$
1
  • \$\begingroup\$ And again Jelly is better than 05AB1E! \$\endgroup\$ – Erik the Outgolfer Jul 27 '17 at 14:54
4
\$\begingroup\$

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

Python 2, 59 bytes

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

Try it online!

-5 bytes from Felipe Nardi Batista

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

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)
\$\endgroup\$
2
\$\begingroup\$

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

Japt (v2.0a0), 12 10 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.

\$\endgroup\$
3
  • 2
    \$\begingroup\$ Couldn't you split on \D, since the only non-digits will be . and -? \$\endgroup\$ – Justin Mariner Jul 27 '17 at 17:48
  • \$\begingroup\$ The best part is, you don't even need the /s :-) \$\endgroup\$ – ETHproductions Jul 28 '17 at 23:27
  • \$\begingroup\$ @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. \$\endgroup\$ – Shaggy Jul 29 '17 at 12:02
1
\$\begingroup\$

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

C (gcc), 170 164 152 146 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!

\$\endgroup\$
1
  • \$\begingroup\$ I think you can save a few bytes by doing #define P printf( and then omitting the parenthesis in the P calls \$\endgroup\$ – Cyoce Aug 7 '17 at 17:57
1
\$\begingroup\$

Jelly, 12 bytes

ŒṘ⁾-.yṣ”.V€€

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ 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) \$\endgroup\$ – Jonathan Allan Jul 27 '17 at 14:45
  • \$\begingroup\$ @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. \$\endgroup\$ – Erik the Outgolfer Jul 27 '17 at 14:48
1
\$\begingroup\$

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
\$\endgroup\$
4
  • 1
    \$\begingroup\$ Outgolfed the creator, nice. \$\endgroup\$ – Zacharý Jul 27 '17 at 15:35
  • \$\begingroup\$ @Zacharý It's just a matter of algorithm choice. \$\endgroup\$ – Erik the Outgolfer Jul 27 '17 at 15:35
  • \$\begingroup\$ And it's Mego you beat, not Dennis of course :) \$\endgroup\$ – Zacharý Jul 27 '17 at 15:36
  • \$\begingroup\$ @Zacharý Well, twice in this challenge to be fair... \$\endgroup\$ – Erik the Outgolfer Jul 27 '17 at 15:36
1
\$\begingroup\$

R, 51 47 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
\$\endgroup\$
2
  • \$\begingroup\$ well, there's numeric(0) for an empty numeric list or list() or NULL? \$\endgroup\$ – Giuseppe Jul 27 '17 at 20:11
  • \$\begingroup\$ 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. \$\endgroup\$ – BLT Jul 27 '17 at 20:29
1
\$\begingroup\$

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
\$\endgroup\$
3
  • \$\begingroup\$ 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! \$\endgroup\$ – Dom Hastings Jul 27 '17 at 20:44
  • 1
    \$\begingroup\$ Used $& from yours. I think the two are basically equivalent now, except that mine handles the ".45" vs. "45" case. \$\endgroup\$ – Xcali Jul 27 '17 at 22:21
  • \$\begingroup\$ Ahhh, yeah, missed that one! \$\endgroup\$ – Dom Hastings Jul 28 '17 at 7:37
1
\$\begingroup\$

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 +«…

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

Python 2, 59 bytes

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

Try it online!

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

CJam, 13 bytes

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

Try it online!

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

Pyth, 12 bytes

sMMcXz\-\.\.

Try it here.

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

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.

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

RUBY, 75 bytes

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

Try it online!

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Can you remove all that whitespace? \$\endgroup\$ – TheLethalCoder Jul 28 '17 at 12:18
  • \$\begingroup\$ Yep, you're right, done! \$\endgroup\$ – Gregory Jul 28 '17 at 12:23
  • \$\begingroup\$ 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.) \$\endgroup\$ – manatwork Jul 28 '17 at 12:32
  • \$\begingroup\$ Fixed, thanks for all the infos. \$\endgroup\$ – Gregory Jul 28 '17 at 12:35
  • \$\begingroup\$ You can swap .map{z|z.to_i} for .map &:to_i \$\endgroup\$ – Cyoce Aug 10 '17 at 0:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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