Skip to content

Commit

Permalink
FindMissing added
Browse files Browse the repository at this point in the history
  • Loading branch information
F1orin committed Aug 7, 2014
1 parent bff0bfc commit 2adb607
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 0 additions & 23 deletions CustomList.iml

This file was deleted.

10 changes: 10 additions & 0 deletions src/main/java/ua/com/florin/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> stringList = new LinkedList<String>();
for (int i = 0; i < 10; i++) {
stringList.add("test " + i);
}
System.out.println(stringList);

stringList.subList(0, 5).clear();
System.out.println(stringList);
}

}
93 changes: 93 additions & 0 deletions src/main/java/ua/com/florin/algorithm/Sorter.java
Original file line number Diff line number Diff line change
@@ -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
* <p/>
* 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;
}
}
39 changes: 39 additions & 0 deletions src/main/java/ua/com/florin/findmissing/FindMissing.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
18 changes: 18 additions & 0 deletions src/main/java/ua/com/florin/substring/SubStringer.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
package main.java.ua.com.florin.substring;

/**
* Contains custom realizations of substring methods for the sake of practice
* <p/>
* 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();

Expand All @@ -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();
Expand Down
29 changes: 29 additions & 0 deletions src/test/java/FindMissingTest.java
Original file line number Diff line number Diff line change
@@ -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));
}
}

0 comments on commit 2adb607

Please sign in to comment.