Damon McNeill

Ranch Hand
+ Follow
since Apr 06, 2010
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 Damon McNeill

Normally a database API will provide a type such as RowSet (or some such), which implements an Iterator interface. That is, it has getCurrent() and next() and hasNext(). You do not need to implement caching on your side as this interface normally provides the necessary buffering.

I am trying to take a signed integer and convert it to hex.

In Java, every integer is signed.

Why are you posting C code here? Are you confused?

EDIT: Apologies I did not realize this was a C forum. Converting from binary to hex is a simple matter of division. I will not have the time to give you the solution (as this is likely a homework assignment and the given code is unreadable).
5 months ago
Isn't there a String.format method that would do that
5 months ago
Ok basically don't "slurp" your data (a term borrowed from Perl, where you transfer all assets into memory), instead process it a chunk at a time. Basic I/O. Think Java DOM vs JAXP.

Please prefer Interface plus Composition over Inheritance. If you need to share implementation details, contain a class that contains the common functionality and COMPOSE that class, not inherit it. Then delegate interface methods to the proper class. Especially in Java where you only get 1 chance to get your hierarchy right. We're sick of re-organizing giant fragile class inheritance hierarchies.

There might be a place for Inheritance in giant frameworks but please don't design your library that way internally, you'll hit cross cutting issues and fragility very quickly.
5 months ago
Surely copying duplicate installations around is not a good solution. Sounds like a maintenance time bomb.

Are you exec()'ing Python from Java? Or exec() Java from Python?

What is the method call in Java to exec() a random process on the OS? I have never heard of nor used a Java exec().

I guess it reduces to a call to the C exec() function, which has a bunch of caveats regarding environment variables, current path state, etc.

It's possible the PYTHON_PATH is not being respected (so imports nor loading would work) or that Python's idea of the current working directory is not initialized or wrong.

Just some ideas....

5 months ago
Have you heard of an ancestral graph? A binary tree is basically the inverse of that relationship. Whereas in an ancestor tree each child has exactly 2 parents, in a binary tree each parent has exactly 2 children.

Each "node," or individual part of the tree at least contains 2 references to its children (typically labeled left and right), and some associated data (this may be an int or any arbitrary data).

A tree is a specific case of a general graph, where there are no cycles (i.e., no parents or siblings are known by each child); AKA, a directed (from parent to direct child), acyclic (no child points anywhere else except to their own children) graph.
5 months ago
I have a situation where I would like to utilize a hash map,
but would not like to leak memory in the case when neither the key nor value are referenced anywhere else.
The values themselves contain a strong reference to the the key, and I believe this prevents the stock WeakHashMap from being collected (since there is at least 1 strong reference to the key, that being contained in the associated value itself).
I have read through the documentation of Java regarding weak vs strong references and regular HashMap vs WeakHashMap. I believe I need to use WeakReference
as my value type.

So, if I use a weak reference for the value type, will that prevent the map from holding on to the values, and the keys would, once GC'd, be
associated with a null'd reference in the map? In this case I would perodically run a purge() method to clear out the null'd weak references from
the map.

To be honest this is a bit confusing to me without understanding the underlying implementation of WeakReference or WeakHashMap. Can anyone provide any
tips or criticism or explanation. I am posting my code below.

[EDIT: on line 18 is supposed to be a return statement]
[EDIT: the HashMap member instance should be a new WeakHashMap]
5 months ago

Al Hobbs wrote:Figuring what to use is also part of the problem.  What's the point of knowing algorithms and data structures if you don't know when to use them.

Agreed with that, but often times its not clear or the mapping of the problem to a particular data structure or algorithm combination is not clear, or is overly-hard for the position's requirements.

Consider this case: your interview required implementing a swap in C (using pointers) and you're co-worker stated that they "never got pointers" or some such. Does this even happen?

1 year ago

Johnny Doe wrote:I needed to take a string and parse it (with a fairly simple algorithm) but decided to  create a new class that would have the methods needed to do what I needed (check for a special code in the string), the string being used to construct the object.

You've just described one of the oldest things in the book: a parser. This pattern is used in Java and any other language to construct an AST (Abstract Syntax Tree), representing the program, from the input source text characters. Depending on how complex your source language is, this can reasonably consist of an entire package of classes to represent the AST, not to mention the lexical analyzer and parser.

Honestly I would say don't worry about creating new classes or objects as long as things are clear to you conceptually. Java is designed to be efficient to do those things, and unless there is a specific performance problem with allocating too many objects, that can be optimized.

As far as the game stuff, a "Game" class has an important role as a single object responsible for orchestrating the logic of the game itself. This could be implemented either as a set of static methods, or a Singleton object, or just a regular object. There's no one right way to do it.

A typical "Game" class includes a start method, a render method, and an update method.

The render method is responsible for updating the screen, reflecting the current state of the world. This is where you call into your graphics API such as OpenGL or DirectX, whatever.

The update method computes the new state of the world based on the current state, actions, and time. This step includes a lot of things like collision detection, animation, etc.

A "Player" object would be a class with methods to poll the current state of the input device, move the player's avatar, select menus, etc.

1 year ago

Jesse Silverman wrote:I don't remember for sure, so I might have this backwards, but I felt that Hacker Rank very rarely gives you those clues and makes you guess 90% of the time -- I mean figure out what data structure and algorithm is needed by analysis of the stated problem, whereas LeetCode jumps to asking you to implement a specific algorithm...

Do you have trouble with HackerRank problems for this reason??

Well, the problems that make you figure out which data structure or algorithm to use are obviously harder to me than just asking to implement a specific algorithm that I know.

It's like the dreaded word problems in math, or statistics. The problem is to figure out which equation to use; after that, it's easy -- just plug in the numbers.

Figuring out "word problems" on the spot in an interview, considering any communication barriers, could be an order of magnitude harder than the problem itself, which is in turn, something relatively simple...
1 year ago