Archives for 3 Sep,2009

You are browsing the site archives by date.

Python: crossword solver + dictionary file

This is a quick and dirty crossword solver that I wrote in python:

word=raw_input('Crossword Solver \nuse * as a wildcard: ')
f=open('dic.txt', 'r')
for line in f:
        if len(line)==len(word):
		for letter in word:
			if not letter=='*':
				if not letter==line[pos]:
		if good==1:
			print line

Example usage:

Crossword Solver
use * as a wildcard: *arn*val

The dictionary file I used is 608.2Kb with 80,368 english words and avaliable here

Read More

Python: diskspace

I wanted a simple function to use in a program I am writing to ensure that the disk isn’t getting full, after a quick search I found a blog post on with a great solution:

# os module required
import os

# retrieves information for the harddrive where root is mounted
# in windows replace this with "C:\" or the relevant drive letter
disk = os.statvfs("/")

# Information is recieved in numbers of blocks free
# so we need to multiply by the block size to get the space free in bytes
capacity = disk.f_bsize * disk.f_blocks
available = disk.f_bsize * disk.f_bavail
used = disk.f_bsize * (disk.f_blocks - disk.f_bavail) 

# print information in bytes
print used, available, capacity

# print information in Kilobytes
print used/1024, available/1024, capacity/1024

# print information in Megabytes
print used/1.048576e6, available/1.048576e6, capacity/1.048576e6

# print information in Gigabytes
print used/1.073741824e9, available/1.073741824e9, capacity/1.073741824e9

You can argue about if they should be KiB or KB if you want, but i take them as 1024 bytes in a kilobyte

Read More

acid3 tests firefox chrome konqueror arora on ubuntu x64 karmic

I preformed some acid3 tests on the browsers I currently had installed.

Chrome: It is sexy (only thing holding it back in Linux is the lack of flash support)
Score: 100/100


Opera: I used to use opera however not really any more, just installed it for this test.
Score: 100/100
Version: 10.00 (build 4585)


Arora: I don’t tend to use Arora, however I’ve heard alot of good things about it. It is a light-weight browser based on web-kit (as are chrome, safari and konqueror) and excels at its speed, highly recommended for notebooks which don’t need the bloat of firefox.
Score: 100/100 Linktest failed.


Firefox: It seems to handle flash better (without crashing) and has lots of useful plugins.
Score: 93/100
Version: 3.5.2

Score: 89/100 Linktest failed.
Version: 4.3.1

Google’s chrome comes first (with chrome having smoother and faster animation) followed by Opera and closely followed by Arora only failing on the link test, then firefox and trailing is konqueror. But I still love konqueror as its so convenient have filemanager and web-browser in one.

Read More

Python: Checking if a number is prime

I’ve been doing a lot of problem solving on Project Euler recently. If your not aware of Project Euler (PE) it is a website with lots of maths / programming based puzzles to solve. Many of the problems involve using or checking prime numbers, in many cases a sieve method would be applicable (see: Sieve of Eratosthenes for a clear explanation / animation).

However sometimes this is not necessary if you only require one prime number, or if memory limitations mean a sieve would be inconceivable. The following is the code I wrote to check if a number is prime or not in python. It tests upwards checking if the number is perfectly divisble, and as it does this it lowers the maximum number need to reach to stop as we know that if a number is not divisible by 2 then it will not be uniquely divisible (a repatition of a divislbe may exist) by anything greater than N/2

def isprime(number):
	if number<=1:
		return 0
	while check<maxneeded+1:
		if number%check==0:
			return 0
	return 1

I hope that this made sense, and is useful to someone. If anyone has any more efficient methods I would be happy to hear from you.

Check out my profile to see which problems I’ve completed. If you’ve completed any I haven’t please get in contact and give me some tips.

Mike sent a suggestion that I could speed up the program by ignoring all factors of 2, it could also propbably be sped up by looking at 3,4,5 .. etc until certain point.

def isprime(number):
	if number<=1 or number%2==0:
		return 0
	while check<maxneeded+1:
		if number%check==0:
			return 0
	return 1

So lets test the speed difference by doing 1,000 checks of the number 982,451,653 (known to be prime from this usefulsite)

Original Code: 9.99917411804 Seconds elapsed
Improved Code: 5.2977039814 Seconds elapsed

That’s approximately a factor of two speed increase, but this makes me think that a combination of the sieve method with this one may lead to an even further increase.

Read More

Partitions and disk space in linux

I’m sure there are many ways to see the partitions and corresponding diskspace in linux, but here is the method I found:

$df -m
Filesystem 1M-blocks Used Available Use% Mounted on
/dev/sda2 7513 5937 1576 80% /
tmpfs 1005 0 1005 0% /lib/init/rw
varrun 1005 1 1005 1% /var/run
varlock 1005 0 1005 0% /var/lock
udev 1005 1 1005 1% /dev
tmpfs 1005 1 1005 1% /dev/shm
/dev/sdc1 469453 353295 92311 80% /media/x
/dev/sdd1 1408345 979205 429141 70% /media/z

or visually:

df -m screenshot

I kept struggeling to remember what letters it was, as there are many two letter commands beginning with d in linux… (dc, dd, df, dh, do, dl, du). I now remember it, because ‘f’ is next to the ‘d’ key on my keyboard, and because df probably stands for disk-free.

For more information check out this site

Read More