The second sorting algorithm I decided to learn was the QuickSort. It’s a little more complicated than the StraightSort, but generally faster for larger numbers of integers.
(more…)
Archive for the ‘Code’ Category
Sorting Algorithms, Part Two
Friday, April 24th, 2009Sorting Algorithms, Part One
Thursday, April 23rd, 2009Inspired by a question from a programming interview, I’ve taken it upon myself to learn how to implement different sorting algorithms in Python. My goal is to:
- Learn a bit more about programming algorithms, and
- Get back into Python-coding shape.
Taking the C++ Challenge
Friday, May 30th, 2008This week I’ve decided to roll up my programming sleeves and finally learn how to program in C++. I’ve been avoiding it for over a decade, so cracking open my copy of the C++ Primer took some courage.
To my surprise, my recent forays into Python and Java have prepared me pretty well for what I’ve encountered so far. I’m 250 pages into the book, and though some elements of the language seem strange to me (why would you use arrays and pointers when you’ve got vectors?), nothing has flown over my head.
That could change as I get deeper into the language.
If any of you are familiar with C++, is there anything I should look out for, or pay special attention to?
Fixing Filenames
Tuesday, January 22nd, 2008I use SoundJuicer to rip my music CDs into Ogg files, and play them in Rhythmbox.
Both programs work great, with the exception of stripping special characters–like ‘,’ ‘<’ or ‘#’–from filenames. SoundJuicer has an option marked “Strip Special Characters,” but it doesn’t always work. Some special characters–anything with an accent mark above it, for instance–it won’t ever strip. Since Rhythmbox chokes on filenames with special characters, you have to change the filename before it’ll play the file.
This is tedious to do manually, so I wrote a Python script to do it for me. It’s not very elegant, but it gets the job done: invoke it from the command line followed by a path to the directory containing the files you need stripped of special characters, and it takes care of the rest.
Here’s the code:
#!/usr/bin/python
# This is a simple python program to look through a directory and
# strip special characters from the filenames in that directory. I use
# it mostly to fix filenames after importing music from CDs.# Invoke this program from the command line followed by the path to the directory
# containing the filenames you want fixed.
#
# Copyright (C) 2008 Ron Toland
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# long with this program. If not, see <http://www.gnu.org/licenses/>.import os, sys
def namefixer(dummy, dirname, filesindir):
for fname in filesindir:
newname = fname.replace(’ ‘, ‘_’)
newname2 = newname.replace(’,', ”)
newname3 = newname2.replace(’+', ‘_’)
newname4 = newname3.replace(’-', ‘_’)
newname5 = newname4.replace(’#', ‘no’)
newname6 = newname5.replace(’>’, ‘gt’)
newname7 = newname6.replace(’<’, ‘lt’)
os.rename(os.path.join(dirname, fname), os.path.join(dirname, newname7))if __name__ == ‘__main__’:
os.path.walk(sys.argv[1], namefixer, None)
Save this file as “fixer.py” somewhere in your home directory, then use “chmod a+x fixer.py” to make it executable. To make it extra easy to use, create a link like this:
“sudo ln -s <full path to fixer.py> /usr/local/bin/namefixer”
That’ll create a link file in your usr/local/bin folder that’ll let you invoke the program from any directory just by typing: “namefixer <path to directory you want fixed>“
I’m still working on getting fixer.py to get rid of accented characters. When I figure it out, I’ll post the corrected code. Should any of you, dear readers, get it working first, please let me know!