-
Notifications
You must be signed in to change notification settings - Fork 0
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Test median #3
Test median #3
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,33 @@ | ||
/* | ||
Написать метод, который принимает на вход массив целых чисел и возвращает медиану (серединное значение) | ||
|
||
Медианой называется число, стоящее в упорядоченном ряде чисел посередине. | ||
Например, в ряду: 1, 2, 3, 7, 9 — тройка является медианой. Если количество чисел чётное, | ||
то за медиану принимают среднее двух стоящих в центре чисел. | ||
Либо можно выбирать элемент под номером k/2, если k чётное и (k+1)/2, если нечетное. | ||
*/ | ||
|
||
import java.util.Arrays; | ||
|
||
public class Median { | ||
public int median(int[] numbers){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Recommended to use descriptive verbs that indicate what the method does It can also return a double value if the length is even-numbered There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When I used assertEquals for double was error, that I use deprecated method, so first i tried with int. Now I found solution. |
||
if (numbers == null) { | ||
return Integer.MAX_VALUE; | ||
} | ||
if (numbers.length == 0){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There no spaces between |
||
return Integer.MIN_VALUE; | ||
} else { | ||
Arrays.sort(numbers); | ||
int median; | ||
if (numbers.length % 2 == 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Variables can be extracted from 'if':
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
int middleIndex = numbers.length / 2; | ||
median = (numbers[middleIndex] + numbers[middleIndex - 1]) /2; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if the length is even, the code returns an incorrect result There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
} else { | ||
int middleIndex = numbers.length / 2; | ||
median = numbers[middleIndex]; | ||
} | ||
return median; | ||
} | ||
|
||
return 1; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import org.testng.annotations.DataProvider; | ||
|
||
public class DataNumbersProvider { | ||
|
||
@DataProvider(name = "validNumberArrayProvider") | ||
public Object[][] validNumberArrayProvider() { | ||
return new Object[][]{ | ||
{new int[]{1, 2, 3, 4}, 2.5}, | ||
{new int[]{-1, -2, -3, -4}, -2.5}, | ||
{new int[]{1, 3, 5}, 3.0}, | ||
{new int[]{5, 3, 2}, 3.0}, | ||
{new int[]{1, 2, 3, 4, 5, 6}, 3.5}, | ||
{new int[]{6, 5, 4, 3, 2, 1}, 3.5}, | ||
{new int[]{3, 1, 4, 1, 5, 9}, 3.5}, | ||
{new int[]{3, 1, 4, 1, 5}, 3.0}, | ||
{new int[]{-1, -2, -3, -4, -5}, -3.0}, | ||
{new int[]{2147483647, 2147483647, 2147483647, 2147483647}, 2147483647}, | ||
{new int[]{2147483647, 2147483647, 2147483647}, 2147483647}, | ||
{new int[]{2147483647}, 2147483647}, | ||
{new int[]{20, 18, 16, 14, 12, 10, 8, 6, 4, 2}, 11.0}, | ||
{new int[]{1000000, 2000000, 3000001, 4000000, 5000000}, 3000001.0}, | ||
}; | ||
} | ||
|
||
@DataProvider(name = "invalidNumberArrayProvider") | ||
public Object[][] invalidNumberArrayProvider() { | ||
return new Object[][]{ | ||
{null, 0x80000000}, | ||
{new int[]{}, 0x80000000}, | ||
}; | ||
} | ||
|
||
@DataProvider(name = "singleElementArrayProvider") | ||
public Object[][] singleElementArrayProvider() { | ||
return new Object[][]{ | ||
{new int[]{0}, 0.0}, | ||
{new int[]{1}, 1.0}, | ||
{new int[]{2}, 2.0}, | ||
{new int[]{5}, 5.0}, | ||
{new int[]{-5}, -5.0}, | ||
}; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,43 @@ | ||
import org.testng.annotations.BeforeClass; | ||
import org.testng.annotations.Test; | ||
|
||
import static org.testng.AssertJUnit.assertEquals; | ||
|
||
public class MedianTest { | ||
|
||
private Median median; | ||
@BeforeClass | ||
public void beforeClass() { | ||
median = new Median(); | ||
} | ||
|
||
@Test | ||
public void testMedianHN() { | ||
} | ||
|
||
@Test(dataProvider = "validNumberArrayProvider", | ||
dataProviderClass = DataNumbersProvider.class) | ||
public void testFindMedianValidNumberArrayProvider(int[] input, double expected) { | ||
double actual = median.median(input); | ||
assertEquals(expected, actual); | ||
} | ||
@Test(dataProvider = "validNumberArrayProvider", | ||
dataProviderClass = DataNumbersProvider.class) | ||
public void testFindMedianValidNumberArrayProviderQuickSelect(int[] input, double expected) { | ||
double actual = median.median(input); | ||
assertEquals(expected, actual); | ||
} | ||
@Test(dataProvider = "invalidNumberArrayProvider", | ||
dataProviderClass = DataNumbersProvider.class) | ||
public void testCalculateMedianWithInvalidInput(int[] numbers, double expected) { | ||
double actual = median.median(numbers); | ||
assertEquals(expected, actual); | ||
} | ||
|
||
@Test(dataProvider = "singleElementArrayProvider", | ||
dataProviderClass = DataNumbersProvider.class) | ||
public void testFindMedianValidNumberArrayProviderWithSingleElementArrayProvider(int[] input, double expected) { | ||
double actual = median.median(input); | ||
assertEquals(actual, expected); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move this file to a named package
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done