2
\$\begingroup\$

Taken from StackOverflow Question

Challenge

Given an encoded string in format n[s] where n is the amount of times s will be repeated, your task is to output the corresponding decoded string.

  • n will always be a positive number
  • s can not contain empty spaces
  • strings only contains letters from the alphabet
  • s length can be >= 1
  • encoded format is number [ string ]
  • string can have nested encoded string number [ number [ string ] ]
  • return the same string if not encoded

Examples

2[a]                 => aa
3[2[a]]              => aaaaaa
2[b3[a]]             => baaabaaa
3[a]5[r3[c]2[b]]a    => aaarcccbbrcccbbrcccbbrcccbbrcccbba
a                    => a  // Not Encoded
3                    => 3  // Not Encoded
3a3[b]               => 3abbb
10[b20[a]]           => baaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaa
3[hello]             => hellohellohello

Other test cases

a[a] => a[a] 
a1[a] => aa
3[] =>    // Nothing because are 3 empty string
2[2][a] => 2[2][a]

This is my own submission.

f=i=>(r=/(\d+)\[([a-z]*)\]/g).test(l=i.replace(r,(m,n,s)=>s.repeat(n)))?f(l):l

console.log(f("2[a]"))
console.log(f("3[2[a]]"))
console.log(f("2[b3[a]]"))
console.log(f("3[a]5[r3[c]2[b]]a"))
console.log(f("a"))
console.log(f("3"))
console.log(f("3a3[b]"))
console.log(f("10[b20[a]]"))
console.log(f("3[hello]"))


If more test cases needed please tell me.

\$\endgroup\$
  • 3
    \$\begingroup\$ Usually, someone who suggests a challenge, first tries putting it on the sandbox and doesn't answer it before a few days... \$\endgroup\$ – Olivier Grégoire Jun 11 '18 at 18:21
  • 7
    \$\begingroup\$ Numbers can apparently be part of the strings, so what about: 12[a]? aaaaaaaaaaaa or 1aa? And why? \$\endgroup\$ – Stewie Griffin Jun 11 '18 at 18:25
  • 3
    \$\begingroup\$ Why is 2[2][a] returned as unmodified string? Shouldn't it go 2[2][a] -> 22[a] -> aaaaaaaaaaaaaaaaaaaaaa? Or can string only contain lower case alphabet? \$\endgroup\$ – JungHwan Min Jun 11 '18 at 18:34
  • 3
    \$\begingroup\$ This looks mostly good, but the 3[] test case is unclear to me: if the length of s is guaranteed to be at least 1, why is there a test case that has an s of length 0? \$\endgroup\$ – ETHproductions Jun 12 '18 at 3:03
  • 4
    \$\begingroup\$ Very similar \$\endgroup\$ – ETHproductions Jun 12 '18 at 6:16
2
\$\begingroup\$

QuadR, 17 + flag = 28 bytes

\d+\[\pL*]
∊(⍎⍵M∩⎕D)⍴⊂⍵M∩⎕A

Try it online!

Find:

\d+\[\pL*] find digit(s) followed by bracketed Letters

Replace with:

⎕A uppercase Alphabet

⍵M∩ intersection of Match and that

 enclose (to treat as a whole)

()⍴reshape to length:

  ⎕DDigits

  ⍵M∩ intersection of Match and that

   execute (converts text to number)

ϵnlist (flatten)


This is equivalent to the Dyalog APL function:

'\d+\[\pL*]'⎕R{∊(⍎⍵.Match∩⎕D)⍴⊂⍵.Match∩⎕A}⍣≡
\$\endgroup\$
1
\$\begingroup\$

Perl 6,  44  36 bytes

{({S{(\d+)\[(<:L>*)\]}=$1 x$0}...*eq*).tail}

Test it

Nil while s{(\d+)\[(<:L>*)\]}=$1 x$0

Test it (with -p)

Expanded:

Nil                   # silence warning by using Nil rather than 0

while                 # do the above while the following is truish

  s {                 # string replace (implicitly on $_)

    ( \d+ )           # capture a series of digits into $0
    \[
      ( <:L>* )       # capture letters
    \]

  } = $1 x $0         # replace it with the string repeated by the number
\$\endgroup\$
1
\$\begingroup\$

Retina, 24 bytes

+`(\d+)\[(\p{L}*)]
$1*$2

Try it online!

Updated to support additional test cases. -1 thanks to @Adám!

\$\endgroup\$
  • 1
    \$\begingroup\$ Fails on "3[]" and "aaaaaaaaaaaaaaaaaaaaaa". (Should give "" and "2[2][a]".) Replacing \w+ with \p{L}* fixes these. \$\endgroup\$ – Adám Jun 11 '18 at 19:03
  • 1
    \$\begingroup\$ I think you can remove the \ before ]. \$\endgroup\$ – Adám Jun 11 '18 at 19:09
  • \$\begingroup\$ I don't know retina, but is changing \p{L} to \w valid? \$\endgroup\$ – tsh Jun 12 '18 at 6:35
  • 1
    \$\begingroup\$ @tsh That would be invalid for 2[2][a] \$\endgroup\$ – Mr. Xcoder Jun 12 '18 at 6:36
  • \$\begingroup\$ @tsh I had that before (when that test case didn't exist) and as Mr. Xcoder correctly points out, that test case invalidates it... \$\endgroup\$ – Dom Hastings Jun 12 '18 at 7:11
0
\$\begingroup\$

Perl 5, 36 bytes

0while s/(\d+)\[([a-z]*)]/$2x$1/e
\$\endgroup\$
0
\$\begingroup\$

Japt v2.0a0, 19 bytes

e/(\d+).(\l+)]/@ZpY

Try it

\$\endgroup\$
  • \$\begingroup\$ "[11a]" produces "[1a" \$\endgroup\$ – recursive Jun 12 '18 at 5:19
  • \$\begingroup\$ @recursive, according to the spec, that wouldn't be valid input; square brackets can only contain letters. \$\endgroup\$ – Shaggy Jun 12 '18 at 9:59
  • \$\begingroup\$ "2[2][a]" is given as a test case. I interpret that to mean that the pattern should apply iff the square braces only contain letters. \$\endgroup\$ – recursive Jun 12 '18 at 13:14
0
\$\begingroup\$

Stax, 21 bytes

è/g1┘+α╣m?╣t╥ó⌂ß─"H║à

Run and debug it

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

ReRegex, 83 bytes

#import base
(\d+)\[([a-z]+)]/Au<$1>,$2,B/_,([a-z]+),/,$1,$1/A,.*?,(.*?)B/$1/#input

ReRegex is a language designed around regex. Sadly, it doesn't really understand numbers. So repeating the string is the hardest part of this.

Try it online!

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

Red, 158 bytes

func[s][b: charset[not"[]"]d: charset"0123456789"until[r: on
parse s[to remove[copy n any d"["copy t any b"]"](insert/dup
c: copy""t do n r: off)insert c]r]s]

Try it online!

More readable:

f: func [ s ] [
    b: charset [ not "[]" ]
    d: charset "0123456789"
    until [
        r: on
        parse s [
            to remove [ copy n some d "[" copy t any b "]" ]
                ( insert/dup c: copy "" t do n
                r: off )
            insert c ]
        r
    ]
    s
]
\$\endgroup\$

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