среда, 14 ноября 2012 г.

DNA Mutate

function in_array (item, array){
    for (var i = 0; i < array.length; i++) {
        if (array[i] === item) {
            return true;
        }
    }
    return false;
}

function getRandomInt(min, max){
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

var Cell = function(){};
    Cell.prototype = {
        DNA: [],
        init: function(){
            var dna_letters = ["A", "T", "G", "C"];
            for (var i = 0; i < 100; i++) {
                this.DNA[i] = dna_letters[getRandomInt(0, 3)];
            }
        },
        hammingDistance: function(cell_object) {
            var distance = 0;
            for (var i = 0; i < 100; i++) {
                if (this.DNA[i] != cell_object.DNA[i]) {
                    distance++;
                }
            }
            return distance;
        },
        mutate: function(x){
            var mutated_cells = [];
            var dna_position;
            var number_of_mutations = x;
            var dna_letters = ["A", "T", "G", "C"];
            while (number_of_mutations > 0) {
                dna_position = getRandomInt(0, 99);
                if (in_array(dna_position, mutated_cells)) {
                    continue;
                } else {
                    this.DNA[dna_position] = dna_letters[getRandomInt(0, 3)];
                    mutated_cells.push(dna_position);
                    number_of_mutations--;
                }
            }
        }
    }

var FirstCell = new Cell();
    FirstCell.init();
  
var SecondCell = new Cell();
    SecondCell.init();

console.log(FirstCell.hammingDistance(SecondCell));

FirstCell.mutate(10);
SecondCell.mutate(20);

console.log(FirstCell.hammingDistance(SecondCell));

Комментариев нет:

Отправить комментарий