# Please help me to find a easiest Algorithm for this case

**URL:** <https://discourse.julialang.org/t/please-help-me-to-find-a-easiest-algorithm-for-this-case/46186>\
**Category:** General Usage\
**Tags:** question, strings, function\
**Created:** [September 7, 2020, 8:40am UTC](https://discourse.julialang.org/t/please-help-me-to-find-a-easiest-algorithm-for-this-case/46186 "2020-09-07T08:40:20Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![xp-gamer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xp-gamer/32/21545_2.png) [@xp-gamer](https://discourse.julialang.org/u/xp-gamer)\
**Post date:** [September 7, 2020, 8:40am UTC](https://discourse.julialang.org/t/please-help-me-to-find-a-easiest-algorithm-for-this-case/46186/1 "2020-09-07T08:40:20Z")

</div>

I am working on strings on Julia language. I working on a function where input string format is need to be converted to respective output mentioned below, please help me in finding a easier algorithm for this condition.

**Input example:**  
  
“aaa[fffff[kkkkk;lllll];ooooo;gggg[nnnnnnn;bbbbbbb]]”  
  
  
  
**Output should be:**  
  
  
  
Array{}  
  
[  
  
“aaafffffkkkkk”,  
  
“aaaffffflllll”,  
  
“aaaooooo”,  
  
“aaaggggnnnnnnn”,  
  
“aaaggggbbbbbbb”  
  
]

Thanks

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 7, 2020, 9:15am UTC](https://discourse.julialang.org/t/please-help-me-to-find-a-easiest-algorithm-for-this-case/46186/2 "2020-09-07T09:15:35Z")

</div>

What have you tried so far? Out of curiosity, what are you using this for?

Also, please mention cross-posting: [https://stackoverflow.com/questions/63771842/please-help-me-to-find-a-easiest-algorithm-for-this-case](https://stackoverflow.com/questions/63771842/please-help-me-to-find-a-easiest-algorithm-for-this-case)

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [September 7, 2020, 9:49am UTC](https://discourse.julialang.org/t/please-help-me-to-find-a-easiest-algorithm-for-this-case/46186/3 "2020-09-07T09:49:57Z")

</div>

Ok, I got [nerd sniped](https://xkcd.com/356/) 😄. It’s not that difficult to write parsers for simple languages like this, with just a little bit of recursion:

```julia
parse(s) = _parse!(IOBuffer(s), [""], 0)

function _parse!(io, a, nesting)
    s = a[end]
    while !eof(io)
        c = read(io, Char)
        if c == ']'
            if nesting == 0
                error("Unexpected \"]\"")
            else
                break
            end
        elseif c == ';'
            push!(a, s)
        elseif c == '['
            _parse!(io, a, nesting + 1)
        else
            a[end] *= c
        end
    end
    return a
end

```

Your example:

```julia
julia> parse("aaa[fffff[kkkkk;lllll];ooooo;gggg[nnnnnnn;bbbbbbb]]")
5-element Array{String,1}:
 "aaafffffkkkkk"
 "aaaffffflllll"
 "aaaooooo"
 "aaaggggnnnnnnn"
 "aaaggggbbbbbbb"

```

Edit: slightly more efficient implementation.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 7, 2020, 10:37am UTC](https://discourse.julialang.org/t/please-help-me-to-find-a-easiest-algorithm-for-this-case/46186/4 "2020-09-07T10:37:55Z")

</div>

> [@simeonschaub](#):
>
> Ok, I got [nerd sniped](https://xkcd.com/356/) 😄. It’s not that difficult to write parsers for simple languages like this, with just a little bit of recursion:

Full marks on the homework 😉

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [September 7, 2020, 11:25am UTC](https://discourse.julialang.org/t/please-help-me-to-find-a-easiest-algorithm-for-this-case/46186/5 "2020-09-07T11:25:41Z")

</div>

A longer version ☹ :

```julia
struct Node
    prefix::String
    childs::Array{Node,1}
end

function parseNode(s)
    parRanges = findall(x -> x == '[' || x == ']',s)
    if length(parRanges)>0
        pref = s[1:parRanges[1]-1]
        nextString = s[parRanges[1]+1:parRanges[end]-1]
        subStrings = String[]
        openSquared = 0
        lastStripPos = 1
        for (i,c) in enumerate(nextString)
            if c == '[' openSquared += 1; end
            if c == ']' openSquared -= 1; end
            if openSquared == 0 && c == ';'
                sString = nextString[lastStripPos:i-1]
                push!(subStrings,sString)
                lastStripPos = i+1
            end
        end
        if lastStripPos <= length(nextString)
            push!(subStrings,nextString[lastStripPos:end])
        end
        return Node(pref,parseNode.(subStrings))
    else
        Node(s,String[])
    end
end

function joinNode(n)
    toReturn = String[]
    if length(n.childs) >0
        v = n.prefix
        for c in n.childs
            for cchild in joinNode(c)
                push!(toReturn,v*cchild)
            end
        end
        return toReturn
    else
        return [n.prefix]
    end
end

s = "aaa[fffff[kkkkk;lllll];ooooo;gggg[nnnnnnn;bbbbbbb]]"
rootNode = parseNode(s);
out = joinNode(rootNode)

```

```julia
julia> out
5-element Array{String,1}:
 "aaafffffkkkkk"
 "aaaffffflllll"
 "aaaooooo"
 "aaaggggnnnnnnn"
 "aaaggggbbbbbbb"

```

---

<div class="post-metadata">

**Author:** ![xp-gamer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xp-gamer/32/21545_2.png) [@xp-gamer](https://discourse.julialang.org/u/xp-gamer)\
**Post date:** [September 8, 2020, 4:58am UTC](https://discourse.julialang.org/t/please-help-me-to-find-a-easiest-algorithm-for-this-case/46186/6 "2020-09-08T04:58:12Z")

</div>

thanks man

---

<div class="post-metadata">

**Author:** ![xp-gamer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xp-gamer/32/21545_2.png) [@xp-gamer](https://discourse.julialang.org/u/xp-gamer)\
**Post date:** [September 8, 2020, 4:59am UTC](https://discourse.julialang.org/t/please-help-me-to-find-a-easiest-algorithm-for-this-case/46186/7 "2020-09-08T04:59:00Z")

</div>

thanks bro

---

<div class="post-metadata">

**Author:** ![xp-gamer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xp-gamer/32/21545_2.png) [@xp-gamer](https://discourse.julialang.org/u/xp-gamer)\
**Post date:** [September 8, 2020, 5:00am UTC](https://discourse.julialang.org/t/please-help-me-to-find-a-easiest-algorithm-for-this-case/46186/8 "2020-09-08T05:00:08Z")

</div>

thanks bro i was expecting this function kind off solution, thanks it helped me a lot

---

<div class="post-metadata">

**Author:** ![xp-gamer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xp-gamer/32/21545_2.png) [@xp-gamer](https://discourse.julialang.org/u/xp-gamer)\
**Post date:** [September 8, 2020, 5:07am UTC](https://discourse.julialang.org/t/please-help-me-to-find-a-easiest-algorithm-for-this-case/46186/9 "2020-09-08T05:07:12Z")

</div>

can u explain u code, caz i am new to julia, it would be easier to understand if u explain like i am lame and i don’t most of ur code, pls

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [September 8, 2020, 10:03pm UTC](https://discourse.julialang.org/t/please-help-me-to-find-a-easiest-algorithm-for-this-case/46186/11 "2020-09-08T22:03:39Z")

</div>

The problem posed seems to be equivalent to the string product:

```julia
 "aaa".*["fffff".*["kkkkk";"lllll"];"ooooo";"gggg".*["nnnnnnn";"bbbbbbb"]]

```

This could be evaluated with metaprogramming after formatting the input as per expression above. See an attempt that seems to be working for type of input indicated:

```julia
str = "aaa[fffff[kkkkk;lllll]; ooooo; gggg[nnnnnnn;bbbbbbb] ]"
s0 = filter(x -> !isspace(x), str) # remove spaces
s0 = replace(s0,"["=>"\".*[\"") # add quotes and string product
s0 = replace(s0,";"=>"\";\"") # add string quotes
s0 = replace(s0,"]"=>"\"]") # add inner quote before ]
s0 = replace(s0,"]\""=>"]") # remove quote surplus
s0 = "\"" * s0 # add leading quote
println(s0) # Julia REPL outputs strings in escaped form, use println to QC
eval(Meta.parse(s0))

```
