• Post Reply Bookmark Topic Watch Topic
  • New Topic

Longest sequence of temperatures <= 0  RSS feed

 
Alicia Perry
Ranch Hand
Posts: 66
Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone, I am studying Algorithms in University and I'm struggling with creating a Java procedure that, given an array of integers, returns an integer representing the longest sequence of freezing temperatures (<= 0).
The professor has proposed this method:

But I created this one:

I tested both and it seems that give the same results.
Someone would be kind enough to tell me if my method is actually correct and which one is better?

Thank you

PS: I don't understand why the professor is complicated life with control i == a.length - 1.

PPS: I'm sorry for my bad English.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alicia Perry wrote:
PS: I don't understand why the professor is complicated life with control i == a.length - 1.


With your professor's version, the "i == a.length - 1" is actually needed, because the code doesn't check to update the max until the end of a negative run. In your version, you check with every value, so it already checks at the end of the array (along with at every element of the array).

Alicia Perry wrote:
Someone would be kind enough to tell me if my method is actually correct and which one is better?


Well, that depends on how you define "better". To me, I guess I would focus more on readability, so I guess I would vote that yours is "better" than your professor's.

Henry
 
Alicia Perry
Ranch Hand
Posts: 66
Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, thanks for the answer.
I hope then that my version is fine even the professor.
 
Campbell Ritchie
Marshal
Posts: 56522
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried it with a for‑each loop instead of the ordinary for?
 
Charles D. Ward
Ranch Hand
Posts: 99
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If's without braces = -10pts.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I vote for Alicia's version as well. Another beginner wrote practically the same exact code recently to count the longest run of 1s in an array.

Alicia, that first two if statements in your code can be combined into one if-else statement, like this:

The end result is the same but the intent will be slightly clearer, IMO. And what does the name att stand for? A more descriptive and intention-revealing name could probably be found to make the code more readable.

It's quite interesting to see this kind of code coming from two beginners now who have no bias towards trying to optimize the code. The beginners have come up with something that seems more elegant than the more "efficient" code that experienced coders come up with.

@Charles: 10 points seems a bit excessive in this case, especially since the professor's code has the same fault, if indeed it was cited verbatim. I would advise Alicia to ask her professor how particular he/she is with the style rule to always use braces with if-else statements.

 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alicia, if your professor argues that his/her version is more efficient than yours, tell your professor that there are some experienced developers at CodeRanch.com who actually admire the simple elegance in your way of doing it. Remind your professor, with due respect, that 1974 Turing Award winner, Donald Knuth, said that "Premature optimization is the root of all evil."
 
Campbell Ritchie
Marshal
Posts: 56522
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would have thought there is no discernible difference in execution speed, but your second offering is more elegant.
 
Alicia Perry
Ranch Hand
Posts: 66
Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Have you tried it with a for‑each loop instead of the ordinary for?

Thanks for the advice, I'll try to do even this version for training.


Charles D. Ward wrote:If's without braces = -10pts.

Why? I thought it was a good thing omit parentheses when not necessary, especially here where the code is really easy.


Junilu Lacar wrote:
Alicia, that first two if statements in your code can be combined into one if-else statement, like this:

The end result is the same but the intent will be slightly clearer, IMO. And what does the name att stand for? A more descriptive and intention-revealing name could probably be found to make the code more readable.

You're right, I have to get used to give more meaningful names to variables.
However att mean actual (or current).


Junilu Lacar wrote:Alicia, if your professor argues that his/her version is more efficient than yours, tell your professor that there are some experienced developers at CodeRanch.com who actually admire the simple elegance in your way of doing it. Remind your professor, with due respect, that 1974 Turing Award winner, Donald Knuth, said that "Premature optimization is the root of all evil."

It's a very good advice!


Campbell Ritchie wrote:I would have thought there is no discernible difference in execution speed, but your second offering is more elegant.

Thanks!


Thank you all!
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alicia Perry wrote:I thought it was a good thing omit parentheses when not necessary, especially here where the code is really easy.

Omitting braces is often frowned upon because it can lead to the introduction of bugs later on. If you add another statement to the if and forget to add braces, the logic executed by the computer will not be what you intend it to be. To avoid this, many coding standards require braces to be used even when they are not necessary.

The indentation of this code suggests that the programmer intended for line 3 to be executed conditionally along with line 2. However, since braces were omitted, line 3 will in fact be executed unconditionally; only line 2 is part of the if statement. If braces had been there from the start, the bug would likely not have been introduced.
 
Campbell Ritchie
Marshal
Posts: 56522
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alicia Perry wrote: . . . Why? I thought it was a good thing omit parentheses when not necessary, especially here where the code is really easy.
Missing out any sort of bracketing () [] {} can cause your code to run incorrectly.
It is very difficult to do any harm with redundant brackets.
. . .
Thank you all!
You're welcome
 
Campbell Ritchie
Marshal
Posts: 56522
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually you can do harm with redundant square brackets []
 
Alicia Perry
Ranch Hand
Posts: 66
Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
Omitting braces is often frowned upon because it can lead to the introduction of bugs later on. If you add another statement to the if and forget to add braces, the logic executed by the computer will not be what you intend it to be. To avoid this, many coding standards require braces to be used even when they are not necessary.

The indentation of this code suggests that the programmer intended for line 3 to be executed conditionally along with line 2. However, since braces were omitted, line 3 will in fact be executed unconditionally; only line 2 is part of the if statement. If braces had been there from the start, the bug would likely not have been introduced.


Campbell Ritchie wrote:Missing out any sort of bracketing () [] {} can cause your code to run incorrectly.
It is very difficult to do any harm with redundant brackets.You're welcome


Thanks, I'll keep that in mind!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!