Compare commits

...

12 Commits

17 changed files with 784 additions and 195 deletions

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,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

@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="../MethodLibrary/MethodLibrary.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,24 @@
using MethodLibrary;
namespace MethodDisplayer
{
public class Program
{
static void Main(string[] args)
{
var methodCollections = new List<IMethodCollection>
{
new Basic(),
new Statement(),
new Loops(),
new Strings()
};
foreach (var method in methodCollections)
{
Console.WriteLine($"====== {method.GetType().Name} ======");
method.DisplayAllMethods();
}
}
}
}

View File

@@ -2,9 +2,11 @@
# Visual Studio Version 17
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}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MethodLibrary", "MethodLibrary\MethodLibrary.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}") = "MethodLibraryTests", "MethodLibraryTests\MethodLibraryTests.csproj", "{427B1690-1D9B-4002-A6DE-435867230BAC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MethodDisplayer", "MethodDisplayer\MethodDisplayer.csproj", "{204AEF70-C8DC-4FB7-A2F6-D4BAEBBA20EB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -20,6 +22,10 @@ Global
{427B1690-1D9B-4002-A6DE-435867230BAC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{427B1690-1D9B-4002-A6DE-435867230BAC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{427B1690-1D9B-4002-A6DE-435867230BAC}.Release|Any CPU.Build.0 = Release|Any CPU
{204AEF70-C8DC-4FB7-A2F6-D4BAEBBA20EB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{204AEF70-C8DC-4FB7-A2F6-D4BAEBBA20EB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{204AEF70-C8DC-4FB7-A2F6-D4BAEBBA20EB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{204AEF70-C8DC-4FB7-A2F6-D4BAEBBA20EB}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

65
MethodLibrary/Basic.cs Normal file
View File

@@ -0,0 +1,65 @@
namespace MethodLibrary
{
public class Basic : IMethodCollection
{
public void DisplayAllMethods()
{
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))}");
}
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];
}
}
}

View File

@@ -0,0 +1,7 @@
namespace MethodLibrary
{
public interface IMethodCollection
{
public void DisplayAllMethods();
}
}

207
MethodLibrary/Loops.cs Normal file
View File

@@ -0,0 +1,207 @@
using System.Text;
using System.Text.Json;
namespace MethodLibrary
{
public class Loops : IMethodCollection
{
public void DisplayAllMethods()
{
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)}");
}
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

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<OutputType>Library</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

View File

@@ -1,25 +1,11 @@
using System.Text.RegularExpressions;
using System.Text.RegularExpressions;
namespace BasicProgramming
namespace MethodLibrary
{
public class Program
public class Statement : IMethodCollection
{
static void Main(string[] args)
public void DisplayAllMethods()
{
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)}");
@@ -40,57 +26,12 @@ namespace BasicProgramming
Console.WriteLine($"IfYearIsLeap(2016): {IfYearIsLeap(2016)}");
Console.WriteLine($"IfYearIsLeap(2018): {IfYearIsLeap(2018)}");
}
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)
public int AbsoluteValue(int a)
{
return Math.Abs(a);
}
public static int DivisibleBy2Or3(int a, int b)
public int DivisibleBy2Or3(int a, int b)
{
if (_DivisibleBy2Or3(a) && _DivisibleBy2Or3(b))
{
@@ -98,7 +39,7 @@ namespace BasicProgramming
}
static bool _DivisibleBy2Or3(int x)
bool _DivisibleBy2Or3(int x)
{
return (x % 3) == 0 || (x % 2) == 0;
}
@@ -106,12 +47,12 @@ namespace BasicProgramming
return a + b;
}
public static bool IfConsistsOfUppercaseLetters(string str)
public bool IfConsistsOfUppercaseLetters(string str)
{
return str.Equals(Regex.Match(str, "[A-ZÆØÅ]+").Value);
}
public static bool IfGreaterThanThirdOne(List<int> ints)
public bool IfGreaterThanThirdOne(List<int> ints)
{
if (ints.Count < 3)
{
@@ -121,12 +62,12 @@ namespace BasicProgramming
return (ints[0] * ints[1]) > ints[2];
}
public static bool IfNumberIsEven(int a)
public bool IfNumberIsEven(int a)
{
return a % 2 == 0;
}
public static bool IfSortedAscending(List<int> ints)
public bool IfSortedAscending(List<int> ints)
{
int lastNumber = 0;
bool retval = true;
@@ -146,7 +87,7 @@ namespace BasicProgramming
return retval;
}
public static string PositiveNegativeOrZero(double a)
public string PositiveNegativeOrZero(double a)
{
switch (a)
{
@@ -159,7 +100,7 @@ namespace BasicProgramming
}
}
public static bool IfYearIsLeap(int year)
public bool IfYearIsLeap(int year)
{
return DateTime.IsLeapYear(year);
}

106
MethodLibrary/Strings.cs Normal file
View File

@@ -0,0 +1,106 @@
using System.Text;
using System.Text.Json;
namespace MethodLibrary
{
public class Strings : IMethodCollection
{
public void DisplayAllMethods()
{
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 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

@@ -0,0 +1,64 @@
using MethodLibrary;
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 MethodLibrary;
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

@@ -15,7 +15,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../BasicProgramming/BasicProgramming.csproj" />
<ProjectReference Include="../MethodLibrary/MethodLibrary.csproj" />
</ItemGroup>
<ItemGroup>

View File

@@ -0,0 +1,75 @@
using MethodLibrary;
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 MethodLibrary;
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));
}
}

9
Readme.md Normal file
View File

@@ -0,0 +1,9 @@
# Basic Programming
Velkommen til projektet hvor jeg måske har overengineeret en smule meget.
Det er et par skole opgaver, hvor man skulle skrive nogle metoder, og derefter lave unit tests.
Jeg har måske kommet til at tage det skridtet videre.
Der er blevet lavet en klasse til hver opgave, så Basic opgaven har klassen Basic osv. Herefter er der blevet implementeret et interface på alle klasserne som jeg kalder en Metode Collection, som sørger for at alle klasserne, har en metode som skriver alle test scenarier ud i konsollen. Dette kan listen i Program.cs bruge til at sørge for at alle Metoder bliver printet i konsolen.