algorithm

PyQt4 QDialog -- require field to not be blank

OK, this is driving me nuts. I'm sure it is trivial, but I've been looking for the answer for a while and don't see it. I'm sure it will be a flat forehead. I'm designing a Qt4 dialog in Python. I generated the code via QDesigner, and have 4 inputs on the system: QLineEdit (can't be blank) QPlainTextEdit QLineEdit (can't be blank...

MFC: CToolTipCtrl causes ASSERT

I have implemented tool tips seemingly successfully in an app which is MFC based and uses CPropertyPage. However after adding a button to open a dialog box called IDC_USERMSG which contains a single CEdit control on one of the CPropety pages an Assert is thrown when the IDC_USERMSG dialog is dismissed. _AFXWIN_INLINE CWnd* CWnd::GetP...

Visual Explanation Guidance needed for reversal of Linked List datastructure code ?

Hi, I have following piece of code for reversing the linked list. I am getting confused in while loop and so would certainly appreciate if someone can provide visual explanation of how actually it's working. static void Reverse (struct node** headRef) { struct node* result = NULL; struct node* current = *headref; stru...

Why don't client computers serve information to other clients over the internet in order to reduce server bandwidth?

Take an image hosting service for example. In order to reduce bandwidth/the number of times you serve images, is it possible to have a client who has just viewed an image then SERVE that same image file (or pieces of that file) to another client who wishes to view the same page/image? Are there security related issues that prevent this f...

uninterlace bits from 16 bit value

I have a 16 bit value with its bits "interlaced". I want to get an array of 8 items (values 0 to 3) that stores the bits in this order: item 0: bits 7 and 15 item 1: bits 6 and 14 item 2: bits 5 and 13 ... item 7: bits 0 and 8 This is a trivial solution: function uninterlace(n) { return [((n>>7)&1)|((n>>14)&2), // bits 7 and 15 ...

Help with drawing a sphere in OpenGL ES

I have the following code, but I can't find the bug. It seems to be a memory-related issue. The original code is taken from the 3d library which comes with the OpenGL superbible, and i'm trying to adapt it for openGL es. Any ideas as to why it's segfaulting all the time? - (void)drawSphereOfRadius:(GLfloat)fRadius nbSlices:(GLint)iSlice...

another Game of Life question (infinite grid) ?

Hi friends, I have been playing around with Conway's Game of life and recently discovered some amazingly fast implementations such as Hashlife and Golly. (download Golly here - http://golly.sourceforge.net/) One thing that I cant get my head around is how do coders implement the infinite grid? We can't keep an infinite array of anythin...

Shortest Path For A Dag

I have a graph with an s and t vertex that I need to find the shortest path between. The graph has a lot of special properties that I would like to capitalize on: *The graph is a DAG (directed acyclic graph). *I can create a topological sort in O(V) time, faster than the traditional O(V + E). *Within the topological sort, s is the first ...

How can we find given two binary trees that they are equal ?

Hi, What would be the efficient algorithm to find if two given binary trees are equal - in structure and content? Thanks. ...

using sscanf(), read string to array of int?

i have this string: 12 4 the quick 99 -1 fox dog \ what i want in my program: myArray[] = {12, 4, 99, -1}; how i do a multiple number scanning? ...

Enumerate search trees

According to this question the number of different search trees of a certain size is equal to a catalan number. Is it possible to enumerate those trees? That is, can someone implement the following two functions: Node* id2tree(int id); // return root of tree int tree2id(Node* root); // return id of tree (I ask because the binary co...

What is the fastest way to check if two given numbers are coprime?

One way is to calculate their gcd and check if it is 1. Is there some faster way? ...

Problem with the ArrayList class in Java

Hi, I have a problem with the following code. I obtain the error message Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:571) at java.util.ArrayList.set(ArrayList.java:364) at Test.main(Test.java:17) and I don't understand why. I have a lis...

Generic pattern algorithms (language agnostic)

I'm sorry to ask such a vague and generic question, but I need to write a set of tools that will aid people in creating 2D and 3D geometric patterns. Does anyone know any good online resources that discuss pattern logic and algorithms (Wikipedia call it tesselations)? Much obliged, David ...

Creating objects makes the VM faster?

Look at this piece of code: MessageParser parser = new MessageParser(); for (int i = 0; i < 10000; i++) { parser.parse(plainMessage, user); } For some reason, it runs SLOWER (by about 100ms) than for (int i = 0; i < 10000; i++) { MessageParser parser = new MessageParser(); parser.parse(plainMessage, user); } Any ideas ...

label of log y-axis: 1000 instead of 1e+03?

I've a problem concerning construction of log y-axis in a graphic. How can I manage that the units/numbers of my log y-axis aren't shown in 1e+03, 1e+04, 1e+05 etc...., but only in regluar arabic numbers (1000, 10000, 100000)? Thanks. ...

Java: Max/min value in an array?

It's trivial to write a function to determine the min/max value in an array, such as: /** * * @param chars * @return the max value in the array of chars */ private static int maxValue(char[] chars) { int max = chars[0]; for (int ktr = 0; ktr < chars.length; ktr++) { if (chars[ktr] > max) { max = chars[ktr]; } } return ma...

C++ map question

I have an integral position-based algorithm. (That is, the output of the algorithm is based on a curvilinear position, and each result is influenced by the values of the previous results). To avoid recalculating each time, I would like to pre-calculate at a given sample rate, and subsequently perform a lookup and either return a pre-cal...

Adding summary statistics (or even raw data points) to dodged position boxplots

Say you have the following dataset: trt <- ifelse(runif(100)<0.5,"drug","placebo") inj.site <- ifelse(runif(100)<0.5,"ankle","wrist") relief <- 20 + 0.5*(inj.site=="ankle") + 0.5*(trt=="drug") + rnorm(100) to.analyze <- data.frame(trt,inj.site,relief) Now, the idea is to make a boxplot with injury site on the x-axis and boxes by trea...

How can I find the common ancestor of two nodes in a binary tree?

The Binary Tree here is not a Binary Search Tree. Its just a Binary Tree. The structure could be taken as - struct node { int data; struct node *left; struct node *right; }; The maximum solution I could work out with a friend was something of this sort - Consider this binary tree (from http://lcm.csa.iisc.ernet.in/dsa/no...