• Post Reply Bookmark Topic Watch Topic
  • New Topic

A better way to do this?  RSS feed

 
Werner Holt
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hej all! I am doing this exercise :
Write a method called removeEvenInRange that accepts three parameters: a LinkedList, a starting index, and an
ending index. The method’s behavior is to remove all occurrences of the even numbers that appear in the list
between the starting index (inclusive) and the ending index (exclusive). Other values and occurrences of the even
numbers that appear outside the given index range are not affected.

I was wondering how could this be done in another (better) way? Can I use iterator here and any ideas how can I avoid the three for loops?
I will appreciate any kind of answers!
Thanks in advance !
 
Brian Schuetz
Greenhorn
Posts: 25
Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your first loop you are including when k=a, and then in the second loop you start with i=a. This would result in value at index a being added twice to the newList if that value is even. Otherwise the way you have it isn't necessarily a bad approach. You could do it with one loop, but you would need to add if conditions to check if the index is less than a, greater than or equal to a and less than b, or greater than or equal to b. Of course that last part can be covered by an else, so no actual check would be needed.
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An Iterator would iterate the whole List rather than a restricted list, but you can get a sublist of every List with this method.
There is probably an elegant way to do it with a Stream.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:There is probably an elegant way to do it with a Stream.

I tried using streams for the filtering logic but was getting a ConcurrentModificationException. I may not be doing it right; I haven't worked with Streams that much.

Using List.subList() and List.addAll(), I eliminated the two bookend for-loops. An enhanced for-loop and List.subList() make for a bit cleaner code for the filtering logic. My solution is 7 lines of code, but no blank lines. It would be 9 lines of code if you were strict about having braces and formatting. I refactored it down to 5 lines with a call to a 6-line helper method and that was even cleaner to read. It's a nice little exercise.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Talking about Java 8: one line if you look at 'removeIf' (in combination with 'sublist').
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:one line if you look at 'removeIf' (in combination with 'sublist').

Tried it. Got it. Still looking in awe at it.
 
Werner Holt
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So here it is in one row
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And I can suggest a simple enhancement which will make the code no longer (no, two keystrokes longer plus two spaces) and will give a slight performance improvement.

I challenge people to post it before I do. Piet Souris: I am sure you already know what it is.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:And I can suggest a simple enhancement which will make the code no longer (no, two keystrokes longer plus two spaces) and will give a slight performance improvement.

I got what Werner got but have no idea how much more performant you can get than that. This is going to drive me nuts! I can only wait another couple of hours for the solution, after which I'll be compelled to bombard you with Purple Mooseages...
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well,

like Junilu, I had exactly what Werner had, so well done, Werner!

But I guess that your slightly optimized solution has something to do witn '(', ')' and '&'.
Am I correct? That is indeed two keystrokes more, but I'm not sure about these two
spaces... I get the same number of spaces.

So, I say: wait another 3 hours to give the boffins around a chance to reply,
and foremost I like to keep Junilu a bit more waiting.

Greetz,
Piet
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:foremost I like to keep Junilu a bit more waiting.

I must give you fair warning: I have a black belt in Aikido and Aikijujutsu. You don't want to make me angry. You wouldn't like me when I'm angry.

 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops... that clearly trumps my blue belt in ju jitsu and kyokushin karate.

But (and that's a BIG but!:

in 2006, as a novice (at the age of 50 ;)) I became third in the European
Country and Western Couples Dancing Championship, where I won the TwoStep (but
blew the Rise & Fall). Over to you...

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet: Sorry, I don't dance like that but it's great that you do. Besides, competition is discouraged in Aikido since it is contrary to the philosophy of the art and the goal of eliminating ego.

Campbell: where's that solution?!
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't want to spoil Campbell's party, but I bet he has in mind testing for
bit 0 being equal to 0. So you would then get:

or with two extra spaces:

(the parentheses being necessary since '==' has a higher precedence than '&')

There was a long thread about bit juggling a while ago on this forum,
but I can't remember when or under what name.

Anyway, over to Campbell. He might have something completely
different in mind.

Greetz,
Piet

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I seriously doubt whether whatever could be gained performance-wise is worth the considerable loss in clarity.
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PS was spot on there.
Agree about clarity, but it is a construct I have become familiar with.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would be ok with the little trick if it was given a good name to stand in its place, like so:
If you really hate typing that much, I could accept the name even for the Predicate.

Edit: Just to tell you how much trouble that little trick can potentially cause, I instinctively went for the Edit button when I read back the code that I just posted, thinking I had made a mistake and wrote "& 1" instead of "% 2". Grrr... and that, despite already knowing how it works and giving an intention-revealing name to it. Maybe it was just a "senior moment" but please be kind to other developers: KISS.
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice improvement
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I admit, the (n & 1) == 0 expression starts to grow on you after a while. I see how you might become fond of it even, as with a precocious child
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to make do with grandchildren. They all seem precocious
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Nice improvement

I don't agree. It is defeating the very purpose of using a lambda.

Besides: I do not think the expression:

is any more clear than the direct lambda.

If you would like to keep the code as clear as possible, then why not
use the classical solution:

But I admit, we're getting into the 'matter of taste' now.
Interesting though...

Greetz,
Piet
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:

But I admit, we're getting into the 'matter of taste' now.

That is definitely NOT pleasing to my palate

If I read it out loud, the code sounds confused.

"Remove elements from the sublist if THIS is even" -- WTH is "This"? There's some kind of disconnect there.

By contrast, the predicate keeps everything encapsulated very nicely. You just need to give it a good name to reveal its intent. Reading it out loud:

"Remove elements from the sublist if the element is an even number."

I don't really see what purpose of using a lambda is being defeated here? Elegance? Brevity? Adding the intention revealing named variable increases the Elegance of the next statement. I can live with the accompanying loss of Brevity from giving it a good name. I think that's a small price to pay for clarity. It's worth it.

But maybe that's just a regional preference. After all, I'm Asian and we Asians are known for our weird tastes. Anyone fancy joining me for some Balut? It's really good when you wash it down with a pint or two
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:
// or, to be absolutely, no doubt, 100% clear:
// if (n % 2 == 0) return true;
// else return false;


Strike two for you, Piet.
This is NOT absolutely, no doubt, 100% clear. This is something that many coding conventions and style guides actually tell you NOT to do. It's a rookie version of the simpler

Another matter of taste, maybe? I don't know, that's kind of pushing it.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ehhh... just googled for ''Balut'... good heavens... I'd say: let's go for the pints!

The purpose of lambda's is making the required code less bulky. With
your code we destroy our beautiful one liner.

And now for some digression:
I've read your blog on 'code smelling', and your topic on this subject.
Well, I do not believe in reading code out loud. First of all, a computer
language has a very strange syntax compared to a normal spoken
language, so if you are reading out loud your code, you tend to speak
very strange English. I bet I can guess what other people might be thinking!

And even, if you have

a.subList(n, m).removeIf(...

then what would you be speaking?

a
dot
sublist
left parenthesis
n
comma
...
et cetera

I bet you do not read it out loud that way! So, what you will be reading out loud
will be something subjective, which makes the method unreliable. And code
read out loud to someone unfamliar with the language would not make much sense
anyway.

But nevertheless: your article and your way of viewing things were very interesting
to read, so keep them coming!

Concluding my contribution to this topic:
at least we have given Werner (and I hope some other people) something to think about!

Greetz,
Piet

 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
Piet Souris wrote:
// or, to be absolutely, no doubt, 100% clear:
// if (n % 2 == 0) return true;
// else return false;


Strike two for you, Piet.
(...)
Another matter of taste, maybe? I don't know, that's kind of pushing it.

Hmmm, was my last reply less last than I intended...

You forgot to quote my ' // '
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:Ehhh... just googled for ''Balut'... good heavens... I'd say: let's go for the pints!

That's what one of my Eurasian-Singaporean friends thought about it at first before he actually got a taste of it. After that, he said it was actually good. It's just a mental thing when you look at it. We usually eat it in the dark, on the side of the road, with drinking buddies. Late at night after we've already had a few. I lied. I would actually eat a few of these for dinner.


The purpose of lambda's is making the required code less bulky. With
your code we destroy our beautiful one liner.

We'll just have to disagree there. I think it made the main line more beautiful. The extra line before it was just a way to dress up the lambda so it would look prettier on the main stage.

I've read your blog on 'code smelling', and your topic on this subject.
Well, I do not believe in reading code out loud. First of all, a computer
language has a very strange syntax compared to a normal spoken
language, so if you are reading out loud your code, you tend to speak
very strange English. I bet I can guess what other people might be thinking!

Thanks for taking time to read the article. But again, we have to disagree on your points. You're taking things too literally there with the way you're reading the code out loud. Notice that when I wrote how I would read the code we had out loud, I didn't enunciate any of the dots or parentheses or semicolons. I read out the words and formed a sentence that tried to convey what the code was communicating to me, or rather, how I understood what the code communicated to me about what it was trying to do. That's very different from your literal reading approach, which frankly, is just kind of silly and seems like it was done like that just for the sake of argument.

Anyway, thanks again for reading the article and offering your perspective on it. I certainly don't expect everyone to agree with my viewpoint but I think it will resonate with a few people.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!