Browse Source

Added KarateChop Kata

Bryan Allred 11 years ago
parent
commit
dd43952214

+ 1 - 0
CodeKata/CodeKata.csproj

@ -45,6 +45,7 @@
45 45
  </ItemGroup>
46 46
  <ItemGroup>
47 47
    <Compile Include="Katas\FizzBuzz.cs" />
48
    <Compile Include="Katas\KarateChop.cs" />
48 49
    <Compile Include="Properties\AssemblyInfo.cs" />
49 50
  </ItemGroup>
50 51
  <ItemGroup>

+ 33 - 0
CodeKata/Katas/KarateChop.cs

@ -0,0 +1,33 @@
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
6
7
namespace CodeKata.Katas
8
{
9
    /// <summary>
10
    /// Karate Chop.
11
    /// </summary>
12
    /// <remarks>
13
    /// Implement a binary search routine (using the specification below) in the language and technique of your choice.
14
    /// Tomorrow, implement it again, using a totally different technique.
15
    /// Do the same the next day, until you have five totally unique implementations of a binary chop.
16
    /// 
17
    /// (For example, one solution might be the traditional iterative approach, one might be recursive,
18
    /// one might use a functional style passing array slices around, and so on).
19
    /// </remarks>
20
    public class KarateChop
21
    {
22
        /// <summary>
23
        /// Chops the specified target.
24
        /// </summary>
25
        /// <param name="target">The target.</param>
26
        /// <param name="array">The array.</param>
27
        /// <returns>The index of the targetted number (-1 if not found).</returns>
28
        public static int Chop(int target, int[] array)
29
        {
30
            throw new NotImplementedException();
31
        }
32
    }
33
}

+ 1 - 0
CodeKataTests/CodeKataTests.csproj

@ -42,6 +42,7 @@
42 42
  <ItemGroup>
43 43
    <Compile Include="Properties\AssemblyInfo.cs" />
44 44
    <Compile Include="Solutions\FizzBuzzTests.cs" />
45
    <Compile Include="Solutions\KarateChopTests.cs" />
45 46
  </ItemGroup>
46 47
  <ItemGroup>
47 48
    <Folder Include="Personal\" />

+ 63 - 0
CodeKataTests/Solutions/KarateChopTests.cs

@ -0,0 +1,63 @@
1
using CodeKata.Katas;
2
using Microsoft.VisualStudio.TestTools.UnitTesting;
3
4
namespace CodeKataTests.Solutions
5
{
6
    /// <summary>
7
    /// Karata Chop Tests.
8
    /// </summary>
9
    [TestClass]
10
    public class KarateChopTests
11
    {
12
        /// <summary>
13
        /// Returneds the index of the results are zero.
14
        /// </summary>
15
        [TestMethod]
16
        public void ReturnedResultsAreZeroIndex()
17
        {
18
            Assert.AreEqual(0, KarateChop.Chop(1, new int[] { 1 }));
19
            Assert.AreEqual(0, KarateChop.Chop(1, new int[] { 1, 3, 5 }));
20
            Assert.AreEqual(0, KarateChop.Chop(1, new int[] { 1, 3, 5, 7 }));
21
        }
22
23
        /// <summary>
24
        /// Determines whether this instance [can find targets].
25
        /// </summary>
26
        [TestMethod]
27
        public void CanFindTargets()
28
        {
29
            Assert.AreEqual(1,  KarateChop.Chop(3, new int[] { 1, 3, 5 }));
30
            Assert.AreEqual(2,  KarateChop.Chop(5, new int[] { 1, 3, 5 }));
31
            Assert.AreEqual(1,  KarateChop.Chop(3, new int[] { 1, 3, 5, 7 }));
32
            Assert.AreEqual(2,  KarateChop.Chop(5, new int[] { 1, 3, 5, 7 }));
33
            Assert.AreEqual(3,  KarateChop.Chop(7, new int[] { 1, 3, 5, 7 }));
34
        }
35
36
        /// <summary>
37
        /// Determines whether this instance [can handle empty arrays].
38
        /// </summary>
39
        [TestMethod]
40
        public void CanHandleEmptyArrays()
41
        {
42
            Assert.AreEqual(-1, KarateChop.Chop(3, new int[] { }));
43
        }
44
45
        /// <summary>
46
        /// Determines whether this instance [can identify not found targets].
47
        /// </summary>
48
        [TestMethod]
49
        public void CanIdentifyNotFoundTargets()
50
        {
51
            Assert.AreEqual(-1, KarateChop.Chop(3, new int[] { 1 }));
52
            Assert.AreEqual(-1, KarateChop.Chop(0, new int[] { 1, 3, 5 }));
53
            Assert.AreEqual(-1, KarateChop.Chop(2, new int[] { 1, 3, 5 }));
54
            Assert.AreEqual(-1, KarateChop.Chop(4, new int[] { 1, 3, 5 }));
55
            Assert.AreEqual(-1, KarateChop.Chop(6, new int[] { 1, 3, 5 }));
56
            Assert.AreEqual(-1, KarateChop.Chop(0, new int[] { 1, 3, 5, 7 }));
57
            Assert.AreEqual(-1, KarateChop.Chop(2, new int[] { 1, 3, 5, 7 }));
58
            Assert.AreEqual(-1, KarateChop.Chop(4, new int[] { 1, 3, 5, 7 }));
59
            Assert.AreEqual(-1, KarateChop.Chop(6, new int[] { 1, 3, 5, 7 }));
60
            Assert.AreEqual(-1, KarateChop.Chop(8, new int[] { 1, 3, 5, 7 }));
61
        }
62
    }
63
}