arrays - "Look and say sequence" in javascript - Stack Overflow

1 11121121122111112213122211....I was trying to solve this problem. It goes like this.I need to check th

1
11
12
1121
122111
112213
122211
....
I was trying to solve this problem. It goes like this. I need to check the former line and write: the number and how many time it was repeated.
ex. 1 -> 1(number)1(time)

var antsArr = [[1]];
var n = 10;

for (var row = 1; row < n; row++) { 
  var lastCheckedNumber = 0;
  var count = 1;
  antsArr[row] = [];

  for (var col = 0; col < antsArr[row-1].length; col++) {

    if (lastCheckedNumber == 0) {   
        lastCheckedNumber = 1;
        antsArr[row].push(lastCheckedNumber);

    } else {

        if (antsArr[row-1][col] == lastCheckedNumber) {
            count++;

        } else {
            lastCheckedNumber = antsArr[row-1][col];
        }

    }

  }
  antsArr[row].push(count);
  antsArr[row].push(lastCheckedNumber);
}



for (var i = 0; i < antsArr.length; i++) {
  console.log(antsArr[i]);
}

I have been on this since 2 days ago. It it so hard to solve by myself. I know it is really basic code to you guys. But still if someone who has a really warm heart help me out, I will be so happy! :>

1
11
12
1121
122111
112213
122211
....
I was trying to solve this problem. It goes like this. I need to check the former line and write: the number and how many time it was repeated.
ex. 1 -> 1(number)1(time)

var antsArr = [[1]];
var n = 10;

for (var row = 1; row < n; row++) { 
  var lastCheckedNumber = 0;
  var count = 1;
  antsArr[row] = [];

  for (var col = 0; col < antsArr[row-1].length; col++) {

    if (lastCheckedNumber == 0) {   
        lastCheckedNumber = 1;
        antsArr[row].push(lastCheckedNumber);

    } else {

        if (antsArr[row-1][col] == lastCheckedNumber) {
            count++;

        } else {
            lastCheckedNumber = antsArr[row-1][col];
        }

    }

  }
  antsArr[row].push(count);
  antsArr[row].push(lastCheckedNumber);
}



for (var i = 0; i < antsArr.length; i++) {
  console.log(antsArr[i]);
}

I have been on this since 2 days ago. It it so hard to solve by myself. I know it is really basic code to you guys. But still if someone who has a really warm heart help me out, I will be so happy! :>

Share Improve this question edited Jul 5, 2015 at 4:27 Sunyoung Kim asked Jul 4, 2015 at 16:39 Sunyoung KimSunyoung Kim 1993 silver badges12 bronze badges 3
  • Can you change the title into "Look-and-say sequence". And than describe the problem. Thanks! – user3589620 Commented Jul 4, 2015 at 22:40
  • And you don't need to write the programming language in the title, because everyone sees it in the tag! :-) Have a nice day! – user3589620 Commented Jul 5, 2015 at 13:01
  • The last member of the sequence in your explanation must be 12221131. – user3589620 Commented Jul 5, 2015 at 13:14
Add a ment  | 

3 Answers 3

Reset to default 3

Try this:

JSFiddle Sample

function lookAndSay(seq){
    var prev = seq[0];
    var freq = 0;
    var output = [];
    seq.forEach(function(s){
        if (s==prev){
            freq++;
        }
        else{
            output.push(prev);
            output.push(freq);
            prev = s;
            freq = 1;
        }
    });
    output.push(prev);
    output.push(freq);
    console.log(output);
    return output;
}

// Sample: try on the first 11 sequences
var seq = [1];
for (var n=0; n<11; n++){
    seq = lookAndSay(seq);
}

Quick explanation

The input sequence is a simple array containing all numbers in the sequence. The function iterates through the element in the sequence, count the frequency of the current occurring number. When it encounters a new number, it pushes the previously occurring number along with the frequency to the output.

Keep the iteration goes until it reaches the end, make sure the last occurring number and the frequency are added to the output and that's it.

I am not sure if this is right,as i didnt know about this sequence before.Please check and let me know if it works.

var hh=0;
function ls(j,j1)
{
     var l1=j.length;
     var fer=j.split('');
     var str='';
     var counter=1;
     for(var t=0;t<fer.length;t++)
     { 
         if(fer[t]==fer[t+1])
            {
                   counter++;            
            }
            else
            {
                   str=str+""+""+fer[t]+counter;
                     counter=1;
            }    
    }
    console.log(str);
    while(hh<5)   //REPLACE THE NUMBER HERE TO CHANGE NUMBER OF COUNTS!
    {
         hh++;
         //console.log(hh);
         ls(str);
    }
}

ls("1");

You can check out the working solution for in this fiddle here

You can solve this by splitting your logic into different modules.
So primarily you have 2 tasks -

  1. For a give sequence of numbers(say [1,1,2]), you need to find the frequency distribution - something like - [1,2,2,1] which is the main logic.
  2. Keep generating new distribution lists until a given number(say n).

So split them into 2 different functions and test them independently.

For task 1, code would look something like this -

 /*
 This takes an input [1,1,2] and return is freq - [1,2,2,1]
 */
 function find_num_freq(arr){
    var freq_list = [];
    var val = arr[0];
    var freq = 1;
    for(i=1; i<arr.length; i++){
        var curr_val = arr[i];
        if(curr_val === val){
            freq += 1;
        }else{
            //Add the values to the freq_list
            freq_list.push([val, freq]);
            val = curr_val;
            freq = 1;
        }
    }
    freq_list.push([val, freq]);

    return freq_list;
}

For task 2, it keeps calling the above function for each line of results. It's code would look something like this -

function look_n_say(n){
    //Starting number
    var seed = 1;
    var antsArr = [[seed]];

    for(var i = 0; i < n; i++){
        var content = antsArr[i];
        var freq_list = find_num_freq(content);
        //freq_list give an array of [[ele, freq],[ele,freq]...]
        //Flatten so that it's of the form - [ele,freq,ele,freq]
        var freq_list_flat = flatten_list(freq_list);
        antsArr.push(freq_list_flat);
    }
    return antsArr;
}

/**
This is used for flattening a list.
Eg - [[1],[1,1],[1,2]] => [1,1,1,1,2]
basically removes only first level nesting
**/
function flatten_list(li){
    var flat_li = [];
    li.forEach(
        function(val){
            for(ind in val){
                flat_li.push(val[ind]);
            }
        }
    );
    return flat_li;
}

The output of this for the first 10 n values -

OUTPUT
n = 1:
[[1],[1,1]]


n = 2:
[[1],[1,1],[1,2]]


n = 3:
[[1],[1,1],[1,2],[1,1,2,1]]


n = 4:
[[1],[1,1],[1,2],[1,1,2,1],[1,2,2,1,1,1]]


n = 5:
[[1],[1,1],[1,2],[1,1,2,1],[1,2,2,1,1,1],[1,1,2,2,1,3]]


n = 6:
[[1],[1,1],[1,2],[1,1,2,1],[1,2,2,1,1,1],[1,1,2,2,1,3],[1,2,2,2,1,1,3,1]]


n = 7:
[[1],[1,1],[1,2],[1,1,2,1],[1,2,2,1,1,1],[1,1,2,2,1,3],[1,2,2,2,1,1,3,1],[1,1,2,3,1,2,3,1,1,1]]


n = 8:
[[1],[1,1],[1,2],[1,1,2,1],[1,2,2,1,1,1],[1,1,2,2,1,3],[1,2,2,2,1,1,3,1],[1,1,2,3,1,2,3,1,1,1],[1,2,2,1,3,1,1,1,2,1,3,1,1,3]]


n = 9:
[[1],[1,1],[1,2],[1,1,2,1],[1,2,2,1,1,1],[1,1,2,2,1,3],[1,2,2,2,1,1,3,1],[1,1,2,3,1,2,3,1,1,1],[1,2,2,1,3,1,1,1,2,1,3,1,1,3],[1,1,2,2,1,1,3,1,1,3,2,1,1,1,3,1,1,2,3,1]]

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744863631a4597848.html

相关推荐

  • arrays - &quot;Look and say sequence&quot; in javascript - Stack Overflow

    1 11121121122111112213122211....I was trying to solve this problem. It goes like this.I need to check th

    1天前
    80

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信