Saturday, March 13, 2010
Tuesday, August 4, 2009
Give me a function that given a line, describes the next
1
11
21
1211
111221
312211
13112221
1113213211
char* get_next_line(char* line)
{
char next_line[4096];
int next_line_ndx = 0;
char letter = line[0];
int num_letters = 0;
for (int i = 0; i < strlen(line); ++i)
{
if (letter != line[i+1])
{
next_line[next_line_ndx] = num_letters + '0';
next_line_ndx++;
next_line[next_line_ndx] = letter;
next_line_ndx++;
next_line[next_line_ndx] = 0;
letter = line[i+1];
num_letters = 0;
}
else
{
num_letters++;
}
}
return next_line;
}
11
21
1211
111221
312211
13112221
1113213211
char* get_next_line(char* line)
{
char next_line[4096];
int next_line_ndx = 0;
char letter = line[0];
int num_letters = 0;
for (int i = 0; i < strlen(line); ++i)
{
if (letter != line[i+1])
{
next_line[next_line_ndx] = num_letters + '0';
next_line_ndx++;
next_line[next_line_ndx] = letter;
next_line_ndx++;
next_line[next_line_ndx] = 0;
letter = line[i+1];
num_letters = 0;
}
else
{
num_letters++;
}
}
return next_line;
}
Wednesday, July 29, 2009
breadth first search of binary tree (w/o the formatting)
7
/ \
1 2
/ \ /
0 3 4
\
2
7
1 2
0 3 4
2
def traverse_tree(node):
q = []
last_level = 0
current_level = 0
q.append([node, current_level])
while len(q) > 0:
node, current_level = q.pop()
if current_level != last_level:
last_level = current_level
print "\n"
print node + " "
left_child = node.left
right_child = node.right
if left_child not NULL:
q.append([left_child, current_level + 1])
if right_child not NULL:
q.append([right_child, current_level + 1])
/ \
1 2
/ \ /
0 3 4
\
2
7
1 2
0 3 4
2
def traverse_tree(node):
q = []
last_level = 0
current_level = 0
q.append([node, current_level])
while len(q) > 0:
node, current_level = q.pop()
if current_level != last_level:
last_level = current_level
print "\n"
print node + " "
left_child = node.left
right_child = node.right
if left_child not NULL:
q.append([left_child, current_level + 1])
if right_child not NULL:
q.append([right_child, current_level + 1])
Monday, October 20, 2008
MySQL restart on Mac OS X
This is the simplest way to restart the server on Mac OSX that I found
my-computer:~ me$ mysqladmin -uroot -p shutdown
Enter password:
my-computer:~ me$ sudo mysqld_safe
Starting mysqld daemon with databases from /sw/var/mysql
my-computer:~ me$ mysqladmin -uroot -p shutdown
Enter password:
my-computer:~ me$ sudo mysqld_safe
Starting mysqld daemon with databases from /sw/var/mysql
Thursday, October 16, 2008
Wednesday, October 8, 2008
MySQL Python API
If you try to install the python API for MySQL on Leopard, and receive the following message:
lipo: can't open input file: /var/tmp//ccDsRjet.out (No such file or directory)
make sure you check out this.
lipo: can't open input file: /var/tmp//ccDsRjet.out (No such file or directory)
make sure you check out this.
Monday, June 9, 2008
Friday, June 6, 2008
Installing BerkeleyDB perl module from CPAN
I was getting an error executing:
cpan -i BerkeleyDB
This seemed to be because I did not have Berkeley installed on my mac. Well no problem, just execute
port install db46
from Macports. After that finishes, just set up a link in /usr/local e.g.
ln -s /usr/local/BerkeleyDB.4.6/ /usr/local/BerkeleyDB
and then
cpan -i BerkeleyDB
should work.
Tuesday, June 3, 2008
Enable sending email on Mac OS X
It wasn't completely obvious to me, but here is what you need to execute in order to be able to send email from your Mac.
in a terminal:
postfix start
Wednesday, May 28, 2008
Installing and setting up a fink installed MySQL with a non-default data directory
So I assume you have successfully executed: sudo fink install mysql
Create or edit /etc/my.cnf and add or change the keyword datadir to the directory you want e.g.
[mysqld]
datadir=/Volumes/another/directory
[mysqld_safe]
datadir=/Volumes/another/directory
During installation, Fink set up some files and directories in /sw/var/mysql. Copy the contents of this directory to your new directory e.g.
sudo cp -r /sw/var/mysql/* /Volumes/another/directory
Go to /Volumes/another/directory and set the owner of all contents to mysql e.g.
sudo chown -R mysql /Volumes/another/directory.
At this point you should be able to run 'sudo mysqld_safe.'
Saturday, May 17, 2008
fink Mysql on Macbook OSX 10.5 leopard
I installed mysql from fink and received some unwelcome news.
My-MacBook:~ me$ sudo mysqld_safe
Password:
chown: mysql: Invalid argument
Starting mysqld daemon with databases from /sw/var/mysql
STOPPING server from pid file /sw/var/mysql/My-MacBook.local.pid
080517 14:39:45 mysqld ended
After looking at /sw/var/mysql/My-MacBook.local.err, it seemed I was missing the user mysql. So I snooped around google and found the answer. Based on this, I executed the following commands:
sudo dscl localhost -create /Local/Default/Users/mysql
My-MacBook:var me$ sudo mysqld_safe Password:
chown: mysql: Invalid argument
Starting mysqld daemon with databases from /sw/var/mysql
STOPPING server from pid file /sw/var/mysql/My-MacBook.local.pid
080517 17:11:24 mysqld ended
This one was easy though. Just go to /sw/var and execute chown -R mysql mysql. You should be good to go from here. sudo mysqld_safe& should work just fine.
My-MacBook:~ me$ sudo mysqld_safe
Password:
chown: mysql: Invalid argument
Starting mysqld daemon with databases from /sw/var/mysql
STOPPING server from pid file /sw/var/mysql/My-MacBook.local.pid
080517 14:39:45 mysqld ended
After looking at /sw/var/mysql/My-MacBook.local.err, it seemed I was missing the user mysql. So I snooped around google and found the answer. Based on this, I executed the following commands:
sudo dscl localhost -create /Local/Default/Users/mysql
sudo dscl localhost -create /Local/Default/Users/mysql NFSHomeDirectory /var/emptyMy next attempt to start mysqld_safe gave me some more bad news:
sudo dscl localhost -create /Local/Default/Users/mysql Password '*'
sudo dscl localhost -create /Local/Default/Users/mysql PrimaryGroupID 74
sudo dscl localhost -create /Local/Default/Users/mysql RealName "MySQL Server"
sudo dscl localhost -create /Local/Default/Users/mysql UniqueID: 74
sudo dscl localhost -create /Local/Default/Users/mysql UserShell: /usr/bin/false
My-MacBook:var me$ sudo mysqld_safe Password:
chown: mysql: Invalid argument
Starting mysqld daemon with databases from /sw/var/mysql
STOPPING server from pid file /sw/var/mysql/My-MacBook.local.pid
080517 17:11:24 mysqld ended
This one was easy though. Just go to /sw/var and execute chown -R mysql mysql. You should be good to go from here. sudo mysqld_safe& should work just fine.
Wednesday, May 14, 2008
Adobe Flex license key Leopard OSX
If you ever receive a message like "License key is invalid" when you enter in your valid license
for Adobe Flex on OSX, try switching to an admin account. For some reason when you enter the license key into Flex Builder (or the Eclipse plug-in) under a non-admin account, you cannot 'activate' the installation.
for Adobe Flex on OSX, try switching to an admin account. For some reason when you enter the license key into Flex Builder (or the Eclipse plug-in) under a non-admin account, you cannot 'activate' the installation.
Saturday, May 10, 2008
gem install mysql mac osx leopard
gem install mysql -- --with-mysql-config=`which mysql_config`
Also you may need some additional hacking to get it all working.
Also you may need some additional hacking to get it all working.
Wednesday, March 19, 2008
Tuesday, March 11, 2008
Debugging ruby rails in production
Helpful little thing that helped me debug my ruby rails production system.
shell$ [RAILS_ROOT]/script/console production
So here you can invoke a model (e.g. User.new), call helper methods (UserHelper.authenticate(user, pass)), etc.
shell$ [RAILS_ROOT]/script/console production
So here you can invoke a model (e.g. User.new), call helper methods (UserHelper.authenticate(user, pass)), etc.
Thursday, March 6, 2008
Ruby Rails column type to MySQL datatype
lifted from PackT.
| Migration column type... | Converts to MySQL field type... | Available options1 |
| :binary | TINYBLOB, BLOB, MEDIUMBLOB, or LONGBLOB2 | :limit => 1 to 4294967296 (default = 65536)2 |
| :boolean | TINYINT(1) | - |
| :date | DATE | - |
| :datetime | DATETIME | - |
| :decimal | DECIMAL | :precision => 1 to 63 (default = 10) :scale => 0 to 30 (default = 0)3 |
| :float | FLOAT | - |
| :integer | INT | :limit => 1 to 11 (default = 11) |
| :primary_key | INT(11) AUTO_INCREMENT PRIMARY KEY | - |
| :string | VARCHAR | :limit => 1 to 255 (default = 255) |
| :text | TINYTEXT, TEXT, MEDIUMTEXT, or LONGTEXT2 | :limit => 1 to 4294967296 (default = 65536)2 |
| :time | TIME | - |
| :timestamp | DATETIME | - |
Tuesday, March 4, 2008
Production Rails Database
To make a production version of a database defined in rails.
rake db:migrate RAILS_ENV="production"
rake db:migrate RAILS_ENV="production"
Monday, March 3, 2008
phpMyAdmin plug
Man... gotta use phpMyAdmin for looking at your MySQL database. On Mac use 'Fink Commander' and look for phpMyAdmin. Follow the configuration instructions here. In order for me to get it working, I had to set the following properties:
$cfg['Servers'][$i]['socket'] = '/tmp/mysql.sock';
$cfg['Servers'][$i]['connect_type'] = 'socket';
$cfg['Servers'][$i]['socket'] = '/tmp/mysql.sock';
$cfg['Servers'][$i]['connect_type'] = 'socket';
Tuesday, February 26, 2008
Instance variable in viewer for ActionMailer
Using the ActionMailer in your Ruby on Rails application, I wanted to display a variable in the viewer. If the content_type is "text/html", the body needs to have the instance variable symbol e.g. body :var => var. In the viewer, you may reference this variable with <%= @var %>.
Subscribe to:
Posts (Atom)