18
\$\begingroup\$

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.)

  • there will be at least one number in the input.

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
42
42
\$\endgroup\$
2
  • \$\begingroup\$ Challenge is pretty old, but a) can the input be empty? b) can the input contain only one number? \$\endgroup\$ Commented Dec 28, 2018 at 18:52
  • \$\begingroup\$ @EriktheOutgolfer I'll go ahead and say there will be ≥1 numbers in the input. \$\endgroup\$
    – Doorknob
    Commented Dec 28, 2018 at 19:23

6 Answers 6

7
\$\begingroup\$

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.
\$\endgroup\$
0
5
\$\begingroup\$

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

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Save 4 bytes using z=z.slice(0,-x.length)+x,z='' (or variable name of your choice). \$\endgroup\$
    – Neil
    Commented Feb 12, 2016 at 10:27
  • \$\begingroup\$ @Neil. Nice one! I knew there should have a way to do that \$\endgroup\$
    – removed
    Commented Feb 12, 2016 at 10:36
  • \$\begingroup\$ (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,. \$\endgroup\$
    – Neil
    Commented Feb 12, 2016 at 10:45
  • \$\begingroup\$ @Neil. Valid point. I just leaved it there because was my first goal when answering... but you are right \$\endgroup\$
    – removed
    Commented Feb 12, 2016 at 10:46
1
\$\begingroup\$

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.

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

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

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"] 
\$\endgroup\$
0
\$\begingroup\$

Python 2, 58 bytes

s=''
for i in input():s=s[:max(len(s)-len(i),0)]+i;print s

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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