Adding Method Displayer, and renaming projects
This commit is contained in:
65
MethodLibrary/Basic.cs
Normal file
65
MethodLibrary/Basic.cs
Normal 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];
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
7
MethodLibrary/IMethodCollection.cs
Normal file
7
MethodLibrary/IMethodCollection.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace MethodLibrary
|
||||
{
|
||||
public interface IMethodCollection
|
||||
{
|
||||
public void DisplayAllMethods();
|
||||
}
|
||||
}
|
||||
207
MethodLibrary/Loops.cs
Normal file
207
MethodLibrary/Loops.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
10
MethodLibrary/MethodLibrary.csproj
Normal file
10
MethodLibrary/MethodLibrary.csproj
Normal file
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
108
MethodLibrary/Statement.cs
Normal file
108
MethodLibrary/Statement.cs
Normal file
@@ -0,0 +1,108 @@
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace MethodLibrary
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
106
MethodLibrary/Strings.cs
Normal file
106
MethodLibrary/Strings.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user