-
Notifications
You must be signed in to change notification settings - Fork 8
complete #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
complete #9
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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<int> list = hyphenNum.Split('-').Select(p => int.Parse(p)).ToList(); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can instead do
Suggested change
when using single character wheres and selects. For example, can use someString.Where(char.IsNumber) without having to do the lambda expression |
||||||
|
|
||||||
| 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<int> 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)) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| { | ||||||
| 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,15 +121,39 @@ 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()); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was expecting a split instead of ToTitleCast But this also works. |
||||||
|
|
||||||
| oneWord = oneWord.Replace(" ", string.Empty); | ||||||
|
|
||||||
| return oneWord; | ||||||
| } | ||||||
|
|
||||||
| } | ||||||
|
|
||||||
| // 5- Write a method that accepts an English word. Count the number of vowels | ||||||
| // (a, e, i, o, u) in the word. So, if the user enters "inadequate", the program should | ||||||
| // 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'|| | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use .Contains instead of this OR statement. Also toLower (or toUpper) the input so that you only need to worry about one case of vowels instead of both. |
||||||
| 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. | ||||||
|
|
||||||
| } | ||||||
| } | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was surprised to see a try-catch! However, a try-parse @ line 24 will work just the same.