Skip to content
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

Closed
wants to merge 3 commits into from
Closed

Test median #3

wants to merge 3 commits into from

Conversation

Lokankara
Copy link
Collaborator

No description provided.

@Lokankara Lokankara requested a review from IrynaKolh March 5, 2024 20:29
} else {
Arrays.sort(numbers);
int median;
if (numbers.length % 2 == 0) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variables can be extracted from 'if':

int size = numbers.length;

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

*/

import java.util.Arrays;

public class Median {
public int median(int[] numbers){
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommended to use descriptive verbs that indicate what the method does
Here are some examples: findMedian, getMedian, calculateMedian

It can also return a double value if the length is even-numbered

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Owner

Choose a reason for hiding this comment

The 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.

*/

import java.util.Arrays;

public class Median {
Copy link
Collaborator Author

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

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

if (numbers == null) {
return Integer.MAX_VALUE;
}
if (numbers.length == 0){
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There no spaces between ){
It's better to format the code: Ctrl+Alt+L

int median;
if (numbers.length % 2 == 0) {
int middleIndex = numbers.length / 2;
median = (numbers[middleIndex] + numbers[middleIndex - 1]) /2;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the length is even, the code returns an incorrect result
Probably because of the int cast

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@IrynaKolh IrynaKolh closed this Mar 6, 2024
@Lokankara Lokankara deleted the test-median branch March 9, 2024 14:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants