Skip to content

Instantly share code, notes, and snippets.

@clamstew
Last active February 28, 2022 03:12
Show Gist options
  • Save clamstew/eed9bd11345f66a074304b4a31c6989e to your computer and use it in GitHub Desktop.
Save clamstew/eed9bd11345f66a074304b4a31c6989e to your computer and use it in GitHub Desktop.
Found a problem on coderpad for pig latin translator
var Mocha = require('mocha');
var assert = require('assert');
var mocha = new Mocha();
mocha.suite.emit('pre-require', this, 'solution', mocha);
//===============
const stringOne = "pig";
const stringTwo = "pig latin";
const stringThree = "Pig Latin";
const longerPhrase = "Clay goes to the park to look for acorns."
const longerPhraseTwo = "foo bar something, foo bar aldkfj. foo bar something, foo bar aldkfj.";
// oo-fay ar-bay omething-say, oo-fay ar-bay aldkfj-way. oo-fay ar-bay omething-say, oo-fay ar-bay aldkfj-way.
/*
1) “pig” => “ig-pay” 2) “pig latin” => “ig-pay atin-lay” 3) “Pig Latin” => “ig-Pay atin-Lay”
2) If a word begins with a vowel, just add “way” at the en
separate the Pig Latin-ized parts of the word with a dash
*/
class PigLatinTranslator {
constructor(args) {
this.phrase = args.phrase;
this.SUFFIX_CONSTANT = "ay";
this.memoizedWords = {};
}
transformWord(word) {
if (this.memoizedWords[word]) {
console.log("hit memoizedWord", word, this.memoizedWords[word]);
return this.memoizedWords[word];
}
const firstLetter = word[0];
const firstLetterIsVowel = /[aeiouAEIOU]/.test(firstLetter);
let restOfWord = !firstLetterIsVowel ? word.slice(1) : word;
// if final letter has punct then take off and save off to re-add
const finalLetter = word.slice(-1);
let hasPunct = false;
if (/[.,\/#!$%\^&\*;:{}=\-_`~()]/.test(finalLetter)) {
hasPunct = true;
const restOfWordMinusPunctation = restOfWord.slice(0, restOfWord.length - 1);
restOfWord = restOfWordMinusPunctation;
}
// build suffix
const fullSuffix = !firstLetterIsVowel ? firstLetter + this.SUFFIX_CONSTANT : "way";
// build word
return this.memoizedWords[word] = `${restOfWord}-${fullSuffix}${hasPunct ? finalLetter : ""}`;
}
pigLatinize() {
// const words = this.phrase.split(" ");
// const pigWords = words.map(word => this.transformWord(word));
// return pigWords.join(" ");
// could improve big O maybe by one loop through chars
// each time char is a space, then a word is built
// and would go through transform word then add to a completed array
let potentialWord = "";
let finalPhrase = "";
for (let i = 0; i < this.phrase.length; i++) {
const char = this.phrase[i];
const nextChar = this.phrase[i + 1];
potentialWord += char;
// if find space:
// 1) transform word (with or without punct) to pig latin
// 2) add to final phrase
// 3) reset potentialWord
if (char === " " || nextChar === undefined) {
const pigWord = this.transformWord(potentialWord.trimEnd());
finalPhrase += `${finalPhrase.length > 0 ? " " : ""}${pigWord}`;
potentialWord = "";
}
}
return finalPhrase;
}
}
describe('PigLatinTranslator', function() {
it('should transform "pig" to "ig-pay"', function() {
const stringOneInstance = new PigLatinTranslator({phrase: stringOne});
assert.equal(stringOneInstance.pigLatinize(), "ig-pay");
});
it('should transform "pig latin" to "ig-pay atin-lay"', function() {
const stringTwoInstance = new PigLatinTranslator({phrase: stringTwo});
assert.equal(stringTwoInstance.pigLatinize(), "ig-pay atin-lay");
});
it('should transform "Pig Latin" with upper case to "ig-Pay atin-Lay"', function() {
const stringThreeInstance = new PigLatinTranslator({phrase: stringThree});
assert.equal(stringThreeInstance.pigLatinize(), "ig-Pay atin-Lay");
});
describe("when first letter is vowel", () => {
it('should always return a suffix of "way"', () => {
const stringFourInstance = new PigLatinTranslator({phrase: "acorn"});
assert.equal(stringFourInstance.pigLatinize(), "acorn-way");
});
it('should always return a suffix of "way"', () => {
const stringFiveInstance = new PigLatinTranslator({phrase: "Acorn"});
assert.equal(stringFiveInstance.pigLatinize(), "Acorn-way");
});
});
describe("longerPhrase", () => {
it("should pig latinize over several words with various cases", () =>{
const longerPhraseInstance = new PigLatinTranslator({phrase: longerPhrase});
// "Clay goes to the park to look for acorns"
assert.equal(longerPhraseInstance.pigLatinize(), "lay-Cay oes-gay o-tay he-tay ark-pay o-tay ook-lay or-fay acorns-way.");
})
it("should pig latinize over several words with various cases", () =>{
const longerPhraseTwoInstance = new PigLatinTranslator({phrase: longerPhraseTwo});
// "foo bar something, foo bar aldkfj. foo bar something, foo bar aldkfj."
assert.equal(longerPhraseTwoInstance.pigLatinize(), "oo-fay ar-bay omething-say, oo-fay ar-bay aldkfj-way. oo-fay ar-bay omething-say, oo-fay ar-bay aldkfj-way.");
})
})
})
mocha.run()
@clamstew
Copy link
Author

java

Intro

To solve this challenge, feel free to use any and all resources available to you. Once you start the exercise, you'll have two hours to complete and submit your solution.

Challenge - Pig Latin

Pig Latin is a farcical "language" used to entertain children, but also to teach them some valuable language concepts along the way. Translating English to Pig Latin is simple:

  1. Take the first consonant (or cluster of consonants) of a word, move it to the end of the word, and add a suffix of "ay"
  2. If a word begins with a vowel, just add "way" at the end
  3. For the sake of readability, separate the Pig Latin-ized parts of the word with a dash -

Your challenge is to implement the method pigLatinize that takes a string phrase and translates it to Pig Latin. You're free to add additional classes, variables, and methods if you would like.

The input phrase could be as short as a single word, or as long as multiple sentences or paragraphs. Whitespace, capitalization, and punctuation should be honored and maintained in your final answer.

Examples

  1. "pig" => "ig-pay"
  2. "pig latin" => "ig-pay atin-lay"
  3. "Pig Latin" => "ig-Pay atin-Lay"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment