Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Given an input of a list of numbers in the format of a shorthand increasing integer sequence, output the sequence in full.

Shorthand increasing integer sequence format works by finding every number n with fewer digits than the number preceding it, m. With d as the number of digits in n, the last d digits of m are replaced with all the digits of n. Here's an example input:

123 45 6 7 89 200

Applying the replacement rule, we first turn 45 into 145 because 45 < 123:

123 145 6 7 89 200

Repeatedly applying the same rule, this becomes:

123 145 146 7 89 200
123 145 146 147 89 200
123 145 146 147 189 200

The sequence is now sorted (there are no numbers for which the rule applies), so this is the final output.

You may assume that

  • shorthand notation is always used when possible. For example, input will be 12 3, never 12 13.

  • numbers will never decrease while remaining the same number of digits. For example, input will never be 333 222.

  • applying the shorthand rule will never result in a number that is still less than the previous number in the sequence. For example, input will never be 123 12.

  • numbers will always be positive integers and never contain leading 0s (if using a string format).

  • the full, expanded sequence will never contain duplicate numbers. (However, the shorthand sequence might; ex. 10 1 20 1 -> 10 11 20 21.)

Input and output can be either lists/arrays of numbers/strings or a single string with elements separated by any non-digit.

Since this is , the shortest code in bytes will win.

Test cases, with input and output on alternating lines:

1 2 3 10 1 2 20 5 100 200 10 3 5 26 9 99 999 9999
1 2 3 10 11 12 20 25 100 200 210 213 215 226 229 299 999 9999
223 1184 334 441 5 927 2073 589 3022 82 390 5 9
223 1184 1334 1441 1445 1927 2073 2589 3022 3082 3390 3395 3399
5 10 5 20 5 30 5 40 5 50 5
5 10 15 20 25 30 35 40 45 50 55
7 8 9 70 80 90 700 800 900 7000 8000 9000
7 8 9 70 80 90 700 800 900 7000 8000 9000
share|improve this question

Jelly, 7 bytes

DUṛ"\UḌ

Try it online! or verify all test cases.

How it works

DUṛ"\UḌ  Main link. Input: A (list of integers)

D        Convert each integer to a list of its base 10 digits.
 U       Reverse each digit list.
    \    Do a cumulative reduce, applying the dyadic link to the left:
   "       For each pair of corresponding digits:
  ṛ          Select the right one.
           Vectorization leaves digits that do not have a counterpart untouched.
     U   Reverse the resulting digit arrays.
      Ḍ  Convert from base 10 to integer.
share|improve this answer

Javascript, 45 42 bytes

3 bytes off thanks @Neil.

a=>a.map(x=>z=z.slice(0,-x.length)+x,z='')

The above function expects an array of strings.

f=
a=>a.map(x=>z=z.slice(0,-x.length)+x,z='')

F=a=>document.body.innerHTML+='<pre>'+a+'\n'+f(a)+'\n</pre>';

F(['1','2','3','10','1','2','20','5','100','200','10','3','5','26','9','99','999','9999'])
F(['223','1184','334','441','5','927','2073','589','3022','82','390','5','9'])
F(['5','10','5','20','5','30','5','40','5','50','5'])
F(['7','8','9','70','80','90','700','800','900','7000','8000','9000'])

share|improve this answer
1  
Save 4 bytes using z=z.slice(0,-x.length)+x,z='' (or variable name of your choice). – Neil Feb 12 at 10:27
    
@Neil. Nice one! I knew there should have a way to do that – removed Feb 12 at 10:36
    
(Sorry for miscounting the saving.) Also, the string version is unnecessary as it turns out that s=>s.split` `.map( is 2 bytes (I double-checked this time) shorter than s=>s.replace(/\d+/g,. – Neil Feb 12 at 10:45
    
@Neil. Valid point. I just leaved it there because was my first goal when answering... but you are right – removed Feb 12 at 10:46

Retina, 45 bytes

+`(?=(?<1>\d)+)(?<=(\d)(?(1)x)(?<-1>\d)+ )
$1

Uses balancing groups to count the digits which costs a lot. Haven't found a better approach yet but I'm interested in it.

Try it online here.

share|improve this answer

Gema, 35 characters

<D>=@set{p;@fill-right{${p;};$0}}$p

Input: string with numbers separated by anything, output string.

Sample run:

bash-4.3$ gema '<D>=@set{p;@fill-right{${p;};$0}}$p' <<< '123 45 6 7 89 200'
123 145 146 147 189 200
share|improve this answer

Ruby, 39 characters

->a{l='';a.map{|c|l=l[0..-c.size-1]+c}}

Input: array of strings, output: array of strings.

Sample run:

2.1.5 :001 > ->a{l='';a.map{|c|l=l[0..-c.size-1]+c}}[%w{123 45 6 7 89 200}]
 => ["123", "145", "146", "147", "189", "200"] 
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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