Mori Gad

+ Follow
since Dec 16, 2011
Merit badge: grant badges
For More
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 Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Mori Gad

The method alternateSymbols() has prints for the board. So when you call it, you get a board. Then you called printBoard, to get it again.
12 years ago

Your constructor is wrong, if you intend to use rainfall outside of it.

It should be

The way you currently have it is creating a local scope variable rainfall.
12 years ago
The trouble in containsKey is that it uses the ordering of the tree, and the id is not necessarily on the ordering.

In this case, you have it ordered with "Test1" id1 as the root and "Test2" id2 as the greater than. "" id2 is tested as a contains, it goes to root, id is not the same, "" is less than "Test1" therefore go to the less than side of the tree where it terminates and returns false.
12 years ago
You seem want it to find the Objects based on object id for your test, but in the comparator you return comparisons based on the name.

Remember, treemap is a tree, so it will search through it that way.
12 years ago
Not so, as per the api

"index of the search key, if it is contained in the list; otherwise, (-(insertion point) - 1). The insertion point is defined as the point at which the key would be inserted into the list: the index of the first element greater than the key, or list.size(), if all elements in the list are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found."

For my shame, I misread it.
12 years ago
"index of the search key, if it is contained in the list"

You can easily do a check to see if you got a hit or not. The potential insertion point is if it misses.
12 years ago

binarySearch(List list, Object key)
binarySearch(List list, Object key, Comparator c)
12 years ago
I am sorry, I should have made myself clear.

To be a constructor should be changed to:

The contents do not change whether it is a constructor or not.

In context, I believe you had the correct contents in the previous version but were not declaring the constructor correctly.

Would create a new RentalCompany with an empty ArrayList of Cars.
12 years ago

Perhaps you meant this to be a constructor for the RentalCompany class? If so, remove void. I am assuming from the error you already have it as a global field for the class.

That way, when you do

you actually set carList as a new ArrayList.
12 years ago
I would suggest you go over your stack code. All you should need is a reference to the head. Kalpana made some good points, and your pop is just plain strange.

The best way to go about this is to use the Stack api and scrap your stack.

If you really need to write the stack, some points:
Write some for Node

You should never need to loop in a stack, all you need is head =, given that head is not null.

On pop and peek, if head is null, you should throw a new EmptyStackException();
12 years ago

7 pushed into stack2.
8 pushed into stack2
Exception in thread "main" java.lang.NullPointerException
at Stack.pop(
at sortStack.main(

The error happened at 110, and it happened after you popped 8, which is the last item in stack1.

You try to pop it before checking if it is empty.

As an aside, were you supposed to write the stack class as well? Otherwise, Stack is already implemented in Java. Reinventing the wheel won't help you.
12 years ago

Where do you reset i after the push backs?
12 years ago

You don't check if stack2 is empty.

45 is in stack2, 67 is the temp. Pop stack2, push it into stack1. Peek at stack2 which is now empty.

I am not 100% sure I am right, as I am a bit tired, but I think that might be it.
12 years ago
I was interested in this problem because I wanted to practice and I was bored.

Borrowed the Weights class and the knownBigs idea from Ryan McGuire, and the algorithm from Mert Sari

Most solutions come in at 16-21 sorts, but the odd one out takes 22.

Example which requires 22 sorts for mine:
[645, 327, 511, 540, 133, 440, 754, 210, 195, 694, 976, 331, 245, 348, 897]

The heavy lifter for keeping the sorts down seems to be the adding of the 3rd item when there are 2 currently big items. Tried a few ways to get the 3rd, this seems to be a decent one, but I am interested in a better. Tempted to make a knownSmaller with the reverse mappings of knownBigger, and use it to find the next item with the largest amount of comparisons showing he is big, although necessarily not as big as the biggest.

12 years ago