Piet Souris

+ Follow
since Mar 08, 2009
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Rancher Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Piet Souris

The solution that I hinted to works even when there are duplicates and more than one missing. For instance:
1 hour ago
Perhaps you could simplify the game a little, by calculating all the rolls for a player upfront. Not only makes that the code a little easier, but you have all the information before the game starts. For instance
22 hours ago
A linear time solution: create an Object[] the size of n, then for every present value i put an Object in Object[i]. In the end, some Object[x] will not have a value.
1 day ago
hi Tuan,

since the upper limit is only 100.000, I would probably do this brute force. The question is then only how to discover the number of beautiful digits, like Campbel writes.

Another way to look at this is: if the upper limit consists of N digits, then consider the numbers 000... (N zeroes) - 999... (N nines). For each of the N digits, we draw a random digit. The number we get, has a Binomial distribution, with N as the number of trials, and a chance of success of (F / 9), where F is the number of beautiful digits.
For instance, let the product numbers be between 0 and 99 (inclusive), and let the beautiful digits be 5 and 7. The chance of getting two beautiful numbers is now: 2! / (2! * 0!) * (2/10)^2 * (8 / 10) ^0 = 0.04, giving a total of 0.04 * 100 = 4.

Unfortunately, then there is the problem of dropping the outcomes that are outside of the bounds. So, this way may be interesting, but not for the faints of heart.

It kind of remembers me of an interesting topic, that also dealt with a number of permutations within a certain interval (where the max could be a whopping 10 ^100). Don't remember whether it is relevant in any way, but it sure was fun:

problem: digit products
1 day ago
But objarray is an array of raw ArrayLists, so you are giving up all compilers type checking. For instance

What are you trying to prove?
A clear way is to create a Map<Character, String>, with the key being the char to be replaced, and the value the string (could be more than one character) that is the replacement.
If you read in a line, put it in a StringBuilder (like Carey advised), and, going from end to start, check if the char is present in the map and if so, replace it by the value. Return the toString of the StringBuilder as result.
2 days ago
Thanks, Stephan.

At the time I experimented wit this, I did not have java 8, and so never applied it to interfaces and what consequences this would have. Interesting...
Then I haven't got a clue what you mean, but I don't mind keeping it that way.

The idea a couple of years ago was to try to mimic Scala's 'type A = B' (or C's typedef), but I'm always in for some better suggestions (but not in this topic, it is polluted enough already).
Perhaps a little more clear is writing the ternary aa follows:

yep, the result of the ternary needs to be assigned to something...

Yes, but it needs not be assigned to a variable. For instance
4 days ago
Well, if it was only for one instance, it would not save any typing or copying. But I sometimes have a generic class with a pretty big type collection, and needing a couple of instance or in an enhanced for-loop. I was happy with my way of doing as described above. It did save on typing and copying. I wish I could apply it to Map.Entry<list of horribly long types>, but unfortunately, that class is abstract, so I could not extend it into something concrete.

But your reference to Gullivers Travels seems to indicate a different experience. Can you elaborate about your experience?
hi Carey,

whether you will use the SumCount.collect in parallel or not, you must provide a non-null adder of two SumCounts. Now, given the SumCount, it is hard to say what the "sum"of two SumCounts should do, but as you write, it is not being used, so it really doesn't matter.

So add something like this to SumCount:

and make this change:

and your code compiles and runs, with nothing but SUCCESSES.

However, whatever your algorithm is, it differs from my interpretation. For example, running your code, I get SUCCESS "message are", 501, -1
while my algo would give +1 (it would not do any split). But I must admit: I did hardly test my code, so a handful of bugs is not unlikely.

But most of all: will Atul still be enthousiastic about Streams?   

By the way: another way is to split the input string, as you already did, and then make a Stream with a dedicated Collector class. For an example, see the Oracle tutorial reduce and collect
So, if I have it correct, the functionality is: break the String up on spaces, such that each piece is as long as possible, but no longer than maxBreak.

Your question is if your code can be expressed using java8 Streams. Well, I did an attempt, starting from my own logic, but it is not so easy.  There are probably handier ways, and I leave it up to you to judge whether this is useless or not. Still, it makes for some nice exercise in streams! So, if you like, see if you can translate your own logic into code that employs some Streams.

My approach is:
1) determine the indices of all the spaces. For technical reasons I add an extra space at the end of the String, unless it already ends with a space.
2) check whether there are no two consecutive indices that differ more than maxBreak
3) now, apply this algorithm: if say the indices are 4, 7, 9, 12, 15, 20, 24,  ... and maxBreak is 10, then the breaks will be 9, 15, 24, ...

A problem when using Streams that need some history, is that you cannot use an ordinary variable to store that history, since then you get the error that such a variable needs to be final or effectively final. The quick and dirty solution that I use is to use an array of size 1, and store some history in array[0]. That is okay, since the content may not be final, the array itself is.

Campbell Ritchie wrote:(...) and implemented it as

But is can be economical to extend an interface to get rid of all these parameters. For instance

so now you can say

hi Caiz,

great reply, and have a well deserved cow!

Caiz wrote:Also thanks Plet, I've used the .setDisabledIcon() method, it still darkens the image a bit but not so much as the default

Hmm, it is quite a while ago since I did anything like this, so maybe my memory is playing tricks. I'll have a look at it and will let you know. But maybe Rob had a better way in mind, so let's hope he will let us know.
6 days ago