Add loops and unit tests

This commit is contained in:
2025-08-05 12:06:47 +02:00
parent 36050dbce3
commit 5bebb4be3c
2 changed files with 299 additions and 1 deletions

View File

@@ -1,4 +1,6 @@
using System.Text.RegularExpressions;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
namespace BasicProgramming
{
@@ -39,6 +41,27 @@ namespace BasicProgramming
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): {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): {DrawTriangle(10)}");
Console.WriteLine($"ToThePowerOf(-2, 3): {ToThePowerOf(-2, 3)}");
Console.WriteLine($"ToThePowerOf(5, 5): {ToThePowerOf(5, 5)}");
Console.WriteLine(DrawTriangle(10));
}
public static int AddAndMultiply(int a, int b, int c)
@@ -163,5 +186,181 @@ namespace BasicProgramming
{
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;
}
}
}

View File

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