Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Input
- //
- // 11111
- // 3
- // Output
- //
- // let input = [
- // '00000',
- // '11111',
- // '3'
- // ]; //11100
- // ________________________________________
- // let input = [
- // '11111',
- // '10101',
- // '3'
- // ]; //11101
- // ________________________________________
- // let input = [
- // '10010',
- // '10101',
- // '3'
- // ]; // 10110
- // ________________________________________
- let input = [
- '00000',
- '10001',
- '3'
- ]; //out:10010..mutation (flipping bits) 11100
- let print = this.print || console.log;
- let gets = this.gets || ((arr, index) => () => arr[index++])(input, 0);
- let parent1 = gets().split('');
- let parent2 = gets().split('');
- let splitPoint = gets();
- let parentLenth = Number(parent1.length); //Both parents have the same length
- let result = '';
- let child1 = (parent1.slice(0, splitPoint) + ',' + parent2.slice(splitPoint)).split(',');
- let child2 = (parent2.slice(0, splitPoint) + ',' + parent1.slice(splitPoint)).split(',');
- let child1OneNumbers = 0;
- let child2OneNumbers = 0;
- for (let i = 0; i <= parentLenth; i++) {
- if (child1[i] === '1') {
- child1OneNumbers++
- }
- if (child2[i] === '1') {
- child2OneNumbers++
- }
- }
- let fitnessLevelChild1 = child1OneNumbers / parentLenth;
- let fitnessLevelChild2 = child2OneNumbers / parentLenth;
- if (fitnessLevelChild1 < 0.5 && fitnessLevelChild2 < 0.5) {
- for (let j = 0; j < parentLenth; j++) {
- if (child1[j] === '0') {
- child1[j] = 1
- }
- if (child1[j] === '1') {
- child1[j] = 0
- }
- if (child2[j] === '0') {
- child2[j] = 1
- }
- if (child2[j] === '1') {
- child2[j] = 0
- }
- }
- if (fitnessLevelChild1 === fitnessLevelChild2) {
- result = child1.join('') + ' - ' + "child1"
- } else if (fitnessLevelChild1 > fitnessLevelChild2) {
- result = child1.join('') + ' - ' + "child1"
- } else {
- result = child2.join('') + ' - ' + "child2"
- }
- } else if (fitnessLevelChild1 > fitnessLevelChild2) {
- result = child1.join('') + ' - ' + "child1";
- } else if (fitnessLevelChild1 < fitnessLevelChild2) {
- result = child2.join('') + ' - ' + "child2";
- } else if (fitnessLevelChild1 === fitnessLevelChild2) {
- result = child1.join('') + ' - ' + "child1(много грешна работа, да оставим от 2 деца //same fitnessLevels// само 1-вото)";
- }
- console.log(result);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement