Piet Souris

Saloon Keeper
+ Follow
since Mar 08, 2009
Forum Moderator
Piet Souris currently moderates these forums:
Netherlands
Cows and Likes
Cows
Total received
146
In last 30 days
18
Total given
84
Likes
Total received
624
Received in last 30 days
15
Total given
66
Given in last 30 days
3
Forums and Threads
Scavenger Hunt
expand Rancher Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Piet Souris

hi Arwa,

a very nice way would be to use a Comparator<double[]>. For instance:

Then you can have something like:

You can use: comparator.reversed() if you want to sort it from high to low.

Warning: I have not tested this, so there might be a tiny error in it!
14 hours ago
hi john,

your Comparator is not correct. Does it always hold that compare(o1, o2) == compare(o2, o1)?

And: your Comparator (when fixed) will put all the Healers at the back, but will leave the relative positions unchanged. So I would suggest that before you do the sorting, you first shuffle your list. See The Collections API.
14 hours ago
hi Dave,

I'm not sure about this. It is true what you write, and I see the effect when I run the code. I did not know this. But if I look at the Java 8 documentation I am unable to find any of this documented (I do see it in the Java 10 API). So, it is probably something that was built into Java 9+. I have deleted my java 8, so can't test, but have a cow for your interesting remark.
hi Alfa,

okay.

I do not like the idea to use an instance of an Employee as indication in what order it should be processed, unless you can show me the real usecase. I must admit that I still do not understand how the real thing operates.

Suppose the rules are given in the form (x, y), meaning "x must be processed after y". It is a straightforward task to turn that into the list that I was talking about. If this list of Rules is a valid one, then there must be an y in this list of Rules that does not appear as x. For instance: if we have these Rules:

(3, 5), (5, 12), (2, 3)

then the x-list is {3, 5, 2} and the y-list is {5, 12, 3}.

As we see, 12 is the value in the y-list that does not appear lin the x-list, so that's our starting point. Now, look up the Rule that has 12 as y value. We find (5, 12). That means that 5 is our next "victim". Look up 5 as right element in the rules. We find the Rule (3, 5). So, 3 is next. And in the same fashion, we find 2 as our last number. So the processlist = {12, 5, 3, 2}. And then you can proceed in the way I described.

If all this is still not correct, then probably Norm is understanding your mission better than I do. Can you give an example of the original list, and what the external users do to this list (i. e. the rules), and when the moment arrives for you to do the processing? A small example would be fine.

4 days ago
For the last two or three weeks CodeRanch has been very unstable. Fetching a page takes quite some time, and every other fetch gives me a Time Out error, or similar.
And when replying to a post: if I do not submit a reply within say 10 seconds, then a submit requires me to log in again. It makes working with the site pretty unpleasat, I must say. Am I the only one?
5 days ago
I see. I apologize for having missed the last part of the JavaForum topic.

Well, sorting based on the index of an employee in the original list, or sorting based on a given id-order is equivalent qua complexity, but the second one seems more reliable, since the sorting then does not depend on some initial order of the list.

But if the user is able to supply a list of rules, like (1 after 2, 4 after 8, et ceera), then why not let the user give just one list like (8, 3, 7, 1, 0,...), to indicate that 3 comes after 8, 7 after 3, et cetera. That would be much less work and is much less errorprone.

Then you could have something like:
5 days ago
Thanks for the info, Norm.

That raises a couple of questions:

1) why is the discussion continued here at CodeRanch?
2) is there some usecase that makes all this a little comprehensible? First of all, the rules like "one comes after three" depends on some order of the list. and secondly, it is a very labourious way of doing. Three, the user must have full control over the Employee list, in order to invoke the method "processAfter()".

So, this seems unrealistic to me. Normally, you would have one or more fields in the Employee class, on which employees can be compared, for instance salary, function, department, service years, age, whatever. There must be a way to keep the employees up to date. Then all that the user needs to specify is on what field(s) the employees should be sorted.

If there IS a clear usecase, then please specify.

Since Norm is very involved in this, I will leave further replying to him, if that's okay.
5 days ago
I think so. But in oder for the function to do a correct sort, it must know these rules. Are the rules in question specified as shown in your first post? If not, how are these specified?
5 days ago
hi Alfa,

a way to automize this, although it is not a very efficient way, is to note that (in this case) the sorting order is: 6, 8, 3, 2.
So we can use a Comparator to sort the array. For instance:
5 days ago
Objection, your Honors!

This talk about aliens is neither meaningless, nor drivel. On the contrary, much more interesting than this dull NP Salesman Dijkstra thingy... but ah well, I rest my case.    
6 days ago
hi mr Kommadi,

I recently found out about the existance of Binary Indexed (Fenwick) Trees, which were exactly what I needed to solve a problem. Do you treat these too?
1 week ago
Go
Whott??? Forgotten 'Mars Attacks' and 'Alien'? No thanks, I'll wait for my new 4Gqbit quantum computer, expecting it any time soon now.
1 week ago
Hi Ryan,

I gave it another read, and you are absolutely right. So I think the difficulty lies in coming up with the idea of such a minimal spanning tree. And certainly given the time limit of 1 second, I think this exercise is pretty hard if you've never heard of these things.
1 week ago
hi DJ,

long time no see! You seem to be very busy, but that is a good thing.

I had to read that assignment twice, but I think I got it. You are given N cities (vertices) and M lines of input indicating where a pilot can fly to and from (edges). So, it is an undirected Graph with N vertices and M edges. And the question is what the shortest path is between any two cities. Correct me if I'm wrong. Now, first thing what comes to mind is Dijkstra's algorithm: Dijkstra.

So, have a read and see if you agree with me!
1 week ago
@Paul
Thanks!

@DJ
It was a pleasure for me to help.

@Salil
And thank you for mentioning dynamic programming, I would never have come up with the idea.
1 week ago