Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Incoherence
May 22, 2004

POYO AND TEAR

fletcher posted:

Is it more common to write variables as someVariable or some_variable? I can't decide which to use. I like using someMethod() instead of some_method(), so I'm thinking it would make more sense to use some_variable.
Depends on the language. Java usually uses camelCase for everything (first letter uppercase for classes, first letter lowercase for methods). C# style is similar to Java's, except that method names have the first letter uppercase. C/C++ varies but tends to prefer underscored_names.

Match whatever your language/environment uses. If neither of those give any guidance, just be consistent.

Adbot
ADBOT LOVES YOU

Incoherence
May 22, 2004

POYO AND TEAR

Triangulum posted:

TI BASIC question. I wrote my first program for my 83 that solves the quadratic formula. The problem is that my program gives decimal approximations instead of exact value when dealing with square roots. How do I program it to show exact values?
You'll have to figure out a way to display the root, since the 83 just gives you the approximation. I wrote something similar for my TI-83 which just displayed
code:
         ___
 X +/- v/ Y
 ----------
     Z
where X = -b, Y = b^2 - 4ac, and Z = 2a... I think it also tried to find exact square roots and simplify that. You'll have to do the output formatting yourself, in other words.

Incoherence
May 22, 2004

POYO AND TEAR

6174 posted:

Interesting. I remember just opening the files in a text editor when I used to play with programs on my TI-86 and then 89. Maybe it was the linking software allows you to view the programs?

This was many years ago though, so my memory is a little fuzzy on some of the details.
The TI calculators can either be programmed in a BASIC variant or in whatever flavor of assembly they use. Maybe it was the latter, and there's some compilation done on it?

Incoherence
May 22, 2004

POYO AND TEAR
Don't think we have a bash scripting thread yet, and at any rate I'm not any good with it... What does -a mean in a [ ] expression? Googling for it is kind of a pain, and the bash resources I found were no help.

edit: Never mind, found it; it's a logical and. It just looked strange because of the context I saw it in.

Incoherence fucked around with this message at 03:10 on Feb 29, 2008

Incoherence
May 22, 2004

POYO AND TEAR

dougdrums posted:

So I was :420: (not now, earlier) and programming and thinking about the finally statement. Is there any god drat reason for the finally statement? The only situation I see using finally in is if I didn't want to put a catch statement after a try which is a horrible practice anyways. And even if I did want to do that, couldn't I just use catch { } instead?

Sorry if I'm retarded and there actually is a good use for finally and I just never figured it out.

Edit: C#.
Imagine that you have some expensive resource, like a file or a database. You try to read from it; if it succeeds, great; if it fails, you need to close it then maybe go try something else. You could write this as follows:
code:
DatabaseConnection db = MakeDatabaseConnection();
try {
  DoSomeDBShit(db);
} catch (DBException e) {
  // complain to the user/calling code
} finally {
  db.CloseConnection();
}
Without finally, you have to duplicate the CloseConnection piece in the try and catch blocks.

Incoherence
May 22, 2004

POYO AND TEAR

ShoulderDaemon posted:

Easy. All I've done is generated the sets in a sorted order, by building them piece-by-piece and never adding an element to a set if there is already a higher element in that set.
I thought about that, but wouldn't that fall down when there are duplicate elements in the input set?

Incoherence
May 22, 2004

POYO AND TEAR

ShoulderDaemon posted:

Well, then it's not an input set, it's an input list, and you can do it by explicitly passing around the list of remaining elements, and chopping off prefixes of it. Or just keep the list out of band, and use an input set of integers from zero to (length of list)-1, then use those as indexes into the list.
You're not solving the problem: if your input set is {0, 1, 1, 2, 3} and you want all subsets of length 3, {1, 2, 3} using the first 1 is the same as {1, 2, 3} using the second 1. Example:
code:
sets({0, 1, 1, 2, 3}, 3) = union of
cons(0, sets({1, 1, 2, 3}, 2}) //a
cons(1, sets({1, 2, 3}, 2})    //b
cons(1, sets({2, 3}, 2})       //c
We'll ignore a for the moment. The recursive call in b will give you {1, 2} and {2, 3}, so you have {1, 1, 2} and {1, 2, 3}. The recursive call in c is trivially {2, 3}, so you have {1, 2, 3} again. Look, a duplicate!

Now that I look at it, you could solve that by not recurring on c, since b will necessarily return a superset of c. In other words, you'd need to add to ShoulderDaemon's algorithm that if the current element is equal to the previous element, don't recur.

Incoherence
May 22, 2004

POYO AND TEAR
Alternative answer, which might work for some applications:
code:
if (!myObject.SetSomeValue(someData)) {
  // handle error
}

Incoherence
May 22, 2004

POYO AND TEAR

ShoulderDaemon posted:

Uhm... a sorted array is a balanced binary tree. The top node is the index closest to the middle, which you can get in constant time as long as you know the highest index, the left subtree is all indices before it, and the right subtree is all indices after it.
Sounds like he wants to start with a sorted array and make a balanced binary tree so that he can add more crap to it (with O(lg n) insertion instead of O(n)).

Incoherence
May 22, 2004

POYO AND TEAR

nbv4 posted:

and no, using radio buttons instead is not a proper solution.
Find your UI designer and stab them. If it's you, stab yourself.

But back to your original question: logical-XORing a bunch of poo poo together just tells you whether an odd number of them are true. Easiest way to find out if more than one is true is just to assign a value (0, 1) to each and add them up.

Incoherence
May 22, 2004

POYO AND TEAR

gotly posted:

I'm taking a data structures class and we designed and implemented our own linked list. Now I'm trying to use the actual integrated LinkedList function in Java and I must be blind but I can't find the basic Next function that goes to the next item in the list in constant time.

There is a get(int) function but that runs through the list in linear time to find it.
I think the method you want is listIterator(int) to start, and then use iterator.next() and iterator.previous() from there.

Explanation: Your question assumes a composite list (or a C style linked list): that is, each LinkedList object represents one node (say, with a first and rest, or with a data and a next). Java's LinkedList is the whole list in one object. So Java collections, and for that matter C++ STL collections, use "iterators" to represent the idea of sequence. You get the iterator, then whatever sequence-dependent things you want to do you do to the iterator. (Or you use foreach, but foreach doesn't solve all your problems.)

Incoherence
May 22, 2004

POYO AND TEAR

fletcher posted:

If I have a class with 3 members: id, name, parentId, and I want to print out the hierarchy like:

something
-here
--there
--every
--where
-zing
--ding
---wing
-test
--this

How would I go about doing it? It seems like recursion would be appropriate here, but I have very little experience with recursion.
You're not being clear on your problem. I am guessing that you have a map of IDs to these structures somewhere, and that id and parentId are keys into that map, thus forming some sort of N-tree of IDs. The thing I can't guess is whether you want to print the tree from children up (so, "here" would be the parent of "something") or from the parents down (so, "something" would be the parent of "here"). The latter is somewhat more complicated, since if you want to do that, what you really want is to know all children of a given parent, at which point you do a preorder tree traversal.

Incoherence
May 22, 2004

POYO AND TEAR

fletcher posted:

Sorry, I want to print the tree from the parents down

parent
-child1
--subchild1
--subchild2
-child2
Now for the other questions: how are you mapping from ID to node, and what parts of the tree do you have references to? You've apparently built a tree with only upward branches, so in order to figure out how to invert the tree (so you can preorder traverse it) we sort of need to know what we're starting from.

Incoherence
May 22, 2004

POYO AND TEAR

darfur posted:

I've searched the web for this, but can't really find a good answer.

What good is an interface? I understand what one is, and how I would implement one, but I don't understand the benefit of using one instead of just writing the classes that would use it by themselves without using the interface. It seems like if your class "inherits" and interface, it must include all of the methods in the interface, but why not just write the class with all the methods anyway?

Can anyone clarify this for me?
An interface allows multiple implementations of the same methods; any member of an interface can be used as a variable of the interface type.

Consider the Java List interface: http://java.sun.com/j2se/1.5.0/docs/api/java/util/List.html
You can see that among the implementations give there are ArrayList, LinkedList, and Vector. So anything that takes a List, and uses the List methods, can be called on any of these implementations. From a consumer's point of view, if they just want to get some data, the fact that ArrayList.get() and LinkedList.get() behave entirely differently under the hood is irrelevant.

Another example: event handlers. All event handlers for a given event implement the same interface, but they may (and hopefully do) have different behavior, and you may have multiple different event handlers for a particular event.

Incoherence fucked around with this message at 20:22 on May 7, 2008

Incoherence
May 22, 2004

POYO AND TEAR

csammis posted:

Huh? Event handlers (callbacks, whatever you call them in your language) are methods, not interfaces. The fact that you can have multiple event handlers for a single event has zilch to do with interfaces, it has to do with how events are implemented in your architecture.
Well, yes, that particular implementation is Java's. Conceptually, a C# delegate is equivalent to (but much less wordy than) a single-method interface.

I was trying to think of an example where you would have multiple implementations of an interface and want to hold them all in a list and run them all in sequence, without using the one on the project I work on (I can't figure out a good way to generalize/anonymize it).

Incoherence
May 22, 2004

POYO AND TEAR

darfur posted:

In MEAT TREAT's tree, for the insert function, would it just take an INode as the parameter, and then you could pass it any class which inherits the INode interface and that class' specific implementation of insert would be what gets called? That is my understanding as to how an interface would work there, is this correct? Would you have to make sure you don't link different types of INodes together or is that what this enables you to do?
You would have your other methods take an INode parameter. If you have INode n, and you call n.insert(), it will call the insert() method corresponding to the actual implementation. You could even have a list of INodes of different types and calling insert() on all of them in sequence will do the right thing.

(Insert yammering about the C++ virtual keyword here, but if you're talking about interfaces you assume the methods are virtual. Also insert yammering about how this is actually an instance of the Composite Design Pattern, and that if you're in academia there are all sorts of neat things you can add to this like the Visitor Pattern.)

Incoherence
May 22, 2004

POYO AND TEAR

Tomed2000 posted:

I'm not, just a student right now. I was having a conversation with a professor and he claims that 90% of the industry programs in a UNIX environment and I thought that was pretty inaccurate.
It's a pretty broad brush you're painting with either way, and ideally you'd have some familiarity with different environments and the ability to pick up whatever one you find yourself in. And for some applications it doesn't matter what your environment is, so you can use whatever you want.

Incoherence
May 22, 2004

POYO AND TEAR

Vanadium posted:

I for one would rather sit down and start coding instead of memorizing Visual Studio stuff that stands between the program in my mind and the program-to-be on the computer. :colbert:
Visual Studio is not so bad about this. Eclipse is rather bad about this unless you already think in Eclipse.

Adbot
ADBOT LOVES YOU

Incoherence
May 22, 2004

POYO AND TEAR

N.Z.'s Champion posted:

I guess it just sounds like an easy optimisation that could result in shorter/clearer code. Is it not?
Could it gain you some memory? Possibly, but it might not be worth worrying about unless you're doing a lot of these swaps.

Could it make your code shorter? Possibly, but this is sometimes in conflict with making it clearer.

Could it make your code clearer? Many optimizations don't, and since you don't know how this would be done, it's difficult for you to say.

The XOR trick probably works in more languages than tuple-unpacking in Python, but it has a significant effect on code clarity (what the gently caress are all of these ^=s doing in my code), and as an optimization it's fairly negligible.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply