Advertisement
AlexTasev

3. DNAex _Exam050918

Oct 10th, 2018
143
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.     for (let [organism, count] of result) {
  31.         let sum = count.reduce((a, b) => a + b);
  32.         console.log(`${organism} has genome size of ${sum}`)
  33.     }
  34. }
  35.  
  36. dna([
  37.     "=12<<cat",
  38.     "!vi@rus?=2--142",
  39.     "?!cur##viba@cter!!=11--800<<cat",
  40.     "!fre?esia#=7--450<<mouse",
  41.     "@pa!rcuba@cteria$=13--351<<mouse",
  42.     "!richel#ia??=8--900<<human"
  43. ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement