Ghoul Fool
Ghoul Fool

Reputation: 6949

Split string into array with alternate one & two digits

Is it possible to split a string (possibly with regex) so that every other number is a pair(starting from the right hand end)?

//             1                [1]
//            12               [12]
//           123             [1,23]
//          1234           [1,2,34]
//         12345          [12,3,45]
//        123456        [1,23,4,56]
//       1234567      [1,2,34,5,67]
//      12345678     [12,3,45,6,78]
//     123456789   [1,23,4,56,7,89]
//    1234567890 [1,2,34,5,67,8,90]

I tried reversing the string & then adding blocks of alternating two and one characters till the end (front) of the string. Then reversing it again. That mainly worked but it was flawed (didn't work for all cases). I also tried the regex

(\d\d)(\d)(\d\d)(\d)(\d\d)(\d)

But that doesn't work either (only in the regex tester ironically) - it's too long but I'll need it to work for 10 digit numbers max.

Upvotes: 3

Views: 291

Answers (2)

Ghoul Fool
Ghoul Fool

Reputation: 6949

I got there in the end.

//             1                [1]
//            12               [12]
//           123             [1,23]
//          1234           [1,2,34]
//         12345          [12,3,45]
//        123456        [1,23,4,56]
//       1234567      [1,2,34,5,67]
//      12345678     [12,3,45,6,78]
//     123456789   [1,23,4,56,7,89]
//    1234567890 [1,2,34,5,67,8,90]

var results = "";

pairUp(1)
pairUp(12)
pairUp(123)
pairUp(1234)
pairUp(12345)
pairUp(123456)
pairUp(12345678)
pairUp(123456789)
pairUp(1234567890)

alert(results);

function pairUp(num)
{
  var s = num.toString();
  s = s.split("").reverse().join("");
  var a = s.split("");

  var r = []; // our results array
  count = 0;

  for (var i = 0; i <= a.length -1; i++)
  {
    temp = a[count];

    if ((i % 2) == 0) // even (0, 2, 4)
    {
      var p = a[count+1];
      var q = a[count];

      if (p == undefined) p = "";
      if (q) r.push(p + q+ "");

      count+=2;
    }
    else 
    {
      if (temp != undefined) r.push(temp + "");
      count+=1;
    }
  } // end loop


  r = r.reverse();

  results+= r + "\n";

} // end pair up

Upvotes: 1

Aminadav Glickshtein
Aminadav Glickshtein

Reputation: 24590

No so hard:

Start from the right, I will take one time 2 digits, the second time 1 digits. Using slice. Then I will use unshift to push it in the begining of the array.

I'm using a flag, to know when to take only 1 parameter, and when to take two parameters (pair flag)

m(1)
m(12)
m(123)
m(1234)
m(12345)
m(123456)
m(12345678)
m(123456789)
m(1234567890)
function m(x){
    x=x.toString()
    var a=[]
    var v;
    var y=2
    while(x){
        v=x.slice(-y)
        x=x.slice(0,-y)        
        y=y==1? 2:1
        a.unshift(v)
    }
    console.log(a)
}

Result:

["1"]
["12"]
["1", "23"]
["1", "2", "34"]
["12", "3", "45"]
["1", "23", "4", "56"]
["12", "3", "45", "6", "78"]
["1", "23", "4", "56", "7", "89"]
["1", "2", "34", "5", "67", "8", "90"]

Upvotes: 3

Related Questions