From 2adb607d952305d1208d0ccd937270f952cb1c3c Mon Sep 17 00:00:00 2001 From: Florin Bicher Date: Thu, 7 Aug 2014 16:00:39 +0300 Subject: [PATCH] FindMissing added --- .idea/.name | 2 +- .idea/modules.xml | 2 +- CustomList.iml | 23 ----- src/main/java/ua/com/florin/Runner.java | 10 ++ .../java/ua/com/florin/algorithm/Sorter.java | 93 +++++++++++++++++++ .../com/florin/findmissing/FindMissing.java | 39 ++++++++ .../ua/com/florin/substring/SubStringer.java | 18 ++++ src/test/java/FindMissingTest.java | 29 ++++++ 8 files changed, 191 insertions(+), 25 deletions(-) delete mode 100644 CustomList.iml create mode 100644 src/main/java/ua/com/florin/algorithm/Sorter.java create mode 100644 src/main/java/ua/com/florin/findmissing/FindMissing.java create mode 100644 src/test/java/FindMissingTest.java diff --git a/.idea/.name b/.idea/.name index b623db3..5821b3f 100644 --- a/.idea/.name +++ b/.idea/.name @@ -1 +1 @@ -CustomList \ No newline at end of file +TrainingProject \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml index 705b872..a0f79ba 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,7 +2,7 @@ - + diff --git a/CustomList.iml b/CustomList.iml deleted file mode 100644 index 8f05fa6..0000000 --- a/CustomList.iml +++ /dev/null @@ -1,23 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/main/java/ua/com/florin/Runner.java b/src/main/java/ua/com/florin/Runner.java index 8e34162..d87ac5d 100644 --- a/src/main/java/ua/com/florin/Runner.java +++ b/src/main/java/ua/com/florin/Runner.java @@ -3,13 +3,23 @@ import main.java.ua.com.florin.customlist.CustomList; import main.java.ua.com.florin.substring.SubStringer; +import java.util.LinkedList; +import java.util.List; + /** * Created by florin on 19.07.2014. */ public class Runner { public static void main(String[] args) { + List stringList = new LinkedList(); + for (int i = 0; i < 10; i++) { + stringList.add("test " + i); + } + System.out.println(stringList); + stringList.subList(0, 5).clear(); + System.out.println(stringList); } } diff --git a/src/main/java/ua/com/florin/algorithm/Sorter.java b/src/main/java/ua/com/florin/algorithm/Sorter.java new file mode 100644 index 0000000..08a6391 --- /dev/null +++ b/src/main/java/ua/com/florin/algorithm/Sorter.java @@ -0,0 +1,93 @@ +package main.java.ua.com.florin.algorithm; + +/** + * Contains static methods with custom realizations of different sorting algorithms for sorting an array + *

+ * Created by florin on 06.08.2014. + */ +public class Sorter { + + /** + * + * @param a target array + */ + public static void sortByInsertion(int[] a) { + for (int i = 1; i < a.length; i++) { + for (int j = i; j > 0 && a[j] < a[j - 1]; j--) { + swap(a, j, j - 1); + } + } + } + + /** + * + * @param a target array + */ + public static void sortBySelection(int[] a) { + for (int i = 1; i < a.length - 1; i++) { + for (int j = i + 1; j < a.length; j++) { + if (a[j] < a[i]) { + swap(a, i, j); + } + } + } + } + + /** + * + * @param a target array + */ + public static void sortByBubble(int[] a) { + for (int i = 0; i < a.length; i++) { + for (int j = 0; j < a.length - i - 1; j++) { + if (a[j] > a[j + 1]) { + swap(a, j, j + 1); + } + } + } + } + + /** + * + * @param a target array + * @param low + * @param high + */ + public static void sortByQuick(int[] a, int low, int high) { + int i = low; + int j = high; + int pivot = a[a.length / 2]; + while (i <= j) { + while (a[i] < pivot) { + i++; + } + while (a[j] > pivot) { + j--; + } + if (i <= j) { + swap(a, i, j); + i++; + j--; + } + } + if (low < j) { + sortByQuick(a, low, j); + } + if (i < high) { + sortByQuick(a, i, high); + } + } + + /** + * Swaps the elements in the given array + * + * @param a target array + * @param i first element to be swapped + * @param j second element to be swapped + */ + private static void swap(int[] a, int i, int j) { + int tmp = a[i]; + a[i] = a[j]; + a[j] = tmp; + } +} diff --git a/src/main/java/ua/com/florin/findmissing/FindMissing.java b/src/main/java/ua/com/florin/findmissing/FindMissing.java new file mode 100644 index 0000000..7e76adb --- /dev/null +++ b/src/main/java/ua/com/florin/findmissing/FindMissing.java @@ -0,0 +1,39 @@ +package main.java.ua.com.florin.findmissing; + +/** + * Created by florin on 06.08.2014. + */ +public class FindMissing { + + /** + * Finds the missing number in the array of consecutive numbers + * + * @param a an array with consecutive numbers with one missing + * @return the missing number + */ + public static int findMissing(int[] a) { + + // sum of all elements in the array + int fullSum = 0; + // number of elements in the array, supposing that one is missing + int fullLen = a.length + 1; + + // calculating the sum of the array's elements + for (int i = 1; i <= fullLen; i++) { + fullSum += i; + } + + // sum of the actual elements in the array + int partSum = 0; + // actual length of the array + int partLen = a.length; + + // calculating the sum of actual array's elements + for (int elem : a) { + partSum += elem; + } + + // the missing element will be equal to the difference between two calculated sums + return fullSum - partSum; + } +} diff --git a/src/main/java/ua/com/florin/substring/SubStringer.java b/src/main/java/ua/com/florin/substring/SubStringer.java index 9ba866e..c5893f7 100644 --- a/src/main/java/ua/com/florin/substring/SubStringer.java +++ b/src/main/java/ua/com/florin/substring/SubStringer.java @@ -1,11 +1,21 @@ package main.java.ua.com.florin.substring; /** + * Contains custom realizations of substring methods for the sake of practice + *

* Created by florin on 20.07.2014. */ public class SubStringer { + /** + * Returns the substring beginning from the given start position inclusively + * + * @param string target string for getting the substring + * @param startPos start position from which to get the substring (inclusively) + * @return substring from startPos inclusively + */ public static String mySubString(String string, int startPos) { + // create char array from target string char[] stringArray = string.toCharArray(); @@ -32,6 +42,14 @@ public static String mySubString(String string, int startPos) { return new String(subStringArray); } + /** + * Returns the substring beginning from the given start position inclusively and endPosition exclusively + * + * @param string target string for getting the substring + * @param startPos start position from which to get the substring (inclusively) + * @param endPos end position to which to get the substring (exclusively) + * @return substring from startPos inclusively and endPos exclusively + */ public static String mySubString(String string, int startPos, int endPos) { // create char array from target string char[] stringArray = string.toCharArray(); diff --git a/src/test/java/FindMissingTest.java b/src/test/java/FindMissingTest.java new file mode 100644 index 0000000..5e16ec9 --- /dev/null +++ b/src/test/java/FindMissingTest.java @@ -0,0 +1,29 @@ +package test.java; + +import main.java.ua.com.florin.findmissing.FindMissing; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +public class FindMissingTest { + + int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 10}; + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + array = null; + } + + @Test + public void testFindMissing() throws Exception { + assertEquals(9, FindMissing.findMissing(array)); + assertNotEquals(10, FindMissing.findMissing(array)); + } +} \ No newline at end of file