Created
November 15, 2018 01:39
-
-
Save rbrick/107feca757d24668ca0f89ae0f6c621c to your computer and use it in GitHub Desktop.
edabit.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| using System.Threading.Tasks; | |
| using System.Text.RegularExpressions; | |
| public class Program | |
| { | |
| static char[] VOWELS = new char[] { 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U' }; | |
| const string VowelSuffix = "yay"; | |
| const string NonVowelSuffix = "ay"; | |
| static Regex punctuation = new Regex("([a-zA-Z]+)([?.!,\"]+)?", RegexOptions.Compiled | RegexOptions.Singleline); | |
| public static string TranslateWord(string word) | |
| { | |
| if (word != null && word.Length > 0) | |
| { | |
| bool shouldCapitalize = char.IsUpper(word[0]); | |
| word = word.ToLower(); | |
| StringBuilder stringBuilder = new StringBuilder(); | |
| if (IsVowel(word[0])) | |
| { | |
| if (shouldCapitalize) | |
| { | |
| word = char.ToUpper(word[0]) + word.Substring(1); | |
| } | |
| return stringBuilder.Append(word).Append(VowelSuffix).ToString(); | |
| } | |
| else | |
| { | |
| int i = 0; | |
| while (!IsVowel(word[i])) { i++; } | |
| string newPrefix = word.Substring(i, word.Length - i); | |
| if (shouldCapitalize) | |
| newPrefix = char.ToUpper(newPrefix[0]) + newPrefix.Substring(1); | |
| return stringBuilder.Append(newPrefix) | |
| .Append(word.Substring(0, i)) | |
| .Append(NonVowelSuffix).ToString(); | |
| } | |
| } | |
| return ""; | |
| } | |
| public static string TranslateSentence(string sentence) | |
| { | |
| if (sentence != null && sentence.Length > 0) | |
| { | |
| StringBuilder builder = new StringBuilder(); | |
| foreach (var s in sentence.Split(' ')) | |
| { | |
| string translated = ""; | |
| if (punctuation.IsMatch(s)) | |
| { | |
| var match = punctuation.Match(s); | |
| translated = TranslateWord(match.Result("$1")); | |
| translated = Regex.Replace(s, "([a-zA-Z]+)", translated); | |
| } | |
| else | |
| { | |
| translated = TranslateWord(s); | |
| } | |
| builder.Append(translated); | |
| builder.Append(' '); | |
| } | |
| return builder.ToString().Trim(); | |
| } | |
| return ""; | |
| } | |
| public static string SilenceTrump(string str) => string.Concat(str.Select(c => | |
| { | |
| if (!VOWELS.Contains(c)) | |
| return c; | |
| return (char)0; | |
| }).Where((i) => i > 0)); | |
| static bool IsVowel(char x) | |
| { | |
| return VOWELS.Contains(x); | |
| } | |
| public static bool IsAnagram(string str1, string str2) | |
| { | |
| return str2.ToLower().ToArray().All(c => str1.ToLower().Contains(c)) && str1.Length == str2.Length; | |
| } | |
| public static bool ValidateEmail(string str) | |
| { | |
| // e@something.com | |
| var result = str.Split('@'); | |
| return result.Length == 2 && !string.IsNullOrEmpty(result[0]) | |
| && result[1].Contains("."); | |
| } | |
| public static string Reverse(string str) | |
| { | |
| return string.Join(" ", str.Split(' ').Select((s) => | |
| { | |
| if (s.Length >= 5) | |
| { | |
| return string.Concat(s.Reverse()); | |
| } | |
| return s; | |
| })); | |
| } | |
| public static string FlipEndChars(object str) | |
| { | |
| if (!(str is string)) return "Incompatible."; | |
| if (str.ToString().Length < 2) return "Incompatible."; | |
| string x = str.ToString(); | |
| char last = x[x.Length - 1]; | |
| char first = x[0]; | |
| if (last == first) return "Two's a pair."; | |
| return last + x.Substring(1, x.Length - 1) + first; | |
| } | |
| public static string RemoveSpecialCharacters(string str) | |
| => string.Concat(str.Where(c => char.IsLetterOrDigit(c) || "-_ ".Contains(c))); | |
| public static bool IsIsogram(string str) | |
| => str.ToLower().Distinct().Count() == str.Length; | |
| public static string AlternatingCaps(string str) | |
| { | |
| // state machine | |
| int state = 0; | |
| return string.Concat(str.Select(c => | |
| { | |
| char x = state == 0 ? char.ToUpper(c) : char.ToLower(c); | |
| if (state == 0) state = 1; | |
| else state = 0; | |
| return x; | |
| })); | |
| } | |
| public static int[] RemoveSmallest(int[] values) | |
| { | |
| if (values.Length == 0) return values; | |
| int min = values.Min(); | |
| bool taken = false; | |
| List<int> list = new List<int>(); | |
| foreach (var x in values) | |
| { | |
| if (x == min && !taken) | |
| { | |
| taken = true; | |
| continue; | |
| } | |
| list.Add(x); | |
| } | |
| return list.ToArray(); | |
| } | |
| public static int[] CountPosSumNeg(double[] arr) | |
| { | |
| if (arr == null || arr.Length == 0) return new int[0]; | |
| return new int[] { (int)arr.Where(x => x > 0).Count(), (int)arr.Where(x => x < 0).Sum() }; | |
| } | |
| public static int SumSmallest(int[] values) | |
| => values.Where(x => x > 0).OrderBy(x => x).Take(2).Sum(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment