This problem has been bugging me for the passed two days. Finally, after a lot of trial and error, I realized it was an issue concerning selecting the correct LayoutManager.
By default, containers (i.e. JPanel, JScrollPanel) added with drag and drop inside the NetBeans uses the GroupLayout. Hence, if you try to add a new runtime generated components to those containers, they won’t show up.
To solve this problem is to reset the Layout manager. So far, GridLayout and FlowLayout managers works great.
touchmepane.setLayout(new GridLayout());
And, this solved me problem.
jPanelServerList.setLayout(new GridLayout()); jPanelServerList.add(rayslabel,0); jPanelServerList.add(servericon,0); jPanelServerList.setBackground(Color.BLUE); jPanelServerList.revalidate(); jPanelServerList.repaint();
revalidate() call is important. Otherwise, there is a lag.
A few days ago, at a job interview, someone asked the following question.
How do you find an English word’s anagrams from a file of 10,000 arbitrary words? Anagram is a random order of certain set of characters, i.e. Tea, Ate, Eat, eta are all anagrams of each other.
I tried very hard explaining to my interviewer that the best solution for solving this problem is using common sense.
While one can always perform a character sort function on each word of those 10,000 words and then sticking them in hash table (the traditional way), i.e. Hash #1 – Tea, Ate, Hash #2 – Nose, Ones. Then, match the word with those hashes. There are better ways. Imagine performing 10,000 or more sort activities, it is such a waste of CPU time, plus storing 10,000 words in the memory just for the matching process. What an inefficient design, especially if you need to process matching across multiple 10,000 words files+. Huge Hashes, long execution time!
Anyway, my solution is based on the business rules. It is obvious that 5 letters words will never be anagrams of a 4 letters word, i.e. SNOWS is not same as SNOW. Even though they look very much similar. Hence, the following:
- Character Counting – Finding the length of the input word. This is as simple as doing a <word>.length() function. With that, when you read in the 10,000 words file, any words that doesn’t have the correct length can be thrown out. It will be a waste of CPU time to process and store them.
- Sum of character’s decimal value – Sorting function is a slow process, involving shifting characters of a word (an array, in fact). Why even wasting time, if there is a better way? We all know each character has a certain ASCII / Unicode representation. For example, the letter “A” is 65 in Decimal representation. The letter “s” is 115 in Decimal representation. Hence, the sum of decimals of “As” is 180. Doing a sum on decimal representation of characters is probably faster than sorting / character shifting of a word. (see http://www.asciitable.com/)
- Regexp of Vowels + Prime number. (improved method) It is true after performing step #2, there could be collisions, i.e. Both “As” and “Gm” will have 180 as the sum of their decimal values. Again, common sense helps us solving this problems. In English, there are 5 vowels, “A”, “E”, “I”, “O”, and “U”. “A” – 65, “E” – 69, “I” – 73”, “O” – 81, “U” – 85. The numerical distance between each is even number based. Between “A” and “I”, it is 4. Between “E” and “I” is also 4. Between “I” and “O”, it is 8. And, between “O” and “U” it is also “4”. As well as, between “A” and “O”, it is 16. All those distances are based on 4, an even number. If you have studied math before, you should know that prime number is not divisible, except it’s self and number one, i.e. 11, 3, 5, and 7 are all prime numbers. To solve the mis-summing problem posed by step 2, you simply do a regexp counting of vowels. (See http://rubular.com/) with [aeiou]. In our case, we jag out the multiplication. i.e. Sum of (Vowels in Decimal representation * A prime number (for example 11)) + Sum (Constant in Decimal). I bet you the odds of seeing a collision are less than one in a billion. So, this will be our improved algorithm.
A lot of programmers like to solve problem in programmatic ways. But, common senses (business rules) are far more efficient at solving complex problems. In our case, imagine you have to do anagram matching across one hundred 10,000 words files. What speed gains you will get by applying business rules. 1. Character counting, 2) following formula => Sum of (Vowels in Decimal representation * A prime number (for example 11)) + Sum (Constant in Decimal). Newton’s method is not precise. But, after a few iterations, the numerical solution to a problem is fairly precise. Same logic applies here. (http://en.wikipedia.org/wiki/Newton’s_method)
There are multiple ways to solving a problems. All paths lead to Rome. Except, some paths are shorter and faster.
Well, my job interview is still a mystery. So far, I am still waiting for a response from that company. It’s tougher than I thought finding a job nowadays. I guess that’s an exception to common senses. :-)
Love to hear your comments!!!
Tags: algorithm, fast anagram solver
OK, today, I gave my 2nd presentation of the year at the University of Burgundy, France, on following topics.
- Lean Start-up Methodology
- General Project Management techniques, i.e. Waterfall, Agile, Lean, SCRUM
- State of Cloud Computing industry, latest news, i.e. mergers and acquisitions, the evolution of cloud computing platforms, coolest technologies, i.e. NoSQL, Functional Language, …
- Microblogging & Liberalization of communication mediums
- Methodology to manage risk
Yes, it was done in French again to a stadium of ~100 students. Since yesterday, I have shared with students my experience of starting company, and providing cool technologies to IT giants, i.e. Salesforce, Amazon, Oracle.
So, here is my presentation on SlideShare.
http://www.slideshare.net/rgao2009/lean-start-cloud-computing-methodology-in-french
The process for installing Ruby 1.9.2 on Mac Lion + IDE is as follows:
# 1. Could have used homebrew. But, MacPort still works. And, RVM does not play nice with Netbeans 6.9.1 #install rubygems for 1.9 is already included in this port, no need to do a separate install. sudo port install ruby19 #2. Change the symbolic links cd /opt/local/bin sudo ln -s erb1.9 erb sudo ln -s gem1.9 gem sudo ln -s irb1.9 irb sudo ln -s rake1.9 rake sudo ln -s rdoc1.9 rdoc sudo ln -s ri1.9 ri sudo ln -s ruby1.9 ruby sudo ln -s testrb1.9 testrb #3. Add /opt/local/bin in front of the $PATH in .bash_profile file # MacPorts Installer addition on 2011-05-08_at_20:54:13: adding an appropriate PATH variable for use with MacPorts. export PATH=/opt/local/bin:/opt/local/sbin:$PATH # 4. install gems sudo gem install rails # 5. install debuggers sudo gem install ruby-debug19 sudo gem install ruby-debug-ide19 sudo gem install linecache19 # 6. fix the defect in the ruby-debug-0.4.12 per http://noteslog.com/post/netbeans-6-9-1-ruby-1-9-2-rails-3-0-0-debugging/
The detailed instruction for fixing the gem is here. Recap as follows
line 142 (optional error) /opt/local/lib/ruby1.9/gems/1.9.1/gems/ruby-debug-ide19-0.4.12/lib/ruby-debug-ide.rb
# error line: $stderr.printf "Fast Debugger (ruby-debug-ide 0.4.9) listens on #{host}:#{port}\n"
$stderr.printf "Fast Debugger (ruby-debug-ide19 0.4.12) listens on #{host}:#{port}\n"
line 78 (serious error) /opt/local/lib/ruby1.9/gems/1.9.1/gems/ruby-debug-ide19-0.4.12/bin/rdebug-ide
#78 Debugger::PROG_SCRIPT = ARGV.shift #replace with following: script = ARGV.shift Debugger::PROG_SCRIPT = (script =~ /script([\\\/])rails/ ? Dir.pwd + $1 : '') + script
Otherwise, you will get missing file error in Netbeans or other types of IDEs, when you try to debug.
Finally, if you are using Macport version of Ruby 1.9.2, you need to added the CA certificate. Otherwise, Ruby will complain with following error
OpenSSL::SSL::SSLError SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed
#solution sudo curl http://curl.haxx.se/ca/cacert.pem -o /opt/local/etc/openssl/cert.pem
Just got the following email from the founder of Meetup.com
Fellow Meetuppers,
I don’t write to our whole community often, but this week is
special because it’s the 10th anniversary of 9/11 and many
people don’t know that Meetup is a 9/11 baby.
Let me tell you the Meetup story. I was living a couple miles
from the Twin Towers, and I was the kind of person who thought
local community doesn’t matter much if we’ve got the internet
and tv. The only time I thought about my neighbors was when I
hoped they wouldn’t bother me.
When the towers fell, I found myself talking to more neighbors
in the days after 9/11 than ever before. People said hello to
neighbors (next-door and across the city) who they’d normally
ignore. People were looking after each other, helping each
other, and meeting up with each other. You know, being
neighborly.
A lot of people were thinking that maybe 9/11 could bring
people together in a lasting way. So the idea for Meetup was
born: Could we use the internet to get off the internet — and
grow local communities?
We didn’t know if it would work. Most people thought it was a
crazy idea — especially because terrorism is designed to make
people distrust one another.
A small team came together, and we launched Meetup 9 months
after 9/11.
Today, almost 10 years and 10 million Meetuppers later, it’s
working. Every day, thousands of Meetups happen. Moms Meetups,
Small Business Meetups, Fitness Meetups… a wild variety of
100,000 Meetup Groups with not much in common — except one
thing.
Every Meetup starts with people simply saying hello to
neighbors. And what often happens next is still amazing to me.
They grow businesses and bands together, they teach and
motivate each other, they babysit each other’s kids and find
other ways to work together. They have fun and find solace
together. They make friends and form powerful community. It’s
powerful stuff.
It’s a wonderful revolution in local community, and it’s thanks
to everyone who shows up.
Meetups aren’t about 9/11, but they may not be happening if it
weren’t for 9/11.
9/11 didn’t make us too scared to go outside or talk to
strangers. 9/11 didn’t rip us apart. No, we’re building new
community together!!!!
The towers fell, but we rise up. And we’re just getting started
with these Meetups.
Scott Heiferman (on behalf of 80 people at Meetup HQ)
Co-Founder & CEO, Meetup
New York City
September 2011
–
Add info@meetup.com to your address book to receive all Meetup
emails
To manage your email settings, go to:
http://www.meetup.com/account/comm/
Meetup, PO Box 4668 #37895
New York, New York 10163-4668
Meetup HQ in NYC is hiring!
http://www.meetup.com/jobs/
Tags: meetup