This week's book giveaway is in the NodeJS forum.
We're giving away four copies of Serverless Applications with Node.js and have Slobodan Stojanovic & Aleksandar Simovic on-line!
See this thread for details.
Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!

Mike Simmons

Ranch Hand
+ Follow
since Mar 05, 2008
Cows and Likes
Cows
Total received
18
In last 30 days
0
Total given
0
Likes
Total received
411
Received in last 30 days
0
Total given
18
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Mike Simmons

I would suggest, try to make regexes that match just part of the pattern.  For example, a pair of matching braces (anywhere)... or a date-time expression.  Use the find() method rather than matches(), so it doesn't try to match everything in the string at once.  See if you can find anything, and print out what you found.  Don't try to handle all of the complexity and options at once.  Show us some specific code for what you've tried.  Good luck...
23 hours ago
I see you're replacing the value of readTextFile[6], and then printing habitatFile[6].  What's the difference between these?  How are these variables related?
1 week ago
Try this program instead, to see what it does:
Favorites in our household:

Stranger Things
La Casa de Papel (boringly renamed "Money Heist" )
The Last Kingdom
La Reina del Sur (Queen of the South, the original)
Pablo Escobar, El Patron del Mal
El Ministerio del Tiempo (The Ministry of Time)
Daredevil
1983

Tim Holloway wrote:Anyway, while there's a lot of stuff I like on Netflix, one recent favorite was produced by Spanish Television: El Ministerio del Tiempo (Ministry of Time)


Well, this sort of became a Netflix series, after the fact.  For season 3 at least.  They've been doing a lot of partnerships with stations and production companies in various countries.  

The Last Kingdom also fits this description, originally coming solely from BBC, but now co-produced with Netflix.  So now we get more elaborate muddy English villages ("cities", such as they were...) to have the battles in. :) Great fun.  Likewise La Casa de Papel, originally produced by a Spanish studio, later acquired by Netflix for further production.  Eagerly awaiting the next installment of that one...
2 weeks ago
Meh - the overhead of creating an extra object here is pretty trivial I think.  Especially as there's no escaped reference to it, it's easy for a modern JVM to free this immediately after completion of the code.  I'm much more concerned with whether it improves readability or not.... and I would say, it doesn't, really.  Too bad - because this is the sort of thing that happens pretty frequently, and it would be nice to have a short non-repetitive way to handle it.

For comparison, the Kotlin equivalent is much nicer:

And you end up with "name" being guaranteed by the compiler to be non-null.
2 months ago
I agree with the general response that null probably should be aggressively prevented here.  But I know there are plenty of times when something may be null and we need to work around that anyway.  Here's the most concise Optional code I can see for that:

It's a bit more verbose than the original with null check:

The only advantage I see of Optional here is that the Optional code only uses "employee" once, while the null check code uses it twice.  So in cases where "employee" is actually a much longer expression, the Optional version can concisely express what you want with no repetition, which is kind of nice.  So:

compared to

or

or

2 months ago

Piet Souris wrote:Java allows you to write 50_000_000_000, to prevent all possible misery  ;



Well,  that still wouldn't prevent me from mixing up "million" and "billion" in my head, which I believe was the issue here.  But it's a good point nonetheless
4 months ago
@Piet - regarding Day 12, I had a similar problem, kept getting "too small" for my answer.  Finally discovered that for "fifty billion" I had typed 50000000 rather than 50000000000.  Not to imply that anyone *else* would ever be so foolish... but don't forget to recheck the little things that seem obvious.

The thing about day 12 is, I was actually pretty proud of the nice efficient implementation that I had, which will calculate each new generation with a minimum of computation.  Except... it turned out to be no help at all in the actual problem. :| . Classic example of premature optimization.

As for the rest, I'm way behind.  I expect to be coming back to these for some time though - they're pretty fun.
4 months ago
@Tim, I had the same experience for day 11 part 2.  I let version 1 keep running while I worked on some speed enhancements for part 2... but then it eventually completed before I was done with the revisions.  I lost motivation to continue optimizing after that since I'm behind on other problems.

But I don't think it's luck, exactly, that the answer is found early on.  More like regression to mean - as the rectangles get bigger and bigger, they become more "average" in a sense, taking in more of a mix of positive and negative values.  Still, it's at least theoretically possible a max could occur later, so we need to complete the search I guess.
4 months ago
Sorry, I misspoke - I meant that doing it kN times, proportionate to the length, becomes O(N^2) overall.  Each individual remove in an ArrayList is indeed O(N), unless it's at the end.

Of course this can also be slightly improved by using an ArrayDeque, O(1) at both ends, but that still doesn't solve the problem in the middle, which is what was needed for day 5.

And obviously, to use LinkedList effectively here you have to use its ListIterator, not any indexed method.  That's precisely why it worked so much better than ArrayList here.
4 months ago

Stephan van Hulst wrote:I really liked that one, because it's the first real example I've seen of a case where a LinkedList greatly outperforms an ArrayList.



Really?  That's a little surprising - I would guess that may just mean that the list lengths aren't usually big enough to notice how big the difference can be.  But as an example, my first solution to day 5 part 2 used a LinkedList, and part 2 runs in 0.25 seconds.  Replacing the LinkedList with an ArrayList causes the code to run in 3.2 seconds.  If you make the input string longer, it gets much worse, as it's fundamentally an O(N^2 ) operation to delete from the ArrayList anywhere but the end.

To be clear, I wasn't using the Lists as stacks, but rather, removing from the list as I went.  I eventually refactored to use a stack instead, which didn't seem to change performance substantially from the original 0.25 seconds - though other changes have gotten it down to about 0.18 seconds.  Hard to tell how much the stack itself contributed really.

Unfortunately I'm way behind since then on the other challenges.  Don't you people have anything else you need to be doing? ;)  I look forward to catching up over time.  Thanks for posting about it and bringing it to my attention.
4 months ago

Campbell Ritchie wrote:If you iterate the entry set, you may get a different order of iteration.



I should hope not.  From the javadoc for java.util.SortedMap:

The map is ordered according to the natural ordering of its keys, or by a Comparator typically provided at sorted map creation time. This order is reflected when iterating over the sorted map's collection views (returned by the entrySet, keySet and values methods).

4 months ago
Well, in the first place, I wouldn't be so concerned about creating a new Map now and then - sometimes it's warranted. Especially if the keys are changing, as was the case in your original code.  Didn't you originally want a TreeMap<Double, T> rather than a TreeMap<T, Double>?  If that's the ultimate goal, you might as well do it all at once, and the cost of a new map is essentially nothing, since you need to reinsert each entry anyway.  I originally did this with that custom remap() function I wrote on the fly, but I now realize Collectors.toMap() works as well:


However if you want a TreeMap<T, Double>, and are willing to mutate the input map (fine here since you just created it with the counting collector, and no one else has a reference) then you can use some creative casting to accomplish what you want even faster:

The above code generates warnings for unchecked casts, which can be cleaned up with the previously shown coerciveCast util method:

This works as long as you know the thing passed in really is a TreeMap, and the values can really be any Object type.  So the same map can have new types for its values.  But make sure no one tries to access the original map using the TreeMap<T, Long> reference, as that will probably throw ClassCastException if you try to access any Long values in the map - they aren't Longs any more.
5 months ago
Hi Piet - thanks for clarifications.

I'm not clear which code you're using that latest code with, but I don't believe it works.  The second argument to collectingAndThen() needs to be something like a  

Function<TreeMap<T, Long>, TreeMap<T, Double>>

whereas you have a

Function<Long, Double>
5 months ago
Or, just for fun, the condensed version :
5 months ago