From 50ff9ebb12fae24ad0bf98206543e2fd97dd89f3 Mon Sep 17 00:00:00 2001 From: Abel Barrientos Date: Thu, 24 Aug 2017 11:38:54 -0400 Subject: [PATCH] Solving the Kata --- .../coding/abel/MiddlePermutation.java | 65 +++++++++++++++++++ .../coding/abel/MiddlePermutationTest.java | 50 ++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 src/main/java/org/fundacionjala/coding/abel/MiddlePermutation.java create mode 100644 src/test/java/org/fundacionjala/coding/abel/MiddlePermutationTest.java diff --git a/src/main/java/org/fundacionjala/coding/abel/MiddlePermutation.java b/src/main/java/org/fundacionjala/coding/abel/MiddlePermutation.java new file mode 100644 index 0000000..13adc5a --- /dev/null +++ b/src/main/java/org/fundacionjala/coding/abel/MiddlePermutation.java @@ -0,0 +1,65 @@ +package org.fundacionjala.coding.abel; + +import java.util.Arrays; + +/** + * You are given a string s. Every letter in s appears once. + * Consider all strings formed by rearranging the letters in s. After ordering these strings in dictionary order, + * return the middle term. (If the sequence has a even length n, define its middle term to be the (n/2)th term.) + * Example + * For s = "abc", the result should be "bac". + * The permutations in order are: + * "abc", "acb", "bac", "bca", "cab", "cba" + * So, The middle term is "bac". + * Input/Output + * [input] string s + * unique letters (2 < length <= 26) + * [output] a string + * middle permutation. + */ +public final class MiddlePermutation { + + /** + * Private Constructor. + */ + private MiddlePermutation() { + + } + + /** + * Returns the Permutation located at mid position of all permutations available in the string. + * + * @param string String. + * @return Permutation located at mid position. + */ + public static String findMidPerm(String string) { + int length = string.length(); + char[] currentCharArray = string.toCharArray(); + Arrays.sort(currentCharArray); + char[] resultCharArray = new char[currentCharArray.length]; + int half = length / 2; + int last = length - 1; + for (int i = last, j = 0; j < half - 1; i--, j++) { + resultCharArray[i] = currentCharArray[j]; + } + if (length % 2 != 0) { + resultCharArray[0] = currentCharArray[half]; + resultCharArray[1] = currentCharArray[half - 1]; + for (int i = last, j = 2; i > half; i--, j++) { + resultCharArray[j] = currentCharArray[i]; + } + } else { + resultCharArray[0] = currentCharArray[half - 1]; + resultCharArray[half] = currentCharArray[half]; + for (int i = last, j = 1; i > half; i--, j++) { + resultCharArray[j] = currentCharArray[i]; + } + } + StringBuilder sb = new StringBuilder(); + for (char c : resultCharArray) { + sb.append(c); + } + return sb.toString(); + } + +} diff --git a/src/test/java/org/fundacionjala/coding/abel/MiddlePermutationTest.java b/src/test/java/org/fundacionjala/coding/abel/MiddlePermutationTest.java new file mode 100644 index 0000000..ff14bc8 --- /dev/null +++ b/src/test/java/org/fundacionjala/coding/abel/MiddlePermutationTest.java @@ -0,0 +1,50 @@ +package org.fundacionjala.coding.abel; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Modifier; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * This class test Middle Permutation Class. + */ +public class MiddlePermutationTest { + + /** + * Test for Private Constructors. + * @throws NoSuchMethodException Thrown when a particular method cannot be found. + * @throws IllegalAccessException Thrown when an application tries to reflectively create an instance. + * @throws InvocationTargetException thrown by an invoked method or constructor. + * @throws InstantiationException Thrown when an application tries to create an instance of a class + * using the {@code newInstance} method in class. + */ + @Test + public void privateConstructorTest() throws NoSuchMethodException, IllegalAccessException, + InvocationTargetException, InstantiationException { + Constructor constructor = MiddlePermutation.class.getDeclaredConstructor(); + assertTrue(Modifier.isPrivate(constructor.getModifiers())); + constructor.setAccessible(true); + constructor.newInstance(); + } + + /** + * Provided and new tests. + */ + @Test + public void basicTests() { + assertEquals("bac", MiddlePermutation.findMidPerm("abc")); + assertEquals("bdca", MiddlePermutation.findMidPerm("abcd")); + assertEquals("cbeda", MiddlePermutation.findMidPerm("abcde")); + assertEquals("cfedba", MiddlePermutation.findMidPerm("abcdef")); + assertEquals("dcgfeba", MiddlePermutation.findMidPerm("abcdefg")); + assertEquals("dhgfecba", MiddlePermutation.findMidPerm("abcdefgh")); + assertEquals("edihgfcba", MiddlePermutation.findMidPerm("abcdefghi")); + assertEquals("ejihgfdcba", MiddlePermutation.findMidPerm("abcdefghij")); + assertEquals("fekjihgdcba", MiddlePermutation.findMidPerm("abcdefghijk")); + + } +}