Advertisement
AlexTasev

03_DNAex

Oct 11th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function dna(input) {
  2.     let text = input.slice(0);
  3.     let regex = /^([a-z!@#$?]+)=([1-9]+)--([0-9]+)<<([a-z]+)$/;
  4.     let result = new Map();
  5.  
  6.     for (let i = 0; i < text.length; i++) {
  7.         let line = text[i];
  8.         if (!regex.test(line)) {
  9.             continue;
  10.         } else {
  11.             let match = regex.exec(line);
  12.             let geneName = match[1].split(/[!@#$?]*/).join("");
  13.             let nameLenght = +match[2];
  14.  
  15.             if (geneName.length !== nameLenght) {
  16.                 continue;
  17.             }
  18.             else {
  19.                 let genesCount = +match[3];
  20.                 let organism = match[4];
  21.  
  22.                 if (result.has(organism) === false) {
  23.                     result.set(organism, [])
  24.                 }
  25.                 result.get(organism).push(genesCount);
  26.             }
  27.         }
  28.     }
  29.  
  30.     let sorted = [...result].sort((a, b) => {
  31.         let first = a[1].reduce((c,d) => c + d);
  32.         let second = b[1].reduce((c, d) => c + d);
  33.         return second - first
  34.     });
  35.  
  36.     for (let [organism, count] of sorted) {
  37.         let total = count.reduce((a, b) => a + b);
  38.         console.log(`${organism} has genome size of ${total}`);
  39.     }
  40. }
  41.  
  42. dna([
  43.     "=12<<cat",
  44.     "!vi@rus?=2--142",
  45.     "?!cur##viba@cter!!=11--800<<cat",
  46.     "!fre?esia#=7--450<<mouse",
  47.     "@pa!rcuba@cteria$=13--351<<mouse",
  48.     "!richel#ia??=8--900<<human"
  49. ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement