"Simple Pig Latin" codewars kata not passing tests

  • Thread starter shivajikobardan
  • Start date
In summary: Yo urgoal?'In summary, the solution to the "harder" problem posed by @jedishrfu is to capture successive instances of anything that isn't a non-alphanumeric character or a vowel with a regular expression, and to use the captured parts in the replace method of a JavaScript string.
  • #1
shivajikobardan
674
54
TL;DR Summary
codewars kata not passing tests
I'm doing this kata.
https://www.codewars.com/kata/520b9d2ad5c005041100000f/train/javascript

JavaScript:
function pigIt(str) {
  const words = str.split(" ");
  for (var i = 0; i < words.length; i++) {
    if (words[i] === "!") {
      return "!";
    }

    var firstLetterSliced = words[i].slice(0, 1);
    var remainingLetterSliced = words[i].slice(1, words[i].length);
    return (remainingLetterSliced + firstLetterSliced + "ay");
  }
}

pigIt('Pig latin is cool'); // igPay atinlay siay oolcay

My code isn't passing a test. Please help fix the issue.

v68mCMW7IrF7LP2uq_KavVMVhXYLyLOf-RaQ6msuCGvZRMkGoA.png

It works as expected so why is it not passing the tests?
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
You may want to dig into understanding what return mean.
 
  • #3
Your input string apparently was "This is my string". Your code produced "hisTay" and failed to process the other words in that string.
 
  • #4
@shivajikobardan , a meta-comment. You are asking a LOT of questions, sometimes several in one day. There is no evidence that yolu are generalizing the answers you get to other pieces of code.

What's your goal? If it's to program on your own without PF help, I don't see you are making progress towards this goal. As I said, you need to generalize answers to one question to new situations.
 
  • Like
Likes pbuk and Filip Larsen
  • #5
By the way, this training exercise is tagged with regular expressions, so to solve it in the right spirit one should probably aim for using regular expressions. Combined with functional programming this can be solved with a "one-line" transformation pipeline.
 
  • Like
Likes pbuk
  • #6
One other point is true pig-latin strips the consonants from the word so that "this" becomes "isthay" not "hisTay".

https://en.wikipedia.org/wiki/Pig_Latin

Any pig-roman will notice you are a foreigner, if you spoke it that way.

This may be why they suggest you use regular expressions for parsing the string.
 
  • Like
Likes Filip Larsen
  • #7
jedishrfu said:
Any pig-roman
Isn't that porcoroman?
 
  • Haha
Likes jedishrfu
  • #8
jedishrfu said:
One other point is true pig-latin strips the consonants from the word so that "this" becomes "isthay" not "hisTay".
Based on the posted image of the test results -- "Expected 'hisThay' to equal 'hisTay siay ymay tringsay' -- the testing mechanism isn't expecting dipthongs such as 'th', 'sh', 'ch', etc. to be separated out.

The only problem I can see with the OP's solution is that his code stopped after the first word of the input string.
 
  • #9
Perhaps this is Wild Boar Latin, a different dialect.

But indeed, the OP might want to print out the values of i and reflect on what values he expects it to take and which ones it actually takes.
 
  • Haha
Likes jedishrfu
  • #10
Or wolf latin as spoken by the parents of Omulusray and emusRay.
 
  • Haha
Likes Vanadium 50
  • #11
Vanadium 50 said:
@shivajikobardan , a meta-comment. You are asking a LOT of questions, sometimes several in one day. There is no evidence that yolu are generalizing the answers you get to other pieces of code.

What's your goal? If it's to program on your own without PF help, I don't see you are making progress towards this goal. As I said, you need to generalize answers to one question to new situations.
yeah I'm questioning that as well. I'm making no progress at all. Any suggestions?
 
  • #12
Filip Larsen said:
By the way, this training exercise is tagged with regular expressions, so to solve it in the right spirit one should probably aim for using regular expressions.
Absolutely, doing it any other way misses the point (and is much harder to boot).

Edit: there is a bug in the solution below: I have rewritten the solution below at https://www.physicsforums.com/threa...s-kata-not-passing-tests.1049368/post-6849397.

To demonstrate just how easy it is to solve, I will work through the solution to the harder problem posed by @jedishrfu:
jedishrfu said:
One other point is true pig-latin strips the consonants from the word so that "this" becomes "isthay" not "hisTay".
  • So we want to capture successive consonants: the easiest way to do this is to capture one or more succesive instances of anything that isn't a non-alphanumeric character or a vowel. \W matches a non-alphanumeric character so we have ([^\Waeiouy]+).
  • We then want to capture the rest of the word; \w matches an alphanumeric ("word") character so we now have ([^aeiouy]+)(\w*).
  • Add a g flag so the regexp acts on the whole string ("global", otherwise it will only look for the first match) and a u flag so it is case insensitive and we have /([^\Waeiouy]+)(\w*)/gu as the regular expression to search for.
  • Now in a JavaScript string's replace method with a regular expression search pattern, we can use the captured parts of the string in the replacement: $1 will give us the first captured group etc. So '$2$1ay' will reverse the order of the groups and add 'ay', and because of the 'global' flag on the regular expression it will do this for each match.

Putting it all together we have:

JavaScript:
function pigIt(str) {
  return str.replace(/([^\Waeiouy]+)(\w*)/gu, '$2$1ay');
}
pigIt('Pig latin is cool')
// 'igPay atinlay isay oolcay'
pigIt('This is my string')
// 'isThay isay ymay ingstray'
pigIt('Test that, even with punctuation, it works!\nReally!!!')
// 'estTay atthay, eenvay ithway unctuationpay, itay orksway!\neallyRay!!!'
 
Last edited:
  • Like
Likes Wrichik Basu and Filip Larsen
  • #13
Shouldn't "even" be transformed to "veneay" rather than "eenvay"?
 
  • Like
Likes pbuk and jedishrfu
  • #14
Mark44 said:
Shouldn't "even" be transformed to "veneay" rather than "eenvay"?
Yeah, that's a bug (although I think it should actually just be "evenay").
 
  • #15
Reworked solution to the harder problem posed by @jedishrfu:

jedishrfu said:
One other point is true pig-latin strips the consonants from the word so that "this" becomes "isthay" not "hisTay".
  • So we want to capture successive consonants: the easiest way to do this in English is to capture anything that isn't a vowel: ([^aeiouy]*) (we will deal with uppercase characters later).
  • We then want to capture the rest of the word; \w matches an alphanumeric ("word") character so we now have ([^aeiouy]*)(\w+). Note the + selector rather than *: this is to fix a bug in my first attempt where it added an extra "ay" at the end of the string.
  • Add a g flag so the regexp acts on the whole string ("global", otherwise it will only look for the first match) and a u flag so it is case insensitive and we have /([^aeiouy]*)(\w+)(\W*)/gu as the regular expression to search for.
  • Now in a JavaScript string's replace method with a regular expression search pattern, we can use the captured parts of the string in the replacement: $1 will give us the first captured group etc. So '$2$1ay$3' will reverse the order of the first two groups and add 'ay', followed by any punctuation or whitespace after the end of the word. Because of the 'global' flag on the regular expression it will do this for each match.

Putting it all together we have:

JavaScript:
const pigIt = (str) => {
  return str.replace(/([^aeiouy]*)(\w+)(\W*)/gu, '$2$1ay$3');
};

console.log(pigIt('Pig latin is cool'));
// 'igPay atinlay isay oolcay'
console.log(pigIt('This is my string'));
// 'isThay isay ymay ingstray'
console.log(pigIt('Test that, even with punctuation, it works!\nReally!!!'));
// 'estTay atthay, evenay ithway unctuationpay, itay orksway!\neallyRay!!!'
 
Last edited:
  • #16
shivajikobardan said:
Any suggestions?
You might limit yourself to one question per day. That will force you to think hard about the responses you are getting and what parts need clarification.

It may sound slower, but I think we can all agree "here's my code; debug it for me" isn't working.
 

Related to "Simple Pig Latin" codewars kata not passing tests

1. What is Simple Pig Latin and how does it work?

Simple Pig Latin is a codewars kata that involves converting a given string into Pig Latin, a language game where the first letter of each word is moved to the end of the word and "ay" is added. For example, "hello" becomes "ellohay".

2. Why is my code for Simple Pig Latin not passing the tests?

There could be several reasons for this. It is important to carefully read the kata instructions and make sure your code follows all the requirements. It is also possible that there are edge cases or test cases that your code is not accounting for. It can be helpful to use console.log() statements to check the outputs at different stages of your code.

3. How can I improve the efficiency of my Simple Pig Latin solution?

One way to improve the efficiency of your Simple Pig Latin solution is to use built-in JavaScript methods such as split(), slice(), and join() to manipulate the string instead of using loops. It is also important to consider how you are handling punctuation and special characters in the string.

4. What is the best way to approach solving the Simple Pig Latin kata?

The best way to approach solving the Simple Pig Latin kata is to break down the problem into smaller steps and tackle each step individually. This can involve splitting the string into an array, manipulating the words in the array, and then joining them back together. It is also helpful to test your code with different inputs to ensure it is handling all cases correctly.

5. Can I use external resources to help me solve the Simple Pig Latin kata?

While it is important to understand the logic behind your code, you can use external resources such as official documentation or online forums for guidance and inspiration. However, it is not allowed to copy and paste code or directly use solutions from other sources as this defeats the purpose of the kata and does not help you improve your skills as a coder.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
7
Views
994
Replies
1
Views
654
  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
3K
  • General Discussion
3
Replies
78
Views
9K
Back
Top