Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

an algorithm to store '{' and '}' in two arrays in javascript

I am trying to make an algorithm. The inputs may be like the following.

- a{1}b{1}c{1}
- a{a{1}b{1}c{1}}
- a{a{a{1}b{1}}b{b{1}c{1}}d{1}}

And I am having two arrays named as "ys" and "ns". I need to store the starting braces '{' in the array "ys" and ending braces '}' in the array "ns". But the challenge is to store the position of the starting and ending braces in the input string to be same corresponding position in the two arrays. For example, consider the 2nd input. The output must be like this.

ys[0]=>1, ys[1]=>3, ys[2]=>7, ys[3]=>11.
ns[0]=>14, ns[1]=>5, ns[2]=>9, ns[3]=>13.

My code:

var lastPoint = line.pathPoints[line.pathPoints.length - 1].anchor; // [x, y]

var eqValues = (prompt("Enter something ", "", "")); //getting the values to write
var eqValuesCopy = eqValues;


    var nFlag=0;
    for(var i=0;i<eqValuesCopy.length;i++){
        if(eqValuesCopy[i]=="{"){
            if(i!=0){
                validBCheck=eqValuesCopy[i-1]+eqValuesCopy[i];
                if(validBCheck!="/{"){
                    ys.push(i);
                }
            }
        }
        ysLength=ys.length;
    }
    for(var i=0;i<eqValuesCopy.length;i++){
        if(eqValuesCopy[i]=="{"){
            nsLength=ns.length;
            var j=i;
            while(j<eqValuesCopy.length){
                j++;
                if(nsLength<ysLength){
                    if(eqValuesCopy[j]=="}"){
                        ns[nsLength]=j;
                        break;
                    }else if(eqValuesCopy[j]=="{"){
                         nsLength++;
                    }else{

                    }
                }
            /*else{ 
                    for(var k=0;k<=ysLength;k++){
                           // alert(ns[k]);
                        if(ns[k]==undefined){
                            ns[k]=j;
                            //alert(ns[k]);
                        }
                    }
                }*/
            }
        }
    }

I am writing this program in javascript inside illustrator. Only thing is I couldn't make the logic for this algorithm.

like image 515
Raj Avatar asked Nov 23 '25 06:11

Raj


2 Answers

Here is a solution you can refer. Not using more javascript array functions since it will be easy to understand the logic if need to convert to other languages

function parseString(s){
var ys = [];
var id = [];
var k = 0;
var ns = [];
s = s.split('');

for (var i = 0; i < s.length; i++) {
	if (s[i] == '{') {
		ys.push(i);
		id.push(ys.length - 1);
	}
	if (s[i] == '}') {
		ns[id[id.length - 1]] = i;
		id.pop();
	}
}
console.log(ys);
console.log(ns);
}

parseString('a{1}b{1}c{1}');
parseString('a{a{1}b{1}c{1}}');
parseString('a{a{a{1}b{1}}b{b{1}c{1}}d{1}}');
like image 132
Vineesh Avatar answered Nov 25 '25 20:11

Vineesh


This might work

    for(var i=0;i<eqValuesCopy.length;i++){
        if(eqValuesCopy[i]=="{"){
            if(i!=0){
                validBCheck=eqValuesCopy[i-1]+eqValuesCopy[i];
                if(validBCheck!="/{"){
                    ys.push(i);
                }
            }
        }
        ysLength=ys.length;
    }
    for(var i=0;i<eqValuesCopy.length;i++){
        nsLength=ns.length;
        if(nsLength<ysLength){
            if(eqValuesCopy[i]=="{"){
                var j=i;
                while(j<eqValuesCopy.length){
                    j++;
                    if(eqValuesCopy[j]=="}"){
                        ns[nsLength]=j;
                        break; 
                    }else if(eqValuesCopy[j]=="{"){ 
                        nsLength++;
                    }else{ 

                    }
                }  
                i=j;
            }
        }
    }
    for(var k=0;k<eqValuesCopy.length;k++){
        if(eqValuesCopy[k]=="}"){
            var h=k;
            while(h<eqValuesCopy.length){
                h++;
                if(eqValuesCopy[h]=="{"){
                    break;
                }else if(eqValuesCopy[h]=="}"){ 
                    nsDummy.push(h);
                    break;
                }else{ 

                }
            }
        }
    }
nsDummy.reverse();
for(var x=0;x<nsLength;x++){
    if(ns[x]===undefined){
        ns[x]=nsDummy[x];
    }
}
like image 24
Raru Avatar answered Nov 25 '25 19:11

Raru



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!