Cleanup code, and implementing interface for displaying methods

This commit is contained in:
2025-08-06 13:09:40 +02:00
parent 39ecc637e8
commit bb32e873c1
5 changed files with 108 additions and 81 deletions

View File

@@ -1,7 +1,23 @@
namespace BasicProgramming
{
public class Basic
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;

View File

@@ -1,9 +1,33 @@
using System.Text;
using System.Text.Json;
namespace BasicProgramming
{
public class Loops
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>>();

View File

@@ -1,85 +1,27 @@
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
namespace BasicProgramming
namespace BasicProgramming
{
public class Program
{
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")}");
var methodCollections = new List<IMethodCollection>()
{
new Basic(),
new Statement(),
new Loops(),
new Strings()
};
methodCollections.ForEach(x =>
{
Console.WriteLine($"====== {x.GetType().Name} ======");
x.DisplayAllMethods();
});
}
}
public interface IMethodCollection
{
public void DisplayAllMethods();
}
}

View File

@@ -2,8 +2,30 @@ using System.Text.RegularExpressions;
namespace BasicProgramming
{
public class Statement
public class Statement : IMethodCollection
{
public void DisplayAllMethods()
{
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)}");
}
public int AbsoluteValue(int a)
{
return Math.Abs(a);

View File

@@ -1,9 +1,32 @@
using System.Text;
using System.Text.Json;
namespace BasicProgramming
{
public class Strings
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());