Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 116 additions & 11 deletions WorkingWithText/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;

namespace WorkingWithText
{
Expand All @@ -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
Copy link
Member

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.

{


List<int> list = hyphenNum.Split('-').Select(p => int.Parse(p)).ToList();
Copy link
Member

@yurisim yurisim Aug 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can instead do

Suggested change
List<int> list = hyphenNum.Split('-').Select(p => int.Parse(p)).ToList();
List<int> list = hyphenNum.Split('-').Select(int.Parse).ToList();

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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (DateTime.TryParseExact(hyphenNum, format, invariant, DateTimeStyles.None, out dt))
return (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
Expand All @@ -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());
Copy link
Member

Choose a reason for hiding this comment

The 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'||
Copy link
Member

Choose a reason for hiding this comment

The 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;
}
}

Expand All @@ -57,7 +162,7 @@ internal static class Program
{
private static void Main()
{
// Method intentionally left empty.

}
}
}