"Simple Pig Latin" codewars kata not passing tests

  • Thread starter Thread starter shivajikobardan
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around a coding challenge from Codewars related to implementing a function that converts English phrases into Pig Latin. Participants are troubleshooting issues with the provided code, discussing the expected behavior of the function, and exploring alternative approaches to the problem, including the use of regular expressions.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant shares their implementation of the Pig Latin function and notes that it is not passing tests, specifically mentioning that it only processes the first word of the input string.
  • Another participant suggests that the original code's use of the return statement causes it to exit the loop prematurely, preventing it from processing all words.
  • Some participants argue that true Pig Latin should strip consonants from the beginning of words, leading to different transformations than what the original code produces.
  • There is a suggestion to use regular expressions for a more effective solution, with one participant providing a rewritten version of the function that incorporates regex.
  • Multiple participants discuss the expected transformations for specific words, such as "this" and "even," leading to differing opinions on the correct output.
  • One participant humorously suggests that the original implementation might represent a different dialect of Pig Latin.
  • Another participant emphasizes the importance of generalizing solutions to similar coding problems rather than seeking direct fixes for specific instances.

Areas of Agreement / Disagreement

Participants express varying opinions on the correct implementation of the Pig Latin transformation, with no consensus on the expected outputs for certain words. The discussion includes both agreement on the need for regex and disagreement on specific transformation rules.

Contextual Notes

Some participants note that the original code does not handle punctuation or multiple consonants correctly, and there are unresolved questions about the handling of specific cases in Pig Latin transformations.

Who May Find This Useful

Readers interested in programming challenges, particularly those involving string manipulation and regular expressions, may find this discussion relevant.

shivajikobardan
Messages
637
Reaction score
54
TL;DR
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
You may want to dig into understanding what return mean.
 
Your input string apparently was "This is my string". Your code produced "hisTay" and failed to process the other words in that string.
 
@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   Reactions: pbuk and Filip Larsen
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   Reactions: pbuk
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   Reactions: Filip Larsen
jedishrfu said:
Any pig-roman
Isn't that porcoroman?
 
  • Haha
Likes   Reactions: jedishrfu
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.
 
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   Reactions: jedishrfu
  • #10
Or wolf latin as spoken by the parents of Omulusray and emusRay.
 
  • Haha
Likes   Reactions: 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   Reactions: Wrichik Basu and Filip Larsen
  • #13
Shouldn't "even" be transformed to "veneay" rather than "eenvay"?
 
  • Like
Likes   Reactions: 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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 21 ·
Replies
21
Views
4K