Pages
Welcome~~~
It is easier to write an incorrect program than understand a correct one.
Tuesday, July 26, 2011
Sunday, July 17, 2011
Thursday, July 7, 2011
Monday, July 4, 2011
Wednesday, June 22, 2011
Sunday, June 19, 2011
Saturday, June 18, 2011
Thursday, June 16, 2011
Tuesday, June 14, 2011
Monday, June 13, 2011
Friday, June 10, 2011
Thursday, June 9, 2011
PyNoAppError when coding wxpython in IDEL (ubuntu)

"Stay hungry, stay foolish"  Steve Jobs
Wednesday, June 8, 2011
About the wxPython: Exit, Destroy, Close
Robin Dunn wrote:
I assume that by "Exit" here you are still talking about the menu item above.
No, I was referring to wx.Exit, which I think is a global call.
It is a way to forcibly kill the app, without any opportunity for the application objects to clean up after themselves. Most of the time you wouldn't want to use it.
You use Close() when you want to programatically tell the frame to go close itself, and is functionally the same as the user telling it to close itself with the "X" button.
Destroy() tells wx to delete the C++ object instance that corresponds to the frame. Normally it will destroy itself when it closes in the default EVT_CLOSE event handler, but if you catch the EVT_CLOSE yourself you either need to call Destroy in your handler, or call event.Skip so the default handler will still run. The EVT_CLOSE handler is where you would normally put the code that checks for open files, asks the user if she wants to save them or cancel, etc. Based on the user's response you can veto the close if you want.
I don't understand. So Close() does nothing more than make the window disappear?
No, it tells the window to close itself. That is a lot different than just hiding it.
Does Close() then automatically call Destroy()?
No, Close causes a EVT_CLOSE event to be sent. The default handler for that event calls Destroy().
What happens if I catch the wx.EVT_CLOSE event and make it call self.frame.Close()?
Then you'll get an endless loop.
Does it get destroyed also, or is the application still running somehow?
According to you and Andrea, I think this is what I need:
self.frame.Bind(wx.EVT_CLOSE, self.OnExitApp)
def OnExitApp(self, event): self.frame.Destroy()
Right? Or do I just use Close() still?
You use Close() from your menu event handler, or wherever you want to programatically cause the frame to be closed. You only need to provide a handler for EVT_CLOSE if you want to have more control over the closing of the frame than the default automatic destroy.
Friday, June 3, 2011
Enthought Python, Sage, or others (in Unix clusters)  Stack Overflow
Thursday, June 2, 2011
Limit the border size on a matplotlib graph
Wednesday, June 1, 2011
Tuesday, May 31, 2011
Monday, May 30, 2011
Sunday, May 29, 2011
Saturday, May 28, 2011
Thursday, May 26, 2011
Fwd: [issue2714] Unable to start IDLE on Windows Server 2003 x64 Edition w/ SP2
Check whether the TCL_DIR or TK_DIR environment variables are set. If so, unset them.
For my computer, deltet the tcl that related to the previous installed ruby.
http://mail.python.org/pipermail/pythonbugslist/2008April/051418.html
Wednesday, May 11, 2011
Monday, May 2, 2011
Sunday, May 1, 2011
Thursday, April 28, 2011
Java sorting  comparable v comparator
java.lang.Comparable  java.util.Comparator 
int objOne.compareTO(objTwo) – A Comparable interfaced class must contain a method called compareTo to compare two objects (one being the object on which it is called and the other being passed as a paramater)  int compare(objOne, objTwo) – A Comparator is a class in its own right, which implements the Comparator interface; that means it must contain a method called compare (two objects as parameters) 
Return Values  negative – if objOne < objTwo zero – if objOne = objTwo positive – if objOne > objTwo  Return Value  same as Comparable 
Only one sort sequence can be created  Many sort sequences can be created 
You must modify the class whose instances you want to sort  You build a separate class from the class whose instances you want to sort 
Implemented frequently in the API by : String, Wrapper Classes, Date, Calendar  Meant to be implemented to sort instances of thirdparty classes 
If you want to sort a collection using its comparable interface, you simply call the static Collections.sort method on it … so if we had an ArrayList called Animal which implemented Comparable, we could write:Collections.sort(Animal);  To sort a collection using a Comparator class, you need to pass an extra parameter into the Collections.sortmethod – that parameter being an instance of a Comparator object. Thus:Collections.sort(Animal, new ByBreed()); 
Monday, April 25, 2011
Sunday, April 24, 2011
Saturday, April 23, 2011
Java Source Code / Java DocumentationJava Source Code and Java Documentation
MatrixpackageforPython
http://sourceforge.net/projects/matpy/
Description:
MatPy is a Python package for numerical linear algebra and plotting with a MatLablike interface. It currently consists of wrappers around Numeric and Gnuplot packages, but eventually may be implemented directly in C/C++, or as interface to Octave.
PyGoogle
URL:  http://sourceforge.net/projects/pygoogle/ 
Description:  A Python wrapper for the Google web API. Allows you to do Google searches, retrieve pages from the Google cache, and ask Google for spelling suggestions 
Python Open Source
Python Open Source  

Friday, April 22, 2011
Thursday, April 21, 2011
Wednesday, April 20, 2011
Monday, April 18, 2011
Printing expressions from Tree
http://www.daniweb.com/forums/thread323976.html
http://www.careercup.com/question?id=7473670
http://wordaligned.org/articles/pythonsurprisemehttp://openbookproject.net/thinkcs/python/english2e/ch21.html
http://stackoverflow.com/questions/1894846/printingbfsbinarytreeinlevelorderwithspecificformatting
http://codinggeeks.blogspot.com/2010/04/serializinganddeserializingbinary.html
http://ponywma.spaces.live.com/blog/cns!3757DF93DF1F3015!334.entry
def printexp(tree):
sVal=""
if tree:
sVal='( ' + printexp(tree.getLeft())
sVal=sVal+str(tree.getRootValue())
sVal= sVal+printexp(tree.getRight()) +' )'
return sVal
Here is the source code and the text file format:
Note that the text file is created "by hand" and only read function is implemented in the code. Implementing write function should be straight forward.
==============================================
#include <stdio.h>
typedef struct tree_struct
{
int data;
int nchildren;
struct tree_struct *children;
}tree_t;
tree_t * create_new()
{
return ((tree_t *) malloc(sizeof(tree_t)));
}
int populate_tree(tree_t *root, FILE *fp)
{
fscanf(fp,"%d",&(root>nchildren));
root>children = NULL;
if(root>nchildren)
{
root>children = (tree_t *) malloc(root>nchildren * sizeof(tree_t));
for(int i=0; i<root>nchildren; i++)
fscanf(fp,"%d",&(root>children[i].data));
for(int i=0; i<root>nchildren; i++)
populate_tree(&root>children[i],fp);
}
else
return 0;
}
int print_tree(tree_t *root)
{
static int count = 0;
if(root)
{
if(!count++)
printf("%d\n",root>data);
if(root>nchildren)
{
for(int i=0; i<root>nchildren; i++)
printf("%d ",root>children[i].data);
printf("\n");
for(int i=0; i<root>nchildren; i++)
print_tree(&(root>children[i]));
}
return 0;
}
else
return 1;
}
int main(int argc, char *argv[])
{
char *filename = argv[1];
FILE *fp = fopen(filename,"r");
tree_t *root = create_new();
fscanf(fp, "%d", &(root>data));
populate_tree(root,fp);
print_tree(root);
}
==============================================
The tree taken for illustration purpose is:
5 >3(1(4,19),7(20),2(25)), 10(11,17), 2(99,8)
The file format used is as follows:
<example.tree>
5
3 3 10 2
3 1 7 2
2 4 19
0
0
1 20
0
1 25
0
2 11 17
0
0
2 99 8
0
0
==============================================
To compile (on linux):
gcc std=c99 g main.c o treeio
==============================================
Usage:
./treeio example.tree
Output:
5
3 10 2
1 7 2
4 19
20
25
11 17
99 8
==============================================
 http://www.pythonforum.org/pythonforum/viewtopic.php?f=14&t=5842
Some OOP websites
It also has a subweb site for the algorithms:http://www.oopweb.com/Algorithms/Files/Algorithms.html
Find the longest palindromes
the most naive solution would be to exhaustively examine all n (n + 1) / 2 substrings of the givennlength string, test each one if it's a palindrome, and keep track of the longest one seen so far. This has worstcase complexity O(n^{3}), but we can easily do better by realizing that a palindrome is centered on either a letter (for oddlength palindromes) or a space between letters (for evenlength palindromes). Therefore we can examine all 2n + 1 possible centers and find the longest palindrome for that center, keeping track of the overall longest palindrome. This has worstcase complexity O(n^{2}).
est data used is HYTBCABADEFGHABCDEDCBAGHTFYW12345678987654321ZWETYGDE
public static string GetPalindromeString(string theInputString)
{ int j = 0;
int k = 0; string aPalindrome = string.Empty; string aLongestPalindrome = string.Empty ; for (int i = 1; i < theInputString.Length; i++) { k = i + 1; j = i  1; while (j >= 0 && k < theInputString.Length) { if (theInputString[j] != theInputString[k]) { break; } else { j; k++; } aPalindrome = theInputString.Substring(j + 1, k  j  1); if (aPalindrome.Length > aLongestPalindrome.Length) { aLongestPalindrome = aPalindrome; } } } return aLongestPalindrome; }
Notes on Path Finding problem
astar c
.)matrix[u][v] = 1
denotes an edge between u and v, and matrix[u][v] = 0
denotes no edge between u and v. Then (matrix)^3
(just simple matrix exponentiation) is 'magically' the path matrix of exactly length 3. An efficient implementation of Dijkstra's algorithm takes O(Elog V) time for a graph with E edges and V vertices.
 Hosam Aly's "flood fill" is a breadth first search, which is O(V). This can be thought of as a special case of Dijkstra's algorithm in which no vertex can have its distance estimate revised.
 The FloydWarshall algorithm takes O(V^3) time, is very easy to code, and is still the fastest for dense graphs (those graphs where vertices are typically connected to many other vertices). But it'snot the right choice for the OP's task, which involves very sparse graphs.
Raimund Seidel gives a simple method using matrix multiplication to compute the allpairs distance matrix on an unweighted, undirected graph (which is exactly what you want) in the first section of his paper On the AllPairsShortestPath Problem in Unweighted Undirected Graphs [pdf].
(This is not exactly the problem that I have, but it's isomorphic, and I think that this explanation will be easiest for others to understand.)
Suppose that I have a set of points in an ndimensional space. Using 3 dimensions for example:
A : [1,2,3] B : [4,5,6] C : [7,8,9]
I also have a set of vectors that describe possible movements in this space:
V1 : [+1,0,1] V2 : [+2,0,0]
Now, given a point dest, I need to find a starting point p and a set of vectors moves that will bring me todest in the most efficient manner. Efficiency is defined as "fewest number of moves", not necessarily "least linear distance": it's permissible to select a p that's further from dest than other candidates if the move set is such that you can get there in fewer moves. The vectors in moves must be a strict subset of the available vectors; you can't use the same vector more than once unless it appears more than once in the input set.
My input contains ~100 starting points and maybe ~10 vectors, and my number of dimensions is ~20. The starting points and available vectors will be fixed for the lifetime of the app, but I'll be finding paths for many, many different dest points. I want to optimize for speed, not memory. It's acceptable for the algorithm to fail (to find no possible paths to dest).
Update w/ Accepted Solution
I adopted a solution very similar to the one marked below as "accepted". I iterate over all points and vectors and build a list of all reachable points with the routes to reach them. I convert this list into a hash of <dest, p+vectors>, selecting the shortest set of vectors for each destination point. (There is also a little bit of optimization for hash size, which isn't relevant here.) Subsequent dest lookups happen in constant time.
Minimum Spaning Tree

Some Notes

Counting sort
Counting sort (sometimes referred to as ultra sort or math sort) is a sorting algorithm which (like bucket sort) takes advantage of knowing the range of the numbers in the array to be sorted (array A).
Counting sort is a stable sort and has a running time of Θ(n+k), where n and k are the lengths of the arrays A (the input array) and C (the counting array), respectively. In order for this algorithm to be efficient, k must not be much larger than n.