Nov 10

Recursion and C++

I played with C++ for the first time in a while this evening. I was helping a friend work on Project Euler issues. The following is what I came up with:

Fibonacci Number:

  1. #include <iostream>
  2. int fib (int n) {
  3.     if (n < 1) return 0;
  4.     if (n == 1) return 1;
  5.  
  6.     return fib(n - 1) + fib(n - 2);
  7. }
  8.  
  9. int main() {
  10.     std::cout << fib(0) << std::endl;
  11.     std::cout << fib(1) << std::endl;
  12.     std::cout << fib(8) << std::endl;
  13.     std::cout << fib(16) << std::endl;
  14.  
  15.     return 0;
  16.  
  17. }

Factorial

  1. #include <iostream>
  2.  
  3. int fact(int x) {
  4.     if ( x == 1 ) return 1;
  5.     return x * fact(x - 1);
  6. }
  7.  
  8. int main() {
  9.     std::cout << fact(3) << std::endl;
  10.  
  11.     return 0;
  12. }

In Python (one liner):

  1. def fact(x): return (1 if x==0 else x * fact(x - 1))

Oct 31

Bubble Sort - Python

Yes, I am aware you can do:

  1. to_sort = [1, 5, 203, 21, 232, 94, 2928, 102, 10, 2, 28]
  2. to_sort.sort()

But, in the spirit of algorithm development, I have implemented my own Bubble sort.

It was quite easy, actually. Here’s the code:

  1. #!/usr/bin/env python
  2.  
  3. """
  4. Bubble sort
  5. """
  6.  
  7. to_sort = [1, 5, 203, 21, 232, 94, 2928, 102, 10, 2, 28]
  8.  
  9. def bubble_sort(lst):
  10.     move = 0
  11.     length = len(lst)
  12.     hit = 0
  13.  
  14.     while True:
  15.         if lst[move] > lst[move + 1]: # left greater than right?
  16.             tmp = lst[move]
  17.             lst[move] = lst[move + 1] # replace right with left
  18.             lst[move + 1] = tmp # replace left with right.
  19.             hit = 0 #reset the amount of items analyzed since last move.
  20.    
  21.         hit += 1 #increment hit, if this reaches length, the list is sorted.
  22.         move += 1 #increment list movement.
  23.  
  24.         if hit + 1 == length:
  25.             return lst
  26.  
  27.         if move + 1 >= length: # check to make sure we're not going to go out of bounds.
  28.             move = 0
  29.  
  30.  
  31. print bubble_sort(to_sort)

Oct 25

Linked List - Python

I’ve been watching a lot of the MIT Podcasts recently. They motivate me to get all sorts of things done.

Tonight, I wrote a Linked List implementation in Python. Even though there are built-in list data structures, it still helps to conceptually grasp the different types of lists.

Anyway, here it is:

  1. #! /usr/bin/env python
  2.  
  3. class node:
  4.     def __init__(self):
  5.         self.data = None # contains the data
  6.         self.next = None # contains the reference to the next node
  7.  
  8.  
  9. class linked_list:
  10.     def __init__(self):
  11.         self.cur_node = None
  12.  
  13.     def add_node(self, data):
  14.         new_node = node() # create a new node
  15.         new_node.data = data
  16.         new_node.next = self.cur_node # link the new node to the 'previous' node.
  17.         self.cur_node = new_node #  set the current node to the new one.
  18.  
  19.     def list_print(self,node=None):
  20.         node = ll.cur_node
  21.         while True:
  22.             try:
  23.                 print node.data
  24.                 node = node.next
  25.             except:
  26.                 return
  27.  
  28.  
  29. ll = linked_list()
  30. ll.add_node(1)
  31. ll.add_node(2)
  32. ll.add_node(3)
  33.  
  34. ll.list_print()

Sep 3

Picasa 3

Love it.

I’ll update this section later but I wanted to post a link to our new gallery.

I’ll be updating the photo section below with some of the ‘Best of’ snapshots.

Jun 26

Welcome, Toby!

Born: June 22, 2008, 4:50PM
Weight: 8lb 14oz
Length: 20 inches

baby boy!

Jun 21

ncurses, day1

I find the lack of documentation of Python + ncurses somewhat exciting, and somewhat irritating. I’ve decided the python project I am currently working on requires a CLI interface, and, thus, today I am beginning to write an implementation. I hope to share some code as it comes along.

I’ll be using this page as my reference. Hopefully it won’t let me down.

edit: Got some code together. This will display the contents of the current directory to the screen.

  1. import curses.wrapper
  2. import sys, time, os
  3.  
  4.  
  5. def main(stdscr):
  6.     stdscr.clear()
  7.     curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLUE)
  8.     index = 0
  9.     while 1:
  10.         list = os.listdir('.')
  11.         for x in range(0, len(list)):
  12.             if x == index:
  13.                 stdscr.attrset(curses.color_pair(1) | curses.A_BOLD)
  14.             else:
  15.                 stdscr.attrset(curses.color_pair(0))
  16.  
  17.             stdscr.addstr(x,0, list[x])
  18.  
  19.  
  20.         ch = stdscr.getch()
  21.         if ch == 32: return "%s/%s/" % (os.getcwd(),list[index]) #space bar
  22.         elif ch == 113: sys.exit(0) # q
  23.         elif ch == 259: index -= 1 # up arrow
  24.         elif ch == 258: index += 1 # down arrow
  25.         #print ch #useful for debugging key presses..
  26.  
  27.         stdscr.refresh()
  28.         time.sleep(0.1)
  29.  
  30.  
  31. if __name__=="__main__":
  32.     try: print curses.wrapper(main)
  33.     except Exception, e: print str(e)

Jun 12

ALSA - Configuring Audigy LS on Gentoo

I had to reinstall ALSA once again, and I had some problems along the way. It was mostly due to choosing the incorrect driver when building the Kernel. Surprisingly, in 2.6.24 there are 2 entries which list the Audigy. The emu10k1 driver is not the one we want to use.

Here are my kernel options:

Device Drivers  —>
Sound  —>
Sound card support
Advanced Linux Sound Architecture  —>
Advanced Linux Sound Architecture
   Sequencer support
     Sequencer dummy client
   OSS Mixer API
   OSS PCM (digital audio) API
[*]   OSS Sequencer API
   RTC Timer support
[*]     Use RTC as default sequencer timer
[*]   Support old ALSA API
PCI devices  —>
SB Audigy LS / Live 24bit

Here is my /etc/modules.d/alsa

alias char-major-116 snd
alias char-major-14 soundcore

alias snd-card-0 snd-ca0106
alias sound-slot-0 snd-card-0

alias sound-service-0-0 snd-mixer-oss
alias sound-service-0-1 snd-seq-oss
alias sound-service-0-3 snd-pcm-oss
alias sound-service-0-8 snd-seq-oss
alias sound-service-0-12 snd-pcm-oss

alias /dev/dsp snd-pcm-oss

options snd cards_limit=1

Lastly my addition to, /etc/modules.autoload.d/kernel-2.6

snd-ca0106

Get everything updated using

update-modules -f

Jun 7

Why OOP?

I get asked this quite a bit, people really don’t understand why I am such a big proponent of OOP.

I wish I had all of the time in the world to really, really write out the benefits of an OO architecture versus a procedural approach. For me, It’s all about maintainability and organization. That’s not to say procedural programming can’t be organized, but, I like to require others who use my code to be organized as well.

I can offer only an experience I had recently (which I face way more often than I should.) It has to do with Python but the same principles apply to PHP and Java as well.
At work I have written a testing framework for the automation of our product. The way it works is kind of simple. Each night we have a job that kicks off to build (and unit test) the code and publish it for us to work with and test the next day. Long ago we had a dilemma, the unit tests were passing but once we actually deployed the software some fatal areas were broken.

To remedy the situation, I came up with a tiny framework which grabs this build, configures, deploys, and executes some basic functionality tests to ensure our team is not wasting their time with the latest build.

The structure is like the following:

  • core/Product — API for different actions within the product
  • core/Database — Wrapper functions for Database calls ( we support 3 different databases, you can think of it as PDO)
  • core/TestSuite — Loads and executes tests.
  • core/Result - contains name of test, status, reference to log location
  • (more things related to the core utilities/necessities of the product)
  • tests/GetBuildTest
  • tests/DeployBuildTest
  • tests/FunctionalityArea1Test
  • (more tests)
  • run.py - loads the tests and executes them, displays the results.

Each test is actually extremely simple. 15-20 lines of code or less. They have a very strict contract that they must adhere to, in order for the execution to succeed.

Each test MUST

  • have an execute() method.
  • is in charge of logging it’s actions in the common log file
  • return it’s status.

Now that all of the hard work is done, a new requirement comes.
We need an HTML Report of each of the tests

If this were a procedural project, you’d go in to run.py, and start writing code to take the status to generate an html report. Simple enough. Not much maintenance, no need to touch anything else.

The status of the test is not enough, we actually would like to see the associated log messages (from the product) as well as the log messages for the framework when an error occurs.

Again, if this were a procedural process we’d potentially have to touch a lot of code, requiring a lot of testing and a longer turn around time to stability.

Instead, we could just plug in a reporting module. Something with a structure like:

  • reporting/Report — contain methods like: add_test(), get_tests()..
  • reporting/HTMLReport - generates the report in some pretty html.
  • reporting/BasicReport - prints the status of the report
  • reporting/EmailReport - emails a report to recipient_list

Each report type would have:

  • a generate() method — in charge of generating content however it wants to.

By providing a usable, yet extensible reporting interface, if a requirement comes along later to report a different way, I have isolated the work completely away from the presentation, such that the work will never have to be altered.
It’s late. This may or may not be coherent, but this is the type of maintenance I face every day. OOP works for me. What works for you?

Mar 30

Diving in to C++

I bought a book on Half.com the other day titled: A Laboratory Course in C++ by Nell Dale. My experience with C++ has always been minimal, and as I am developing more and more in interpreted languages, I thought it best to get back to basics and really learn. I have decided I am going to do 1 chapter / day, and for the past 2 days I have really stuck with it.

I really like the way it’s laid out, as well. Having a pre-lab to get you acquainted with the chapter, then a lab to learn the material, and a post-lab to extend your knowledge is a really cool concept and an approach I really learn best with.

I am also in the process of writing a front-end to mpd in C++ with my friend Glen. Socket programming is something that kind of intrigues me and kind of scares me. It’s open source (of course!) and progress can be monitored here

Mar 21

VIM Pasting

I switched from nano to vim recently. Happy with the results. One thing was really, really starting to get annoying, though.

There are times when I need to copy/paste a block of XML. Pasting XML directly was causing it to format really weird and it was a pain in the ass to get it properly formatted.

Using the P command was not working. I don’t know about you, but I don’t like counting how many lines, then hitting p, then pasting.

Anyway, the proper way to do it is:

  • Select and copy the block you need to copy

:set paste

  • Paste

No formatting!

Some extra content

  • here
  • here
  • here
  • here
  • here
  • here
  • here
  • here
  • here
  • here

Pictures

Some day I'll need to add some