since Mar 08, 2009

Netherlands

Cows and Likes

Cows

Total received

100

In last 30 days

4

Total given

71

Likes

Total received

533

Received in last 30 days

15

Total given

54

Given in last 30 days

2

Forums and Threads

Scavenger Hunt

Ranch Hand Scavenger Hunt

Greenhorn Scavenger Hunt

First, follow Carey's advice. In stead of using i and j, use the names studentNumber and assignment. If you do that, then your routine will look like:

Does line 3 look right?

If there are 10 assignments, and a student has 6 of them correct (so grade[studentNumber] = 6), how would you calculate the percentage? And where in your code would you do that calculation?

Does line 3 look right?

If there are 10 assignments, and a student has 6 of them correct (so grade[studentNumber] = 6), how would you calculate the percentage? And where in your code would you do that calculation?

40 minutes ago

Campbell Ritchie wrote:O yes, we have no bananas, we have no bananas today!

That song was recorded in 1923!

1 hour ago

An alternative way, and a bit more flexible in that you can also obtain the index of the nth smallest element, is to take a copy of your array, sort it, take element n, and do a search for it in the original array. The Arrays class can help you here.

1 hour ago

Mike Simmons wrote:(...) Didn't you originally want a TreeMap<Double, T> rather than a TreeMap<T, Double>?

The reason I want to have a Map<T, Double> is that my input will probably be in that form, or even a Map<T, Map<Year, Double>> (see my remark about modern mortality tables).

I used the toMap myself in the Function of collectingAndThen, but this method, in this way, is less usefull than I thought.

Well, anyway, I have gained experience and some wise lessons, so: thanks Stephan and Mike!

11 hours ago

hi Robert,

is the method 'choosingR(int n, int r)' supposed to be equal to C(n, r)?

If so, then your recursion is incorrect, you should have that (so, the base case is: if (r == 0) return 1; else ...)

Another recursion that you could use is (Pascals Triangle), but that is not very efficient.

If you work with faculties (like n!), then be careful: n! overflows an int pretty fast. Better to use longs, or BigIntegers.

is the method 'choosingR(int n, int r)' supposed to be equal to C(n, r)?

If so, then your recursion is incorrect, you should have that (so, the base case is: if (r == 0) return 1; else ...)

Another recursion that you could use is (Pascals Triangle), but that is not very efficient.

If you work with faculties (like n!), then be careful: n! overflows an int pretty fast. Better to use longs, or BigIntegers.

11 hours ago

Travis Risner wrote:Jon, first a question for Piet.

Piet, did you mean to use a forward slash rather than a backslash in your reply?

Ahhh, messed up here. I meant integer division (so 1 / 4 = 0), but I screwed up and I forgot that this is about Python and not Java. Thanks for correcting me.

@Jon

the idea is to determine the required number of rooms first, then you know how many beds there are, and given the number of students: voilĂ . But I seem to be facing a lesser period nowadays (messing up a lot lately), I leave you to the safe hands of Travis.

1 day ago

Salvin,

you're the master... absolutely stunning!

you're the master... absolutely stunning!

1 day ago

I apologize, you are absolutely right, I misunderstood the API.

But using a Function<Map<T, Long>, Map<T, Double>> would that not require the use of an additional Map as I was criticized for in the first place?

What efficient method do you have for the Function?

But using a Function<Map<T, Long>, Map<T, Double>> would that not require the use of an additional Map as I was criticized for in the first place?

What efficient method do you have for the Function?

1 day ago

hi Mike,

the API says that the Collector will give you a Collector<T, A, R>, and the finisher should be a Function<R, RR>, so as I read it I should indeed use a Function<Long, Double>.

Here's the API

`public static <T,A,R,RR> Collector<T,A,RR> collectingAndThenâ€‹(Collector<T,A,R> downstream,
`

Function<R,RR> finisher)

I tried it in a different context, but still I got all these errors. Time to update my NetBeans, maybe?

the API says that the Collector will give you a Collector<T, A, R>, and the finisher should be a Function<R, RR>, so as I read it I should indeed use a Function<Long, Double>.

Here's the API

Function<R,RR> finisher)

I tried it in a different context, but still I got all these errors. Time to update my NetBeans, maybe?

1 day ago

hi Jon,

what is the question? How much rent a group of N students must pay, given the surcharge, or the number of rooms required?

To determine the number of rooms needed, why not use the '\' operator. For instance, if 11 = 2 * 5 + 1, then 11 % 5 = 1, and 11 \ 5 = 2.

If we have 1, 2, 3 or 4 students, we need 1 room. Unfortunately, students \ 4 will yield the reaults 0, 0, 0, 1. However, studens 0, 1, 2, 3 \ 4 will give 0 in all cases. That is not 1, but it is equal for all the nr of studens.

So, what do we get when we first subtract 1 from the number of students, and THEN do the '\4' operation?

We get:

nr of st. nr of rooms (students - 1) \ 4

1, 2, 3, 4 0

5, 6, 7, 8 1

9, 10, 11, 12 2

Not quite correct, but close. Can you make it perfect?

what is the question? How much rent a group of N students must pay, given the surcharge, or the number of rooms required?

To determine the number of rooms needed, why not use the '\' operator. For instance, if 11 = 2 * 5 + 1, then 11 % 5 = 1, and 11 \ 5 = 2.

If we have 1, 2, 3 or 4 students, we need 1 room. Unfortunately, students \ 4 will yield the reaults 0, 0, 0, 1. However, studens 0, 1, 2, 3 \ 4 will give 0 in all cases. That is not 1, but it is equal for all the nr of studens.

So, what do we get when we first subtract 1 from the number of students, and THEN do the '\4' operation?

We get:

nr of st. nr of rooms (students - 1) \ 4

1, 2, 3, 4 0

5, 6, 7, 8 1

9, 10, 11, 12 2

Not quite correct, but close. Can you make it perfect?

1 day ago

BigDecimals are out of the question.

Nowadays mortality tables come (per gender) as a 2D array (the development package we use does not have Maps as far as I know), where the first dimension is the age (up to 120 currently), and the second dimension is the year of consideration, so a 30-year old has 120 different chances of dying at that age. Traversing a person through time will make you have to go via the diagonals.

Lastly and then I'll shut up: I tried to implement the collectingAndThen, expected to be easy, this is wat I came up with

but I can't check whether this is correct. I seem to have stumbled upon a NetBeans bug (9RC), and with every key press I get IndexOutOfBoundExceptions, AssertionErrors, et cetera, sending bug reports to Apache, and when I try to run the code, I get a ClassNotFoundError. Hairgraying misery.

Nowadays mortality tables come (per gender) as a 2D array (the development package we use does not have Maps as far as I know), where the first dimension is the age (up to 120 currently), and the second dimension is the year of consideration, so a 30-year old has 120 different chances of dying at that age. Traversing a person through time will make you have to go via the diagonals.

Lastly and then I'll shut up: I tried to implement the collectingAndThen, expected to be easy, this is wat I came up with

but I can't check whether this is correct. I seem to have stumbled upon a NetBeans bug (9RC), and with every key press I get IndexOutOfBoundExceptions, AssertionErrors, et cetera, sending bug reports to Apache, and when I try to run the code, I get a ClassNotFoundError. Hairgraying misery.

1 day ago

salvin francis wrote:(...) I love that piet too is thinking in my way that a cell simply is there or is not there, there is no state to it.

Yes, the world does not have a limit other than memory allows, and it would make a Cell class redundant (apart from the Business Rules, that is)

But the current set up is also fine, via a Map<Location, Cell>. Liutauras (alias the author) uses both techniques: the World having Locations that by that virtue exist, and the associate Cells have a status of Alive or Dead. That is the prerogative of the coder, choosing what suits/likes him/her best.

I'm happy with the code, only things missing is something that sets the world to moving/ticking, and a colorful gui (like you showed us in your GoL, some time ago!).

But one thing is sure: I know one who is going to the GDCR coming saturday, prepared to the teeth!

2 days ago

Mike Simmons wrote:Hi Piet, Stephen. I'm enjoying this - I hope you are too.

Sure I do, I had not expected the response at all!

Gosh Mike, I thought I knew a thing or two when it comes to Streams and Lambdas, but reading your great innovations I get the feeling I should have put this topic in the Beginners Forum!

I just gave Stephan a well-deserved cow, I'll give you a well deserved cow too, tomorrow.

Thanks guys, for the lessons! And if you have more for me: send them in!

3 days ago

Stephan van Hulst wrote:

Piet Souris wrote:thanks for the critiques, even if it is not very egoless!

If, going by the description of peer reviews that I found on the Wiki for egoless programming, you mean that my review wasn't friendly or collegial, I apologize. I try to write in a neutral way but that doesn't always come across through text.

Now I must apologize. I meant to say that accepting comments in an "egoless" way is even harder than I imagined. So far I was the only one looking at my code (or "the" code), and I always felt pleased about it. Your comments put me right back with two feet on the ground. Lot to think about, foremost that code that I think is obvious, need not be obvious at all.

3 days ago

hi Stephan,

thanks for the critiques, even if it is not very egoless!

You're right, the code is kinda sloppy, it shows all the signs of hasty speed. But I'll give some response.

Well, it is such a light local 'offence' that the convenience far outweighs it.

The map.forEach() allows the very convenient (k,v) -> construct, and for that I'm willing to skip functional code. As you see, it is just a very clear short one-liner.

The intent is to do sampling, and if I have a stochastic variable X with cdf F(x), then under circumstances F(X) is uniform distributed, If you think of the graph of F(x), then the procedure is to draw a number (uniform) from the y-axis, go horizontally to the right until it crosses the graph, and go vertically down to the x-axis, and that is your random sample. The TreeMap methods 'floor' and 'ceiling' are ideal for this.

Good point. Never used that downstream collector so far, but I will read the API again and incorporate it.

Hmm, cdf is a very much used abbreviation, so for me it was very clear.

Correct, but for now I limit myself to numeric random variables, with their in-built natural ordering.

The whole purpose arose when someone at another site was assigned the question about a mortality table, the X being the age of death of a person who is now x years old. That made me realize I'd never done this in Java so far.

Funny, in Liutauras' topic about the GoL, Junilu talks about egoless programming. Now I find myself defending my code... It IS a long way to Tipparary

thanks for the critiques, even if it is not very egoless!

You're right, the code is kinda sloppy, it shows all the signs of hasty speed. But I'll give some response.

I find it really strange that you like using Java's functional features so much Piet, but then shirk the 'proper way' of doing things in preference of using hacks like modifying objects to get around the limitation that you can't modify local variables from lambdas, which is there for a good reason.

Well, it is such a light local 'offence' that the convenience far outweighs it.

The forEach() in your cdf2() method is not functional. It's procedural code. Then why not just use a for-each loop? That way you won't even have to resort to silly things like using the array.

The map.forEach() allows the very convenient (k,v) -> construct, and for that I'm willing to skip functional code. As you see, it is just a very clear short one-liner.

Why is your CDF keyed by probability? A CDF returns a probability given an observation, so it makes sense to keep T as the key.

The intent is to do sampling, and if I have a stochastic variable X with cdf F(x), then under circumstances F(X) is uniform distributed, If you think of the graph of F(x), then the procedure is to draw a number (uniform) from the y-axis, go horizontally to the right until it crosses the graph, and go vertically down to the x-axis, and that is your random sample. The TreeMap methods 'floor' and 'ceiling' are ideal for this.

You use two collect() operations in your cdf() method. That's wasteful. Use a downstream collector to specify what should be done with the values of the map while the groupingBy() is doing its magic.

Good point. Never used that downstream collector so far, but I will read the API again and incorporate it.

I'd call cdf() getCumulativeDistribution()

Hmm, cdf is a very much used abbreviation, so for me it was very clear.

Why is T a Number? The only thing that your methods require about T is that it has a natural order.

Correct, but for now I limit myself to numeric random variables, with their in-built natural ordering.

The whole purpose arose when someone at another site was assigned the question about a mortality table, the X being the age of death of a person who is now x years old. That made me realize I'd never done this in Java so far.

Funny, in Liutauras' topic about the GoL, Junilu talks about egoless programming. Now I find myself defending my code... It IS a long way to Tipparary

3 days ago