Tuesday, May 20, 2008

Neophyte/Unoptimized version of Euler Number 42 in Common Lisp


;;**********************************************
;; Author: Berlin Brown <berlin dot brown @ gmail.com>
;; Overview: Euler Example Number 42 (neophyte version)
;; Target Environment: Common Lisp -> Clisp/Sbcl
;; Date: 5/19/2008
;; --------------
;; The nth term of the sequence of triangle numbers is given
;; by, tn = 0.5 * n * (n+1); so the first ten triangle numbers are:
;;
;; 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
;;
;; By converting each letter in a word to a number corresponding to
;; its alphabetical position and adding these values we form a word value.
;; For example, the word
;; value for SKY is 19 + 11 + 25 = 55 = t10. If the word value
;; is a triangle number then we shall call the word a triangle word.
;;
;; Using words.txt (right click and 'Save Link/Target As...'),
;; a 16K text file containing nearly two-thousand common English words,
;; how many are triangle words?
;;
;; References:
;; [1] http://www.lispworks.com/documentation/HyperSpec/Front/X_Master.htm
;; [2] http://www.unixuser.org/~euske/doc/cl/loop.html
;; [3] http://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node154.html
;; [4] http://cl-cookbook.sourceforge.net/strings.html
;;**********************************************

(defconstant *max-tri-n* 30)

(defun split (string delim)
"Returns a list of substrings of string
divided by ONE space each.
Note: Two consecutive spaces will be seen as
if there were an empty string between them. E.g. #\Space"

(loop for i = 0 then (1+ j)
as j = (position delim string :start i)
collect (subseq string i j)
while j))
(defun triangle-num (n)
" (0.5 * n * (n + 1))"
(loop for i from 1 to n
collect (floor (* 0.5 (* i (1+ i))))))

(defun str-triangle (tri str)
"Determine if a string is a triangle number"
(if (member (loop for i across str
sum (- (char-code i) 64)) tri)
1 0))
(defun euler-items (items)
(loop for str in items
sum (str-triangle (triangle-num *max-tri-n*)
(subseq str 1 (1- (length str))))))
(defun euler-line (line)
(print (euler-items (split line #\Comma))))

(defun read-words ()
(with-open-file (stream "./words.txt")
(do ((line (read-line stream nil)
(read-line stream nil)))
((null line))
(euler-line line))))
(defun main ()
" Euler problem number 42"
(format t "INFO: Running Project Euler~%")
(time (read-words))
(format t "~%INFO: Done~%"))
(main)

Wednesday, May 7, 2008

Running Google AppEngine Django Unit Test Cases

If you want to run standalone google appengine code, you will need to establish some setup routines and initializations. Here is some code that I used to prepare the google appengine datastore and run a collection of python unit test cases.


#!/usr/bin/env python

'''
Author: Berlin Brown
Filename: run_all_tests.py
Application: test case for botlist django front-end (ghostnet)
Environment: Python 2.5.2
-------------------------- Description --

With the following code, you can run google-appengine
stand-alone database driven test cases.

* Run batch python unit test scripts
* The script ensures that the google libraries are imported and
added to the python sys path.

'''


import os
import sys
import unittest

# Add the parent directory, ghostnet to the sys path.
# We are assuming that google_appengine is also in the
# For example:
# -----------------------------
# PROJECT_HOME/google_appengine
# PROJECT_HOME/projects/ghostnet/tests

DIR_PATH = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
PROJECT_HOME = os.path.join(DIR_PATH, '..', '..', '..')

print("INFO: project_home=%s" % PROJECT_HOME)

EXTRA_PATHS = [
DIR_PATH,
os.path.join(PROJECT_HOME, 'projects', 'ghostnet'),
os.path.join(PROJECT_HOME, 'google_appengine'),
os.path.join(PROJECT_HOME, 'google_appengine', 'lib', 'django'),
os.path.join(PROJECT_HOME, 'google_appengine', 'lib', 'webob'),
os.path.join(PROJECT_HOME, 'google_appengine', 'lib', 'yaml', 'lib')
]
sys.path = EXTRA_PATHS + sys.path

# End of path setup

import datetime
import logging

# Google App Engine includes
from google.appengine.api import apiproxy_stub_map
from google.appengine.api import datastore_file_stub
from google.appengine.api import mail_stub
from google.appengine.api import user_service_stub
from google.appengine.ext import db

APP_ID = 'ghostnet_tests'
DS_PATH = '/tmp/dev_ds_tests.db'
HIST_PATH = '/tmp/dev_ds_tests.hist'

apiproxy_stub_map.apiproxy.RegisterStub(
'user',
user_service_stub.UserServiceStub())

# From the dev_appserver source:
# ----------------------
# datastore = datastore_file_stub.DatastoreFileStub(
# app_id, datastore_path, history_path, require_indexes=require_indexes)
# ----------------------
apiproxy_stub_map.apiproxy.RegisterStub(
'datastore_v3',
datastore_file_stub.DatastoreFileStub('ghostnet_tests',
DS_PATH,
HIST_PATH))
apiproxy_stub_map.apiproxy.RegisterStub(
'mail',
mail_stub.MailServiceStub())

# ----------------------
# Django Setups
# ----------------------

# A workaround to fix the partial initialization of Django before we are ready
from django.conf import settings
settings._target = None

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

# Import various parts of Django.
import django.core.handlers.wsgi
import django.core.signals
import django.dispatch.dispatcher
import django.db

# --------------
# Tests includes
# --------------
from tests.create_models import suite as suite_create
from tests.read_models import suite as suite_read
from tests.agent_rpc_test import suite as suite_rpc
from tests.client.client_rpc_test import suite as suite_client_rpc

def run_test_suite():
suite = unittest.TestSuite()
suite.addTest(suite_create())
suite.addTest(suite_read())
suite.addTest(suite_rpc())
suite.addTest(suite_client_rpc())
runner = unittest.TextTestRunner()
runner.run(suite)

# End of appengine setups for command-line test apps

if __name__ == '__main__':
print "Running model create"
run_test_suite()
print "Done"

# End of Script

Friday, May 2, 2008

Intelligent Agents by Norvig

This is not much of a post; but Peter Norvig has some good references to his book on intelligent agents:

http://aima.cs.berkeley.edu/

Thursday, May 1, 2008

Ubuntu Hardy Heron: Horrible web experience with Firefox, horrible experience

Who do you blame? Adobe Flash, Firefox, the Ubuntu version of FF?

I don't know; but I would just like to say, the bugs with firefox make for a horrible web experience, a horrible experience over all.

From reading the forums, it looks like flash might be the issue and the main culprit.

That is fine, but aren't the people that create Ubuntu big enough to give Adobe a call and try to work out the issue.

Sorry for the rant, but I am frustrated. I don't think I have ever used a piece of software that has crashed 5 minutes after use and then opened up again to crash again. What was my crime. I don't know; clicking on links?

I know I will get flamed for this, but I just gave my honest opinion. I have dealt with this issue for every FF upgrade since the early FF2 releases. Every time, epic fail.

I even had FF3b5 from Mozilla's site and I deleted it, thinking the Heron version would have better integration. That was a big mistake.

Anybody, who is new to Ubuntu; there are going to be issues that the reporters won't tell you about. Don't believe the hype. Please, I am just trying to help you out.

I will continue to use Ubuntu, as I haven't owned a Windows system since 2000 or so; so I don't hate it overall, there are just aspects that certain products couldn't get away with. I even posted about this on the ubuntu.brainstorms, along with a million other people. I guess that didn't get through.

Back on topic; my issue is that FF on Heron sucks and I don't feel like going threw 500 browsers to browse the web.

I will use Lynx or Emacs I guess. I give up.