| Bytes | Lang | Time | Link |
|---|---|---|---|
| 015 | Pip p | 211215T231428Z | DLosc |
| 107 | JavaScript Node.js | 230322T145546Z | Fhuvi |
| nan | Unlike my other regex answer | 230322T001522Z | Deadcode |
| 030 | Regex .NET | 230321T175311Z | Deadcode |
| 011 | Nekomata | 230322T020350Z | alephalp |
| 041 | Wolfram Language Mathematica | 211217T063126Z | att |
| nan | 230315T105703Z | The Thon | |
| 092 | Scala | 211216T170630Z | Kjetil S |
| 060 | Haskell | 211218T024950Z | Steven F |
| 073 | brainfuck | 211217T152514Z | Nitrodon |
| 047 | Perl 5 F | 211216T232817Z | Xcali |
| 037 | Raku | 211216T001245Z | Jo King |
| 043 | R | 211216T194450Z | pajonk |
| 056 | PowerShell Core | 211216T005909Z | Julian |
| 074 | C gcc | 211216T200636Z | AZTECCO |
| 051 | QBasic | 211216T182235Z | DLosc |
| 057 | Python 2 | 211215T235120Z | pxeger |
| 063 | Perl 5 F p | 211216T150136Z | Kjetil S |
| 125 | Python 3 | 211216T142617Z | Alan Bag |
| 009 | Jelly | 211216T123529Z | Unrelate |
| 047 | Pari/GP | 211216T123509Z | alephalp |
| 013 | APL Dyalog Unicode | 211215T225456Z | Adá |
| 046 | JavaScript ES6 | 211215T230210Z | Arnauld |
| 009 | 05AB1E | 211215T234911Z | Luis Men |
| 014 | MathGolf | 211216T075441Z | Kevin Cr |
| 019 | Charcoal | 211216T003714Z | Neil |
| 025 | Retina 0.8.2 | 211216T002127Z | Neil |
| 009 | Vyxal | 211215T230041Z | lyxal |
| 009 | Jelly | 211215T232618Z | lynn |
| 055 | JavaScript | 211215T230428Z | tjjfvi |
Pip -p, 26 24 22 15 bytes
-7 bytes with a completely new approach:
Wa&lPUaDa@MFXal
Explanation
Works backwards from the number and outputs the results in reverse order.
Wa&lPUaDa@MFXal
a is command-line arg (the input number); l is empty list
Wa While a is nonzero...
&lPUa (and we push current value of a onto the front of l)
... loop:
FXa Filter indices of a, keeping the ones that correspond
to truthy (non-zero) values
M Get the maximum (rightmost) such index
Da@ Decrement the digit at that index
l After the loop is over, output l
JavaScript (Node.js), 107 bytes (non-competitive)
I tried to do this challenge in JS with a "functional programming approach", without any recursive call and without regex, and it has become surprinsingly long!
I'm still quite new to JS, so does any of you see if this could this be shortened in some way while keeping this approach?
p=>[...p+=""].flatMap((a,i)=>(r=[...Array(a*=1)].map((b,j)=>(t*10+j+1)*10**(p.length-i-1)),t=t*10+a,r),t=0)
(I can ungolf it if this needs explanations)
Unlike my other regex answer, this outputs in reverse order, in order to support regex engines that are incapable of outputting it in ascending order.
The algorithm used here was conceived of independently from, but is very similar to, Arnauld's JavaScript answer (and therefore pxeger's Python answer) and Unrelated String's 2nd Jelly answer. Unlike those answers, it is impossible to reverse the order of the list before outputting it.
Regex (Perl / PCRE / .NET), 39 35 bytes
(?=((((?(3)\3{10}|x{9}))*x)\2*$))\2
Takes its input in unary, as a string of x characters whose length represents the number. Returns its output as the list of matches' \1 captures.
Try it online! - Perl
Try it online! - PCRE
Try it online! - .NET
# on the first match, tail = N = input number
(?=
( # \1 = tail (the return value)
( # \2 = the largest power of 10 that divides tail
( # \3 = the following, on each iteration (and on each
# iteration, subtract it from tail):
(?(3) # Conditional on whether \3 is set; on the first
# iteration, it is unset, and on all subsequent
# iterations it is set:
\3{10} # if \3 is set: \3 * 10
|
x{9} # if \3 is unset: 9
)
)* # Iterate the above as many times as possible
# without preventing the following from matching:
x # Assert tail ≥ 1; tail -= 1
)
\2*$ # Assert that \2 divides tail
)
)
\2 # tail -= \2, in preparation for the next match
It's impossible for regex flavors without variable-length lookbehind to output this sequence in ascending order. Emulating this with recursion and fixed-width lookbehind in Perl/PCRE doesn't work, because captures made inside a subroutine call are erased upon returning from the call. It's impossible even to guess what the capture will be before it's made using non-atomic lookahead in PCRE2, because there isn't enough room to capture that after the sequence has passed \$n/2\$.
Regex (Perl / Java / PCRE / .NET), 39 37 bytes
(?=(((\3{10}|(?!\4)x{9}())*x)\2*$))\2
Try it online! - Perl
Try it online! - Java
Try it online! - PCRE
Try it online! - .NET
This adds Java support to the 35 byte version by removing the use of the conditional.
Regex (Perl / Pythonregex / Ruby / PCRE / .NET), 45 bytes
(?=((((?=((?(5)\5{10}|x{9})))(\4))*x)\2*$))\2
Try it online! - Perl
Attempt This Online! - Python (with regex)
Try it online! - Ruby
Try it online! - PCRE
Try it online! - .NET
This adds Pythonregex and Ruby support to the 35 byte version, by replacing the use of the nested backreference \3 with copying a value back and forth between two forward-declared backreferences \4 and \5.
Regex (Perl / Java / Pythonregex / Ruby / PCRE / .NET), 47 bytes
(?=((((?=(\6{10}|(?!\5)x{9}()))(\4))*x)\2*$))\2
Try it online! - Perl
Try it online! - Python (with regex)
Try it online! - Ruby
Try it online! - PCRE
Try it online! - .NET
This attempts to add Java support to the 45 byte version by removing the use of the conditional (just like the 37 byte version does to the 35 byte version), but while it works on the other engines, it exposes a bug in Java's regex engine: Try it online! / Attempt This Online!
Regex (ECMAScript / Boost / Python or better), 54 52 bytes
(?=((|x+)\2*(?=(\2$|.*$\2))((x+)\5{8}(?=\5$))*x$))\3
Try it online! - ECMAScript
Try it online! - ECMAScript 2018
Try it online! - Perl
Try it online! - Java
Try it online! - Boost - only works with small numbers
Try it online! - Python
Attempt This Online! - Python (with regex)
Try it online! - Ruby
Try it online! - PCRE
Try it online! - .NET
(?=
( # \1 = tail (the return value)
(|x+) # \2 = 0 or the largest number that satisfies the
# following, whichever matches first
\2* # tail -= \2 * {any nonnegative integer}, to
# satisfy the following:
(?= # Lookahead (atomic - first match is locked in)
( # \3 = tail
\2$ # Assert tail == \2
| # or...
.*$\2 # Assert \2 == 0
)
)
# Assert that tail is a power of 10
(
(x+)\5{8}(?=\5$) # Assert that 10 divides tail; tail /= 10
)* # Iterate the above as many times as necessary
# (minimum 0) to satisfy the following:
x$ # Assert tail == 1
)
)
\3 # tail -= \3, in preparation for the next match
Boost works fine with the previous 54 byte version: Try it online!
Regex (.NET), 30 bytes
((?(1)\1{10}|x{9}))*x(?<=(x*))
Takes its input in unary, as a string of x characters whose length represents the number. Returns its output as the list of matches' \2 captures.
# head + tail = N = input number; first match starts with head=0 and tail=N
# Subtract the largest possible power of 10 from tail
( # \1 = the following, on each iteration (and on each
# iteration, add it to head and subtract it from tail):
(?(1) # Conditional on whether \1 is set; on the first iteration,
# it is unset, and on all subsequent iterations it is set:
\1{10} # if \1 is set: \1 * 10
|
x{9} # if \1 is unset: 9
)
)* # Iterate the above as many times as possible without preventing
# the following from matching:
x # Assert tail ≥ 1; head += 1; tail -= 1
(?<=(x*)) # Inside a lookbehind, \2 = head
Regex (Java / .NET), 33 bytes
(\1{10}|(?!\2)x{9}())*x(?<=(^x*))
Returns its output as the list of matches' \3 captures.
Try it online! - Java
Try it online! - .NET
This adds Java support to the 30 byte version by removing the use of the conditional, and hinting how far backwards to go inside the lookbehind.
Regex (.NET), 36 bytes
(((?<=^\3?)x{9}|\2{10})*x(?<=(x*)))*
Returns its output as the list of captures on the Balancing Group \3 stack.
# head + tail = N = input number; first match starts with head=0 and tail=N
(
# Subtract the largest possible power of 10 from tail
( # \1 = the following, on each iteration (and on each
# iteration, add it to head and subtract it from tail):
(?<=^\3?) # Make sure this alternative is only used on the first
# iteration of this loop, by asserting that head == 0 on the
# first iteration of the outermost loop, or head == \3
# on subsequent iterations of the outermost loop.
x{9} # 9
|
\2{10} # \2 * 10
)* # Iterate the above as many times as possible without
# preventing the following from matching:
x # Assert tail ≥ 1; head += 1; tail -= 1
(?<=(x*)) # Inside a lookbehind, \3 = head (which is pushed onto the
# Group 3 capture stack)
)* # Iterate the above as many times as possible, minimum 0
Regex (Pythonregex / .NET), 41 40 bytes
((?=((?(3)\3{10}|x{9})))(\2))*x(?<=(x*))
Returns its output as the list of matches' \4 captures.
Attempt This Online! - Python (with regex)
Try it online! - .NET
This adds Pythonregex support to the 30 byte version, by replacing the use of the nested backreference \1 with copying a value back and forth between two forward-declared backreferences \2 and \3.
Regex (ECMAScript 2018 / Pythonregex / .NET), 41 bytes
(?=.*?(((x+)\3{8}(?=\3$))*x$))\1(?<=(x*))
Returns its output as the list of matches' \4 captures.
Try it online! - ECMAScript 2018
Attempt This Online! - Python (with regex)
Try it online! - .NET
# head + tail = N = input number; first match starts with head=0 and tail=N
(?= # Atomic lookahead - whatever matches first is
# locked in.
.*? # Subtract as little as possible from tail in
# order to satisfy the following:
# Assert that tail is a power of 10, and capture it in \1.
( # \1 = tail
(
(x+)\3{8}(?=\3$) # Assert that 10 divides tail; tail /= 10
)* # Iterate the above as many times as necessary
# (minimum 0) to satisfy the following:
x$ # Assert tail == 1
)
)
\1 # head += \1; tail -= \1
# (this applies also to the next match)
(?<=(x*)) # Inside a lookbehind, \4 = head
Regex (ECMAScript 2018 / Java / Pythonregex / .NET), 42 bytes
(?=.*?(((x+)\3{8}(?=\3$))*x$))\1(?<=(^x*))
Try it online! - ECMAScript 2018
Try it online! - Java
Attempt This Online! - Python (with regex)
Try it online! - .NET
This adds Java support to the 41 byte version by hinting how far backwards to go inside the lookbehind.
Nekomata, 11 bytes
¢DsCr↔~c¢b-
Take 47 as an example.
¢DsCr↔~c¢b-
¢D Convert the input to decimal digits.
47 becomes [4, 7]
s Non-deterministically choose a suffix of the list.
Possible suffixes are [4, 7], [7], [].
C Split the list into the head and the tail.
Possible heads are 4 and 7, and the corresponding tails are [7] and [].
r Range from 0 to the head minus 1.
4 becomes [0, 1, 2, 3], and 7 becomes [0, 1, 2, 3, 4, 5, 6].
↔ Reverse the range.
Possible results are [3, 2, 1, 0] and [6, 5, 4, 3, 2, 1, 0].
~ Non-deterministically choose an element from the range.
Possible elements are 3, 2, 1, 0, 6, 5, 4, 3, 2, 1, 0.
c Join the element with the tail.
Possible results are [3, 7], [2, 7], [1, 7], [0, 7], [6], [5], [4], [3], [2], [1], [0].
¢b Convert the list to a base-10 number.
Possible results are 37, 27, 17, 7, 6, 5, 4, 3, 2, 1, 0.
- Subtract it from the input.
Possible results are 10, 20, 30, 40, 41, 42, 43, 44, 45, 46, 47.
The interpreter will print all of them.
Wolfram Language (Mathematica), 49 44 43 41 bytes
If[#<1,{0},Max[p=10#0[.1#]]~Range~#⋃p]&
Includes one leading zero.
Range stops before the first number greater than the maximum. For example, Range[3.14] yields {1,2,3}.
Thunno D, \$ 20 \log_{256}(96) \approx \$ 16.46 bytes
LR10@rsZZeAuZOA*ESz(
Input as a digit list.
Thunno, \$ 22 \log_{256}(96) \approx \$ 18.11 bytes
dDLR10@rsZZeAuZOA*ESz(
Input as an integer.
Port of Lynn's Jelly answer.
Explanation
dD # Get digits and duplicate
# (Not needed in the first answer)
LR # Pop one and push the length range
10@ # Pop and push 10 ** each
rs # Reverse and swap
ZZ # Zip with the digits of the input
eAu # Map over this list:
ZO # Wrap the power of ten in a list
A* # And repeat it the digit times
E # End map
S # Flatten the list
z( # And push the cumulative sums
Scala, 105 101 92 bytes
i=>i.indices.flatMap(e=>Seq.fill(i(e)-48)(("1"+"0"*(i.size-1-e)).toLong)).scan(0L)(_+_).tail
Haskell, 60 bytes
c 0=[]
c x|(d,m)<-x`divMod`10=map(*10)(c d)++map(x-m+)[1..m]
Simple recursive solution, for example c 345 is equal to
map (*10) (f 34) ++ map (340+) [1..5]
34 is 345 `div` 10
5 is 345 `mod` 10
340 is 345 minus the remainder
brainfuck, 73 bytes
,[>-[-----<-<+>>]<++++<--->>>,]+<++++++++++[<]>>[-[<+[<<]>>[.>>]<[<<]]>>]
Treats this as a string processing task. At each step, we find the first digit that still needs to be incremented, increment it, and print the number.
Input loop
,[
Place 48 in cell while subtracting 47 from input cell
>-[-----<-<+>>]<++++<--->>>
Repeat until input exhausted
,]
If input number was 1024 we now have 48 2 48 1 48 3 48 5 0 (0)
Set up fake 0 digit at the end (to save bytes later)
+
Set up output LF
<++++++++++
Return to first input cell
[<]>>
This loop always starts at the first nonzero input cell remaining
Loop until done:
[
Decrement digit
-
If cell is zero we just blanked an already "zero" digit so do nothing
[
Increment corresponding output digit
<+
Output entire number with LF
[<<]>>[.>>]
Return to input cell prior to first nonzero input cell
<[<<]
]
Move to first nonzero input cell
>>
]
Raku, 37 bytes
{[\R+] flat (10 X**^$_)Zxx.flip.comb}
An anonymous code block that takes a number and returns an array.
Explanation:
{ } # Anonymous code block
(10 X**^$_) # Generate powers of 10
Zxx # Zip repeat each by
.flip.comb # The reversed digits of the number
flat # Flatten this list
[ R+] # Reduce by reverse addition
\ # Keeping intermediate values
R, 64 52 43 bytes
Or R>=4.1, 36 bytes by replacing the word function with a \.
Edit: -12 bytes thanks to @Giuseppe.
function(d)cumsum(rep(10^(sum(d|1):1-1),d))
Yet another port of @Lynn's answer.
Takes input as a vector of digits.
R, 49 bytes
Or R>=4.1, 42 bytes by replacing the word function with a \.
function(n)while(n>F)show(F<-F+10^(nchar(n-F)-1))
Direct approach inspired by @DLosc's answer.
PowerShell Core, 56 bytes
($args|%{0.."$_"-ne0|%{"$c$_"}
$c+=$_;$i++})|% *ht $i 48
Takes a number as a string using splatting in input and returns a list of numbers
-8 bytes thanks to mazzy !
C (gcc), 74 bytes
a;g(x){a=0;f(x,1);}f(x,y){y<x&&f(x,y*10);for(;a+y<=x;)printf("%d ",a+=y);}
QBasic, 51 bytes
INPUT n
WHILE n>g
g=g+10^(LEN(STR$(n-g))-2)
?g
WEND
Explanation
We can calculate which digit we want to increment by getting the length of the difference between the input number n and the current number g:
1234
-1210
=====
24 -> length 2, increment by 10^1
Since QBasic's STR$ function adds a space to the front of nonnegative numbers, the power of 10 that we need is LEN minus 2. Thus, we add 10^(LEN(STR$(n-g))-2) to g, print g (? is a shortcut for PRINT), and loop until g and n are equal.
Python 2, 58 57 bytes
f=lambda n,k=1:n%k and f(n-k/10)+[n]or n*[1]and f(n,k*10)
Port of Arnauld's answer.
-1 thanks to @ovs
Python 2, 76 64 bytes
x=input() c=0 i=len(x) for d in x:i-=1;exec"c+=10**i;print c;"*d
Port of Lynn's answer.
-12 thanks to @ovs
Python 2, 74 bytes
x=input() o=[0]*len(x) i=0 for d in x: while o[i]<d:o[i]+=1;print o i+=1
Perl 5 -F -p, 63 bytes
//+push@a,map$a[-1]+$_*10**(@F-$'),1..$F[$_-1]for 1..@F;$_="@a"
Python 3, 125 bytes
def f(x):
s=c=int("1"+~-len(str(x))*"0")
while s<=x//10*10:
yield s;s+=c
for i in range(1,x%10+1):yield i
Jelly, 9 bytes
æḟ⁵ạƊƬINÄ
9 bytes sure seems to be special here.
Ƭ Collect results while unique from repeating:
ạ absolute difference from
æḟ⁵ greatest less than or equal power of 10.
Ä Take the cumulative sum of
IN each amount by which it decreased.
Jelly, 9 bytes
ọ⁵⁵*ạoµƬU
Conceived of independently from, but very similar to, Arnauld's solution.
µƬ Collect results while unique from repeating:
ọ⁵ How many times does 10 evenly divide it?
⁵* Raise 10 to that power,
ạ take the absolute difference,
o and keep the previous value (ending the loop) if it's 0.
U Reverse.
Pari/GP, 47 bytes
f(n)=if(n,concat(f(n-10^valuation(n,10)),n),[])
Port of @Arnauld's JavaScript answer.
APL (Dyalog Unicode), 13 bytes
Port of Lynn's solution – upvote that!
Anonymous prefix lambda, taking a digit list as argument and returning a numeric list. Requires 0-based indexing.
{+\⍵/10*⌽⍳≢⍵}
{…} "dfn"; argument is ⍵:
+\ cumulative sum of…
⍵/ the argument numbers replicating the respective numbers in…
10* ten raised to the powers of…
⌽ the reversed…
⍳ indices in an array of size…
≢ tally of elements in…
⍵ argument
Old solution: 24 17 bytes
−4 thanks to ovs
Full program. Prompts for digit list.
↑¨{⍺,,¨∘⍵⊃⌽⍺}/⍳¨⎕
⎕ prompt for digit list
⍳¨ generate 1…n for each digit
{…}/ reduce (from the right) using this lambda:
⊃⌽⍺ the last element of the left argument (lit. the first of the reverse)
,¨∘⍵ prepend that to each of the left argument elements
⍺, prepend the left argument to that
↑¨ combine each list of lists into a matrix, zero-padding on the right
JavaScript (ES6), 48 46 bytes
f=(n,k=1)=>n?n%k?[...f(n-k/10),n]:f(n,k*10):[]
How?
Instead of going from \$0\$ to \$n\$, we go from \$n\$ to \$0\$ and store the intermediate steps in reverse order.
At each step, we start with \$k=1\$ and recursively multiply \$k\$ by \$10\$ until \$n\bmod k\neq 0\$, which is a way to locate the least significant non-zero digit in \$n\$. We decrement this digit by subtracting \$k/10\$ from \$n\$ and repeat the process until \$n=0\$.
JavaScript (ES6), 49 bytes
This version expects a string and uses a lookahead assertion to locate and decrement the least significant non-zero digit.
f=n=>+n?[...f(n.replace(/.(?=0*$)/,c=>c-1)),n]:[]
05AB1E, 11 9 bytes
Thanks to @ovs for 2 bytes off, and to @KevinCruijssen for pointing out that the input can be an integer instead of an array
ā<R°¹ÅΓηO
Port of Lynn's answer.
MathGolf, 14 bytes
hrxúma\m*─Å+o;
Input as a digit-list.
Explanation:
h # Push the length of the (implicit) input
r # Pop and push a list in the range [0,length)
x # Reverse it to (length,0]
ú # Convert each value in this list to 10**value
ma # Wrap each inner number into a list
\ # Swap so the input-list is at the top
m* # Repeat each wrapped [10**v] that amount of times
─ # Flatten the list of lists
Å # Loop over this list, using 2 characters as inner code-block:
+ # Add the top two values on the stack together
o # Print this number (without popping)
; # After the loop, discard the number (since MathGolf implicitly
# outputs the entire stack after a program ends)
Charcoal, 22 19 bytes
⭆θ⭆Iι⁺⭆◨⁺…θκ⊕λLθΣν¶
Try it online! Link is to verbose version of code. Explanation:
θ Input string
⭆ Map over digits and join
ι Current digit
I Cast to integer
⭆ Map over implicit range and join
θ Input string
… Truncated to length
κ Outer index
⁺ Concatenated with
λ Inner value
⊕ Incremented
◨ Right pad with spaces to
L Length of
θ Input string
⭆ Map over characters and join
ν Current character
Σ Change space to zero
⁺ Concatenated with
¶ Literal newline
Retina 0.8.2, 25 bytes
^
$%'¶
1T`d`0d`.0*¶
}A`^0
Try it online! Link includes test cases (sorry the output is smashed together). Explanation:
^
$%'¶
Duplicate the first line.
1T`d`0d`.0*¶
Decrement the last nonzero digit on that line.
A`^0
Delete the first line if it's zero.
}`
Repeat until the first line had been reduced to zero.
Jelly, 9 bytes
DLḶU⁵*xDÄ
DL Digits, length
ḶU⁵* Compute [10^(n-1), …, 10, 1]
xD Use the digits as repeat counts for this array
e.g. 423 -> [100, 100, 100, 100, 10, 10, 1, 1, 1]
Ä Cumulative sum: [100, 200, 300, 400, 410, 420, 421, 422, 423]