forked from 12meses12katas/Octubre-Karate-Chop
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request 12meses12katas#2 from tmoreno/master
Versión recursiva
- Loading branch information
Showing
8 changed files
with
256 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" output="target/classes" path="src/main/java"/> | ||
<classpathentry kind="src" output="target/test-classes" path="src/test/java"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/> | ||
<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/> | ||
<classpathentry kind="output" path="target/classes"/> | ||
</classpath> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>karatechop</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
<buildCommand> | ||
<name>org.maven.ide.eclipse.maven2Builder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
<nature>org.maven.ide.eclipse.maven2Nature</nature> | ||
</natures> | ||
</projectDescription> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#Tue Oct 04 18:38:54 CEST 2011 | ||
eclipse.preferences.version=1 | ||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 | ||
org.eclipse.jdt.core.compiler.compliance=1.6 | ||
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning | ||
org.eclipse.jdt.core.compiler.source=1.6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#Tue Oct 04 18:37:06 CEST 2011 | ||
activeProfiles= | ||
eclipse.preferences.version=1 | ||
fullBuildGoals=process-test-resources | ||
resolveWorkspaceProjects=true | ||
resourceFilterGoals=process-resources resources\:testResources | ||
skipCompilerPlugin=true | ||
version=1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>org.kata</groupId> | ||
<artifactId>karatechop</artifactId> | ||
<version>1.0.0</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>karatechop</name> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>2.3.2</version> | ||
<configuration> | ||
<source>1.6</source> | ||
<target>1.6</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.9</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
29 changes: 29 additions & 0 deletions
29
tmoreno/karatechop/src/main/java/org/kata/karatechop/KarateChopIterativo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.kata.karatechop; | ||
|
||
public class KarateChopIterativo { | ||
|
||
public static int chop(int value, int [] array){ | ||
int low = 0; | ||
int high = array.length; | ||
int middle = (high - low) / 2; | ||
|
||
int position = -1; | ||
|
||
while((high - low) > 0){ | ||
if(array[middle] == value){ | ||
position = middle; | ||
break; | ||
} | ||
else if(value < array[middle]){ | ||
high = middle; | ||
middle = (high - low) / 2; | ||
} | ||
else{ | ||
low = middle + 1; | ||
middle = ((high - low) / 2) + middle + 1; | ||
} | ||
} | ||
|
||
return position; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
tmoreno/karatechop/src/main/java/org/kata/karatechop/KarateChopRecursivo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.kata.karatechop; | ||
|
||
public class KarateChopRecursivo { | ||
|
||
public static int chop(int value, int [] array){ | ||
int position = -1; | ||
int middle = array.length / 2; | ||
|
||
if(array.length != 0){ | ||
if(array[middle] == value){ | ||
position = middle; | ||
} | ||
else if(value < array[middle]){ | ||
position = chop(value, subArray(array, 0, middle)); | ||
} | ||
else{ | ||
position = chop(value, subArray(array, middle + 1, array.length)); | ||
|
||
// Cuando se encuentra la posición del valor | ||
// en la mitad superior, hay que añadir la posición de la mitad | ||
if(position != -1){ | ||
position += middle + 1; | ||
} | ||
} | ||
} | ||
|
||
return position; | ||
} | ||
|
||
private static int[] subArray(int[] original, int from, int to) { | ||
int newLength = to - from; | ||
|
||
if (newLength < 0){ | ||
return new int [0]; | ||
} | ||
else{ | ||
int[] copy = new int[newLength]; | ||
System.arraycopy(original, from, copy, 0, | ||
Math.min(original.length - from, newLength)); | ||
return copy; | ||
} | ||
} | ||
} |
101 changes: 101 additions & 0 deletions
101
tmoreno/karatechop/src/test/java/org/kata/karatechop/KarateChopTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package org.kata.karatechop; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.junit.Test; | ||
|
||
|
||
public class KarateChopTest { | ||
|
||
private int [] emptyArray = {}; | ||
|
||
private int [] intArray1 = {1}; | ||
|
||
private int [] intArray3 = {1, 3, 5}; | ||
|
||
private int [] intArray4 = {1, 3, 5, 7}; | ||
|
||
private int [] intArray6 = {1, 3, 5, 7, 9, 11, 13}; | ||
|
||
@Test | ||
public void karateChopTest() { | ||
assertEquals(-1, KarateChopRecursivo.chop(3, emptyArray)); | ||
assertEquals(-1, KarateChopRecursivo.chop(3, intArray1)); | ||
assertEquals(0, KarateChopRecursivo.chop(1, intArray1)); | ||
|
||
assertEquals(0, KarateChopRecursivo.chop(1, intArray3)); | ||
assertEquals(1, KarateChopRecursivo.chop(3, intArray3)); | ||
assertEquals(2, KarateChopRecursivo.chop(5, intArray3)); | ||
assertEquals(-1, KarateChopRecursivo.chop(0, intArray3)); | ||
assertEquals(-1, KarateChopRecursivo.chop(2, intArray3)); | ||
assertEquals(-1, KarateChopRecursivo.chop(4, intArray3)); | ||
assertEquals(-1, KarateChopRecursivo.chop(6, intArray3)); | ||
|
||
assertEquals(0, KarateChopRecursivo.chop(1, intArray4)); | ||
assertEquals(1, KarateChopRecursivo.chop(3, intArray4)); | ||
assertEquals(2, KarateChopRecursivo.chop(5, intArray4)); | ||
assertEquals(3, KarateChopRecursivo.chop(7, intArray4)); | ||
assertEquals(-1, KarateChopRecursivo.chop(0, intArray4)); | ||
assertEquals(-1, KarateChopRecursivo.chop(2, intArray4)); | ||
assertEquals(-1, KarateChopRecursivo.chop(4, intArray4)); | ||
assertEquals(-1, KarateChopRecursivo.chop(6, intArray4)); | ||
assertEquals(-1, KarateChopRecursivo.chop(8, intArray4)); | ||
|
||
assertEquals(0, KarateChopRecursivo.chop(1, intArray6)); | ||
assertEquals(1, KarateChopRecursivo.chop(3, intArray6)); | ||
assertEquals(2, KarateChopRecursivo.chop(5, intArray6)); | ||
assertEquals(3, KarateChopRecursivo.chop(7, intArray6)); | ||
assertEquals(4, KarateChopRecursivo.chop(9, intArray6)); | ||
assertEquals(5, KarateChopRecursivo.chop(11, intArray6)); | ||
assertEquals(6, KarateChopRecursivo.chop(13, intArray6)); | ||
assertEquals(-1, KarateChopRecursivo.chop(0, intArray6)); | ||
assertEquals(-1, KarateChopRecursivo.chop(2, intArray6)); | ||
assertEquals(-1, KarateChopRecursivo.chop(4, intArray6)); | ||
assertEquals(-1, KarateChopRecursivo.chop(6, intArray6)); | ||
assertEquals(-1, KarateChopRecursivo.chop(8, intArray6)); | ||
assertEquals(-1, KarateChopRecursivo.chop(10, intArray6)); | ||
assertEquals(-1, KarateChopRecursivo.chop(12, intArray6)); | ||
assertEquals(-1, KarateChopRecursivo.chop(14, intArray6)); | ||
} | ||
|
||
@Test | ||
public void karateChopIterativoTest() { | ||
assertEquals(-1, KarateChopIterativo.chop(3, emptyArray)); | ||
assertEquals(-1, KarateChopIterativo.chop(3, intArray1)); | ||
assertEquals(0, KarateChopIterativo.chop(1, intArray1)); | ||
|
||
assertEquals(0, KarateChopIterativo.chop(1, intArray3)); | ||
assertEquals(1, KarateChopIterativo.chop(3, intArray3)); | ||
assertEquals(2, KarateChopIterativo.chop(5, intArray3)); | ||
assertEquals(-1, KarateChopIterativo.chop(0, intArray3)); | ||
assertEquals(-1, KarateChopIterativo.chop(2, intArray3)); | ||
assertEquals(-1, KarateChopIterativo.chop(4, intArray3)); | ||
assertEquals(-1, KarateChopIterativo.chop(6, intArray3)); | ||
|
||
assertEquals(0, KarateChopIterativo.chop(1, intArray4)); | ||
assertEquals(1, KarateChopIterativo.chop(3, intArray4)); | ||
assertEquals(2, KarateChopIterativo.chop(5, intArray4)); | ||
assertEquals(3, KarateChopIterativo.chop(7, intArray4)); | ||
assertEquals(-1, KarateChopIterativo.chop(0, intArray4)); | ||
assertEquals(-1, KarateChopIterativo.chop(2, intArray4)); | ||
assertEquals(-1, KarateChopIterativo.chop(4, intArray4)); | ||
assertEquals(-1, KarateChopIterativo.chop(6, intArray4)); | ||
assertEquals(-1, KarateChopIterativo.chop(8, intArray4)); | ||
|
||
assertEquals(0, KarateChopIterativo.chop(1, intArray6)); | ||
assertEquals(1, KarateChopIterativo.chop(3, intArray6)); | ||
assertEquals(2, KarateChopIterativo.chop(5, intArray6)); | ||
assertEquals(3, KarateChopIterativo.chop(7, intArray6)); | ||
assertEquals(4, KarateChopIterativo.chop(9, intArray6)); | ||
assertEquals(5, KarateChopIterativo.chop(11, intArray6)); | ||
assertEquals(6, KarateChopIterativo.chop(13, intArray6)); | ||
assertEquals(-1, KarateChopIterativo.chop(0, intArray6)); | ||
assertEquals(-1, KarateChopIterativo.chop(2, intArray6)); | ||
assertEquals(-1, KarateChopIterativo.chop(4, intArray6)); | ||
assertEquals(-1, KarateChopIterativo.chop(6, intArray6)); | ||
assertEquals(-1, KarateChopIterativo.chop(8, intArray6)); | ||
assertEquals(-1, KarateChopIterativo.chop(10, intArray6)); | ||
assertEquals(-1, KarateChopIterativo.chop(12, intArray6)); | ||
assertEquals(-1, KarateChopIterativo.chop(14, intArray6)); | ||
} | ||
} |