collapse collapse

* Who's Online

  • Dot Guests: 69
  • Dot Hidden: 0
  • Dot Users: 0

There aren't any users online.

* Board Stats

  • stats Total Members: 88
  • stats Total Posts: 11163
  • stats Total Topics: 1699
  • stats Total Categories: 4
  • stats Total Boards: 76
  • stats Most Online: 248

Author Topic: Piglatin Converter  (Read 6404 times)

0 Members and 1 Guest are viewing this topic.

Offline mager1794

  • Jr. Member
  • **
  • Posts: 91
  • Reputation 293
    • View Profile
    • My Game Challenge
Piglatin Converter
« on: March 14, 2014, 10:16:04 am »
Simple algorithm to parse typed in the console and to return an sentence output in the form of piglatin

Follows a standard pattern of finding the first vowel and shifting all the letters before the vowel to the end of the sentence and adding 'ay'

if the first letter is the first vowel, it just adds 'yay' to the word. if there are no common vowels. then the system will check for 'y' as a vowel.


Code: [Select]
static char[] vowels = { 'a', 'e', 'i', 'o', 'u'};
static string ConvertToPigLatin(string message)
        {
            string[] words = message.Split(' ');
            string pigLatin = "";

            foreach(string word in words)
            {

                pigLatin += ConvertWordToPigLatin(word) + " ";
            }

            return pigLatin;

        }

        static string ConvertWordToPigLatin(string word)
        {
            string pigLatin = "";
            string temp_word_b = "";
            string temp_word_e = "";
            int index = 0;
            int found_index = -1;

            foreach (char c in word.ToLower())
            {
               
                if (temp_word_b == "")
                {
                    foreach (char v in vowels)
                    {
                        if (c == v)
                        {
                            found_index = index;
                        }

                    }

                    if (found_index > -1)
                        temp_word_b += c;
                    else
                        temp_word_e += c;
                }
                else
                {
                    temp_word_b += c;
                }

                index++;

            }

            if (found_index > -1)
            {
                string ender = "ay";
                if (found_index == 0)
                    ender = "yay";

                pigLatin = (temp_word_b + temp_word_e + ender);

                return pigLatin;
            }
            else
            {
                return ConvertWordToPigLatin(word, true);
            }
        }

        static string ConvertWordToPigLatin(string word, bool useY)
        {
            if (useY)
            {
                string pigLatin = "";
                string temp_word_b = "";
                string temp_word_e = "";

                char[] vowelset = new char[vowels.Length + 1];
                int i = 0;
                foreach (char v in vowels)
                {
                    vowelset[i] = v;
                    i++;
                }

                vowelset[i] = 'y';


                foreach (char c in word.ToLower())
                {
                    bool vowel_found = false;
                    if (temp_word_b == "")
                    {
                        foreach (char v in vowelset)
                        {
                            if (c == v)
                            {
                                vowel_found = true;
                            }

                        }

                        if (vowel_found)
                            temp_word_b += c;
                        else
                            temp_word_e += c;
                    }
                    else
                    {
                        temp_word_b += c;
                    }

                }

                pigLatin = (temp_word_b + temp_word_e + "ay");

                return pigLatin;
            }
            else
            {
                return ConvertWordToPigLatin(word);
            }
        }
       
http://mygamechallenge.wordpress.com/2010/07/23/personal-game-challenge/ - My Personal Challenge To Finish My First C++ Game

Offline Vlykarye

  • Full Member
  • ***
  • Posts: 131
  • Reputation 34
  • Gender: Male
    • View Profile
Re: Piglatin Converter
« Reply #1 on: March 25, 2014, 04:25:22 pm »
Your logic was pretty hard more work than I like to do (xD) to follow, but this is a neat program. May I suggest you add comments to other programs you write, so we can follow them more quickly?
« Last Edit: March 25, 2014, 05:31:25 pm by Vlykarye »

Offline Vlykarye

  • Full Member
  • ***
  • Posts: 131
  • Reputation 34
  • Gender: Male
    • View Profile
Re: Piglatin Converter
« Reply #2 on: March 25, 2014, 07:56:28 pm »
Ok. I went through your code, and realized that it was very repetitive and didn't utilize C# for the one reason it's a decent language: it's massive pre-made libraries.
Here is an optimized version of your code.

Code: C
  1. using System;
  2.  
  3. namespace Piglatin
  4. {
  5.     class Program
  6.     {
  7.         static Char[] Vowels = "aeiouy".ToCharArray();
  8.  
  9.         static String ToPigLatin(String Sentence)
  10.         {
  11.             String Result = "";
  12.             foreach (String Word in Sentence.Split(' '))
  13.             {
  14.                 Result += WordToPigLatin(Word) + " ";
  15.             }
  16.  
  17.             return Result;
  18.         }
  19.  
  20.         static String WordToPigLatin(String Word)
  21.         {
  22.             String Word_LowerCase = Word.ToLower();
  23.             Int32 Index_FirstVowel = Word_LowerCase.IndexOfAny(Vowels);
  24.  
  25.             if (Index_FirstVowel == 0)
  26.             {
  27.                 if (Word_LowerCase[0] != 'y')
  28.                 {
  29.                     return Word + "yay";
  30.                 }
  31.                 return Word.Substring(1) + "yay";
  32.             }
  33.             return Word.Substring(Index_FirstVowel) + Word.Substring(0, Index_FirstVowel) + "ay";
  34.         }
  35.  
  36.         static void Main(String[] args)
  37.         {
  38.             // Notice that words starting with silent constanants don't get processed correctly with this algorithm.
  39.             String Sentence = "happy duck glove egg inbox eight you hour";
  40.  
  41.             System.Console.Out.WriteLine(ToPigLatin(Sentence));
  42.         }
  43.     }
  44. }

Offline Celestialkey

  • Administrator
  • Hero Member
  • *
  • Posts: 3962
  • Reputation 4874
  • Gender: Male
  • Its Alive!!
    • View Profile
    • www.CelestialCoding.com
Re: Piglatin Converter
« Reply #3 on: March 25, 2014, 11:39:48 pm »
Ok. I went through your code, and realized that it was very repetitive and didn't utilize C# for the one reason it's a decent language: it's massive pre-made libraries.
Here is an optimized version of your code.

Code: C
  1. using System;
  2.  
  3. namespace Piglatin
  4. {
  5.     class Program
  6.     {
  7.         static Char[] Vowels = "aeiouy".ToCharArray();
  8.  
  9.         static String ToPigLatin(String Sentence)
  10.         {
  11.             String Result = "";
  12.             foreach (String Word in Sentence.Split(' '))
  13.             {
  14.                 Result += WordToPigLatin(Word) + " ";
  15.             }
  16.  
  17.             return Result;
  18.         }
  19.  
  20.         static String WordToPigLatin(String Word)
  21.         {
  22.             String Word_LowerCase = Word.ToLower();
  23.             Int32 Index_FirstVowel = Word_LowerCase.IndexOfAny(Vowels);
  24.  
  25.             if (Index_FirstVowel == 0)
  26.             {
  27.                 if (Word_LowerCase[0] != 'y')
  28.                 {
  29.                     return Word + "yay";
  30.                 }
  31.                 return Word.Substring(1) + "yay";
  32.             }
  33.             return Word.Substring(Index_FirstVowel) + Word.Substring(0, Index_FirstVowel) + "ay";
  34.         }
  35.  
  36.         static void Main(String[] args)
  37.         {
  38.             // Notice that words starting with silent constanants don't get processed correctly with this algorithm.
  39.             String Sentence = "happy duck glove egg inbox eight you hour";
  40.  
  41.             System.Console.Out.WriteLine(ToPigLatin(Sentence));
  42.         }
  43.     }
  44. }

There are many better ways to accomplish this. Using actual C++ classes could allow the code to be even smaller than the C# version you posted. Even C has some functions that could have made this easier.
Created By: Eczuo
Quote
I have noticed that people who claim that everything is predestined, and we can do nothing to change it, look both ways before they cross the road.
Quote
I'd prefer to die standing, than to live on my knees - Che Guevara
Quote
If you change the way you look at something, does that something change in any way?
- Quantum Theory

Hacking
Quote
Never in the field of human conflict was so much owed by so many to so few. - Winston Churchill


Quote from: Revelations 12:4
And his tail drew the third part of the stars of heaven, and did cast them into the earth; and the dragon stood before the woman which was ready to be delivered, for to devour her child as soon as it was born.

Quote
It takes skill to build an empire. It takes an idiot to maintain it.

Offline Vlykarye

  • Full Member
  • ***
  • Posts: 131
  • Reputation 34
  • Gender: Male
    • View Profile
Re: Piglatin Converter
« Reply #4 on: March 26, 2014, 12:04:14 am »
That's all true, but the goal isn't to make the code smaller, as that has no real application. And though I would prefer C over C#, mager wrote the original program in C#, so I decided that he would have no use for a C version, and it wouldn't help him with coding in C#.

Offline Celestialkey

  • Administrator
  • Hero Member
  • *
  • Posts: 3962
  • Reputation 4874
  • Gender: Male
  • Its Alive!!
    • View Profile
    • www.CelestialCoding.com
Re: Piglatin Converter
« Reply #5 on: March 26, 2014, 09:14:43 am »
That's all true, but the goal isn't to make the code smaller, as that has no real application. And though I would prefer C over C#, mager wrote the original program in C#, so I decided that he would have no use for a C version, and it wouldn't help him with coding in C#.
Point taken, didn't notice it was C# looking at the original code. Looks like C to me until I hit the ToLower function.
Created By: Eczuo
Quote
I have noticed that people who claim that everything is predestined, and we can do nothing to change it, look both ways before they cross the road.
Quote
I'd prefer to die standing, than to live on my knees - Che Guevara
Quote
If you change the way you look at something, does that something change in any way?
- Quantum Theory

Hacking
Quote
Never in the field of human conflict was so much owed by so many to so few. - Winston Churchill


Quote from: Revelations 12:4
And his tail drew the third part of the stars of heaven, and did cast them into the earth; and the dragon stood before the woman which was ready to be delivered, for to devour her child as soon as it was born.

Quote
It takes skill to build an empire. It takes an idiot to maintain it.

Offline 0xDEAD10CC

  • Newbie
  • *
  • Posts: 21
  • Reputation 0
  • Gender: Male
  • void of_space();
    • View Profile
Re: Piglatin Converter
« Reply #6 on: April 06, 2014, 06:08:58 pm »
@Vlykarye - String's in .NET are already essentially character arrays, just like most languages; there is an indexing operator available to the string. Thus, you don't need to cast it to an array:
Code: [Select]
static Char[] Vowels = "aeiouy".ToCharArray();
Just write:
Code: [Select]
static string _vowels = "aeiou";
You weren't a former Pascal programmer were you? Your naming conventions are odd.

Also, because you're using the System namespace (default), you don't need to be so explicit:
Code: [Select]
System.Console.Out.WriteLine
You would only need:
Code: [Select]
Console.Out.WriteLine
Although standard output is by default, so you should probably just use:
Code: [Select]
Console.WriteLine
For shorthand.

You also could use LINQ to replace this:
Code: [Select]
String Result = "";
      foreach (String Word in Sentence.Split(' '))
      {
        Result += WordToPigLatin(Word) + " ";
      }

      return Result;

string.Join() would achieve everything here:
Code: [Select]
string pigLagin = string.Join(" ", sentence.Split(' ').Select(WordToPigLatin));
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Offline Vlykarye

  • Full Member
  • ***
  • Posts: 131
  • Reputation 34
  • Gender: Male
    • View Profile
Re: Piglatin Converter
« Reply #7 on: April 06, 2014, 06:36:09 pm »
1: No, I wasn't a Pascal programmer. My naming conventions are efficient for me.
2: I know what Strings are. This should easily show it: "if (Word_LowerCase[0] != 'y')"
3: http://msdn.microsoft.com/en-us/library/11w09h50%28v=vs.110%29.aspx
4: I think System.Console.Out.WriteLine is better, as I know exactly where WriteLine is coming from.
5: I don't use LINQ because I haven't learned LINQ, yet. Also, I don't program in C# often, so I didn't know about Select either.
« Last Edit: April 07, 2014, 07:26:18 am by Celestialkey »

Offline Celestialkey

  • Administrator
  • Hero Member
  • *
  • Posts: 3962
  • Reputation 4874
  • Gender: Male
  • Its Alive!!
    • View Profile
    • www.CelestialCoding.com
Re: Piglatin Converter
« Reply #8 on: April 06, 2014, 10:31:49 pm »
1: No, I wasn't a Pascal programmer. My naming conventions are efficient for me.
2: I know what Strings are. This should easily show it: "if (Word_LowerCase[0] != 'y')"
3: http://msdn.microsoft.com/en-us/library/11w09h50%28v=vs.110%29.aspx
4: I think System.Console.Out.WriteLine is better, as I know exactly where WriteLine is coming from.
5: I don't use LINQ because I haven't learned LINQ, yet. Also, I don't program in C# often, so I didn't know about Select either.



I agree with you 100% here. I thought your post was tailored quite well to the question asked.
« Last Edit: April 07, 2014, 07:27:04 am by Celestialkey »
Created By: Eczuo
Quote
I have noticed that people who claim that everything is predestined, and we can do nothing to change it, look both ways before they cross the road.
Quote
I'd prefer to die standing, than to live on my knees - Che Guevara
Quote
If you change the way you look at something, does that something change in any way?
- Quantum Theory

Hacking
Quote
Never in the field of human conflict was so much owed by so many to so few. - Winston Churchill


Quote from: Revelations 12:4
And his tail drew the third part of the stars of heaven, and did cast them into the earth; and the dragon stood before the woman which was ready to be delivered, for to devour her child as soon as it was born.

Quote
It takes skill to build an empire. It takes an idiot to maintain it.

Offline 0xDEAD10CC

  • Newbie
  • *
  • Posts: 21
  • Reputation 0
  • Gender: Male
  • void of_space();
    • View Profile
Re: Piglatin Converter
« Reply #9 on: April 06, 2014, 11:34:04 pm »
1: No, I wasn't a Pascal programmer. My naming conventions are efficient for me.
2: I know what Strings are. This should easily show it: "if (Word_LowerCase[0] != 'y')"
3: http://msdn.microsoft.com/en-us/library/11w09h50%28v=vs.110%29.aspx
4: I think System.Console.Out.WriteLine is better, as I know exactly where WriteLine is coming from.
5: I don't use LINQ because I haven't learned LINQ, yet. Also, I don't program in C# often, so I didn't know about Select either.


That's all fine, fair enough, but as for 3, for an input string of an unknown locale as a possibility, you should be using a string comparers or specify a CultureInfo, hence my suggestion of using a string (regardless if the function you are using takes in a char array). ToLower() is not the best/greatest method for comparing characters of a string (by itself). Even if it may not pertain here for what OP wants to do, I think it's prevalent to mention for a good read. :)

Run this code:
Code: [Select]
TThread.CurrentThread.CurrentCulture = new CultureInfo("tr-TR");
Console.WriteLine("Equal? " + (char.ToUpper('i') == 'I'));
Console.ReadKey();

And this:
Code: [Select]
Thread.CurrentThread.CurrentCulture = new CultureInfo("tr-TR");
string input = "celestial";
Console.WriteLine("Equal? " + (input.ToUpper() == "CELESTIAL"));
Console.ReadKey();

The.NET BCL has a full library customized for string comparisons that take into consideration localization. If used, they become a useful tool:
Code: [Select]
Thread.CurrentThread.CurrentCulture = new CultureInfo("tr-TR");
string input = "celestial";
Console.WriteLine("Equal? " + (input.ToUpper(CultureInfo.InvariantCulture) == "CELESTIAL"));
Console.ReadKey();

If you keep your method, this would be my suggestion. Why? Because if you don't specify a CultureInfo or a string comparer that will do what you think the code is doing in the first place, you may run into the errors demonstrated by the code examples within my post. The current culture is by default, so if you have other people around the globe using your program, there is a possibility of this kind of error.

I mentioned .NET naming conventions because there IS a standard. Typically camel casing for variables, prefixed with "_" for fields, and PascalCasing for classes, function names, interfaces, structs, enums, etc...

Although there's not much wrong with your code, I just pointed it out because your conventions (although consistent), make no such distinction. And:
Code: [Select]
Word_LowerCase
Is typically the way most Pascal programmers identify 'things'. It was not an insult, so chill. :)
« Last Edit: April 07, 2014, 07:27:22 am by Celestialkey »
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Offline Vlykarye

  • Full Member
  • ***
  • Posts: 131
  • Reputation 34
  • Gender: Male
    • View Profile
Re: Piglatin Converter
« Reply #10 on: April 06, 2014, 11:38:08 pm »
Locale? What are you implying? That I think my code should be run all over the world? Please sir, I live in America. My locale is en-US only. You have no power here.
« Last Edit: April 07, 2014, 07:27:37 am by Celestialkey »

Offline 0xDEAD10CC

  • Newbie
  • *
  • Posts: 21
  • Reputation 0
  • Gender: Male
  • void of_space();
    • View Profile
Re: Piglatin Converter
« Reply #11 on: April 07, 2014, 12:05:06 am »
Locale? What are you implying? That I think my code should be run all over the world? Please sir, I live in America. My locale is en-US only. You have no power here. Either get real or go away.

What YOUR locale is plays no role in the fact that others may not have the same locale as you (en-US). Are you just being stubborn to accept facts here or, do you not fully understand string localization?  ??? I'm trying to keep this civilized. I pointed it out because I thought (with good reason) it was viable to mention.

I think you should read this: http://haacked.com/archive/2012/07/05/turkish-i-problem-and-why-you-should-care.aspx/

Quote
My app is English only. AMURRICA!

Even if you have no plans to translate your application into other languages, your application can be affected by this. After all, the sample I posted is English only.

Perhaps there arenít going to be that many Turkish folks using your app, but why subject the ones that do to easily preventable bugs? If you donít pay attention to this, itís very easy to end up with a costly security bug as a result.

The solution is simple. In most cases, when you compare strings, you want to compare them using StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase. It just turns out there are so many ways to compare strings. Itís not just String.Equals.

Then come back to this thread... And the issue doesn't just exist because the turks wanted to have a different character, this is just one of many examples, for a demonstration on why programmers should care.
« Last Edit: April 07, 2014, 07:29:08 am by Celestialkey »
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Offline Celestialkey

  • Administrator
  • Hero Member
  • *
  • Posts: 3962
  • Reputation 4874
  • Gender: Male
  • Its Alive!!
    • View Profile
    • www.CelestialCoding.com
Re: Piglatin Converter
« Reply #12 on: April 07, 2014, 07:19:43 am »
Locked due to flaming.

Sigh. Mr. Key, can you please just delete all of our posts up to the point at which this began? I would appreciate it.

I cannot delete all of the posts since he has valid points I believe are constructive and important for a general programmers to know and understand. I can and will however remove some of the more emotional posts that contain no real value.

Understand that I will not being doing this again. Next time, I will lock the post as is and issue a warning.

Because they are being removed though, this topic is being locked.
« Last Edit: April 07, 2014, 07:29:54 am by Celestialkey »
Created By: Eczuo
Quote
I have noticed that people who claim that everything is predestined, and we can do nothing to change it, look both ways before they cross the road.
Quote
I'd prefer to die standing, than to live on my knees - Che Guevara
Quote
If you change the way you look at something, does that something change in any way?
- Quantum Theory

Hacking
Quote
Never in the field of human conflict was so much owed by so many to so few. - Winston Churchill


Quote from: Revelations 12:4
And his tail drew the third part of the stars of heaven, and did cast them into the earth; and the dragon stood before the woman which was ready to be delivered, for to devour her child as soon as it was born.

Quote
It takes skill to build an empire. It takes an idiot to maintain it.

 

Donate


* Search


* Recent Posts

Image Comparison by Shishka
[May 15, 2017, 01:18:02 pm]


Re: srchub - free source code hosting by Nathan
[December 14, 2015, 11:37:02 pm]


Re: srchub - free source code hosting by Celestialkey
[November 27, 2015, 08:51:42 am]


Updates by Nathan
[October 30, 2015, 08:27:36 pm]


Re: Client-Server Messaging by Nathan
[October 25, 2015, 05:48:57 pm]