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

Solve UVa problems #29

Merged
merged 46 commits into from
Apr 13, 2017
Merged

Solve UVa problems #29

merged 46 commits into from
Apr 13, 2017

Conversation

fatosmorina
Copy link
Contributor

Solutions of some UVa problems

Copy link
Collaborator

@EugenHotaj EugenHotaj left a comment

Choose a reason for hiding this comment

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

Hey fatosmorina, thanks for the PR. Have some overall comments that affect all files.

Once these are answered I'll review more in depth.

}
}

static BufferedReader in;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Here and in other files: global variables should go at the top of the class.

}

public static void main(String[] args) {
try {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why catch, print, and exit? If there is an error this will happen anyways, no need for the try/catch.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As I mentioned above, I simply used a template and did not make any changes in it.

public static void main(String[] args) {
try {
solve();
} catch (Throwable e) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why catch a Throwable instead of an exception?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I simply used a template and did not make any changes in it.

input.close();
}

static int nextInt() throws IOException {
Copy link
Collaborator

Choose a reason for hiding this comment

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

What are this and the next method used for?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have used a template that a friend sent me. It is a general template that is supposed to be useful and avoid the necessity to repeat the same procedures of getting input and output the result for some common coding challenges that are at Online Judges. This is a similar template with the one that I used
https://github.com/ifhuang/Problems/blob/master/src/codeforces/util/Template7.java

However, in this problem, I had to get a BigInteger as an input, so I used Scanner to get the input from the console, but did not remove the methods of the template that are automatically inserted by the IDE when a new class is created.

I used .close() method of Scanner, because of resource leaks. However, I researched more on this and decided to remove it, as JVM will close it.

@fatosmorina
Copy link
Contributor Author

@EugenHotaj I committed my changes. Please review them.

Copy link
Collaborator

@EugenHotaj EugenHotaj left a comment

Choose a reason for hiding this comment

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

Thanks for the new changes. Took another pass just looking at surface level stuff.

Once you fix these, I'll take an actual look at the logic.

}

input.close();
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think you have an extra right bracket here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed the extra bracket as well.

10
789
*/
import static java.lang.Integer.parseInt;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please fix your imports. Only import files you're actually using in the class. This goes for all other files in the PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed unused classes that were left imported from earlier changes.

System.out.println((p.mod(m)).toString(baseNumber));
}

input.close();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: unnecessary close statement.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed this unnecessary close statement.

}
}

public static int findMaximalElement(int[] numbers) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please remove this method. It's not being used anywhere AFAICT

Sample Output
3.74$*/

package UVa;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: remove package or code will break.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed the package declaration.

200 = 2 x 2 x 2 x 5 x 5*/

//https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=524
package UVa;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed the package declaration.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Doesn't seem like you removed the package declaration here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am sorry @EugenHotaj. There were a lot of package declarations, and I thought I removed all of them. I committed the change with the removed package declaration.

boolean isNegative = false;
if (number < 0) {
isNegative = true;
number = (int) Math.abs(number);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why are you casting to an int here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed the unnecessary casting that I did.


static void formatOutput(int number, List<Integer> primeFactors, boolean isNegative) {
if (isNegative) {
number = number * (-1);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Change this to be number *= -1;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed it in this shorter form. It looks better now :).

@fatosmorina
Copy link
Contributor Author

@EugenHotaj I committed these changes. Please review them.

Copy link
Collaborator

@EugenHotaj EugenHotaj left a comment

Choose a reason for hiding this comment

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

Thanks for the PRs Fatos. I'll leave it to @kdn251 to review correctness of the algos before he meges. Looks good from a readability standpoint after you fix this last comment.

@@ -0,0 +1,41 @@

/*
Given a base b and two non-negative base b integers
Copy link
Collaborator

@EugenHotaj EugenHotaj Apr 12, 2017

Choose a reason for hiding this comment

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

Could you please fix these comments to follow either javadoc style or multiline comment stye:

/**
 * commet here
 * comment here
 */

Note that the only difference between a javadoc and a multiline comment is that a javadoc starts with /** while a comment starts with /*

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I fixed the comments and used javadoc. Actually, I intentionally used that type of commenting in the beginning, so that people who are struggling to solve these problems can find these solutions a lot easier by searching with the problem descriptions. Adding asterisks or slashes in the beginning of lines can make it difficult for other people to find these solutions by simply using the problem description as a Google search query.

Copy link
Collaborator

@EugenHotaj EugenHotaj Apr 12, 2017

Choose a reason for hiding this comment

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

You actually make a very good point. I'll leave it up to @kdn251 to have the final say on if he wants to have a consistent style or "searchability".

@fatosmorina
Copy link
Contributor Author

fatosmorina commented Apr 12, 2017

@EugenHotaj I am glad and grateful to be able to contribute to this very helpful repository. As for the correctness, all these solutions of these problems have been accepted as correct solutions by the Online Judge at https://uva.onlinejudge.org/. @kdn251 may verify them :).

@@ -0,0 +1,44 @@

/**
*Given a base b and two non-negative base b integers
Copy link
Collaborator

@EugenHotaj EugenHotaj Apr 12, 2017

Choose a reason for hiding this comment

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

Nit: by the way, for the comment to really follow javadoc style it should be in this form:

/**
 * <- all asterisks line up with the first asterisk
 * <- one space between an asterisk and the first character of a line
 */

Up to you if you want to make this change or not.

@kdn251
Copy link
Owner

kdn251 commented Apr 12, 2017

Hi @fatosmorina, thank you so much for the PR and thanks @EugenHotaj for reviewing the commits. I have a few notes before I can merge your code...

-Missing import statements for JollyJumpers (please add the S to the title of the class)
-TLE for The Last Non-Zero Digit (I imagine this has to do with you using Scanner as oppose to BufferedReader)
-I receive a runtime error for Prime Factors, also please rename generatePrimeNumbers function to sieveOfEratosthenes so people understand how you're generating the primes

Looking forward to your changes!

@fatosmorina
Copy link
Contributor Author

Hi @kdn251
Thank you too for taking the time to review my changes. I have committed the changes that you asked. I submitted LastNonZeroDigit and got it accepted in UVa. Can you please try again?

@kdn251
Copy link
Owner

kdn251 commented Apr 12, 2017

Anytime! I just resubmitted and it worked. My mistake I believe I was submitting it for the wrong problem! Now as just one last minor correction could you just change the name of the LastNonZeroDigit file to TheLastNonZeroDigit please. I would just like to stay consistent with the naming conventions. Thank you!

@fatosmorina
Copy link
Contributor Author

@kdn251 I renamed the file. You can have look at it yourself. Thanks.

@kdn251
Copy link
Owner

kdn251 commented Apr 13, 2017

Looks great, I'm merging the changes now. Thank you again for your contribution @fatosmorina!

@kdn251 kdn251 merged commit 8121b77 into kdn251:master Apr 13, 2017
@fatosmorina
Copy link
Contributor Author

Thank you too @kdn251 . It is my pleasure. I am planning to continue with further contributions.

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.

3 participants