diff --git a/WorkingWithText/Program.cs b/WorkingWithText/Program.cs index 30c0dfc..4f50cb7 100644 --- a/WorkingWithText/Program.cs +++ b/WorkingWithText/Program.cs @@ -2,6 +2,7 @@ using System.Collections; using System.Linq; using System.Collections.Generic; +using System.Globalization; namespace WorkingWithText { @@ -14,24 +15,104 @@ public static class WorkingWithText // Do not use .Sort, it will cause the test to pass when it actually does not. public static bool IsConsecutive(string hyphenNum) { - return default; + bool valid = false; + + try + { + + + List list = hyphenNum.Split('-').Select(p => int.Parse(p)).ToList(); + + bool hasDuplicates = list.Count != list.Distinct().Count(); + bool isAsc = false; + bool isDsc = false; + + var orderedByAsc = list.OrderBy(d => d); + if (list.SequenceEqual(orderedByAsc)) + { + isAsc = true; + } + + var orderedByDsc = list.OrderByDescending(d => d); + if (list.SequenceEqual(orderedByDsc)) + { + isDsc = true; + } + + if (isAsc && !hasDuplicates || isDsc && !hasDuplicates) + { + valid = true; + } + else + { + valid = false; + } + } + catch (Exception e) + { + valid = false; + } + + + return valid; } // 2- Write a method that accepts a few numbers separated by a hyphen. Check // to see if there are duplicates. If so, return bool True; otherwise, return bool False. public static bool AreThereDuplicates(string hyphenNum) { - return default; + bool valid = false; + + try + { + List list = hyphenNum.Split('-').Select(p => int.Parse(p)).ToList(); + + bool hasDuplicates = list.Count != list.Distinct().Count(); + + if (hasDuplicates) + { + valid = true; + } + else + { + valid = false; + } + } + catch (Exception e) + { + valid = false; + } + + return valid; } + - // 3- Write a method that accepts a string of a time 24-hour time format - // (e.g. "09:00"). A valid time should be between 00:00 and 23:59. If the time is valid, - // return a bool True; otherwise, return a bool False. If the user doesn't provide any values, - // consider it as False. Make sure that its returns false if any letters are passed. - public static bool IsValidTime(string hyphenNum) + // 3- Write a method that accepts a string of a time 24-hour time format + // (e.g. "09:00"). A valid time should be between 00:00 and 23:59. If the time is valid, + // return a bool True; otherwise, return a bool False. If the user doesn't provide any values, + // consider it as False. Make sure that its returns false if any letters are passed. + public static bool IsValidTime(string hyphenNum) { - return default; + bool passed = false; + + string format = "HH:mm"; + + CultureInfo invariant = System.Globalization.CultureInfo.InvariantCulture; + + DateTime dt; + + if (DateTime.TryParseExact(hyphenNum, format, invariant, DateTimeStyles.None, out dt)) + { + passed = true; + } + else + { + passed = false; + } + + return passed; } + // 4- Write a method that accepts a string of a few words separated by a space. Use the // words to create a variable name with PascalCase. For example, if the user types: "number @@ -40,7 +121,21 @@ public static bool IsValidTime(string hyphenNum) // Trim off unneeded spaces. public static string PascalConverter(string aFewWords) { - return default; + string oneWord; + + if(string.IsNullOrEmpty(aFewWords)) + { + return aFewWords; + } + else + { + oneWord = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(aFewWords.Trim().ToLower()); + + oneWord = oneWord.Replace(" ", string.Empty); + + return oneWord; + } + } // 5- Write a method that accepts an English word. Count the number of vowels @@ -48,7 +143,17 @@ public static string PascalConverter(string aFewWords) // return 6. public static int VowelCounter(string aWord) { - return default; + int total = 0; + + for(int i = 0; i < aWord.Length; i++) + { + if (aWord[i] == 'a' || aWord[i] == 'e'|| aWord[i] == 'i'|| aWord[i] == 'o' || aWord[i] == 'u'|| + aWord[i] == 'A' || aWord[i] == 'E'|| aWord[i] == 'I'|| aWord[i] == 'O' || aWord[i] == 'U') + { + total++; + } + } + return total; } } @@ -57,7 +162,7 @@ internal static class Program { private static void Main() { - // Method intentionally left empty. + } } }