Adding Method Displayer, and renaming projects
This commit is contained in:
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user