Create a DLL in C# must contain the following extension methods: void Print() –
ID: 3822947 • Letter: C
Question
Create a DLL in C# must contain the following extension methods:
void Print() – Targets IEnumerable. Prints out the objects in any collection, separating each value with a comma (,) while making sure there is NO dangling tail symbol.
int ToPower(int exponent) – Targets int. Returns the value of the target int raised to the power of the exponent parameter.
bool IsPalindrome() – Targets string. Returns a bool signifying whether the string is a palindrome (reads backward the same as forward; Bob, racecar, tacocat), ignoring both casing and any whitespace. For example, “tacocat”, “tAcoCaT” and “taco cat” are all the same palindrome.
This is what I have at the moment
public static class Class1
{
public static void Print<T>(this IEnumerable<T> collect )
{
foreach(IEnumerable<T> collection in collect)
{
Console.WriteLine(collect);
}
}
public static int ToPower(this int x, int exponent)
{
return;
}
public static bool IsPalendrome(this string targetstr )
{
return;
}
}
}
Explanation / Answer
Answer:
The functions are implemented as below :
int ToPower(int x ,int exponent)
{
int input = 1;
while ( exponent != 0 )
{
if ( (exponent & 1) == 1 )
input *= x;
x *= x;
exponent >>= 1;
}
return input;
}
public static bool IsPalindrome(string targetstr)
{
int small = 0;
int high = targetstr.Length - 1;
while (true)
{
if (small > high)
{
return true;
}
char input1 = targetstr[small];
char input2 = targetstr[high];
if (char.ToLower(input1) != char.ToLower(input2))
{
return false;
}
small++;
high--;
}
}