Adding Strings and unit test
This commit is contained in:
@@ -61,6 +61,25 @@ namespace BasicProgramming
|
|||||||
Console.WriteLine($"DrawTriangle(10):\n{DrawTriangle(10)}");
|
Console.WriteLine($"DrawTriangle(10):\n{DrawTriangle(10)}");
|
||||||
Console.WriteLine($"ToThePowerOf(-2, 3): {ToThePowerOf(-2, 3)}");
|
Console.WriteLine($"ToThePowerOf(-2, 3): {ToThePowerOf(-2, 3)}");
|
||||||
Console.WriteLine($"ToThePowerOf(5, 5): {ToThePowerOf(5, 5)}");
|
Console.WriteLine($"ToThePowerOf(5, 5): {ToThePowerOf(5, 5)}");
|
||||||
|
Console.WriteLine("====== Strings ======");
|
||||||
|
Console.WriteLine($"AddSeparator(\"ABCD\", \"^\"): {AddSeparator("ABCD", "^")}");
|
||||||
|
Console.WriteLine($"AddSeparator(\"chocolate\", \"-\"): {AddSeparator("chocolate", "-")}");
|
||||||
|
Console.WriteLine($"IsPalindrome(\"eye\"): {IsPalindrome("eye")}");
|
||||||
|
Console.WriteLine($"IsPalindrome(\"home\"): {IsPalindrome("home")}");
|
||||||
|
Console.WriteLine($"LengthOfAString(\"computer\"): {LengthOfAString("computer")}");
|
||||||
|
Console.WriteLine($"LengthOfAString(\"ice cream\"): {LengthOfAString("ice cream")}");
|
||||||
|
Console.WriteLine($"StringInReverseOrder(\"qwerty\"): {StringInReverseOrder("qwerty")}");
|
||||||
|
Console.WriteLine($"StringInReverseOrder(\"oe93 kr\"): {StringInReverseOrder("oe93 kr")}");
|
||||||
|
Console.WriteLine($"NumberOfWords(\"This is sample sentence\"): {NumberOfWords("This is sample sentence")}");
|
||||||
|
Console.WriteLine($"NumberOfWords(\"OK\"): {NumberOfWords("OK")}");
|
||||||
|
Console.WriteLine($"RevertWordsOrder(\"John Doe.\"): {RevertWordsOrder("John Doe.")}");
|
||||||
|
Console.WriteLine($"RevertWordsOrder(\"A, B. C\"): {RevertWordsOrder("A, B. C")}");
|
||||||
|
Console.WriteLine($"HowManyOccurrences(\"do it now\", \"do\"): {HowManyOccurrences("do it now", "do")}");
|
||||||
|
Console.WriteLine($"HowManyOccurrences(\"empty\", \"d\"): {HowManyOccurrences("empty", "d")}");
|
||||||
|
Console.WriteLine($"SortCharactersDescending(\"onomatopoeia\"): {JsonSerializer.Serialize(SortCharactersDescending("onomatopoeia"))}");
|
||||||
|
Console.WriteLine($"SortCharactersDescending(\"fohjwf42os\"): {JsonSerializer.Serialize(SortCharactersDescending("fohjwf42os"))}");
|
||||||
|
Console.WriteLine($"CompressString(\"kkkktttrrrrrrrrrr\"): {CompressString("kkkktttrrrrrrrrrr")}");
|
||||||
|
Console.WriteLine($"CompressString(\"p555ppp7www\"): {CompressString("p555ppp7www")}");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int AddAndMultiply(int a, int b, int c)
|
public static int AddAndMultiply(int a, int b, int c)
|
||||||
@@ -361,5 +380,81 @@ namespace BasicProgramming
|
|||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static string AddSeparator(string str, string seperator)
|
||||||
|
{
|
||||||
|
return string.Join(seperator, str.ToList());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool IsPalindrome(string str)
|
||||||
|
{
|
||||||
|
bool isPalindrome = true;
|
||||||
|
|
||||||
|
var stringParts = str.ToList();
|
||||||
|
|
||||||
|
for (int i = 0; i < stringParts.Count - 1; i++)
|
||||||
|
{
|
||||||
|
if (!stringParts[i].Equals(stringParts[stringParts.Count - i - 1]))
|
||||||
|
isPalindrome = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return isPalindrome;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int LengthOfAString(string str)
|
||||||
|
{
|
||||||
|
return str.Count();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string StringInReverseOrder(string str)
|
||||||
|
{
|
||||||
|
return string.Join("", str.Reverse());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int NumberOfWords(string str)
|
||||||
|
{
|
||||||
|
return str.Split(" ").Count();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string RevertWordsOrder(string str)
|
||||||
|
{
|
||||||
|
return string.Join(" ", str.Split(" ").Reverse());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int HowManyOccurrences(string str, string strToCheck)
|
||||||
|
{
|
||||||
|
return str.Split(" ").Count(x => x.Equals(strToCheck));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static char[] SortCharactersDescending(string str)
|
||||||
|
{
|
||||||
|
return str.OrderBy(x => (char)x).Reverse().ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string CompressString(string str)
|
||||||
|
{
|
||||||
|
StringBuilder retval = new StringBuilder();
|
||||||
|
|
||||||
|
int count = 1;
|
||||||
|
|
||||||
|
for (int i = 1; i < str.Length; i++)
|
||||||
|
{
|
||||||
|
if (str[i] == str[i - 1])
|
||||||
|
{
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
retval.Append(str[i - 1]);
|
||||||
|
retval.Append(count);
|
||||||
|
count = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
retval.Append(str[^1]);
|
||||||
|
retval.Append(count);
|
||||||
|
|
||||||
|
return retval.ToString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
69
BasicProgrammingUnitTest/BasicProgramming__STRINGS.cs
Normal file
69
BasicProgrammingUnitTest/BasicProgramming__STRINGS.cs
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
using static BasicProgramming.Program;
|
||||||
|
|
||||||
|
namespace BasicProgrammingUnitTest;
|
||||||
|
|
||||||
|
public class BasicProgramming__STRINGS
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void AddSeperator_TEST()
|
||||||
|
{
|
||||||
|
Assert.Equal("A^B^C^D", AddSeparator("ABCD", "^"));
|
||||||
|
Assert.Equal("c-h-o-c-o-l-a-t-e", AddSeparator("chocolate", "-"));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void IsPalindrome_TEST()
|
||||||
|
{
|
||||||
|
Assert.True(IsPalindrome("eye"));
|
||||||
|
Assert.False(IsPalindrome("home"));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void LengthOfAString_TEST()
|
||||||
|
{
|
||||||
|
Assert.Equal(8, LengthOfAString("computer"));
|
||||||
|
Assert.Equal(9, LengthOfAString("ice cream"));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void StringInReverseOrder_TEST()
|
||||||
|
{
|
||||||
|
Assert.Equal("ytrewq", StringInReverseOrder("qwerty"));
|
||||||
|
Assert.Equal("rk 39eo", StringInReverseOrder("oe93 kr"));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void NumberOfWords_TEST()
|
||||||
|
{
|
||||||
|
Assert.Equal(4, NumberOfWords("This is sample sentence"));
|
||||||
|
Assert.Equal(1, NumberOfWords("OK"));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void RevertWordsOrder_TEST()
|
||||||
|
{
|
||||||
|
Assert.Equal("Doe. John", RevertWordsOrder("John Doe."));
|
||||||
|
Assert.Equal("C B. A,", RevertWordsOrder("A, B. C"));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void HowManyOccurrences_TEST()
|
||||||
|
{
|
||||||
|
Assert.Equal(1, HowManyOccurrences("do it now", "do"));
|
||||||
|
Assert.Equal(0, HowManyOccurrences("empty", "d"));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SortCharactersDescending_TEST()
|
||||||
|
{
|
||||||
|
Assert.Equal(['t', 'p', 'o', 'o', 'o', 'o', 'n', 'm', 'i', 'e', 'a', 'a'], SortCharactersDescending("onomatopoeia"));
|
||||||
|
Assert.Equal(['w', 's', 'o', 'o', 'j', 'h', 'f', 'f', '4', '2'], SortCharactersDescending("fohjwf42os"));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void CompressString_TEST()
|
||||||
|
{
|
||||||
|
Assert.Equal("k4t3r10", CompressString("kkkktttrrrrrrrrrr"));
|
||||||
|
Assert.Equal("p153p371w3", CompressString("p555ppp7www"));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user