Junilu Lacar wrote:I don't know about this one. I doubt I'd get to this code after I got the code that lined up with the unit tests. It takes me a little more effort to decipher what this is doing. The other way makes me think of finding a way to use tail recursion. This way makes me think of refactoring for clarity.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
That is partially recursive solution, one loop still is used.Ruslan Salimych wrote:I've solved it recursively this way
Winston Gutkowski wrote:
Don't know if it makes things any clearer, but it's the way my mind works.
Liutauras Vilda wrote:
That is partially recursive solution, one loop still is used.Ruslan Salimych wrote:I've solved it recursively this way
Thinking In Java
You got only one there, so it must be "for"Ruslan Salimych wrote:Sorry, but what loop do you mean? "for" loop?
Junilu Lacar wrote:BTW, I refactored to a tail-recursive version:
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Junilu Lacar wrote:@Chandler, I haven't tested it but I guess that works. It's not how a typical recursive solution is patterned though. Your solution keeps the original string intact and just keeps passing it along on the stack. The second parameter is an index that gets incremented with each recursive call and the third parameter c looks like it's something that you could actually calculate based on the second parameter. Bottom line: it works but it could be tighter.
Recursive algorithms usually break down the original input progressively into smaller pieces until it gets to some trivial or degenerate case at which point the recursion ends and "unwinds". See the solution and the other examples for tail call recursion in the Dr Dobb's article that I cited in my last two replies.
Chandler Deng wrote:Ya I agree, can just use s.charAt(p - 1) to get the previous character. The reason that I didn't break the String is simply because of the efficiency (substring() is an O(n) operation).
Junilu Lacar wrote:
Chandler Deng wrote:Ya I agree, can just use s.charAt(p - 1) to get the previous character. The reason that I didn't break the String is simply because of the efficiency (substring() is an O(n) operation).
What makes you think it's O(n)? If you study the implementation at http://www.docjar.com/html/api/java/lang/String.java.html carefully, you'll see that substring() is not O(n).
This is why it's this quote is often cited: "Premature optimization is the root of all evil" --Donald Knuth
Optimization is not something you should be guessing at nor should you be thinking about it too much until you have written clear, clean code.
Chandler Deng wrote:The reason that I didn't break the String is simply because of the efficiency (substring() is an O(n) operation)...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
Chandler Deng wrote:The reason that I didn't break the String is simply because of the efficiency (substring() is an O(n) operation)...
Erm ... actually, it isn't. Or rather, it needn't be if String itself contains a start and end index - ie, all Strings are actually "substrings".
TBH, I have no idea how String is implemented these days; but I seem to remember it contained something like it in the past.
Just one reason for not optimizing too soon.
[Edit] Dang! 32 minutes too late.
Winston
There are three kinds of actuaries: those who can count, and those who can't.
Piet Souris wrote:So here's my attempt
There are three kinds of actuaries: those who can count, and those who can't.
Piet Souris wrote:The solutions are getting uglier and uglier
So here's my attempt:
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Paweł Baczyński wrote:Streams way
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote: Erm, doesn't distinct() remove ALL duplicate characters? We're only trying to remove consecutive duplicates (actually, all but one).
Winston Gutkowski wrote:
Piet Souris wrote:The solutions are getting uglier and uglier
So here's my attempt:
Ie, my solution with regexes.
Gave you a tick anyway.
Winston
There are three kinds of actuaries: those who can count, and those who can't.
Paweł Baczyński wrote:
Winston Gutkowski wrote: Erm, doesn't distinct() remove ALL duplicate characters? We're only trying to remove consecutive duplicates (actually, all but one).
Yes, it does. I feel stupid
There are three kinds of actuaries: those who can count, and those who can't.
Paweł Baczyński wrote:Streams way:
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Piet Souris wrote:I honestly edited my reply so that it contained ("Winstons solution") but that somehow did not make it into the reply. Sorry! But it was indeed your solution that reminded me of a regex!
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:will create an IntStream of non-repeating "characters"; but how best to reconstitute that back to a String, I'm not sure.
Paweł Baczyński wrote:Also, you'll get an error with prev variable. A variable used in lambda must be final or effectively final.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Paweł Baczyński wrote:EDIT 2:
Even better:Wow! This is evolving!
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:Strings are about as basic as it gets, so it stands to reason that if you create an IntStream from one, you might well want to convert it back, so why not just supply a toString() or asString() method?
Winston Gutkowski wrote:Yeah, that looks much more like what I was thinking about....
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Paweł Baczyński wrote:What would you have IntStream.of(65, 98).asString() return? 6598? Ab?
String#chars returns IntStream but those can be used for thing unrelated to strings at all.
Maybe CharacterStream (or CharStream) wouldn't be a bad idea?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Paweł Baczyński wrote:If you want to pass a parameter to the constructor you need to use
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Steffen Bauer wrote:I found this solution:]
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Can you shoot lasers out of your eyes? Don't look at this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|