Changing from Fact to Theory, and moving methods into classes

This commit is contained in:
2025-08-06 12:54:44 +02:00
parent a70ea5d707
commit 39ecc637e8
15 changed files with 802 additions and 734 deletions

View File

@@ -4,7 +4,7 @@ VisualStudioVersion = 17.5.2.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BasicProgramming", "BasicProgramming\BasicProgramming.csproj", "{F852DB4D-952E-CFAF-DFC6-298FB48C8A28}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BasicProgrammingUnitTest", "BasicProgrammingUnitTest\BasicProgrammingUnitTest.csproj", "{427B1690-1D9B-4002-A6DE-435867230BAC}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BasicProgrammingTests", "BasicProgrammingUnitTest\BasicProgrammingTests.csproj", "{427B1690-1D9B-4002-A6DE-435867230BAC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution

49
BasicProgramming/Basic.cs Normal file
View File

@@ -0,0 +1,49 @@
namespace BasicProgramming
{
public class Basic
{
public int AddAndMultiply(int a, int b, int c)
{
return (a + b) * c;
}
public string CtoF(int celcius)
{
if (celcius <= -271.15)
return "Temperature below absolute zero!";
double fahrenheit = (celcius * 1.8) + 32;
return $"T = {fahrenheit}F";
}
public List<double> ElementaryOperations(double a, double b)
{
List<double> retval = [a + b, a - b, a * b];
if (a > 0 && b > 0)
retval.Add(a / b);
return retval;
}
public bool IsResultTheSame(double a, double b)
{
return a == b;
}
public int ModuloOperations(int a, int b, int c)
{
return a % b % c;
}
public double CubeOf(double a)
{
return a * a * a;
}
public List<int> SwapTwoNumbers(int a, int b)
{
return [b, a];
}
}
}

183
BasicProgramming/Loops.cs Normal file
View File

@@ -0,0 +1,183 @@
using System.Text;
namespace BasicProgramming
{
public class Loops
{
public List<List<int>> MultiplicationTable(int x, int y)
{
var rows = new List<List<int>>();
for (int i = 0; i < y; i++)
{
var colums = new List<int>();
for (int b = 0; b < x; b++)
{
colums.Add((i + 1) * (b + 1));
}
rows.Add(colums);
}
return rows;
}
public int TheBiggestNumber(List<int> numbers)
{
int biggestNumber = int.MinValue;
numbers.ForEach(x =>
{
if (biggestNumber < x)
biggestNumber = x;
});
return biggestNumber;
}
public int Two7sNextToEachOther(List<int> numbers)
{
int count = 0;
for (int i = 0; i < (numbers.Count - 1); i++)
{
if (numbers[i] == 7 && numbers[i + 1] == 7)
{
count++;
}
}
return count;
}
public bool ThreeIncreasingAdjacent(List<int> numbers)
{
for (int i = 0; i < (numbers.Count - 2); i++)
{
int currentNumber = numbers[i];
if ((numbers[i + 1] - 1).Equals(currentNumber) && (numbers[i + 2] - 2).Equals(currentNumber))
{
return true;
}
}
return false;
}
public List<int> SieveOfEratosthenes(int maxPrime)
{
List<int> primeList = new List<int>();
for (int i = 0; i < maxPrime; i++)
{
if (IsPrime(i))
{
primeList.Add(i);
}
}
return primeList;
}
private bool IsPrime(int number)
{
if (number <= 1) return false;
if (number == 2) return true;
if (number % 2 == 0) return false;
var boundary = (int)Math.Floor(Math.Sqrt(number));
for (int i = 3; i <= boundary; i += 2)
{
if (number % i == 0)
{
return false;
}
}
return true;
}
public string ExtractString(string str)
{
if (str.Contains("##"))
{
var parts = str.Split("##");
if (parts.Count() > 2)
return parts[1];
}
return string.Empty;
}
public string FullSequenceOfLetters(string str)
{
var retval = new StringBuilder();
var startLetter = str[0];
var endLetter = str[1];
retval.Append(startLetter);
char Lastchar = startLetter;
do
{
char nextChar = (char)((int)Lastchar + 1);
Lastchar = nextChar;
retval.Append(nextChar.ToString());
}
while (Lastchar != endLetter);
return retval.ToString();
}
public string SumAndAverage(int a, int b)
{
var sum = 0;
for (int i = a; i <= b; i++)
{
sum = sum + i;
}
double average = (double)sum / (b - a + 1);
return $"Sum: {sum}, Average: {average}";
}
public string DrawTriangle(int height)
{
var sb = new StringBuilder();
var layer = 0;
for (int i = 0; i < height; i++)
{
for (int y = 0; y < height - 1 - layer; y++)
{
sb.Append(" ");
}
for (int y = 0; y < layer; y++)
{
sb.Append("**");
}
sb.Append("*");
if (layer + 1 != height)
{
sb.AppendLine();
}
layer++;
}
return sb.ToString();
}
public int ToThePowerOf(int a, int b)
{
var retval = a;
for (int i = 0; i < b - 1; i++)
{
retval = retval * a;
}
return retval;
}
}
}

View File

@@ -8,453 +8,78 @@ namespace BasicProgramming
{
static void Main(string[] args)
{
Console.WriteLine("====== Basic Programming ======");
Console.WriteLine($"AddAndMultiply: {AddAndMultiply(2, 4, 5)}");
Console.WriteLine($"CToF(0): {CtoF(0)}");
Console.WriteLine($"CToF(100): {CtoF(100)}");
Console.WriteLine($"CToF(-300): {CtoF(-300)}");
Console.WriteLine($"ElementaryOperations(3, 8): {string.Join(", ", ElementaryOperations(3, 8))}");
Console.WriteLine($"IsResultTheSame(2+2, 2*2): {IsResultTheSame(2 + 2, 2 * 2)}");
Console.WriteLine($"IsResultTheSame(9/3, 16-1): {IsResultTheSame(9 / 3, 16 - 1)}");
Console.WriteLine($"ModuloOperations(8, 5, 2): {ModuloOperations(8, 5, 2)}");
Console.WriteLine($"CubeOf(2): {CubeOf(2)}");
Console.WriteLine($"CubeOf(-5.5): {CubeOf(-5.5)}");
Console.WriteLine($"SwapTwoNumbers(87, 45): {string.Join(", ", SwapTwoNumbers(87, 45))}");
Console.WriteLine($"SwapTwoNumbers(-13, 2): {string.Join(", ", SwapTwoNumbers(-13, 2))}");
Console.WriteLine("====== Conditional Statements ======");
Console.WriteLine($"AbsoluteValue(6832): {AbsoluteValue(6832)}");
Console.WriteLine($"AbsoluteValue(-392): {AbsoluteValue(-392)}");
Console.WriteLine($"DivisibleBy2Or3(15, 30): {DivisibleBy2Or3(15, 30)}");
Console.WriteLine($"DivisibleBy2Or3(2, 90): {DivisibleBy2Or3(2, 90)}");
Console.WriteLine($"DivisibleBy2Or3(7, 12): {DivisibleBy2Or3(7, 12)}");
Console.WriteLine($"IfConsistsOfUppercaseLetters('xyz'): {IfConsistsOfUppercaseLetters("xyz")}");
Console.WriteLine($"IfConsistsOfUppercaseLetters('DOG'): {IfConsistsOfUppercaseLetters("DOG")}");
Console.WriteLine($"IfConsistsOfUppercaseLetters('L9#'): {IfConsistsOfUppercaseLetters("L9#")}");
Console.WriteLine($"IfGreaterThanThirdOne([2, 7, 12]) : {IfGreaterThanThirdOne([2, 7, 12])}");
Console.WriteLine($"IfGreaterThanThirdOne([-5, -8, 50]): {IfGreaterThanThirdOne([-5, -8, 50])}");
Console.WriteLine($"IfNumberIsEven(721): {IfNumberIsEven(721)}");
Console.WriteLine($"IfNumberIsEven(1248): {IfNumberIsEven(1248)}");
Console.WriteLine($"IfSortedAscending([3, 7, 10]): {IfSortedAscending([3, 7, 10])}");
Console.WriteLine($"IfSortedAscending([74, 62, 99]): {IfSortedAscending([74, 62, 99])}");
Console.WriteLine($"PositiveNegativeOrZero(5.24): {PositiveNegativeOrZero(5.24)}");
Console.WriteLine($"PositiveNegativeOrZero(0.0): {PositiveNegativeOrZero(0.0)}");
Console.WriteLine($"PositiveNegativeOrZero(-994.53): {PositiveNegativeOrZero(-994.53)}");
Console.WriteLine($"IfYearIsLeap(2016): {IfYearIsLeap(2016)}");
Console.WriteLine($"IfYearIsLeap(2018): {IfYearIsLeap(2018)}");
Console.WriteLine("====== Loops ======");
Console.WriteLine($"MultiplicationTable(10, 10): {JsonSerializer.Serialize(MultiplicationTable(10, 10))}");
Console.WriteLine($"TheBiggestNumber([190, 291, 145, 209, 280, 200]): {TheBiggestNumber([190, 291, 145, 209, 280, 200])}");
Console.WriteLine($"TheBiggestNumber([-9, -2, -7, -8, -4]): {TheBiggestNumber([-9, -2, -7, -8, -4])}");
Console.WriteLine($"Two7sNextToEachOther([ 8, 2, 5, 7, 9, 0, 7, 7, 3, 1]): {Two7sNextToEachOther([8, 2, 5, 7, 9, 0, 7, 7, 3, 1])}");
Console.WriteLine($"Two7sNextToEachOther([ 9, 4, 5, 3, 7, 7, 7, 3, 2, 5, 7, 7 ]): {Two7sNextToEachOther([9, 4, 5, 3, 7, 7, 7, 3, 2, 5, 7, 7])}");
Console.WriteLine($"ThreeIncreasingAdjacent([45, 23, 44, 68, 65, 70, 80, 81, 82 ]): {ThreeIncreasingAdjacent([45, 23, 44, 68, 65, 70, 80, 81, 82])}");
Console.WriteLine($"ThreeIncreasingAdjacent([7, 3, 5, 8, 9, 3, 1, 4 ]): {ThreeIncreasingAdjacent([7, 3, 5, 8, 9, 3, 1, 4])}");
Console.WriteLine($"SieveOfEratosthenes(30): {JsonSerializer.Serialize(SieveOfEratosthenes(30))}");
Console.WriteLine($"ExtractString(\"##abc##def\"): {ExtractString("##abc##def")}");
Console.WriteLine($"ExtractString(\"12####78\"): {ExtractString("12####78")}");
Console.WriteLine($"ExtractString(\"gar##d#en\"): {ExtractString("gar##d#en")}");
Console.WriteLine($"ExtractString(\"++##--##++\"): {ExtractString("++##--##++")}");
Console.WriteLine($"FullSequenceOfLetters(\"ds\"): {FullSequenceOfLetters("ds")}");
Console.WriteLine($"FullSequenceOfLetters(\"or\"): {FullSequenceOfLetters("or")}");
Console.WriteLine($"SumAndAverage(11, 66): {SumAndAverage(11, 66)}");
Console.WriteLine($"SumAndAverage(-10, 0): {SumAndAverage(-10, 0)}");
Console.WriteLine($"DrawTriangle(10):\n{DrawTriangle(10)}");
Console.WriteLine($"ToThePowerOf(-2, 3): {ToThePowerOf(-2, 3)}");
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)
{
return (a + b) * c;
}
public static string CtoF(int celcius)
{
if (celcius <= -271.15)
return "Temperature below absolute zero!";
double fahrenheit = (celcius * 1.8) + 32;
return $"T = {fahrenheit}F";
}
public static List<double> ElementaryOperations(double a, double b)
{
List<double> retval = [a + b, a - b, a * b];
if (a > 0 && b > 0)
retval.Add(a / b);
return retval;
}
public static bool IsResultTheSame(double a, double b)
{
return a == b;
}
public static int ModuloOperations(int a, int b, int c)
{
return a % b % c;
}
public static double CubeOf(double a)
{
return a * a * a;
}
public static List<int> SwapTwoNumbers(int a, int b)
{
return [b, a];
}
public static int AbsoluteValue(int a)
{
return Math.Abs(a);
}
public static int DivisibleBy2Or3(int a, int b)
{
if (_DivisibleBy2Or3(a) && _DivisibleBy2Or3(b))
{
return a * b;
}
static bool _DivisibleBy2Or3(int x)
{
return (x % 3) == 0 || (x % 2) == 0;
}
return a + b;
}
public static bool IfConsistsOfUppercaseLetters(string str)
{
return str.Equals(Regex.Match(str, "[A-ZÆØÅ]+").Value);
}
public static bool IfGreaterThanThirdOne(List<int> ints)
{
if (ints.Count < 3)
{
throw new Exception("Must have 3 or more ints");
}
return (ints[0] * ints[1]) > ints[2];
}
public static bool IfNumberIsEven(int a)
{
return a % 2 == 0;
}
public static bool IfSortedAscending(List<int> ints)
{
int lastNumber = 0;
bool retval = true;
ints.ForEach(x =>
{
if (lastNumber < x)
{
lastNumber = x;
}
else
{
retval = false;
}
});
return retval;
}
public static string PositiveNegativeOrZero(double a)
{
switch (a)
{
case > 0:
return "positive";
case < 0:
return "negative";
default:
return "zero";
}
}
public static bool IfYearIsLeap(int year)
{
return DateTime.IsLeapYear(year);
}
public static List<List<int>> MultiplicationTable(int x, int y)
{
var rows = new List<List<int>>();
for (int i = 0; i < y; i++)
{
var colums = new List<int>();
for (int b = 0; b < x; b++)
{
colums.Add((i + 1) * (b + 1));
}
rows.Add(colums);
}
return rows;
}
public static int TheBiggestNumber(List<int> numbers)
{
int biggestNumber = int.MinValue;
numbers.ForEach(x =>
{
if (biggestNumber < x)
biggestNumber = x;
});
return biggestNumber;
}
public static int Two7sNextToEachOther(List<int> numbers)
{
int count = 0;
for (int i = 0; i < (numbers.Count - 1); i++)
{
if (numbers[i] == 7 && numbers[i + 1] == 7)
{
count++;
}
}
return count;
}
public static bool ThreeIncreasingAdjacent(List<int> numbers)
{
for (int i = 0; i < (numbers.Count - 2); i++)
{
int currentNumber = numbers[i];
if ((numbers[i + 1] - 1).Equals(currentNumber) && (numbers[i + 2] - 2).Equals(currentNumber))
{
return true;
}
}
return false;
}
public static List<int> SieveOfEratosthenes(int maxPrime)
{
List<int> primeList = new List<int>();
for (int i = 0; i < maxPrime; i++)
{
if (IsPrime(i))
{
primeList.Add(i);
}
}
return primeList;
}
private static bool IsPrime(int number)
{
if (number <= 1) return false;
if (number == 2) return true;
if (number % 2 == 0) return false;
var boundary = (int)Math.Floor(Math.Sqrt(number));
for (int i = 3; i <= boundary; i += 2)
{
if (number % i == 0)
{
return false;
}
}
return true;
}
public static string ExtractString(string str)
{
if (str.Contains("##"))
{
var parts = str.Split("##");
if (parts.Count() > 2)
return parts[1];
}
return string.Empty;
}
public static string FullSequenceOfLetters(string str)
{
var retval = new StringBuilder();
var startLetter = str[0];
var endLetter = str[1];
retval.Append(startLetter);
char Lastchar = startLetter;
do
{
char nextChar = (char)((int)Lastchar + 1);
Lastchar = nextChar;
retval.Append(nextChar.ToString());
}
while (Lastchar != endLetter);
return retval.ToString();
}
public static string SumAndAverage(int a, int b)
{
var sum = 0;
for (int i = a; i <= b; i++)
{
sum = sum + i;
}
double average = (double)sum / (b - a + 1);
return $"Sum: {sum}, Average: {average}";
}
public static string DrawTriangle(int height)
{
var sb = new StringBuilder();
var layer = 0;
for (int i = 0; i < height; i++)
{
for (int y = 0; y < height - 1 - layer; y++)
{
sb.Append(" ");
}
for (int y = 0; y < layer; y++)
{
sb.Append("**");
}
sb.Append("*");
if (layer + 1 != height)
{
sb.AppendLine();
}
layer++;
}
return sb.ToString();
}
public static int ToThePowerOf(int a, int b)
{
var retval = a;
for (int i = 0; i < b - 1; i++)
{
retval = retval * a;
}
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();
// Console.WriteLine("====== Basic Programming ======");
// Console.WriteLine($"AddAndMultiply: {AddAndMultiply(2, 4, 5)}");
// Console.WriteLine($"CToF(0): {CtoF(0)}");
// Console.WriteLine($"CToF(100): {CtoF(100)}");
// Console.WriteLine($"CToF(-300): {CtoF(-300)}");
// Console.WriteLine($"ElementaryOperations(3, 8): {string.Join(", ", ElementaryOperations(3, 8))}");
// Console.WriteLine($"IsResultTheSame(2+2, 2*2): {IsResultTheSame(2 + 2, 2 * 2)}");
// Console.WriteLine($"IsResultTheSame(9/3, 16-1): {IsResultTheSame(9 / 3, 16 - 1)}");
// Console.WriteLine($"ModuloOperations(8, 5, 2): {ModuloOperations(8, 5, 2)}");
// Console.WriteLine($"CubeOf(2): {CubeOf(2)}");
// Console.WriteLine($"CubeOf(-5.5): {CubeOf(-5.5)}");
// Console.WriteLine($"SwapTwoNumbers(87, 45): {string.Join(", ", SwapTwoNumbers(87, 45))}");
// Console.WriteLine($"SwapTwoNumbers(-13, 2): {string.Join(", ", SwapTwoNumbers(-13, 2))}");
// Console.WriteLine("====== Conditional Statements ======");
// Console.WriteLine($"AbsoluteValue(6832): {AbsoluteValue(6832)}");
// Console.WriteLine($"AbsoluteValue(-392): {AbsoluteValue(-392)}");
// Console.WriteLine($"DivisibleBy2Or3(15, 30): {DivisibleBy2Or3(15, 30)}");
// Console.WriteLine($"DivisibleBy2Or3(2, 90): {DivisibleBy2Or3(2, 90)}");
// Console.WriteLine($"DivisibleBy2Or3(7, 12): {DivisibleBy2Or3(7, 12)}");
// Console.WriteLine($"IfConsistsOfUppercaseLetters('xyz'): {IfConsistsOfUppercaseLetters("xyz")}");
// Console.WriteLine($"IfConsistsOfUppercaseLetters('DOG'): {IfConsistsOfUppercaseLetters("DOG")}");
// Console.WriteLine($"IfConsistsOfUppercaseLetters('L9#'): {IfConsistsOfUppercaseLetters("L9#")}");
// Console.WriteLine($"IfGreaterThanThirdOne([2, 7, 12]) : {IfGreaterThanThirdOne([2, 7, 12])}");
// Console.WriteLine($"IfGreaterThanThirdOne([-5, -8, 50]): {IfGreaterThanThirdOne([-5, -8, 50])}");
// Console.WriteLine($"IfNumberIsEven(721): {IfNumberIsEven(721)}");
// Console.WriteLine($"IfNumberIsEven(1248): {IfNumberIsEven(1248)}");
// Console.WriteLine($"IfSortedAscending([3, 7, 10]): {IfSortedAscending([3, 7, 10])}");
// Console.WriteLine($"IfSortedAscending([74, 62, 99]): {IfSortedAscending([74, 62, 99])}");
// Console.WriteLine($"PositiveNegativeOrZero(5.24): {PositiveNegativeOrZero(5.24)}");
// Console.WriteLine($"PositiveNegativeOrZero(0.0): {PositiveNegativeOrZero(0.0)}");
// Console.WriteLine($"PositiveNegativeOrZero(-994.53): {PositiveNegativeOrZero(-994.53)}");
// Console.WriteLine($"IfYearIsLeap(2016): {IfYearIsLeap(2016)}");
// Console.WriteLine($"IfYearIsLeap(2018): {IfYearIsLeap(2018)}");
// Console.WriteLine("====== Loops ======");
// Console.WriteLine($"MultiplicationTable(10, 10): {JsonSerializer.Serialize(MultiplicationTable(10, 10))}");
// Console.WriteLine($"TheBiggestNumber([190, 291, 145, 209, 280, 200]): {TheBiggestNumber([190, 291, 145, 209, 280, 200])}");
// Console.WriteLine($"TheBiggestNumber([-9, -2, -7, -8, -4]): {TheBiggestNumber([-9, -2, -7, -8, -4])}");
// Console.WriteLine($"Two7sNextToEachOther([ 8, 2, 5, 7, 9, 0, 7, 7, 3, 1]): {Two7sNextToEachOther([8, 2, 5, 7, 9, 0, 7, 7, 3, 1])}");
// Console.WriteLine($"Two7sNextToEachOther([ 9, 4, 5, 3, 7, 7, 7, 3, 2, 5, 7, 7 ]): {Two7sNextToEachOther([9, 4, 5, 3, 7, 7, 7, 3, 2, 5, 7, 7])}");
// Console.WriteLine($"ThreeIncreasingAdjacent([45, 23, 44, 68, 65, 70, 80, 81, 82 ]): {ThreeIncreasingAdjacent([45, 23, 44, 68, 65, 70, 80, 81, 82])}");
// Console.WriteLine($"ThreeIncreasingAdjacent([7, 3, 5, 8, 9, 3, 1, 4 ]): {ThreeIncreasingAdjacent([7, 3, 5, 8, 9, 3, 1, 4])}");
// Console.WriteLine($"SieveOfEratosthenes(30): {JsonSerializer.Serialize(SieveOfEratosthenes(30))}");
// Console.WriteLine($"ExtractString(\"##abc##def\"): {ExtractString("##abc##def")}");
// Console.WriteLine($"ExtractString(\"12####78\"): {ExtractString("12####78")}");
// Console.WriteLine($"ExtractString(\"gar##d#en\"): {ExtractString("gar##d#en")}");
// Console.WriteLine($"ExtractString(\"++##--##++\"): {ExtractString("++##--##++")}");
// Console.WriteLine($"FullSequenceOfLetters(\"ds\"): {FullSequenceOfLetters("ds")}");
// Console.WriteLine($"FullSequenceOfLetters(\"or\"): {FullSequenceOfLetters("or")}");
// Console.WriteLine($"SumAndAverage(11, 66): {SumAndAverage(11, 66)}");
// Console.WriteLine($"SumAndAverage(-10, 0): {SumAndAverage(-10, 0)}");
// Console.WriteLine($"DrawTriangle(10):\n{DrawTriangle(10)}");
// Console.WriteLine($"ToThePowerOf(-2, 3): {ToThePowerOf(-2, 3)}");
// 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")}");
}
}
}

View File

@@ -0,0 +1,86 @@
using System.Text.RegularExpressions;
namespace BasicProgramming
{
public class Statement
{
public int AbsoluteValue(int a)
{
return Math.Abs(a);
}
public int DivisibleBy2Or3(int a, int b)
{
if (_DivisibleBy2Or3(a) && _DivisibleBy2Or3(b))
{
return a * b;
}
bool _DivisibleBy2Or3(int x)
{
return (x % 3) == 0 || (x % 2) == 0;
}
return a + b;
}
public bool IfConsistsOfUppercaseLetters(string str)
{
return str.Equals(Regex.Match(str, "[A-ZÆØÅ]+").Value);
}
public bool IfGreaterThanThirdOne(List<int> ints)
{
if (ints.Count < 3)
{
throw new Exception("Must have 3 or more ints");
}
return (ints[0] * ints[1]) > ints[2];
}
public bool IfNumberIsEven(int a)
{
return a % 2 == 0;
}
public bool IfSortedAscending(List<int> ints)
{
int lastNumber = 0;
bool retval = true;
ints.ForEach(x =>
{
if (lastNumber < x)
{
lastNumber = x;
}
else
{
retval = false;
}
});
return retval;
}
public string PositiveNegativeOrZero(double a)
{
switch (a)
{
case > 0:
return "positive";
case < 0:
return "negative";
default:
return "zero";
}
}
public bool IfYearIsLeap(int year)
{
return DateTime.IsLeapYear(year);
}
}
}

View File

@@ -0,0 +1,83 @@
using System.Text;
namespace BasicProgramming
{
public class Strings
{
public string AddSeparator(string str, string seperator)
{
return string.Join(seperator, str.ToList());
}
public 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 int LengthOfAString(string str)
{
return str.Count();
}
public string StringInReverseOrder(string str)
{
return string.Join("", str.Reverse());
}
public int NumberOfWords(string str)
{
return str.Split(" ").Count();
}
public string RevertWordsOrder(string str)
{
return string.Join(" ", str.Split(" ").Reverse());
}
public int HowManyOccurrences(string str, string strToCheck)
{
return str.Split(" ").Count(x => x.Equals(strToCheck));
}
public char[] SortCharactersDescending(string str)
{
return str.OrderBy(x => (char)x).Reverse().ToArray();
}
public 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();
}
}
}

View File

@@ -1,53 +0,0 @@
using static BasicProgramming.Program;
namespace BasicProgrammingUnitTest;
public class BasicProgramming__BASIC
{
[Fact]
public void AddAndMultiply_TEST()
{
Assert.Equal(30, AddAndMultiply(2, 4, 5));
}
[Fact]
public void CToF_TEST()
{
Assert.Equal("T = 32F", CtoF(0));
Assert.Equal("T = 212F", CtoF(100));
Assert.Equal("Temperature below absolute zero!", CtoF(-300));
}
[Fact]
public void ElementaryOperations_TEST()
{
Assert.Equal([11, -5, 24, 0.375], ElementaryOperations(3, 8));
}
[Fact]
public void IsResultTheSame_TEST()
{
Assert.True(IsResultTheSame(2 + 2, 2 * 2));
Assert.False(IsResultTheSame(9 / 3, 16 - 1));
}
[Fact]
public void ModuloOperations_TEST()
{
Assert.Equal(1, ModuloOperations(8, 5, 2));
}
[Fact]
public void CubeOf_TEST()
{
Assert.Equal(8, CubeOf(2));
Assert.Equal(-166.375, CubeOf(-5.5));
}
[Fact]
public void SwapTwoNumbers_TEST()
{
Assert.Equal([45, 87], SwapTwoNumbers(87, 45));
Assert.Equal([2, -13], SwapTwoNumbers(-13, 2));
}
}

View File

@@ -1,99 +0,0 @@
using static BasicProgramming.Program;
namespace BasicProgrammingUnitTest;
public class BasicProgramming__LOOPS
{
[Fact]
public void MultiplicationTable_TEST()
{
var expected = new List<List<int>>
{
new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
new List<int> { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 },
new List<int> { 3, 6, 9, 12, 15, 18, 21, 24, 27, 30 },
new List<int> { 4, 8, 12, 16, 20, 24, 28, 32, 36, 40 },
new List<int> { 5, 10, 15, 20, 25, 30, 35, 40, 45, 50 },
new List<int> { 6, 12, 18, 24, 30, 36, 42, 48, 54, 60 },
new List<int> { 7, 14, 21, 28, 35, 42, 49, 56, 63, 70 },
new List<int> { 8, 16, 24, 32, 40, 48, 56, 64, 72, 80 },
new List<int> { 9, 18, 27, 36, 45, 54, 63, 72, 81, 90 },
new List<int> { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 }
};
Assert.Equal(expected, MultiplicationTable(10, 10));
}
[Fact]
public void TheBiggestNumber_TEST()
{
Assert.Equal(291, TheBiggestNumber([190, 291, 145, 209, 280, 200]));
Assert.Equal(-2, TheBiggestNumber([-9, -2, -7, -8, -4]));
}
[Fact]
public void Two7sNextToEachOther_TEST()
{
Assert.Equal(1, Two7sNextToEachOther([8, 2, 5, 7, 9, 0, 7, 7, 3, 1]));
Assert.Equal(3, Two7sNextToEachOther([9, 4, 5, 3, 7, 7, 7, 3, 2, 5, 7, 7]));
}
[Fact]
public void ThreeIncreasingAdjacent_TEST()
{
Assert.True(ThreeIncreasingAdjacent([45, 23, 44, 68, 65, 70, 80, 81, 82]));
Assert.False(ThreeIncreasingAdjacent([7, 3, 5, 8, 9, 3, 1, 4]));
}
[Fact]
public void SieveOfEratosthenes_TEST()
{
Assert.Equal([2, 3, 5, 7, 11, 13, 17, 19, 23, 29], SieveOfEratosthenes(30));
}
[Fact]
public void ExtractString_TEST()
{
Assert.Equal("abc", ExtractString("##abc##def"));
Assert.Equal(string.Empty, ExtractString("12####78"));
Assert.Equal(string.Empty, ExtractString("gar##d#en"));
Assert.Equal("--", ExtractString("++##--##++"));
}
[Fact]
public void FullSequenceOfLetters_TEST()
{
Assert.Equal("defghijklmnopqrs", FullSequenceOfLetters("ds"));
Assert.Equal("opqr", FullSequenceOfLetters("or"));
}
[Fact]
public void SumAndAverage_TEST()
{
Assert.Equal("Sum: 2156, Average: 38,5", SumAndAverage(11, 66));
Assert.Equal("Sum: -55, Average: -5", SumAndAverage(-10, 0));
}
[Fact]
public void DrawTriangle_TEST()
{
Assert.Equal(
@" *
***
*****
*******
*********
***********
*************
***************
*****************
*******************", DrawTriangle(10));
}
[Fact]
public void ToThePowerOf_TEST()
{
Assert.Equal(-8, ToThePowerOf(-2, 3));
Assert.Equal(3125, ToThePowerOf(5, 5));
}
}

View File

@@ -1,65 +0,0 @@
using static BasicProgramming.Program;
namespace BasicProgrammingUnitTest;
public class BasicProgramming__STATEMENTS
{
[Fact]
public void AbsoluteValue_TEST()
{
Assert.Equal(6832, AbsoluteValue(6832));
Assert.Equal(392, AbsoluteValue(-392));
}
[Fact]
public void DivisibleBy2Or3_TEST()
{
Assert.Equal(450, DivisibleBy2Or3(15, 30));
Assert.Equal(180, DivisibleBy2Or3(2, 90));
Assert.Equal(19, DivisibleBy2Or3(7, 12));
}
[Fact]
public void IfConsistsOfUppercaseLetters_TEST()
{
Assert.False(IfConsistsOfUppercaseLetters("xyz"));
Assert.True(IfConsistsOfUppercaseLetters("DOG"));
Assert.False(IfConsistsOfUppercaseLetters("L9#"));
}
[Fact]
public void IfGreaterThanThirdOne_TEST()
{
Assert.True(IfGreaterThanThirdOne([2, 7, 12]));
Assert.False(IfGreaterThanThirdOne([-5, -8, 50]));
}
[Fact]
public void IfNumberIsEven_TEST()
{
Assert.False(IfNumberIsEven(721));
Assert.True(IfNumberIsEven(1248));
}
[Fact]
public void IfSortedAscending_TEST()
{
Assert.True(IfSortedAscending([3, 7, 10]));
Assert.False(IfSortedAscending([74, 62, 99]));
}
[Fact]
public void PositiveNegativeOrZero_TEST()
{
Assert.Equal("positive", PositiveNegativeOrZero(5.24));
Assert.Equal("zero", PositiveNegativeOrZero(0.0));
Assert.Equal("negative", PositiveNegativeOrZero(-994.53));
}
[Fact]
public void IfYearIsLeap_TEST()
{
Assert.True(IfYearIsLeap(2016));
Assert.False(IfYearIsLeap(2018));
}
}

View File

@@ -1,69 +0,0 @@
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"));
}
}

View File

@@ -0,0 +1,64 @@
using BasicProgramming;
namespace BasicProgrammingTests;
public class BasicTests
{
private readonly Basic basic = new Basic();
[Theory]
[InlineData(30, 2, 4, 5)]
public void AddAndMultiply(int expected, int a, int b, int c)
{
Assert.Equal(expected, basic.AddAndMultiply(a, b, c));
}
[Theory]
[InlineData("T = 32F", 0)]
[InlineData("T = 212F", 100)]
[InlineData("Temperature below absolute zero!", -300)]
public void CToF(string expected, int input)
{
Assert.Equal(expected, basic.CtoF(input));
}
[Theory]
[InlineData(new double[] { 11.0, -5.0, 24.0, 0.375 }, 3, 8)]
public void ElementaryOperations(double[] expected, int a, int b)
{
var result = basic.ElementaryOperations(a, b);
Assert.Equal(expected, result);
}
[Theory]
[InlineData(true, 2 + 2, 2 * 2)]
[InlineData(false, 9 / 3, 16 - 1)]
public void IsResultTheSame(bool expected, int a, int b)
{
Assert.Equal(expected, basic.IsResultTheSame(a, b));
}
[Theory]
[InlineData(1, 8, 5, 2)]
public void ModuloOperations(int expected, int a, int b, int c)
{
Assert.Equal(expected, basic.ModuloOperations(a, b, c));
}
[Theory]
[InlineData(8, 2)]
[InlineData(-166.375, -5.5)]
public void CubeOf(double expected, double a)
{
Assert.Equal(expected, basic.CubeOf(a));
}
[Theory]
[InlineData(new int[] { 45, 87 }, 87, 45)]
[InlineData(new int[] {2, -13}, -13, 2)]
public void SwapTwoNumbers(int[] expected, int a, int b)
{
Assert.Equal(expected, basic.SwapTwoNumbers(a, b));
}
}

View File

@@ -0,0 +1,109 @@
using BasicProgramming;
namespace BasicProgrammingTests;
public class LoopsTests
{
Loops loops = new Loops();
[Fact]
public void MultiplicationTable()
{
var expected = new List<List<int>>
{
new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
new List<int> { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 },
new List<int> { 3, 6, 9, 12, 15, 18, 21, 24, 27, 30 },
new List<int> { 4, 8, 12, 16, 20, 24, 28, 32, 36, 40 },
new List<int> { 5, 10, 15, 20, 25, 30, 35, 40, 45, 50 },
new List<int> { 6, 12, 18, 24, 30, 36, 42, 48, 54, 60 },
new List<int> { 7, 14, 21, 28, 35, 42, 49, 56, 63, 70 },
new List<int> { 8, 16, 24, 32, 40, 48, 56, 64, 72, 80 },
new List<int> { 9, 18, 27, 36, 45, 54, 63, 72, 81, 90 },
new List<int> { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 }
};
Assert.Equal(expected, loops.MultiplicationTable(10, 10));
}
[Theory]
[InlineData(291, new int[] { 190, 291, 145, 209, 280, 200 })]
[InlineData(-2, new int[] { -9, -2, -7, -8, -4 })]
public void TheBiggestNumber(int expected, int[] a)
{
Assert.Equal(expected, loops.TheBiggestNumber(a.ToList()));
}
[Theory]
[InlineData(1, new int[] { 8, 2, 5, 7, 9, 0, 7, 7, 3, 1 })]
[InlineData(3, new int[] { 9, 4, 5, 3, 7, 7, 7, 3, 2, 5, 7, 7 })]
public void Two7sNextToEachOther(int expected, int[] a)
{
Assert.Equal(expected, loops.Two7sNextToEachOther(a.ToList()));
}
[Theory]
[InlineData(true, new int[] { 45, 23, 44, 68, 65, 70, 80, 81, 82 })]
[InlineData(false, new int[] { 7, 3, 5, 8, 9, 3, 1, 4 })]
public void ThreeIncreasingAdjacent(bool expected, int[] a)
{
Assert.Equal(expected, loops.ThreeIncreasingAdjacent(a.ToList()));
}
[Theory]
[InlineData(new int[] {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}, 30)]
public void SieveOfEratosthenes(int[] expected, int a)
{
Assert.Equal(expected.ToList(), loops.SieveOfEratosthenes(a));
}
[Theory]
[InlineData("abc", "##abc##def")]
[InlineData("", "12####78")]
[InlineData("", "gar##d#en")]
[InlineData("--", "++##--##++")]
public void ExtractString(string expected, string a)
{
Assert.Equal(expected, loops.ExtractString(a));
}
[Theory]
[InlineData("defghijklmnopqrs", "ds")]
[InlineData("opqr", "or")]
public void FullSequenceOfLetters(string expected, string a)
{
Assert.Equal(expected, loops.FullSequenceOfLetters(a));
}
[Theory]
[InlineData("Sum: 2156, Average: 38,5", 11, 66)]
[InlineData("Sum: -55, Average: -5", -10, 0)]
public void SumAndAverage(string expected, int a, int b)
{
Assert.Equal(expected, loops.SumAndAverage(a, b));
}
[Fact]
public void DrawTriangle()
{
Assert.Equal(
@" *
***
*****
*******
*********
***********
*************
***************
*****************
*******************", loops.DrawTriangle(10));
}
[Theory]
[InlineData(-8, -2, 3)]
[InlineData(3125, 5, 5)]
public void ToThePowerOf(int expected, int a, int b)
{
Assert.Equal(expected, loops.ToThePowerOf(a, b));
}
}

View File

@@ -0,0 +1,75 @@
using BasicProgramming;
namespace BasicProgrammingTests;
public class StatementTests
{
Statement statement = new Statement();
[Theory]
[InlineData(6832, 6832)]
[InlineData(392, -392)]
public void AbsoluteValue(int expected, int a)
{
Assert.Equal(expected, statement.AbsoluteValue(a));
}
[Theory]
[InlineData(450, 15, 30)]
[InlineData(180, 2, 90)]
[InlineData(19, 7, 12)]
public void DivisibleBy2Or3(int expected, int a, int b)
{
Assert.Equal(expected, statement.DivisibleBy2Or3(a, b));
}
[Theory]
[InlineData(false, "xyz")]
[InlineData(true, "DOG")]
[InlineData(false, "L9#")]
public void IfConsistsOfUppercaseLetters(bool expected, string a)
{
Assert.Equal(expected, statement.IfConsistsOfUppercaseLetters(a));
}
[Theory]
[InlineData(true, new int[] { 2, 7, 12 })]
[InlineData(false, new int[] {-5, -8, 50})]
public void IfGreaterThanThirdOne(bool expected, int[] a)
{
Assert.Equal(expected, statement.IfGreaterThanThirdOne(a.ToList()));
}
[Theory]
[InlineData(false, 721)]
[InlineData(true, 1248)]
public void IfNumberIsEven(bool expected, int a)
{
Assert.Equal(expected, statement.IfNumberIsEven(a));
}
[Theory]
[InlineData(true, new int[] { 3, 7, 10 })]
[InlineData(false, new int[] {74, 62, 99})]
public void IfSortedAscending(bool expected, int[] a)
{
Assert.Equal(expected, statement.IfSortedAscending(a.ToList()));
}
[Theory]
[InlineData("positive", 5.24)]
[InlineData("zero", 0)]
[InlineData("negative", -994.53)]
public void PositiveNegativeOrZero(string expected, double a)
{
Assert.Equal(expected, statement.PositiveNegativeOrZero(a));
}
[Theory]
[InlineData(true, 2016)]
[InlineData(false, 2018)]
public void IfYearIsLeap(bool expected, int a)
{
Assert.Equal(expected, statement.IfYearIsLeap(a));
}
}

View File

@@ -0,0 +1,80 @@
using BasicProgramming;
namespace BasicProgrammingTests;
public class StringsTests
{
Strings strings = new Strings();
[Theory]
[InlineData("A^B^C^D", "ABCD", "^")]
[InlineData("c-h-o-c-o-l-a-t-e", "chocolate", "-")]
public void AddSeperator(string expected, string a, string b)
{
Assert.Equal(expected, strings.AddSeparator(a, b));
}
[Theory]
[InlineData(true, "eye")]
[InlineData(false, "home")]
public void IsPalindrome(bool expected, string a)
{
Assert.Equal(expected, strings.IsPalindrome(a));
}
[Theory]
[InlineData(8, "computer")]
[InlineData(9, "ice cream")]
public void LengthOfAString(int expected, string a)
{
Assert.Equal(expected, strings.LengthOfAString(a));
}
[Theory]
[InlineData("ytrewq", "qwerty")]
[InlineData("rk 39eo", "oe93 kr")]
public void StringInReverseOrder(string expected, string a)
{
Assert.Equal(expected, strings.StringInReverseOrder(a));
}
[Theory]
[InlineData(4, "This is sample sentence")]
[InlineData(1, "OK")]
public void NumberOfWords(int expected, string a)
{
Assert.Equal(expected, strings.NumberOfWords(a));
}
[Theory]
[InlineData("Doe. John", "John Doe.")]
[InlineData("A, B. C", "C B. A,")]
public void RevertWordsOrder(string expected, string a)
{
Assert.Equal(expected, strings.RevertWordsOrder(a));
}
[Theory]
[InlineData(1, "do it now", "do")]
[InlineData(0, "empty", "d")]
public void HowManyOccurrences(int expected, string a, string b)
{
Assert.Equal(expected, strings.HowManyOccurrences(a, b));
}
[Theory]
[InlineData(new char[] { 't', 'p', 'o', 'o', 'o', 'o', 'n', 'm', 'i', 'e', 'a', 'a' }, "onomatopoeia")]
[InlineData(new char[] { 'w', 's', 'o', 'o', 'j', 'h', 'f', 'f', '4', '2' }, "fohjwf42os")]
public void SortCharactersDescending(char[] expected, string a)
{
Assert.Equal(expected.ToList(), strings.SortCharactersDescending(a));
}
[Theory]
[InlineData("k4t3r10", "kkkktttrrrrrrrrrr")]
[InlineData("p153p371w3","p555ppp7www")]
public void CompressString(string expected, string a)
{
Assert.Equal(expected, strings.CompressString(a));
}
}