Sha256: c7b0e3b171dc630b977fc423c6e5fc936ec7defc05cc834301f387824250d1b3

Contents?: true

Size: 1.21 KB

Versions: 258

Compression:

Stored size: 1.21 KB

Contents

using System.Linq;
using System.Text.RegularExpressions;

public class PigLatin
{
    public static string Translate(string word)
    {
        return string.Join(" ", word.Split(' ').Select(x => (TranslateWord(x))));
    }

    private static string TranslateWord(string word)
    {
        if (WordStartsWithVowelLike(word))
            return word + "ay";
        if (WordStartsWithPrefixes(word, "thr", "sch"))
            return word.Substring(3) + word.Substring(0, 3) + "ay";
        if (WordStartsWithPrefixes(word, "ch", "qu", "th"))
            return word.Substring(2) + word.Substring(0, 2) + "ay";
        if (WordStartsWithConsonantAndQu(word))
            return word.Substring(3) + word[0] + "quay";

        return word.Substring(1) + word[0] + "ay";
    }

    private static bool WordStartsWithVowelLike(string word)
    {
        return Regex.IsMatch("[aeiou]", word[0].ToString()) || word.StartsWith("yt") || word.StartsWith("xr");
    }

    private static bool WordStartsWithPrefixes(string word, params string[] prefixes)
    {
        return prefixes.Any(word.StartsWith);
    }

    private static bool WordStartsWithConsonantAndQu(string word)
    {
        return word.Substring(1).StartsWith("qu");
    }
}

Version data entries

258 entries across 258 versions & 1 rubygems

Version Path
trackler-2.2.1.0 tracks/csharp/exercises/pig-latin/Example.cs
trackler-2.2.0.6 tracks/csharp/exercises/pig-latin/Example.cs
trackler-2.2.0.5 tracks/csharp/exercises/pig-latin/Example.cs
trackler-2.2.0.4 tracks/csharp/exercises/pig-latin/Example.cs
trackler-2.2.0.3 tracks/csharp/exercises/pig-latin/Example.cs
trackler-2.2.0.2 tracks/csharp/exercises/pig-latin/Example.cs
trackler-2.2.0.1 tracks/csharp/exercises/pig-latin/Example.cs
trackler-2.2.0.0 tracks/csharp/exercises/pig-latin/Example.cs
trackler-2.1.0.55 tracks/csharp/exercises/pig-latin/Example.cs
trackler-2.1.0.54 tracks/csharp/exercises/pig-latin/Example.cs
trackler-2.1.0.53 tracks/csharp/exercises/pig-latin/Example.cs
trackler-2.1.0.52 tracks/csharp/exercises/pig-latin/Example.cs
trackler-2.1.0.51 tracks/csharp/exercises/pig-latin/Example.cs
trackler-2.1.0.50 tracks/csharp/exercises/pig-latin/Example.cs
trackler-2.1.0.49 tracks/csharp/exercises/pig-latin/Example.cs
trackler-2.1.0.48 tracks/csharp/exercises/pig-latin/Example.cs
trackler-2.1.0.47 tracks/csharp/exercises/pig-latin/Example.cs
trackler-2.1.0.46 tracks/csharp/exercises/pig-latin/Example.cs
trackler-2.1.0.45 tracks/csharp/exercises/pig-latin/Example.cs
trackler-2.1.0.44 tracks/csharp/exercises/pig-latin/Example.cs