CoddityTeam / movaicode

Concours mensuel du Pire Développeur de France
123 stars 10 forks source link

can I left shift ? #68

Closed frenchfal closed 2 years ago

frenchfal commented 3 years ago

Je voulais compter le nombre de lettres en commun entre les inputs et "cadeau" mais j'aime pas la propriété length alors je l'ai recodé avec du left shift et des nombres binaires.

const getStringLength = string => {
  return getArrayLength(string.split(''));
}

const getArrayLength = array => {
  let length = 0b0;
  array.map((letter, i) => {
    length = 1 << i;
  })

  return convertBinaryToPowerOfTwo(length.toString(2));
}

const convertBinaryToPowerOfTwo = number => {
  let output = 0;
  for (let i = parseInt(number, 2); i>=1; i/=2) {
    output++;
  }

  return output
}

const findOccurencesInArray = (string, array) => {
  const formated = array.map(word => {
    let similar_letters = 0b0;
    for (var i=0; i<getStringLength(word); i++) {
      if (word[i] === string[i]) {
        similar_letters = 1 << i;
      }
    }

    return convertBinaryToPowerOfTwo(similar_letters.toString(2));
  });

  return getArrayLength(formated.filter(count => count === getStringLength(string)));
}

console.log(findOccurencesInArray(
  "cadeau", 
  ["cadeau", "pourri", "cadeau", "revendre", "écharpe", "transformer", "smartbox", "pyrénéens", "goldeneye64", "cadeau"]
));

// 3
louismarslen commented 3 years ago

Hello Baptiste, j'ai une bonne et une mauvaise (movaize) nouvelle :

Bravo et pas bravo donc ! 😊

PS : oui c'est un copié-collé, j'ai pas suffisamment d'inspiration pour faire 26 commentaires différents...

louismarslen commented 2 years ago

Je fais un peu de ménage dans les issues -> d'ailleurs go participer à l'édition 5