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.40 tracks/csharp/exercises/pig-latin/Example.cs
trackler-2.2.1.39 tracks/csharp/exercises/pig-latin/Example.cs
trackler-2.2.1.38 tracks/csharp/exercises/pig-latin/Example.cs
trackler-2.2.1.37 tracks/csharp/exercises/pig-latin/Example.cs
trackler-2.2.1.36 tracks/csharp/exercises/pig-latin/Example.cs
trackler-2.2.1.35 tracks/csharp/exercises/pig-latin/Example.cs
trackler-2.2.1.34 tracks/csharp/exercises/pig-latin/Example.cs
trackler-2.2.1.33 tracks/csharp/exercises/pig-latin/Example.cs
trackler-2.2.1.32 tracks/csharp/exercises/pig-latin/Example.cs
trackler-2.2.1.31 tracks/csharp/exercises/pig-latin/Example.cs
trackler-2.2.1.30 tracks/csharp/exercises/pig-latin/Example.cs
trackler-2.2.1.29 tracks/csharp/exercises/pig-latin/Example.cs
trackler-2.2.1.28 tracks/csharp/exercises/pig-latin/Example.cs
trackler-2.2.1.27 tracks/csharp/exercises/pig-latin/Example.cs
trackler-2.2.1.26 tracks/csharp/exercises/pig-latin/Example.cs
trackler-2.2.1.25 tracks/csharp/exercises/pig-latin/Example.cs
trackler-2.2.1.24 tracks/csharp/exercises/pig-latin/Example.cs
trackler-2.2.1.23 tracks/csharp/exercises/pig-latin/Example.cs
trackler-2.2.1.22 tracks/csharp/exercises/pig-latin/Example.cs
trackler-2.2.1.21 tracks/csharp/exercises/pig-latin/Example.cs