There can be only one: Numeric Extension
Description:
#Note: This is C# specific kata.
#Description: Did you ever try to create extension methods, supporting all .Net numeric data types (ignore non-numeric types, such as bool)?
Typically, you would need to have something like this:
public static class DummyExtensions
{
public static decimal DummyConversion(this int i, decimal d, float factor)
{
return i * d * (decimal)factor;
}
public static float DummyConversion(this double i, decimal d, float factor)
{
return (float)(i * (double) d * factor);
}
}
I'm fully aware, this is a bad example, hence the name of it (dummy). But, I'm sure you got the point of potential explosion of such extensions.
Wouldn't it be nice, if we can have only one extension method, wich would accept any numeric type as its arguments, so that we need to write its logic only once?!
Well... that is your task now.
#Task:
Write exactly one FeetToCentimeters extension method and exactly one CentimetersToFeet extension method (1 foot = 30.48 centimeters
), so that they can be consumed like this:
var initialDouble = 32.43;
var endResult =(float)((decimal)((double) ((float) initialDouble.FeetToCentimeters()).CentimetersToFeet()).FeetToCentimeters()).CentimetersToFeet();
Debug.Assert(initialDouble == endResult);
Console.WriteLine($"initial: {initialDouble} end result: {endResult}");
Console.WriteLine($"32.2 as double to centimeters: {32.2D.FeetToCentimeters()}");
Console.WriteLine($"32.2 as decimal to centimeters: {32.2M.FeetToCentimeters()}");
Console.WriteLine($"32.2 as float to centimeters: {32.2F.FeetToCentimeters()}");
Console.WriteLine($"32 as byte to centimeters and back to feet: {(byte)32.FeetToCentimeters().CentimetersToFeet()}");
where the output would than be:
initial: 32.43 end result: 32.43
32.2 as double to centimeters: 981.456
32.2 as decimal to centimeters: 981.456
32.2 as float to centimeters: 981.456
32 as byte to centimeters and back to feet: 32
#Rules:
- Both extensions are not allowed to call any other method
- There can be only one static class in the namespace named NumberExtension
- There can be only one additional class or struct in the namespace (non static)
- No private or nested classes
- You have to support sbyte, short, int, long, byte, ushort, uint, ulong, decimal, double and float numeric types. Types from System.Numerics namespace (BigInteger for example) are not subject of this kata.
- Attempt to call those extension methods with non-numeric type, should throw Exception
- Fractions will be evaluated by 0.05 precision (tests will have 0.005 delta, when comparing equality of results), due to possible precission loss when converting from float do decimal and back (for example).
Similar Kata:
Stats:
Created | Sep 4, 2016 |
Published | Sep 4, 2016 |
Warriors Trained | 196 |
Total Skips | 55 |
Total Code Submissions | 242 |
Total Times Completed | 24 |
C# Completions | 24 |
Total Stars | 4 |
% of votes with a positive feedback rating | 75% of 10 |
Total "Very Satisfied" Votes | 6 |
Total "Somewhat Satisfied" Votes | 3 |
Total "Not Satisfied" Votes | 1 |
Total Rank Assessments | 10 |
Average Assessed Rank | 6 kyu |
Highest Assessed Rank | 6 kyu |
Lowest Assessed Rank | 7 kyu |