Advertisement
3vo

Genetics

3vo
Nov 22nd, 2022
571
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Input
  2. //
  3. // 11111
  4. // 3
  5. // Output
  6. //
  7.  
  8. // let input = [
  9. //     '00000',
  10. //     '11111',
  11. //     '3'
  12. // ]; //11100
  13. // ________________________________________
  14. // let input = [
  15. //     '11111',
  16. //     '10101',
  17. //     '3'
  18. // ]; //11101
  19. // ________________________________________
  20. // let input = [
  21. //     '10010',
  22. //     '10101',
  23. //     '3'
  24. // ]; // 10110
  25. // ________________________________________
  26. let input = [
  27.     '00000',
  28.     '10001',
  29.     '3'
  30. ]; //out:10010..mutation (flipping bits) 11100
  31.  
  32.  
  33. let print = this.print || console.log;
  34. let gets = this.gets || ((arr, index) => () => arr[index++])(input, 0);
  35.  
  36.  
  37. let parent1 = gets().split('');
  38. let parent2 = gets().split('');
  39. let splitPoint = gets();
  40. let parentLenth = Number(parent1.length); //Both parents have the same length
  41. let result = '';
  42.  
  43. let child1 = (parent1.slice(0, splitPoint) + ',' + parent2.slice(splitPoint)).split(',');
  44. let child2 = (parent2.slice(0, splitPoint) + ',' + parent1.slice(splitPoint)).split(',');
  45. let child1OneNumbers = 0;
  46. let child2OneNumbers = 0;
  47.  
  48. for (let i = 0; i <= parentLenth; i++) {
  49.     if (child1[i] === '1') {
  50.         child1OneNumbers++
  51.     }
  52.     if (child2[i] === '1') {
  53.         child2OneNumbers++
  54.     }
  55. }
  56.  
  57. let fitnessLevelChild1 = child1OneNumbers / parentLenth;
  58. let fitnessLevelChild2 = child2OneNumbers / parentLenth;
  59.  
  60. if (fitnessLevelChild1 < 0.5 && fitnessLevelChild2 < 0.5) {
  61.     for (let j = 0; j < parentLenth; j++) {
  62.         if (child1[j] === '0') {
  63.             child1[j] = 1
  64.         }
  65.         if (child1[j] === '1') {
  66.             child1[j] = 0
  67.         }
  68.         if (child2[j] === '0') {
  69.             child2[j] = 1
  70.         }
  71.         if (child2[j] === '1') {
  72.             child2[j] = 0
  73.         }
  74.     }
  75.     if (fitnessLevelChild1 === fitnessLevelChild2) {
  76.         result = child1.join('') + ' - ' + "child1"
  77.     } else if (fitnessLevelChild1 > fitnessLevelChild2) {
  78.         result = child1.join('') + ' - ' + "child1"
  79.     } else {
  80.         result = child2.join('') + ' - ' + "child2"
  81.     }
  82.  
  83. } else if (fitnessLevelChild1 > fitnessLevelChild2) {
  84.     result = child1.join('') + ' - ' + "child1";
  85. } else if (fitnessLevelChild1 < fitnessLevelChild2) {
  86.     result = child2.join('') + ' - ' + "child2";
  87. } else if (fitnessLevelChild1 === fitnessLevelChild2) {
  88.     result = child1.join('') + ' - ' + "child1(много грешна работа, да оставим от 2 деца //same fitnessLevels// само 1-вото)";
  89. }
  90.  
  91. console.log(result);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement