Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1240,6 +1240,30 @@
],
"unlocked_by": "markdown",
"uuid": "a5794706-58d2-48f7-8aab-78639d7bce77"
},
{
"core": false,
"deprecated": true,
"slug": "binary",
"uuid": "cef7deef-54ce-4201-b263-7cd2098533f8"
},
{
"core": false,
"deprecated": true,
"slug": "trinary",
"uuid": "c7dd8467-87e2-4997-a96e-a04cb8b891e8"
},
{
"core": false,
"deprecated": true,
"slug": "octal",
"uuid": "c8555f37-9976-4f52-a5db-6a680ec8d53b"
},
{
"core": false,
"deprecated": true,
"slug": "hexadecimal",
"uuid": "5d30c5a0-0f69-4b79-8c7e-3b1fe6a5707f"
}
],
"foregone": [
Expand Down
9 changes: 9 additions & 0 deletions exercises/binary/Binary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

public class Binary
{
public static int ToDecimal(string binary)
{
throw new NotImplementedException("You need to implement this function.");
}
}
17 changes: 17 additions & 0 deletions exercises/binary/Binary.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Remove="Example.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
</ItemGroup>

</Project>
94 changes: 94 additions & 0 deletions exercises/binary/BinaryTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using Xunit;

public class BinaryTest
{
[Fact]
public void Binary_0_is_decimal_0()
{
Assert.Equal(0, Binary.ToDecimal("0"));
}

[Fact(Skip = "Remove to run test")]
public void Binary_1_is_decimal_1()
{
Assert.Equal(1, Binary.ToDecimal("1"));
}

[Fact(Skip = "Remove to run test")]
public void Binary_10_is_decimal_2()
{
Assert.Equal(2, Binary.ToDecimal("10"));
}

[Fact(Skip = "Remove to run test")]
public void Binary_11_is_decimal_3()
{
Assert.Equal(3, Binary.ToDecimal("11"));
}

[Fact(Skip = "Remove to run test")]
public void Binary_100_is_decimal_4()
{
Assert.Equal(4, Binary.ToDecimal("100"));
}

[Fact(Skip = "Remove to run test")]
public void Binary_1001_is_decimal_9()
{
Assert.Equal(9, Binary.ToDecimal("1001"));
}

[Fact(Skip = "Remove to run test")]
public void Binary_11010_is_decimal_26()
{
Assert.Equal(26, Binary.ToDecimal("11010"));
}

[Fact(Skip = "Remove to run test")]
public void Binary_10001101000_is_decimal_1128()
{
Assert.Equal(1128, Binary.ToDecimal("10001101000"));
}

[Fact(Skip = "Remove to run test")]
public void Binary_ignores_leading_zeros()
{
Assert.Equal(31, Binary.ToDecimal("000011111"));
}

[Fact(Skip = "Remove to run test")]
public void Invalid_binary_2_converts_to_decimal_0()
{
Assert.Equal(0, Binary.ToDecimal("2"));
}

[Fact(Skip = "Remove to run test")]
public void A_number_containing_a_non_binary_digit_is_invalid()
{
Assert.Equal(0, Binary.ToDecimal("01201"));
}

[Fact(Skip = "Remove to run test")]
public void A_number_with_trailing_non_binary_characters_is_invalid()
{
Assert.Equal(0, Binary.ToDecimal("10nope"));
}

[Fact(Skip = "Remove to run test")]
public void A_number_with_leading_non_binary_characters_is_invalid()
{
Assert.Equal(0, Binary.ToDecimal("nope10"));
}

[Fact(Skip = "Remove to run test")]
public void A_number_with_internal_non_binary_characters_is_invalid()
{
Assert.Equal(0, Binary.ToDecimal("10nope10"));
}

[Fact(Skip = "Remove to run test")]
public void A_number_and_a_word_whitespace_separated_is_invalid()
{
Assert.Equal(0, Binary.ToDecimal("001 nope"));
}
}
24 changes: 24 additions & 0 deletions exercises/binary/Example.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Linq;

public class Binary
{
public static int ToDecimal(string binary)
{
if (IsNotValidBinary(binary)) return 0;

return binary
.Select((c, i) => int.Parse(c.ToString()) * TwoToThePowerOf(binary.Length - i - 1))
.Sum();
}

private static bool IsNotValidBinary(string binary)
{
return !binary.All(x => char.IsDigit(x) && int.Parse(x.ToString()) < 2);
}

private static int TwoToThePowerOf(int power)
{
return (int)Math.Pow(2, power);
}
}
41 changes: 41 additions & 0 deletions exercises/binary/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Binary

Convert a binary number, represented as a string (e.g. '101010'), to its decimal equivalent using first principles.

Implement binary to decimal conversion. Given a binary input
string, your program should produce a decimal output. The
program should handle invalid inputs.

## Note
- Implement the conversion yourself.
Do not use something else to perform the conversion for you.

## About Binary (Base-2)
Decimal is a base-10 system.

A number 23 in base 10 notation can be understood
as a linear combination of powers of 10:

- The rightmost digit gets multiplied by 10^0 = 1
- The next number gets multiplied by 10^1 = 10
- ...
- The *n*th number gets multiplied by 10^*(n-1)*.
- All these values are summed.

So: `23 => 2*10^1 + 3*10^0 => 2*10 + 3*1 = 23 base 10`

Binary is similar, but uses powers of 2 rather than powers of 10.

So: `101 => 1*2^2 + 0*2^1 + 1*2^0 => 1*4 + 0*2 + 1*1 => 4 + 1 => 5 base 10`.

### Submitting Exercises

Note that, when trying to submit an exercise, make sure the exercise file that you're submitting is in the `exercism/csharp/<exerciseName>` directory.

For example, if you're submitting `bob.cs` for the Bob exercise, the submit command would be something like `exercism submit <path_to_exercism_dir>/csharp/bob/bob.cs`.
## Source

All of Computer Science [http://www.wolframalpha.com/input/?i=binary&a=*C.binary-_*MathWorld-](http://www.wolframalpha.com/input/?i=binary&a=*C.binary-_*MathWorld-)

## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
41 changes: 41 additions & 0 deletions exercises/hexadecimal/Example.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

public class Hexadecimal
{
private static readonly Dictionary<char, int> AlphaValues = new Dictionary<char, int>
{
{ 'a', 10 },
{ 'b', 11 },
{ 'c', 12 },
{ 'd', 13 },
{ 'e', 14 },
{ 'f', 15 }
};

public static int ToDecimal(string value)
{
if (IsNotValidHexadecimal(value)) return 0;

return value.Select((c, i) => GetNumericValue(c) * SixteenToThePowerOf(value.Length - i - 1)).Sum();
}

private static bool IsNotValidHexadecimal(string value)
{
return Regex.IsMatch(value, "[^0-9abcdef]", RegexOptions.IgnoreCase);
}

private static int GetNumericValue(char value)
{
if (char.IsNumber(value))
return (int)char.GetNumericValue(value);
return AlphaValues[value];
}

private static int SixteenToThePowerOf(int power)
{
return (int)Math.Pow(16, power);
}
}
9 changes: 9 additions & 0 deletions exercises/hexadecimal/Hexadecimal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

public class Hexadecimal
{
public static int ToDecimal(string value)
{
throw new NotImplementedException("You need to implement this function.");
}
}
17 changes: 17 additions & 0 deletions exercises/hexadecimal/Hexadecimal.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Remove="Example.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
</ItemGroup>

</Project>
65 changes: 65 additions & 0 deletions exercises/hexadecimal/HexadecimalTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using Xunit;

public class HexadecimalTest
{
[Fact(Skip = "Remove to run test")]
public void Hexadecimal_1_is_decimal_1()
{
Assert.Equal(1, Hexadecimal.ToDecimal("1"));
}

[Fact(Skip = "Remove to run test")]
public void Hexadecimal_c_is_decimal_12()
{
Assert.Equal(12, Hexadecimal.ToDecimal("c"));
}

[Fact(Skip = "Remove to run test")]
public void Hexadecimal_10_is_decimal_16()
{
Assert.Equal(16, Hexadecimal.ToDecimal("10"));
}

[Fact(Skip = "Remove to run test")]
public void Hexadecimal_af_is_decimal_175()
{
Assert.Equal(175, Hexadecimal.ToDecimal("af"));
}

[Fact(Skip = "Remove to run test")]
public void Hexadecimal_100_is_decimal_256()
{
Assert.Equal(256, Hexadecimal.ToDecimal("100"));
}

[Fact(Skip = "Remove to run test")]
public void Hexadecimal_19ace_is_decimal_105166()
{
Assert.Equal(105166, Hexadecimal.ToDecimal("19ace"));
}

[Fact(Skip = "Remove to run test")]
public void Hexadecimal_carrot_is_decimal_0()
{
Assert.Equal(0, Hexadecimal.ToDecimal("carrot"));
}

[Fact(Skip = "Remove to run test")]
public void Hexadecimal_000000_is_decimal_0()
{
Assert.Equal(0, Hexadecimal.ToDecimal("000000"));
}

[Fact(Skip = "Remove to run test")]
public void Hexadecimal_ffffff_is_decimal_16777215()
{
Assert.Equal(16777215, Hexadecimal.ToDecimal("ffffff"));
}

[Fact(Skip = "Remove to run test")]
public void Hexadecimal_ffff00_is_decimal_16776960()
{
Assert.Equal(16776960, Hexadecimal.ToDecimal("ffff00"));
}

}
20 changes: 20 additions & 0 deletions exercises/hexadecimal/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Hexadecimal

Convert a hexadecimal number, represented as a string (e.g. "10af8c"), to its decimal equivalent using first principles (i.e. no, you may not use built-in or external libraries to accomplish the conversion).

On the web we use hexadecimal to represent colors, e.g. green: 008000,
teal: 008080, navy: 000080).

The program should handle invalid hexadecimal strings.

### Submitting Exercises

Note that, when trying to submit an exercise, make sure the exercise file that you're submitting is in the `exercism/csharp/<exerciseName>` directory.

For example, if you're submitting `bob.cs` for the Bob exercise, the submit command would be something like `exercism submit <path_to_exercism_dir>/csharp/bob/bob.cs`.
## Source

All of Computer Science [http://www.wolframalpha.com/examples/NumberBases.html](http://www.wolframalpha.com/examples/NumberBases.html)

## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
Loading