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

Develop into Master: Finished challenges module 1 #1

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

DavidH29
Copy link

No description provided.

Comment on lines +27 to +37
int p1Selection = Integer.parseInt(player1.stream().
sorted(Comparator.reverseOrder()).
map(Objects::toString).
limit(2).
reduce("", (s, n) -> s+n));

int p2Selection = Integer.parseInt(player2.stream().
sorted(Comparator.reverseOrder()).
map(Objects::toString).
limit(2).
reduce("", (s, n) -> s+n));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Good use of streams, I wish there was an easier way to do "reduce with index" so that we could create the number without first converting it to a String and then parsing it

Copy link
Collaborator

Choose a reason for hiding this comment

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

Well, as a matter of fact there's a way to do the same using arithmetic and reduce with acc * 10 + b

Comment on lines +64 to +68
map(c -> switch (c.getType()){
case "International" -> new CallSummary(c, c.getDuration() > 3 ? 22.68 + (c.getDuration() - 3) * 3.03 : 7.56 * c.getDuration());
case "National" -> new CallSummary(c, c.getDuration() > 3 ? 3.60 + (c.getDuration() - 3) * 0.48 : 1.20 * c.getDuration());
case "Local" -> new CallSummary(c, 0.20 * c.getDuration());
default -> new CallSummary(c, 0.00);
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: Similar comment as here

Comment on lines +28 to +34
String[] a = {h, m, s};
for(int i = 0; i < 3; ++i){
if (a[i].length() == 1){
a[i] = "0" + a[i];
}
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: String.format could be a more reliable solution than adding the padding yourself

Comment on lines +60 to +61
int firstE = index;
if(index >= 5) firstE = index % 5;
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: Even though we know that COUNTRY_NAMES has 5 elements, it's better to use .size(), your code would inadvertently break if we decided to add or remove elements from the COUNTRY_NAMES array, and this code wasn't properly tested this could potentially go undetected

cont = cont.add(aux);
}

int l = (int) (Math.log10(cont.doubleValue()) + 1); //Obtaining length of the number
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: Not that this is incorrect, but by using doubles we are entering a world of possible round errors, this may never happen, but a safer bet would be modulo to get the last n digits and then padding with zeros the string

Comment on lines +133 to +135
var res = facts[n];
int l = (int) (Math.log10(res.doubleValue()) + 1);
int sum = 0;
Copy link
Collaborator

@quebin31 quebin31 Nov 20, 2024

Choose a reason for hiding this comment

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

nit: Same comment as here

Comment on lines +138 to +141
for(int i = 0; i < l; ++i){
sum += res.mod(BigInteger.valueOf(10)).intValue();
res = res.divide(BigInteger.valueOf(10));
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Good idea here, you could do a while instead of for to end the loop when your division reaches zero, i.e. we're already at the last digit

Copy link
Author

Choose a reason for hiding this comment

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

Thank you!
I know a while would have been more intuitive instead of calculating a limit with the number of digits and using it in a for loop, but personally I prefer to use for before while, this because across the majority of programming languages, for loop are generally quicker than while loops.
I attach an interesting experiment someone did with java 17 to confirm this: https://tonisagrista.com/blog/2022/java-loop-performance/#java-17

Copy link
Collaborator

Choose a reason for hiding this comment

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

Well, you know you can also write for(;cond;)? And also, incurring into premature optimization could be a problem, we first write code that works and is readable and maintainable, then we care about optimization if needed.

Comment on lines +160 to +162
for(int i = 0; i < ascivalues.size() - 1; ++i){
message.append((char)(c + ascivalues.get(i + 1)));
c = message.charAt(message.length() - 1);
Copy link
Collaborator

@quebin31 quebin31 Nov 20, 2024

Choose a reason for hiding this comment

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

nit: You could maybe improve how readable your code is here if you started with i = 1 until asciivalues.size (not inclusive)

Copy link
Author

@DavidH29 DavidH29 Nov 22, 2024

Choose a reason for hiding this comment

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

Yeah you are right, usually I see for loops like "number of times to iterate" so in my mind is like "Ok so I have to iterate all over this the number of times of the size of ascivalues, but I have already did the first one, so I have to subtract 1 to size"
Thanks for the comment, I just notice that from myself and the way I think until now XD

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.

2 participants