The Wayback Machine - https://web.archive.org/web/20250504190912/https://github.com/TheAlgorithms/Java/pull/2005
Skip to content

Added New Java file in Java/Misc #2005

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

Closed
wants to merge 6 commits into from

Conversation

AzadN
Copy link
Contributor

@AzadN AzadN commented Nov 4, 2020

@shellhub as discussed in PR #1990, this is the 1st separate pull request where I've added a new Java File to find the longest length of consecutive numbers occurring in an array. More to follow :)
@Kush1101 could you too review this and my other PRs too?

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

References

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Java files are placed inside an existing directory.
  • All filenames are in all uppercase characters with no spaces or dashes.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

@AzadN
Copy link
Contributor Author

AzadN commented Nov 4, 2020

I can't understand why this prettier check is failing, didn't fail for the previous commit with all files together :( Please help @shellhub @AkMo3

{
int longestRange = 0;
HashMap<Integer,Boolean> num = new HashMap<>(); // Stores a mapping of a number to whether the current number is part of a particular consecutive range or not
for(int x : nums)
Copy link

Choose a reason for hiding this comment

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

The code may be failing prettier check because of this. According to pritter as far I know the correct code will be
for(int x : nums) num.put(x, true);

Copy link

@AkMo3 AkMo3 Nov 4, 2020

Choose a reason for hiding this comment

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

Seems I was right, this is the actual format in the final format.

@AkMo3
Copy link

AkMo3 commented Nov 4, 2020

Now, first things first, you had sent PR in wrong branch :) . The correct branch is development. Now don`t close this PR, first lets fix the issue and then close the PR and make a new one. The above is not the only error so it is better to show how to format code.

public static int longestRange(int[] nums)
{
int longestRange = 0;
HashMap<Integer,Boolean> num = new HashMap<>(); // Stores a mapping of a number to whether the current number is part of a particular consecutive range or not
Copy link

Choose a reason for hiding this comment

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

You would face a lot of problem while formatting this comment. Rather than using // try something like this -
/** Stores a mapping of a number to whether the current number {NEW_LINE} * is part of a particular consecutive range or not. */
These javadoc comments are formated better by google-java-format, as well as prettier.

@AkMo3
Copy link

AkMo3 commented Nov 4, 2020

After doing the above steps download Prettier, and install it. Now comes the trick part. What actually happens here is that usually Prettier and Google-java-format collide and undo each others code changes. What worked for is the following pattern -

  1. Run Google-java-formatter
  2. Run Prettier
  3. And again google-java-formatter

For confirmation that this work go here Prettier-test. This is exactly your formatted code, which has passed all the tests.

import java.util.*;
public class largestRange
{
// Finds the length of longest occurring consecutive numbers range in an array
Copy link

Choose a reason for hiding this comment

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

Add javadoc comments explaining, what this code does in breif.

Copy link

Choose a reason for hiding this comment

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

Better do that before formatting so mentioned it right away.

@AzadN
Copy link
Contributor Author

AzadN commented Nov 4, 2020

Well thanks, @AkMo3 for such a detailed explanation, but the prettier check has failed again despite using the formatted code sent by you. Should I format these on my own device following all the said steps before committing here?

@AkMo3
Copy link

AkMo3 commented Nov 4, 2020

I just ran through test results, the problem is not with your code. The test is failing because it is not able to fetch your repository, so the test is failing. Close this PR and try this on development branch from your development branch. Hope this things get solved then. The code is formatted properly no issue with the code.

@AzadN
Copy link
Contributor Author

AzadN commented Nov 4, 2020

@AkMo3 I'm unable to send a PR from my development branch to this development branch. It doesn't allow to set more than one upstreams on my local machine :(
I issued PR on master since the recent merges were from master to master themselves.
@shellhub could you review the current commit and merge my changes?

@AkMo3
Copy link

AkMo3 commented Nov 4, 2020

@AkMo3 I'm unable to send a PR from my development branch to this development branch. It doesn't allow to set more than one upstreams on my local machine :(
I issued PR on master since the recent merges were from master to master themselves.
@shellhub could you review the current commit and merge my changes?

No need to send from your machine. Send PR to your development branch from AzadN:Largest_Range using website only. Squash Merge them. Then send PR to TheAlgorithms development branch.

* Stores a mapping of a number to whether the current number is part of a particular
* consecutive range or not.
*/
for (int x : nums) num.put(x, true);
Copy link

@prashantdoshi28 prashantdoshi28 Nov 12, 2020

Choose a reason for hiding this comment

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

I'm not sure but can't we just use lookahead type of approach over here?

For example,

int[] input_arr = new int[]{0,1,2,4,5,6,0,1};
int count = 1;
int temp_count = 1;
for (int i = 1; i < input_arr.length; i++) {
    if (input_arr[i] != input_arr[i - 1] + 1) {
        if (temp_count > count) {
            count = temp_count;
        }
        temp_count = 1;
    } else temp_count++;
}
System.out.println(count); // Prints 3

If I have misunderstood requirements or this does not work as intended (I haven't tested it, so it is be possible), please let me know.

If above works just as fine, please can you explain this approach's necessity?

This is just a suggestion, not a requirement.

Please do needful and let me know.

@AzadN AzadN requested a review from siriak as a code owner September 17, 2021 09:47
@github-actions
Copy link

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions github-actions bot added the stale label Sep 26, 2021
@github-actions
Copy link

Please reopen this pull request once you commit the changes requested or make improvements on the code. If this is not the case and you need some help, feel free to seek help from our Gitter or ping one of the reviewers. Thank you for your contributions!

@github-actions github-actions bot closed this Sep 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants