CoddityTeam / movaicode

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

Cher monsieur Stallone. #256

Open NeoxAzrot opened 1 year ago

NeoxAzrot commented 1 year ago

Bonjour monsieur Stallone, Voici ma contribution pour distribuer vos coups de poings imaginaires.

Je suis néanmoins désolé, car je n'ai pas trouvé plus simple comme raisonnement...

/**
 *  @name: movaicode.js
 *  @description: Punch distribution function - Movaicode
 *  @author: Sami Lafrance - contact@samilafrance.com
 *  @version: 1.0.0
 */

var sentence =
  "Synopsys : dans les quartiers populaires de Philadelphie, Rocky Balboa collecte des dettes non payées pour Tony Gazzo, un usurier ; il dispute de temps à autre, pour quelques dizaines de dollars, des combats de boxe sous l'appellation de l'étalon italien.";

/* -------------------------------------------------------------------------- */
/*                             getAllCharPosition                             */
/* -------------------------------------------------------------------------- */
function getAllCharPosition(c) {
  var charPosition = [];

  for (var i = 0; i < sentence.length; i++) {
    if (sentence[i] === c) {
      charPosition.push(i);
    }
  }

  return charPosition;
}

/* -------------------------------------------------------------------------- */
/*                                  setCharAt                                 */
/* -------------------------------------------------------------------------- */
function setCharAt(s, i, c) {
  if (i > s.length - 1) return s;
  return s.substring(0, i) + c + s.substring(i + 1);
}

/* -------------------------------------------------------------------------- */
/*                                addCharAfter                                */
/* -------------------------------------------------------------------------- */
function addCharAfter(s, i, c) {
  if (i > s.length - 1) return s;
  return s.substring(0, i + 1) + c + s.substring(i + 1);
}

/* -------------------------------------------------------------------------- */
/*                               replaceAllChar                               */
/* -------------------------------------------------------------------------- */
function replaceAllChar(c, r) {
  var charPosition = getAllCharPosition(decodeHTMLChars(c));

  var replacementSplit = r.split('');

  for (var i = charPosition.length - 1; i >= 0; i--) {
    for (var j = 0; j < replacementSplit.length; j++) {
      if (j === 0) {
        sentence = setCharAt(sentence, charPosition[i], replacementSplit[j]);
      } else {
        sentence = addCharAfter(
          sentence,
          charPosition[i] + j - 1,
          replacementSplit[j]
        );
      }
    }
  }

  return sentence;
}

/* -------------------------------------------------------------------------- */
/*                               encodeHTMLChars                              */
/* -------------------------------------------------------------------------- */
function encodeHTMLChars(c) {
  var buf = [];

  for (var i = c.length - 1; i >= 0; i--) {
    buf.unshift(['&#', c[i].charCodeAt(), ';'].join(''));
  }

  return buf.join('');
}

/* -------------------------------------------------------------------------- */
/*                               decodeHTMLChars                              */
/* -------------------------------------------------------------------------- */
function decodeHTMLChars(c) {
  return c.replace(/&#(\d+);/g, (m, d) => {
    return String.fromCharCode(d);
  });
}

/* -------------------------------------------------------------------------- */
/*                                    main                                    */
/* -------------------------------------------------------------------------- */
function main() {
  var chars = [
    {
      char: encodeHTMLChars('p'),
      replacement: 'poing',
      order: 5,
    },
    {
      char: encodeHTMLChars('P'),
      replacement: 'Poing',
      order: 4,
    },
    {
      char: encodeHTMLChars('.'),
      replacement: 'poing',
      order: 1,
    },
    {
      char: encodeHTMLChars(':'),
      replacement: 'deuxpoings',
      order: 3,
    },
    {
      char: encodeHTMLChars(';'),
      replacement: 'poingvirgule',
      order: 2,
    },
  ];

  chars = chars.sort((a, b) => {
    return a.order - b.order;
  });

  var newSentence = '';

  for (var i = chars.length - 1; i >= 0; i--) {
    newSentence = replaceAllChar(chars[i].char, chars[i].replacement);
  }

  console.log(newSentence);
}

main();
Matthieu-Coddity commented 1 year ago

you win this month ! \o/