Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add JUnit test to some Algorithms#23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
TUGVince wants to merge1 commit intorampatra:master
base:master
Choose a base branch
Loading
fromTUGVince:master
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions.project
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Algorithms-and-Data-Structures-in-Java</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
2 changes: 2 additions & 0 deletions.settings/org.eclipse.core.resources.prefs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
eclipse.preferences.version=1
encoding/<project>=UTF-8
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
package InsertionSort;

import static org.junit.Assert.assertArrayEquals;

import org.junit.Test;

public class InsertionSort2Test {
@Test
public void shouldDoNothingWithEmptyArray() {
int[] values = {};

InsertionSort2.insertionSortPart2(values);
}

@Test
public void shouldDoNothingWithOneElementArray() {
int[] values = {16};

InsertionSort2.insertionSortPart2(values);

assertArrayEquals(new int[] {16}, values);
}

@Test
public void shouldSortValues() {
int[] values = { 11, -4, 3, 0, 1};
int[] expectedOrder = { -4, 0, 1, 3, 11};

InsertionSort2.insertionSortPart2(values);

assertArrayEquals(expectedOrder, values);
}
}
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
package palimdromeIndex;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

public class PalindromeIndexTest {

private String input;

@Before
public void setUp() throws Exception {

input = null;

}

@After
public void tearDown() throws Exception {

}

@Test(expected = NullPointerException.class)
public void nullStringTest() throws Exception {

PalindromeIndex.isPalindrome(null);

}

@Test
public void emptyStringTest() throws Exception {

input = "";

assertTrue(PalindromeIndex.isPalindrome(input));

}

@Test
public void singleCharTest() throws Exception {

input = "H";

assertTrue(PalindromeIndex.isPalindrome(input));

}

@Test
public void alphaNumericPalindromeTest() throws Exception {

input = "1234321";

assertTrue(PalindromeIndex.isPalindrome(input));
}

@Test
public void validPalindromeTest() throws Exception {

input = "madam";

assertTrue(PalindromeIndex.isPalindrome(input));
}

@Test
public void invalidWordPalindromeTest() throws Exception {

input = "rotators";

assertFalse(PalindromeIndex.isPalindrome(input));
}

@Test
public void invalidPalindromeTest() throws Exception {

input = "I am a tester";

assertFalse(PalindromeIndex.isPalindrome(input));
}
}
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
package sentencePalindrome;

public class SentencePalindrome {
static boolean isPalindrome(String str) {
int l = 0;
int h = str.length() - 1;

// Lowercase string
str = str.toLowerCase();

// Compares character until they are equal
while (l <= h) {

char getAtl = str.charAt(l);
char getAth = str.charAt(h);

// If there is another symbol in left
// of sentence
if (!(getAtl >= 'a' && getAtl <= 'z'))
l++;

// If there is another symbol in right
// of sentence
else if (!(getAth >= 'a' && getAth <= 'z'))
h--;

// If characters are equal
else if (getAtl == getAth) {
l++;
h--;
}

else
return false;
}
return true;
}
public static void main(String[] args) {


}
}
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
package sentencePalindrome;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

public class SentencePalindromeTest {

private String input;

@Before
public void setUp() throws Exception {

input = null;

}

@After
public void tearDown() throws Exception {

}

@Test(expected = NullPointerException.class)
public void nullStringTest() throws Exception {

SentencePalindrome.isPalindrome(null);
}

@Test
public void emptyStringTest() throws Exception {

input = "";

assertTrue(SentencePalindrome.isPalindrome(input));

}

@Test
public void multipleWhiteSpaceTest() throws Exception {

input = "A Santa at Nasa";

assertTrue(SentencePalindrome.isPalindrome(input));

}

@Test
public void singleCharTest() throws Exception {

input = "H";

assertTrue(SentencePalindrome.isPalindrome(input));

}

@Test
public void punctuationTest() throws Exception {

input = "Eva, can I see bees in a cave?";

assertTrue(SentencePalindrome.isPalindrome(input));

}

@Test
public void unicodeTest() throws Exception {

input = "Step on no pet.";

assertFalse(SentencePalindrome.isPalindrome(input));

}

@Test
public void alphaNumericPalindromeTest() throws Exception {

input = "Air 2 an a2ria";

assertTrue(SentencePalindrome.isPalindrome(input));
}

@Test
public void validPalindromeTest() throws Exception {

input = "No lemon no melon";

assertTrue(SentencePalindrome.isPalindrome(input));
}

@Test
public void invalidPalindromeTest() throws Exception {

input = "I am a tester";

assertFalse(SentencePalindrome.isPalindrome(input));
}
}
72 changes: 36 additions & 36 deletionssrc/main/java/com/rampatra/sorting/BubbleSort.java
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,42 +8,42 @@
*/
public class BubbleSort {

/**
* In bubble sort, we start at the beginning of the array and swap
* the first twoelements if the first is greater than the second.
* Then, we go to the nextpair, and so on, continuously making sweeps
* ofthearray until it is sorted. In doing so,thesmaller items
* slowly "bubble" up to the beginning ofthelist and in each inner
* iteration the largest element is sorted. Ergo, the inner loop runs
* until {@code length - i - 1} times. Timecomplexity: O(n^2). Space
* complexity: O(1), in place. To learn more: {@see https://youtu.be/6Gv8vg0kcHc}
*
* @param ar to be sorted
*/
privatestatic void bubbleSort(int[] ar) {
for (int i = 0; i < ar.length - 1; i++) {
for (int j = 0; j < ar.length - i - 1; j++) {
if (ar[j] > ar[j + 1]) {
swap(ar, j, j + 1);
}
}
}
}
/**
* In bubble sort, we start at the beginning of the array and swap the first two
*elements if the first is greater than the second. Then, we go to the next
*pair, and so on, continuously making sweeps of the array until it is sorted.
* In doing so,thesmaller items slowly "bubble" up tothebeginning of the
* list and in each inner iterationthelargest element is sorted. Ergo, the
* inner loop runs until {@code length - i - 1} times. Time complexity: O(n^2).
* Spacecomplexity: O(1), in place. To learn more:
* {@see https://youtu.be/6Gv8vg0kcHc}
*
* @param ar to be sorted
*/
static void bubbleSort(int[] ar) {
for (int i = 0; i < ar.length - 1; i++) {
for (int j = 0; j < ar.length - i - 1; j++) {
if (ar[j] > ar[j + 1]) {
swap(ar, j, j + 1);
}
}
}
}

private static void swap(int[] ar, int i, int j) {
int temp = ar[i];
ar[i] = ar[j];
ar[j] = temp;
}
private static void swap(int[] ar, int i, int j) {
int temp = ar[i];
ar[i] = ar[j];
ar[j] = temp;
}

public static void main(String[] args) {
int[] ar = {2, 5, 1, 7, 8};
System.out.println(Arrays.toString(ar));
bubbleSort(ar);
System.out.println(Arrays.toString(ar));
ar = new int[]{7, 5, 1, 7, 8, 0, 23};
System.out.println(Arrays.toString(ar));
bubbleSort(ar);
System.out.println(Arrays.toString(ar));
}
public static void main(String[] args) {
int[] ar = {2, 5, 1, 7, 8};
System.out.println(Arrays.toString(ar));
bubbleSort(ar);
System.out.println(Arrays.toString(ar));
ar = new int[] {7, 5, 1, 7, 8, 0, 23};
System.out.println(Arrays.toString(ar));
bubbleSort(ar);
System.out.println(Arrays.toString(ar));
}
}
32 changes: 32 additions & 0 deletionssrc/main/java/com/rampatra/sorting/BubbleSortTest.java
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
package BubbleSort;
import static org.junit.Assert.assertArrayEquals;

import org.junit.Test;

public class BubbleSortTest {
@Test
public void shouldDoNothingWithEmptyArray() {
int[] values = {};

BubbleSort.bubbleSort(values);
}

@Test
public void shouldDoNothingWithOneElementArray() {
int[] values = {16};

BubbleSort.bubbleSort(values);

assertArrayEquals(new int[] {16}, values);
}

@Test
public void shouldSortValues() {
int[] values = { 11, -4, 3, 0, 1};
int[] expectedOrder = { -4, 0, 1, 3, 11};

BubbleSort.bubbleSort(values);

assertArrayEquals(expectedOrder, values);
}
}

[8]ページ先頭

©2009-2025 Movatter.jp