views:

82

answers:

2

Last night I was messing around with Piglatin using Arrays and found out I could not reverse the process. How would I shift the phrase and take out the Char's "a" and "y" at the end of the word and return the original word in the phrase.

For instance if I entered "piggy" it would come out as "iggypay" shifting the word piggy so "p" is at the end of the word and "ay" is appended.

Here is the example code so you can try it as well.

    public string ay;
    public string PigLatin(string phrase)
    {
        string[] pLatin;
        ArrayList pLatinPhrase = new ArrayList();
        int wordLength;
        pLatin = phrase.Split();

        foreach (string pl in pLatin)
        {
            wordLength = pl.Length;
            pLatinPhrase.Add(pl.Substring(1, wordLength - 1) + pl.Substring(0, 1) + "ay");
        }

        foreach (string p in pLatinPhrase)
        {
            ay += p; 
        }
        return ay;
    }

You will notice that is example is not programmed to find vowels and append them to the end along with "ay". Just simply a basic way of doing it.

If you where wondering how to reverse the above try this example of uPiglatinify

    public string way;
    public string uPigLatinify(string word)
    {
        string[] latin;
        int wordLength;
        // Using arrraylist to store split words.
        ArrayList Phrase = new ArrayList();
        // Split string phrase into words.
        latin = word.Split(' ');

        foreach (string i in latin)
        {
            wordLength = i.Length;
            if (wordLength > 0)
            {
                // Grab 3rd letter from the end of word and append to front
                // of word chopping off "ay" as it was not included in the indexing.
                Phrase.Add(i.Substring(wordLength - 3, 1) + i.Substring(0, wordLength - 3) + " ");
            }
        }

        foreach (string _word in Phrase)
        {
            // Add words to string and return.
            way += _word;
        }
        return way;
    }
A: 

The work to split the phrase into words and recombine the words after transforming them is the same as in the original case. The difficulty is in un-pig-latin-ifying an individual word. With some error checking, I imagine you could do this:

string UnPigLatinify(string word)
{
    if ((word == null) || !Regex.IsMatch(word, @"^\w+ay$", RegexOptions.IgnoreCase))
        return word;

    return word[word.Length - 3] + word.Substring(0, word.Length - 3);
}

The regular expression just checks to make sure the word is at least 3 letters long, composed of characters, and ends with "ay".

The actual transform takes the third to last letter (the original first letter) and appends the rest of the word minus the "ay" and the original letter.

Is this what you meant?

Chris Schmich
This is what I am looking for! Interesting approach might I add. I was using string.Remove and trying to use it like so Remove(pl.Substring(1, wordLength -1) + pl.Substring(0,1).TrimEnd(Convert.ToChar("a", "y"))); The result choped the "ay" off but it left "gi" instead. I was editing the wrong part I guess. It was late and I was tired as well. I will have to remember this as well it was so simple! Thanks again for explaining it.
Nightforce2
@Nightforce2: Not wanting to sound disrespectful, but are you sure that you fully understand Chris Schmich’s code? Do you realise that this function only converts a single word, while your original one attempted to convert a whole sentence? Do you know what a regular expression is and have you used one before? And do you fully understand why your own attempt failed? Honestly, I’m not trying to pick on you here, I’m trying to give some well-meaning advice: You shouldn’t copy other people’s code without understanding it.
Timwi
+2  A: 

Please don’t take this the wrong way, but although you can probably get people here to give you the C# code to implement the algorithm you want, I suspect this is not enough if you want to learn how it works. To learn the basics of programming, there are some good tutorials to delve into (whether websites or books). In particular, if you aspire to be a programmer, you will need to learn not just how to write code. In your example:

  • You should first write a specification of what your PigLatin function is supposed to do. Think about all the corner-cases: What if the first letter is a vowel? What if there are several consonants at the beginning? What if there are only consonants? What if the input starts with a number, a parenthesis, or a space? What if the input string is empty? Write down exactly what should happen in all of these cases — even if it’s “throw an exception”.

  • Only then can you implement the algorithm according to the specification (i.e. write the actual C# code). While doing this, you may find that the specification is incomplete, in which case you need to go back and correct it.

  • Once your code is finished, you need to test it. Run it on several testcases, especially the corner-cases you came up with above: For example, try PigLatin("air"), PigLatin("x"), PigLatin("1"), PigLatin(""), etc. In each case, make yourself aware first what behaviour you expect, and then see if the behaviour matches your expectation. If it doesn’t, you need to go back and fix the code.

Once you have implemented the forward PigLatin algorithm and it works (read: passes all your testcases), then you will already have the skills needed to write the reverse function youself. I guarantee you that you will feel achieved and excited then! Whereas, if you just copy the code from this website, you are setting yourself up for feeling dumb because you will think other people can do it and you can’t.

Of course, we are nonetheless happy to help you with specific technical questions, for example “What is the difference between ArrayList and List<string>?” or “What does the scope of a local variable mean?” (but search first — these may have already been asked before) — but you probably shouldn’t ask to have the code fully written and finished for you.

Timwi
Good advice, I usually comment my code and organize it pretty well. In this case it was a quick write up . Actually, in this case I learned a lot from it. I have learned more from when people have shown me how to do things rather then try to read a book and "attempt" to interpret, It's a faster approach and reputable not saying I do not do things for myself as i have made some pretty big project so far. I spent 2 hours trying many different approaches to get this to work properly but didn't get it work the way It was supposed to work. I didn't find much refernce either using google.
Nightforce2
@Nightforce2: Once again, please don’t take this the wrong way — but unfortunately there are many people who fall into the trap of thinking that just because they wrote something (even a “pretty big project”), that it was automatically a success. From the code that you posted in the question, and your saying that you spent 2 hours on this simple problem, frankly the impression it leaves is that you probably do not have the skills to maintain a large project. Of course I’m not saying you can’t acquire those skills — but you first need to reaffirm to yourself exactly what skills you are lacking.
Timwi
Nightforce2
@Nightforce2: Way to go for an overreaction. :) I didn’t assume that “This guy doesn’t know anything”, I looked at your code and the question you asked and drew my conclusion from that. Of course I don’t know the whole picture, but that doesn’t mean I’m not allowed to comment on the small picture that I do see.
Timwi