<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>stealthcopter.com &#187; python</title>
	<atom:link href="http://www.stealthcopter.com/blog/tag/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.stealthcopter.com/blog</link>
	<description>Android, Linux, Python and stealthcopters</description>
	<lastBuildDate>Sat, 24 Jul 2010 00:01:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Installing PyCuda on ubuntu 10.04</title>
		<link>http://www.stealthcopter.com/blog/2010/07/installing-pycuda-on-ubuntu-10-04/</link>
		<comments>http://www.stealthcopter.com/blog/2010/07/installing-pycuda-on-ubuntu-10-04/#comments</comments>
		<pubDate>Thu, 08 Jul 2010 10:30:43 +0000</pubDate>
		<dc:creator>mat</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[CUDA]]></category>

		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=918</guid>
		<description><![CDATA[Using graphics cards to hand massively parallel tasks can now be realised in python, thanks to PyCuda a module for python which allows interaction with the CUDA libaries/binaries provided by Nvidia. To get this working in ubuntu 10.04 lucid lynx I followed this guide http://wiki.tiker.net/PyCuda/Installation/Linux/Ubuntu. Prep / Installation: First install CUDA, most places will tell [...]]]></description>
			<content:encoded><![CDATA[<p>Using graphics cards to hand massively parallel tasks can now be realised in python, thanks to PyCuda a module for python which allows interaction with the CUDA libaries/binaries provided by Nvidia. To get this working in ubuntu 10.04 lucid lynx I followed this guide <a href="http://wiki.tiker.net/PyCuda/Installation/Linux/Ubuntu">http://wiki.tiker.net/PyCuda/Installation/Linux/Ubuntu</a>.</p>
<p><strong>Prep / Installation:</strong></p>
<p>First install CUDA, most places will tell you that CUDA is incompatible with gcc-4.3 but this is not true if you make a few small changes to the configuration.mk file, please see <a href="http://www.stealthcopter.com/blog/2010/02/compiling-and-running-cuda-2-3-sdk-and-toolkit-on-ubuntu-9-10-x64-64-bit/">this post about installing CUDA in ubuntu</a></p>
<p>Install the required packages (or use package manager):</p>
<blockquote><p>
sudo apt-get install python-numpy -y<br />
sudo apt-get install build-essential python-dev python-setuptools libboost-python-dev -y
</p></blockquote>
<p>Download pycuda from: <a href="http://pypi.python.org/pypi/pycuda">http://pypi.python.org/pypi/pycuda</a></p>
<p>Untar the achive:</p>
<blockquote><p>tar xzvf pycuda-0.94rc.tar.gz</p></blockquote>
<p>Configure, make and install:</p>
<blockquote><p>cd pycuda-0.94rc<br />
./configure.py &#8211;cuda-root=/usr/local/cuda &#8211;cudadrv-lib-dir=/usr/lib &#8211;boost-inc-dir=/usr/include &#8211;boost-lib-dir=/usr/lib &#8211;boost-python-libname=boost_python-mt &#8211;boost-thread-libname=boost_thread-mt<br />
make -j 4<br />
sudo python setup.py install</p></blockquote>
<p><strong>Problems</strong></p>
<p>I had some problems with the compliation because it was complaining about pytools missing, this was resolved by removing and reinstalling python-setuptools:</p>
<blockquote><p>sudo apt-get remove python-setuptools<br />
sudo apt-get install python-setuptools</p></blockquote>
<p><strong>Running the example</strong></p>
<p>You should now be able to run the <em>hellogpu.py</em> demo in the examples folder and use the <em>download-examples-from-wiki.py</em> to download further demos from the pycuda wiki.</p>
<p><strong>Next</strong><br />
I plan to learn how to use PyCuda and aim to make a MD5 cracker as discussed previously (<a href="http://www.stealthcopter.com/blog/2010/06/cracking-md5-hashes-or-passwords-ultra-fast-with-gpu-acceleration/">here</a> and <a href="http://www.stealthcopter.com/blog/2010/06/cracking-real-world-salted-md5-passwords-in-python-with-several-dictionaries/">here</a>)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stealthcopter.com/blog/2010/07/installing-pycuda-on-ubuntu-10-04/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cracking real world salted MD5 passwords in python with several dictionaries</title>
		<link>http://www.stealthcopter.com/blog/2010/06/cracking-real-world-salted-md5-passwords-in-python-with-several-dictionaries/</link>
		<comments>http://www.stealthcopter.com/blog/2010/06/cracking-real-world-salted-md5-passwords-in-python-with-several-dictionaries/#comments</comments>
		<pubDate>Mon, 28 Jun 2010 10:45:56 +0000</pubDate>
		<dc:creator>mat</dc:creator>
				<category><![CDATA[cryptography]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[cracking]]></category>
		<category><![CDATA[hacking]]></category>
		<category><![CDATA[website]]></category>

		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=917</guid>
		<description><![CDATA[Recently a friend (who will remain unnamed for obvious reasons) asked me to penetration test a website he created. I found a very simple exploit where I could upload an avatar but the file was not checked to ensure it was an image, so I uploaded a php script I wrote an began exploring the [...]]]></description>
			<content:encoded><![CDATA[<p>Recently a friend (who will remain unnamed for obvious reasons) asked me to penetration test a website he created. I found a very simple exploit where I could upload an avatar but the file was not checked to ensure it was an image, so I uploaded a php script I wrote an began exploring the server. I printed out all of the usernames, passwords and salts from the database to see how many of the 1,109 passwords could be easily cracked.</p>
<p>The passwords were stored as MD5 hashes with a random 6 character alphanumeric salt. To create the MD5 hash of the password the salt was prefixed to the password and then the combination was hashed. Thanks to this method we can employ a simple bruteforce/dictionary attack on the passwords. I will start with the wordlists creation, then results I obtained to keep your interest, and finally show my python code.</p>
<p><strong>Creating wordlists</strong><br />
I already has two reasnoble sized dictionaries that I use for different things like <a href="http://www.stealthcopter.com/wordcube">wordcube</a>. I used john the ripper on my double sized dictionary to create lots of common permutations on words, such as captial first letter, and a number affixed to the end. To do this you run john with the following parameters, where dic.txt is the input dictionary and dic_plus_rules.txt is the output from john with all of the additions it has made.</p>
<blockquote><p>
john &#8211;wordlist=dic.txt &#8211;rules &#8211;stdout > dic_plus_rules.txt
</p></blockquote>
<p>I also download two wordlists from <a href="http://download.openwall.net/pub/wordlists/">openwall</a>, one which is a list of ~3100 <a href="http://download.openwall.net/pub/wordlists/passwords/">common passwords</a>, and one labelled <a href="http://download.openwall.net/pub/wordlists/">ALL</a> that has a large amount of words (~4 million) in various languages. Because of the highly compressible nature of text the files are available in small gzip files. ALL is 11.5Mb which unzips to 41.4Mb and password 12kb which unzips to 21.8kb. There are also more <a href="http://download.openwall.net/pub/wordlists/languages/">wordlists avaliable for different languages</a>, but the ALL file includes these.</p>
<p>The size of all of the wordlists I used is shown below:</p>
<table>
<tr>
<th>Dictionary</th>
<th>Combinations</th>
</tr>
<tr>
<td>English</td>
<td>42,987</td>
</tr>
<tr>
<td>Double-English</td>
<td>80,368</td>
</tr>
<tr>
<td>Double+john-rules</td>
<td>3,986,706</td>
</tr>
<tr>
<td>Openwall Common Passwords</td>
<td>3,158</td>
</tr>
<tr>
<td>Openwall ALL</td>
<td>3,917,116</td>
</tr>
</table>
<p><strong>Results</strong></p>
<table>
<tr>
<th>Dictionary</th>
<th>Cracked</th>
<th>Percentage</th>
<th>Time</th>
</tr>
<tr>
<td>English</td>
<td>60</td>
<td>5.41%</td>
<td>80s</td>
</tr>
<tr>
<td>Double-English</td>
<td>65</td>
<td>5.86%</td>
<td>170s</td>
</tr>
<tr>
<td>Double+john-rules</td>
<td>116</td>
<td>10.46%</td>
<td>2.5hrs (8393s)</td>
</tr>
<tr>
<td>Openwall Common Passwords</td>
<td>112</td>
<td>10.10%</td>
<td>7s</td>
</tr>
<tr>
<td>Openwall All</td>
<td>210</td>
<td>18.94%</td>
<td>2.45hrs (8829s)</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</td>
<tr>
<td>Total Passwords Obtained</td>
<td>254</td>
<td>22.90%</td>
<td>~5hrs</td>
</tr>
</table>
<p><strong>Comical passwords</strong></p>
<p>Here are some of the more amusingly bad passwords, the number in brackets shows the frequency of the password.</p>
<p><em><strong>Crap passwords</strong></em>: 123456 (18), password (4), 1234567 (4), 123456789 (3) 12345678 (2),  12345 (2), abc123 (2), asdfgh (2), nintendo (2), 123123, abcd1234, abcdefg, qwerty<br />
<em><strong>Self-describing passwords</strong></em>: catholic, cowboy, creator, doger, ginger, killer, maggot, player, princess, skater, smallcock, smooth, super, superman, superstar, tester, veggie, winner, wolverine<br />
<em><strong>Some other passwords</strong></em>:bananas, cheese, cinnamon, hampster ,DRAGON, dribble1, poopie, poopoo</p>
<p><strong>Python Program</strong></p>
<pre name="code" class="python">
# -*- coding: utf-8 -*-
#pymd5cracker.py
import hashlib, sys
from time import time

# Change to commandline swtiches when you have the time!
hash = ""
hash_file = "hash2.csv"
wordlist = "mass_rules.txt"; 

# Read the hash file entered
try:
	hashdocument = open(hash_file,"r")
except IOError:
	print "Invalid file."
	raw_input()
	sys.exit()
else:
	# Read the csv values seperated by colons into an array
	hashes=[]
	for line in hashdocument:
		line=line.replace("\n","")
		inp = line.split(":")
		if (line.count(":")<2):
			inp.append("")
		hashes.append(inp)
	hashdocument.close();

# Read wordlist in
try:
	wordlistfile = open(wordlist,"r")
except IOError:
	print "Invalid file."
	raw_input()
	sys.exit()
else:
	pass

tested=0
cracked=0
tic = time()
for line in wordlistfile:

	line = line.replace("\n","")
	tested+=1
	for i in range(0,len(hashes)):

		m = hashlib.md5()
		m.update(hashes[i][2]+line)
		word_hash = m.hexdigest()
		if word_hash==hashes[i][1]:
			toc = time()
			cracked+=1
			hashes[i].append(line)
			print hashes[i][0]," : ", line, "\t(",time()-tic,"s)"

	# Show progress evey 1000 passwords tested
	if tested%1000==0:
		print "Cracked: ",cracked," (",tested,") ", line

# Save the output of this program so we can use again
# with another program/dictionary adding the password
# to each line we have solved.
crackout = open("pycrackout.txt","w")
for i in hashes:
	s=""
	for j in i:
		if s!="":
			s+=":"
		s+=j
	s+="\n"
	crackout.write(s)
crackout.close()

print "Passwords found: ",cracked,"/",len(hashes)
print "Wordlist Words :", test
print "Hashes computed: ",len(hashes)*tested
print "Total time taken: ",time()-tic,'s'
</pre>
<p><strong>Next</strong></p>
<ul>
<li>Play with more dictionaries</li>
<li>Speed up code:
<ul>
<li>Add multi-threading: My experience with multi-threading in python is that it doesn't work well for cpu intensive tasks, if you know otherwise please let me know.</li>
<li>Have a look at <a href="http://mathema.tician.de/software/pycuda">PyCUDA</a> to see if I can use my graphics card to speed up the code significantly (another type of mutli-threading really...) without having to change language like in my previous post of <a href="http://www.stealthcopter.com/blog/2010/06/cracking-md5-hashes-or-passwords-ultra-fast-with-gpu-acceleration/">CUDA MD5 cracking</a></li>
</ul>
</li>
<li>Remove hash once found to stop pointless checking</li>
<li>Add command line switches to all it to be used like a real program</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.stealthcopter.com/blog/2010/06/cracking-real-world-salted-md5-passwords-in-python-with-several-dictionaries/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>How to create a cryptogram in python (random substitution cipher)</title>
		<link>http://www.stealthcopter.com/blog/2010/04/how-to-create-a-cryptogram-in-python-random-substitution-cipher/</link>
		<comments>http://www.stealthcopter.com/blog/2010/04/how-to-create-a-cryptogram-in-python-random-substitution-cipher/#comments</comments>
		<pubDate>Tue, 20 Apr 2010 15:20:52 +0000</pubDate>
		<dc:creator>mat</dc:creator>
				<category><![CDATA[cryptography]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=859</guid>
		<description><![CDATA[Cryptograms are enjoyable puzzles created from a saying or phrase encrypted with a substitutional cipher. They can be fun to decipher by hand by looking for common letter combinations, doublets, guesswork, and other flaws in this encryption mechanism. I wrote a quick python script which will accept an input text and create a random substitutional [...]]]></description>
			<content:encoded><![CDATA[<p>Cryptograms are enjoyable puzzles created from a saying or phrase encrypted with a substitutional cipher. They can be fun to decipher by hand by looking for common letter combinations, doublets, guesswork, and other flaws in this encryption mechanism.</p>
<p>I wrote a quick python script which will accept an input text and create a random substitutional cipher and encrypt it. It then outputs the cipher alphabet and the encrypted text.</p>
<p><strong>Source code:</strong></p>
<pre name="code" class="python">
# -*- coding: utf-8 -*-
import sys
from random import randint
from string import maketrans

if (len(sys.argv)>1):
	# Normal alphabet
	alphabet="abcdefghijklmnopqrstuvwxyz"

	# Randomly create a new cipherbet
	cipherbet=""
	left=alphabet
	for i in range(0,len(alphabet)):
		x=randint(0,len(left)-1)
		cipherbet+=left[x]
		left=left[:x]+left[x+1:]

	# Get input text to translate
	text=sys.argv[1].lower()

	trantab = maketrans(alphabet,cipherbet)
	text=text.translate(trantab)

	# Replace unused letters in cipherbet with _'s
	for i in cipherbet:
		if i not in text:
			cipherbet=cipherbet.replace(i,"_")

	# Print cipherbet (solution) and the text (cryptogram)
	print cipherbet
	print text
</pre>
<p><strong>Example usage</strong></p>
<blockquote><p>
python create_cipher.py &#8220;The Science gets done. And you make a neat gun. For the people who are still alive.&#8221;<br />
b_lpievrm_acqxuj_fzdgwn_o_<br />
dri zlmixli vidz puxi. bxp oug qbai b xibd vgx. euf dri jiujci nru bfi zdmcc bcmwi.
</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.stealthcopter.com/blog/2010/04/how-to-create-a-cryptogram-in-python-random-substitution-cipher/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Saving settings in python with YAML (an XML alternative)</title>
		<link>http://www.stealthcopter.com/blog/2010/02/saving-settings-in-python-with-yaml-an-xml-alternative/</link>
		<comments>http://www.stealthcopter.com/blog/2010/02/saving-settings-in-python-with-yaml-an-xml-alternative/#comments</comments>
		<pubDate>Thu, 25 Feb 2010 12:03:08 +0000</pubDate>
		<dc:creator>mat</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[yaml]]></category>

		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=770</guid>
		<description><![CDATA[When transferring information or settings between computers and programs is is useful to use something simple and program independent. For a recent project I need to send a settings file between a PHP script and a python program, I needed something that would support trees so an INI file was out of the question, and [...]]]></description>
			<content:encoded><![CDATA[<p>When transferring information or settings between computers and programs is is useful to use something simple and program independent. For a recent project I need to send a settings file between a PHP script and a python program, I needed something that would support trees so an INI file was out of the question, and I naturally thought of using XML, however when looking for more information I stumbled upon YAML (seemingly forgotten alternative to XML).</p>
<p><a href="http://www.yaml.org/spec/1.2/spec.html">YAML</a> is a recursive acronym that stands for YAML Ain&#8217;t Markup Language and below is a simplified example taken from the YAML site. The layout is similar to python with indents representing the nested layers. Semicolons are used to seperate variable names and value pairs.</p>
<pre name="code" class="python">
name   : Joe Bloggs
product:
    - sku         : BL394D
      quantity    : 4
      description : Basketball
      price       : 450.00
    - sku         : BL4438H
      quantity    : 1
      description : Super Hoop
      price       : 2392.00
tax  : 251.42
total: 4443.5
</pre>
<p>To use YAML in python you will need the PyYAML library avaliable <a href="http://pyyaml.org/wiki/PyYAML">here</a> or in via your package manager in linux. (I.E. &#8220;sudo apt-get install http://pyyaml.org/wiki/PyYAML&#8221; in ubuntu/debian).</p>
<p>The code for then using YAML is very simple, first we import the yaml library, then we open our settings file we have created and then use yaml&#8217;s load feature to load our settings into a python dictionary, and then finally printing it out so we can see what it contains</p>
<pre name="code" class="python">
import yaml
f=open('settings.cfg')
settings=yaml.load(f)
print settings
</pre>
<p>Using the example YAML file above saved to settings.cfg this python script will output the following when run:</p>
<pre name="code" class="python">
{'product': [{'sku': 'BL394D', 'price': 450.0, 'description':
'Basketball', 'quantity': 4}, {'sku': 'BL4438H', 'price': 2392.0,
'description': 'Super Hoop', 'quantity': 1}], 'total': 4443.5, 'tax':
251.41999999999999, 'name': 'Joe Bloggs'}
</pre>
<p>We can now access all the information in the file very easily (I.E. settings['name'] will give us the name Joe Bloggs).</p>
<p>YAML supports much much more than what is discussed here and further information can be found on the YAML website.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stealthcopter.com/blog/2010/02/saving-settings-in-python-with-yaml-an-xml-alternative/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Advancing PyMan (using python&#8217;s pygame to recreate pacman)</title>
		<link>http://www.stealthcopter.com/blog/2010/02/advancing-pyman-using-pythons-pygame-to-recreate-pacman/</link>
		<comments>http://www.stealthcopter.com/blog/2010/02/advancing-pyman-using-pythons-pygame-to-recreate-pacman/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 22:25:32 +0000</pubDate>
		<dc:creator>mat</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[games]]></category>

		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=201</guid>
		<description><![CDATA[A while back I followed a few tutorials on creating a pacman game in python using pygame. I took the resulting code from these tutorials and added several enhancements. Unfortunately I haven&#8217;t had enough time to continue with the code, so hopefully this code is of use to someone learning python to play with and [...]]]></description>
			<content:encoded><![CDATA[<p>A while back I followed a few tutorials on creating a pacman game in python using pygame. I took the resulting code from these tutorials and added several enhancements. Unfortunately I haven&#8217;t had enough time to continue with the code, so hopefully this code is of use to someone learning python to play with and further enhance (Who knows might end up feature complete!).</p>
<div id="attachment_714" class="wp-caption aligncenter" style="width: 518px"><a href="http://www.stealthcopter.com/blog/2010/02/advancing-pyman-using-pythons-pygame-to-recreate-pacman/matpan/" rel="attachment wp-att-714"><img src="http://www.stealthcopter.com/blog/wp-content/uploads/2010/02/matpan.png" alt="Screen shot of PyMan in action" title="Screen shot of PyMan in action" width="508" height="606" class="size-full wp-image-714" /></a><p class="wp-caption-text">Screen shot of PyMan in action</p></div>
<p><strong>Enhancements over original code</strong></p>
<ul>
<li>start menu</li>
<li>sounds</li>
<li>multiple ghosts</li>
<li>not having to hold down a directional button constantly</li>
<li>can press a direction before a turn and will turn at next opertunity (like real pacman)</li>
<li>probably more stuff that I&#8217;ve forgotten about by now</li>
</ul>
<p><strong>Source</strong><br />
<a href="http://www.stealthcopter.com/files/python/matpan.zip">Source Files</a> &#8211; The source and files, to run the game just run &#8220;python PyMan.py&#8221;</p>
<p><strong>References</strong><br />
I believe the site where I got the original code from was <a href="http://www.learningpython.com">learningpython.com</a> (not 100% sure) and the following links to the tutorials:<br />
<a href="http://www.learningpython.com/2006/03/12/creating-a-game-in-python-using-pygame-part-one/">Tutorial 1</a><br />
<a href="http://www.learningpython.com/2006/03/19/creating-a-game-in-python-using-pygame-part-two-creating-a-level/">Tutorial 2</a><br />
<a href="http://www.learningpython.com/2006/04/16/creating-a-game-in-python-using-pygame-part-3-adding-the-bad-guys/">Tutorial 3</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.stealthcopter.com/blog/2010/02/advancing-pyman-using-pythons-pygame-to-recreate-pacman/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python: interfacing with an arduino</title>
		<link>http://www.stealthcopter.com/blog/2010/02/python-interfacing-with-an-arduino/</link>
		<comments>http://www.stealthcopter.com/blog/2010/02/python-interfacing-with-an-arduino/#comments</comments>
		<pubDate>Wed, 03 Feb 2010 11:42:56 +0000</pubDate>
		<dc:creator>mat</dc:creator>
				<category><![CDATA[arduino]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[Electronics]]></category>

		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=204</guid>
		<description><![CDATA[So what is an arduino? An arduino is an open source open hardware programmable controller with several inputs and outputs. The image below shows an Ardunio Dicemella. It (Arduino Dicemella) has 14 digital input/output pins (of which 6 can be used as PWM outputs), 6 analog inputs, a 16 MHz crystal oscillator, a USB connection, [...]]]></description>
			<content:encoded><![CDATA[<p><strong>So what is an arduino?</strong><br />
An <a href="http://www.arduino.cc/">arduino</a> is an open source open hardware programmable controller with several inputs and outputs. The image below shows an <a href="http://www.arduino.cc/en/Main/ArduinoBoardDiecimila">Ardunio Dicemella</a>.</p>
<div id="attachment_472" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.stealthcopter.com/blog/wp-content/uploads/2009/12/arduino.jpg"><img src="http://www.stealthcopter.com/blog/wp-content/uploads/2009/12/arduino-300x172.jpg" alt="Ardunio Dicemella Annotated Photo" title="Ardunio Dicemella Annotated Photo" width="300" height="172" class="size-medium wp-image-472" /></a><p class="wp-caption-text">Ardunio Dicemella Annotated Photo</p></div>
<blockquote><p>It (Arduino Dicemella) has 14 digital input/output pins (of which 6 can be used as PWM outputs), 6 analog inputs, a 16 MHz crystal oscillator, a USB connection, a power jack, an ICSP header, and a reset button. It contains everything needed to support the microcontroller; simply connect it to a computer with a USB cable or power it with a AC-to-DC adapter or battery to get started.</p></blockquote>
<p>They are very useful for people who know how to program but have little experience with hardware interaction.</p>
<p><strong>Programming the arduino</strong><br />
This post will not contain in-depth detail on how to program the arduino, instead focussing briefly on setting up serial (over serial or usb cable) communications in order to talk to a python script. The arduino can be programmed via a IDE provided by the creators in a C-style hardware language.</p>
<p><strong>Code example</strong></p>
<pre name="code" class="c">
int ledPin = 13;            // choose the pin for the LED
int inputPin = 2;          // choose the input pin (for a pushbutton)
int val = 0;                // variable for reading the pin status

void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare pushbutton as input
}

void loop(){
  val = digitalRead(inputPin);  // read input value
  if (val == HIGH) {            // check if the input is HIGH
    digitalWrite(ledPin, HIGH);  // turn LED ON
  } else {
    digitalWrite(ledPin, LOW); // turn LED OFF
  }
}
</pre>
<div id="attachment_596" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.stealthcopter.com/blog/wp-content/uploads/2010/01/arduino1.jpg"><img src="http://www.stealthcopter.com/blog/wp-content/uploads/2010/01/arduino1-300x170.jpg" alt="Arduino LED switch circuit off" title="Arduino LED switch circuit off" width="300" height="170" class="size-medium wp-image-596" /></a><p class="wp-caption-text">Arduino LED switch circuit off</p></div>
<div id="attachment_598" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.stealthcopter.com/blog/wp-content/uploads/2010/01/arduino2.jpg"><img src="http://www.stealthcopter.com/blog/wp-content/uploads/2010/01/arduino2-300x170.jpg" alt="Arduino LED switch circuit on" title="Arduino LED switch circuit on" width="300" height="170" class="size-medium wp-image-598" /></a><p class="wp-caption-text">Arduino LED switch circuit on</p></div>
<p>Now we add a few lines to enable the writing of information from our arduino over the serial connection. We first need to set up the transfer speed in our setup (<em>Serial.begin(9600);</em>). Then we can simply send messages over serial using <em>Serial.print(&#8220;message\n&#8221;);</em>. You can choose between print and println with the difference been that the latter automatically appends the newline char, so we would use the former to write multiple things to the same line. Below is our modified code:</p>
<p><strong>Serial write example</strong></p>
<pre name="code" class="c">
int ledPin = 13;           // choose the pin for the LED
int inputPin = 2;         // choose the input pin (for a pushbutton)
int val = 0;               // variable for reading the pin status

void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare pushbutton as input
  Serial.begin(9600);
  Serial.print("Program Initiated\n");
}

void loop(){
  val = digitalRead(inputPin);  // read input value
  if (val == HIGH) {            // check if the input is HIGH
    digitalWrite(ledPin, HIGH);  // turn LED ON
    Serial.print("LED Activated\n");
  } else {
    digitalWrite(ledPin, LOW); // turn LED OFF
  }
}
</pre>
<p>We now add into this code the ability to receive information via serial. Below is the modified example which removes the action of the button and replaces it by activating the LED when &#8216;Y&#8217; is sent via serial. </p>
<p><strong>Serial read example</strong></p>
<pre name="code" class="c">
int ledPin = 13;  // choose the pin for the LED
int val = 0;      // variable for reading the pin status
char msg = '  ';   // variable to hold data from serial

void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  Serial.begin(9600);
  Serial.print("Program Initiated\n");
}

void loop(){
        // While data is sent over serial assign it to the msg
	while (Serial.available()>0){
		msg=Serial.read();
	}

  // Turn LED on/off if we recieve 'Y'/'N' over serial
  if (msg=='Y') {
    digitalWrite(ledPin, HIGH);  // turn LED ON
    Serial.print("LED Activated\n");
    msg=' ';
  } else if (msg=='N') {
    digitalWrite(ledPin, LOW); // turn LED OFF
  }
}
</pre>
<p><strong>Interaction with python</strong></p>
<p>First we import the serial library to python in order to communicate with the arduino (this includes talking over usb).</p>
<pre name="code" class="python">import serial</pre>
<p>We then attempt to connect to our arduino on /dev/ttyUSB0, using try and except to catch an exception if we are unable to find the arduino on USB0. The 9600 corresponds to the baud rate (speed of communication) that we are using with the arduino and should be the same as set in the program on the arduino otherwise your communication may appear garbled.</p>
<pre name="code" class="python">try:
	arduino = serial.Serial('/dev/ttyUSB0', 9600)
except:
	print "Failed to connect on /dev/ttyUSB0"
</pre>
<p>The address will be /dev/ttyUSB# where # is replaced by a number for arduinos connected via usb and /dev/ttyS# where # is replaced by a number for arduinos connected via serial. If you are not sure of the location of your arduino, it can be found in the arduino IDE or you can write some python to scroll through possible locations until a response is found</p>
<pre name="code" class="python">
locations=['/dev/ttyUSB0','/dev/ttyUSB1','/dev/ttyUSB2','/dev/ttyUSB3',
'/dev/ttyS0','/dev/ttyS1','/dev/ttyS2','/dev/ttyS3']

for device in locations:
	try:
		arduino = serial.Serial(device, 9600)
	except:
		print "Failed to connect on",device
</pre>
<p>You may need to be careful as other devices can be connected. For example if I try to connect to /dev/ttyS0 I will connect to the wacom tablet on my laptop.</p>
<p>Once you have connected to your arduino successfully you can write information to it using write and read information sent from it using read (you will need to import time to use the sleep function). If your arduino does not send any messages via serial then attempting to readline will result in your program hanging until it receives a message.</p>
<pre name="code" class="python">
try:
	arduino.write('Y')
	time.sleep(1)
	print arduino.readline()
except:
	print "Failed to send!"
</pre>
<p>So the python code should now look like the following and we should be able to control the LED over serial.</p>
<pre name="code" class="python">
import serial
import time

locations=['/dev/ttyUSB0','/dev/ttyUSB1','/dev/ttyUSB2','/dev/ttyUSB3',
'/dev/ttyS0','/dev/ttyS1','/dev/ttyS2','/dev/ttyS3']  

for device in locations:
	try:
		print "Trying...",device
		arduino = serial.Serial(device, 9600)
		break
	except:
		print "Failed to connect on",device   

try:
    arduino.write('Y')
    time.sleep(1)
    print arduino.readline()
except:
    print "Failed to send!"
</pre>
<p>The above will send the character &#8216;Y&#8217; (Y for Yes please turn on the LED) to the arduino wait for 1 second and then read from the arduino which will have hopefully posted a response to our &#8216;Y&#8217;. Using the program on this should turn the LED on, and report <em>LED Activated</em> back via serial to our python program. This should be enough for people to get started with ardunios and communicating with them in python.</p>
<p><strong>References</strong></p>
<ul>
<li><a href="http://www.arduino.cc/">Arduino</a> &#8211; The arduino website with everything you are likely to need (programming examples and reference guide, and hardware information)</li>
<li>
<a href="http://www.ladyada.net/learn/arduino/">Arduino tutorial </a> &#8211;  a basic and easy to understand tutorial on programming the arduino
</li>
<li><a href="http://lemonodor.com/archives/2008/02/arduino_serial.html/">Python port of arduino-serial.c</a> &#8211; By John Wiseman from which I based my program.</li>
<li><a href="http://todbot.com/blog/2006/12/06/arduino-serial-c-code-to-talk-to-arduino/">original arduino-serial.c</a> &#8211; by Tod E. Kurt.</li>
<li><a href="http://www.sparkfun.com">Sparkfun</a> &#8211; Here is a good place to purchase ardunio and other electronics parts. Try <a href="http://www.coolcomponents.co.uk">coolcomponents</a> if your from the uk like me</li>
<li><a href="http://www.dealextreme.com/default.dx/r.27613104">Dealextreme</a> &#8211; Hong Kong based retailer that sells a lot of cheap DIY electronics and also has worldwide free delivery with no min spend (crazy). Does take about two weeks to arrive though (uk).</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.stealthcopter.com/blog/2010/02/python-interfacing-with-an-arduino/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Python: Cryptography decoding a Caesar shift (frequency analysis)</title>
		<link>http://www.stealthcopter.com/blog/2010/01/python-cryptography-decoding-a-caesar-shift-frequency-analysis/</link>
		<comments>http://www.stealthcopter.com/blog/2010/01/python-cryptography-decoding-a-caesar-shift-frequency-analysis/#comments</comments>
		<pubDate>Mon, 04 Jan 2010 15:00:42 +0000</pubDate>
		<dc:creator>mat</dc:creator>
				<category><![CDATA[cryptography]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=347</guid>
		<description><![CDATA[Due to the simple nature of the Caesar cipher, it could easily be brute forced by trying all possible 25 keys and then looking by eye to see if the plaintext was revealed (this too can be automated by checking for common English words to see if the solution was probable). However the much more [...]]]></description>
			<content:encoded><![CDATA[<p>Due to the simple nature of the Caesar cipher, it could easily be brute forced by trying all possible 25 keys and then looking by eye to see if the plaintext was revealed (this too can be automated by checking for common English words to see if the solution was probable). However the much more elegant method of frequency analysis can be used.</p>
<p>Below is a table of the frequency of letters in the English language:</p>
<p><code></p>
<table>
<tr>
<td>Letter</td>
<td>Frequency (percent)</td>
<td>Frequency (decimal)</td>
<td>Normalised Frequency</td>
</tr>
<tr>
<td>a</td>
<td>8.17%</td>
<td>0.08167</td>
<td>0.64297</td>
</tr>
<tr>
<td>b</td>
<td>1.49%</td>
<td>0.01492</td>
<td>0.11746</td>
</tr>
<tr>
<td>c</td>
<td>2.78%</td>
<td>0.02782</td>
<td>0.21902</td>
</tr>
<tr>
<td>d</td>
<td>4.25%</td>
<td>0.04253</td>
<td>0.33483</td>
</tr>
<tr>
<td>e</td>
<td>12.70%</td>
<td>0.12702</td>
<td>1.00000</td>
</tr>
<tr>
<td>f</td>
<td>2.23%</td>
<td>0.02228</td>
<td>0.17541</td>
</tr>
<tr>
<td>g</td>
<td>2.02%</td>
<td>0.02015</td>
<td>0.15864</td>
</tr>
<tr>
<td>h</td>
<td>6.09%</td>
<td>0.06094</td>
<td>0.47977</td>
</tr>
<tr>
<td>i</td>
<td>6.97%</td>
<td>0.06966</td>
<td>0.54842</td>
</tr>
<tr>
<td>j</td>
<td>0.15%</td>
<td>0.00153</td>
<td>0.01205</td>
</tr>
<tr>
<td>k</td>
<td>0.77%</td>
<td>0.00772</td>
<td>0.06078</td>
</tr>
<tr>
<td>l</td>
<td>4.03%</td>
<td>0.04025</td>
<td>0.31688</td>
</tr>
<tr>
<td>m</td>
<td>2.41%</td>
<td>0.02406</td>
<td>0.18942</td>
</tr>
<tr>
<td>n</td>
<td>6.75%</td>
<td>0.06749</td>
<td>0.53133</td>
</tr>
<tr>
<td>o</td>
<td>7.51%</td>
<td>0.07507</td>
<td>0.59101</td>
</tr>
<tr>
<td>p</td>
<td>1.93%</td>
<td>0.01929</td>
<td>0.15187</td>
</tr>
<tr>
<td>q</td>
<td>0.10%</td>
<td>0.00095</td>
<td>0.00748</td>
</tr>
<tr>
<td>r</td>
<td>5.99%</td>
<td>0.05987</td>
<td>0.47134</td>
</tr>
<tr>
<td>s</td>
<td>6.33%</td>
<td>0.06327</td>
<td>0.49811</td>
</tr>
<tr>
<td>t</td>
<td>9.06%</td>
<td>0.09056</td>
<td>0.71296</td>
</tr>
<tr>
<td>u</td>
<td>2.76%</td>
<td>0.02758</td>
<td>0.21713</td>
</tr>
<tr>
<td>v</td>
<td>0.98%</td>
<td>0.00978</td>
<td>0.07700</td>
</tr>
<tr>
<td>w</td>
<td>2.36%</td>
<td>0.02360</td>
<td>0.18580</td>
</tr>
<tr>
<td>x</td>
<td>0.15%</td>
<td>0.00150</td>
<td>0.01181</td>
</tr>
<tr>
<td>y</td>
<td>1.97%</td>
<td>0.01974</td>
<td>0.15541</td>
</tr>
<tr>
<td>z</td>
<td>0.07%</td>
<td>0.00074</td>
<td>0.00583</td>
</tr>
</table>
<p></code></p>
<p>And shown graphically:</p>
<div id="attachment_349" class="wp-caption aligncenter" style="width: 572px"><a href="http://www.stealthcopter.com/blog/wp-content/uploads/2009/12/letter_freq.png"><img src="http://www.stealthcopter.com/blog/wp-content/uploads/2009/12/letter_freq.png" alt="English Letter Frequency" title="English Letter Frequency" width="562" height="342" class="size-full wp-image-349" /></a><p class="wp-caption-text">English Letter Frequency</p></div>
<p>Using the following code we can use frequency analysis to find the solution to ciphertext created using the Caesar shift demonstrated previously (see <a href="http://www.stealthcopter.com/blog/2009/12/python-cryptography-caesar-shift-encryption-shift-cipher/">Caesar shift</a> and <a href="http://www.stealthcopter.com/blog/2009/12/python-cryptograph-using-maketrans-for-substitution-and-caesar-ciphers/">Caesar shift using makestrans</a>).</p>
<pre name="code" class="python">
from Numeric import *
from string import maketrans

def translator(text,alphabet,key):
	trantab = maketrans(alphabet,key)
	return text.translate(trantab)

def caesar_decode(ciphertext,s):
	alpha="abcdefghijklmnopqrstuvwxyz"
	return translator(ciphertext,alpha,alpha[-s:]+alpha[:-s])

class frequency_analysis:
	def __init__(self, ciphertext):
		self.cor=[0.64297,0.11746,0.21902,0.33483,1.00000,0.17541,
		0.15864,0.47977,0.54842,0.01205,0.06078,0.31688,0.18942,
		0.53133,0.59101,0.15187,0.00748,0.47134,0.49811,0.71296,
		0.21713,0.07700,0.18580,0.01181,0.15541,0.00583]
		self.ciphertext=ciphertext.lower()
		self.freq()
		self.min_error()
		self.key=self.minimum[0]
		self.solution=caesar_decode(self.ciphertext,self.minimum[0])

	def freq(self):
		self.arr=zeros(26,Float64)
		for l in self.ciphertext:
			x=ord(l)
			if (x&gt;=97 and x&lt;=122):
				self.arr[x-97]+=1.0
		self.arr/=max(self.arr)

	def error(self):
		e=0
		for i in range(0,len(self.arr)):
			e+=abs(self.arr[i]-self.cor[i])**2
		return e

	def min_error(self):
		self.minimum=[0,10000]
		for rot in range(0,25):
			e=self.error()
			print rot,e
			if e&lt;self.minimum[1]:
				self.minimum[1]=e
				self.minimum[0]=rot
			x=self.arr[-1]
			del self.cor[-1]
			self.cor.insert(0,x)

ciphertext="ymjwj fwj ybt ydujx tk jshwduynts: tsj ymfy bnqq "+\
"uwjajsy dtzw xnxyjw kwtr wjfinsl dtzw infwd fsi tsj ymfy bnqq "+\
"uwjajsy dtzw ltajwsrjsy. ymnx nx f ajwd nrutwyfsy qjxxts yt "+\
"wjrjgjwjxujhnfqqd ktw fyyfhpx zxnsl kwjvzjshd fsfqdxnx bmnhm "+\
"wjvznwj qtsljw ufxxflj tk yjcy ns twijw yt fhmnjaj gjyyjw wjxzqyx."
FA=frequency_analysis(ciphertext)
print FA.solution
</pre>
<p>This code will calculate the error in statistical frequency for each letter squared to generate an error for each possible rotation. Using a sufficiently long piece of ciphertext this code should accurately reveal the Caesar rotation use. The table below shows the error for each rotation:</p>
<p><code><br />
<table>
<tr>
<td>Rotation</td>
<td>Error</td>
</tr>
<tr>
<td>0</td>
<td>4.11797847386</td>
</tr>
<tr>
<td>1</td>
<td>3.05305477067</td>
</tr>
<tr>
<td>2</td>
<td>3.70059678828</td>
</tr>
<tr>
<td>3</td>
<td>3.66330931218</td>
</tr>
<tr>
<td>4</td>
<td>3.5078619579</td>
</tr>
<tr>
<td>5</td>
<td>0.361318100755</td>
</tr>
<tr>
<td>6</td>
<td>3.17289666386</td>
</tr>
<tr>
<td>7</td>
<td>3.66072641654</td>
</tr>
<tr>
<td>8</td>
<td>3.39769855873</td>
</tr>
<tr>
<td>9</td>
<td>1.74854802027</td>
</tr>
<tr>
<td>10</td>
<td>2.92550921273</td>
</tr>
<tr>
<td>11</td>
<td>2.67524757297</td>
</tr>
<tr>
<td>12</td>
<td>2.86847189573</td>
</tr>
<tr>
<td>13</td>
<td>3.06980318397</td>
</tr>
<tr>
<td>14</td>
<td>2.56886153328</td>
</tr>
<tr>
<td>15</td>
<td>2.17180117031</td>
</tr>
<tr>
<td>16</td>
<td>2.24503724763</td>
</tr>
<tr>
<td>17</td>
<td>2.95579718798</td>
</tr>
<tr>
<td>18</td>
<td>1.74002183444</td>
</tr>
<tr>
<td>19</td>
<td>1.83328601011</td>
</tr>
<tr>
<td>20</td>
<td>1.74779021766</td>
</tr>
<tr>
<td>21</td>
<td>2.71332097813</td>
</tr>
<tr>
<td>22</td>
<td>1.5409364067</td>
</tr>
<tr>
<td>23</td>
<td>1.83209213494</td>
</tr>
<tr>
<td>24</td>
<td>1.54904808883</td>
</tr>
</table>
<p></code></p>
<p>The lowest error is for 5 rotations (correctly so) with an error of 0.361318100755, the next lowest error is 22 rotations with an error of 1.5409364067. This is ~4.3x difference, which gives a very large degree of confidence to our solution and below is the deciphered text.</p>
<blockquote><p>
there are two types of encryption: one that will prevent your sister from reading your diary and one that will prevent your government. this is a very important lesson to remeberespecially for attacks using frequency analysis which require longer passage of text in order to achieve better results.
</p></blockquote>
<p><strong>Future</strong><br />
The frequency analysis presented here can be used along with some other techniques in order to crack the viginere cipher.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stealthcopter.com/blog/2010/01/python-cryptography-decoding-a-caesar-shift-frequency-analysis/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python: Cryptograph using maketrans for substitution and Caesar ciphers</title>
		<link>http://www.stealthcopter.com/blog/2009/12/python-cryptograph-using-maketrans-for-substitution-and-caesar-ciphers/</link>
		<comments>http://www.stealthcopter.com/blog/2009/12/python-cryptograph-using-maketrans-for-substitution-and-caesar-ciphers/#comments</comments>
		<pubDate>Tue, 22 Dec 2009 19:05:28 +0000</pubDate>
		<dc:creator>mat</dc:creator>
				<category><![CDATA[cryptography]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=323</guid>
		<description><![CDATA[I&#8217;ve rewritten the functions in my previous two posts (caesar and substitution ciphers) using the maketrans function from the strings module in python (With thanks to wumzi for pointing this out). maketrans takes an input alphabet and an output alphabet and can then be used on a string. This can be used to greatly simplify [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve rewritten the functions in my previous two posts (<a href="http://www.stealthcopter.com/blog/2009/12/python-cryptography-caesar-shift-encryption-shift-cipher/">caesar</a> and <a href="http://www.stealthcopter.com/blog/2009/12/python-cryptography-substitution-cipher-improving-on-the-caesar-cipher/">substitution</a> ciphers) using the maketrans function from the strings module in python (With thanks to <a href="http://www.wumzi.info/">wumzi</a> for pointing this out).</p>
<p>maketrans takes an input alphabet and an output alphabet and can then be used on a string. This can be used to greatly simplify the two ciphers I produced previously. Example below:</p>
<pre name="code" class="python">
input_alphabet="abcde"
output_alphabet="12345"
trantab = maketrans(input_alphabet,output_alphabet)
text="translate abcdefg"
print text.translate(trantab)
# This will output:
# tr1nsl1t5 12345fg
</pre>
<p>This nows means my code can be rewritten in just a fraction of what it was before:</p>
<pre name="code" class="python">
from random import shuffle
from string import maketrans

alphabet="abcdefghijklmnopqrstuvwxyz" + \
	 "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"+ \
         ":.;,?!@#$%&#038;()+=-*/_<> []{}`~^"

def translator(text,alphabet,key):
	trantab = maketrans(alphabet,key)
	return text.translate(trantab)

def caesar_encode(plaintext,s,alphabet):
	return translator(plaintext,alphabet,alphabet[s:]+alphabet[:s])

def caesar_decode(ciphertext,s,alphabet):
	return translator(ciphertext,alphabet,alphabet[-s:]+alphabet[:-s])

def substitution_encode(plaintext,alphabet):
	randarray=range(0,len(alphabet))
	shuffle(randarray)
	key=""
	for i in range(0,len(alphabet)):
		key+=alphabet[randarray[i]]

	return translator(plaintext,alphabet,key),key

def substitution_decode(ciphertext,key,alphabet):
	return translator(ciphertext,key,alphabet)

# Example useage
plaintext="The wheels on the bus go round and round. round" + \
" and round. round and round. The wheels on the bus go round and"+ \
" round, all through the town!"

print
print "SUBSTITUTION"
ciphertext,key=substitution_encode(plaintext,alphabet)
print "Key: ", key
print "Plaintext:", plaintext
print "Cipertext:", ciphertext
print "Decoded  :", substitution_decode(ciphertext,key,alphabet)

print
print "CAESAR SHIFT"
ciphertext=caesar_encode(plaintext,5,alphabet)
print "Key: ", 5
print "Plaintext:", plaintext
print "Cipertext:", ciphertext
print "Decoded  :", caesar_decode(ciphertext,5,alphabet)
</pre>
<p>This will output the following</p>
<blockquote><p>
SUBSTITUTION<br />
Key:  &#038;fywQ.%!lmx_sRGu:{<(5jqAXvMFgk]SIY[4iK+3P8pcV$2da@DT/ZnOJ*E>-r},BH16zUb#L?N`e7C ~9t)oW^=;h0<br />
Plaintext: The wheels on the bus go round and round. round and round.round and round. The wheels on the bus go round and round, all through the town!<br />
Cipertext: O!Q)q!QQ_<)GR)(!Q)f5<)%G){G5Rw)&#038;Rw){G5Rw,){G5Rw)&#038;Rw){G5Rw,{G5Rw)&#038;Rw){G5Rw,)O!Q)q!QQ_<)GR)(!Q)f5<)%G){G5Rw)&#038;Rw){G5RwH)&#038;__)(!{G5%!)(!Q)(GqR6<br />
Decoded  : The wheels on the bus go round and round. round and round.round and round. The wheels on the bus go round and round, all through the town!</p>
<p>CAESAR SHIFT<br />
Key:  5<br />
Plaintext: The wheels on the bus go round and round. round and round.round and round. The wheels on the bus go round and round, all through the town!<br />
Cipertext: Ymj`2mjjqx`ts`ymj`gzx`lt`wtzsi`fsi`wtzsi@`wtzsi`fsi`wtzsi@wtzsi`fsi`wtzsi@`Ymj`2mjjqx`ts`ymj`gzx`lt`wtzsi`fsi`wtzsi$`fqq`ymwtzlm`ymj`yt2s&#038;<br />
Decoded  : The wheels on the bus go round and round. round and round.round and round. The wheels on the bus go round and round, all through the town!
</p></blockquote>
<p><strong>Conclusion</strong><br />
maketrans is awesome <img src='http://www.stealthcopter.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.stealthcopter.com/blog/2009/12/python-cryptograph-using-maketrans-for-substitution-and-caesar-ciphers/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Python: Cryptography Substitution Cipher improving on the Caesar cipher</title>
		<link>http://www.stealthcopter.com/blog/2009/12/python-cryptography-substitution-cipher-improving-on-the-caesar-cipher/</link>
		<comments>http://www.stealthcopter.com/blog/2009/12/python-cryptography-substitution-cipher-improving-on-the-caesar-cipher/#comments</comments>
		<pubDate>Tue, 22 Dec 2009 03:06:24 +0000</pubDate>
		<dc:creator>mat</dc:creator>
				<category><![CDATA[cryptography]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=291</guid>
		<description><![CDATA[This post builds upon the Caesar shift presented previously; converting it to a full substitution cipher. The substitution cipher will practically remove bruteforce style methods of defeating the encryption and provide a basis for more complicated ciphers. Subsitution Cipher Because a Caesar shift only rotates the alphabet there are only 25 possible unique solutions, this [...]]]></description>
			<content:encoded><![CDATA[<p>This post builds upon the Caesar shift presented <a href="http://www.stealthcopter.com/blog/2009/12/python-cryptography-caesar-shift-encryption-shift-cipher/">previously</a>; converting it to a full substitution cipher. The substitution cipher will practically remove bruteforce style methods of defeating the encryption and provide a basis for more complicated ciphers.</p>
<p><strong>Subsitution Cipher</strong><br />
Because a Caesar shift only rotates the alphabet there are only 25 possible unique solutions, this leaves the cipher quite vulnerable to brute force. If rather than just rotating the alphabet and keeping it &#8216;linear&#8217; we can shuffle it to create a substitution cipher. This improves the number of possible solutions to a shocking<br />
(26! &#8211; 1) = 4.03291461126605635584e26 (unfortunatly, the substitution cipher is alot weaker than it seems as it is vunerable to several different cryptanalysis attacks).</p>
<div id="attachment_292" class="wp-caption aligncenter" style="width: 330px"><a href="http://www.stealthcopter.com/blog/wp-content/uploads/2009/12/320px-Substitution.png"><img src="http://www.stealthcopter.com/blog/wp-content/uploads/2009/12/320px-Substitution.png" alt="Substitution Cipher Diagram" title="320px-Substitution" width="320" height="135" class="size-full wp-image-292" /></a><p class="wp-caption-text">Substitution Cipher Diagram</p></div>
<p>We start with the simple Caesar shift function with the following changes:</p>
<ul>
<li>A new array is introduced filled with the numbers 0 &#8211; the length of the alphabet. This array is shuffled using shuffle from the random module. The alphabet substitution dictionary is created using this array to decide which letters go where (this is probably clearer in the code than in my explanation).</li>
<li>With the Caesar shift we only needed to know the number of rotations in order to decrypt the text, now we need a full list of the letter substitutions. This is stored as the key and will be needed in order to decode our substitution cipher. </li>
<li>We also now store the alphabet outside of the function so that it can be used in a decode function. This function was added along with some example usage to make the full process more understandable</li>
</ul>
<pre name="code" class="python">
from random import shuffle

alphabet="abcdefghijklmnopqrstuvwxyz"

def substitution(alphabet,plaintext):

	# Create array to use to randomise alphabet position
	randarray=range(0,len(alphabet))
	shuffle(randarray)

	key=""

	#Create our substitution dictionary
	dic={}
	for i in range(0,len(alphabet)):
		key+=alphabet[randarray[i]]
		dic[alphabet[i]]=alphabet[randarray[i]]

	#Convert each letter of plaintext to the corrsponding
	#encrypted letter in our dictionary creating the cryptext
	ciphertext=""
	for l in plaintext:
		if l in dic:
			l=dic[l]
		ciphertext+=l
	for i in alphabet:
		print i,
	print
	for i in key:
		print i,
	print
	return ciphertext,key

# This function decodes the ciphertext using the key and creating
# the reverse of the dictionary created in substitution to retrieve
# the plaintext again
def decode(alphabet,ciphertext,key):

	dic={}
	for i in range(0,len(key)):
		dic[key[i]]=alphabet[i]

	plaintext=""
	for l in ciphertext:
		if l in dic:
			l=dic[l]
		plaintext+=l

	return plaintext

# Example useage
plaintext="the cat sat on the mat"
ciphertext,key=substitution(plaintext)
print "Key: ", key
print "Plaintext:", plaintext
print "Cipertext:", ciphertext
print "Decoded  :", decode(ciphertext,key)
</pre>
<p>Running this will output the following (This will be different on each run due to the use of random to generate the key).</p>
<blockquote><p>
Key:  miylbsowutgdkfvjepqhazrncx<br />
Plaintext: the cat sat on the mat<br />
Cipertext: hwb ymh qmh vf hwb kmh<br />
Decoded  : the cat sat on the mat
</p></blockquote>
<p><strong>Improvement by using additional characters</strong><br />
Adding additional characters into the substitution will it more difficult to solve. For example if we change our alphabet from:</p>
<blockquote><p>
alphabet=&#8221;abcdefghijklmnopqrstuvwxyz&#8221;
</p></blockquote>
<p>If we include capital letters, numbers from 0 -9 and special characters:</p>
<blockquote><p>
alphabet=&#8221;ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890&#8243;+ \<br />
               &#8220;:.;,?!@#$%&#038;()+=-*/_<> []{}`~^&#8221;+ \<br />
               &#8220;abcdefghijklmnopqrstuvwxyz&#8221;
</p></blockquote>
<p>We increase the number of possible solutions from (26!-1) to (63!-1) which is 1.98260831540444006412e87. This also has the added benefit of making the encrypted text alot cooler and harder to guess at by eye (unfortunately still very easy to see what character represents space).</p>
<blockquote><p>
Key:  MXjAarLzWqFePI7E botO5f1kym29RZd3Sh8JTiQGKVDp6YBCsU4nucNgwlx0vH<br />
Plaintext: this plaintext will be much more of a challenge to decode compared to the Caesar shift cipher<br />
Cipertext: tzWoHEeMWIta1tHfWeeHXaHPOjzHP7baH7rHMHjzMeeaILaHt7HAaj7AaHj7PEMbaAHt7HtzaHjMaoMbHozWrtHjWEzab<br />
Decoded  : this plaintext will be much more of a challenge to decode compared to the caesar shift cipher
</p></blockquote>
<p><strong>Future</strong><br />
The substitution cipher is a lot more secure than Caesar shift cipher but unfortunately is very insecure towards frequency analysis. In future posts I will address using frequency analysis and methods to prevent this type of attack as well as improving on this cipher by creating multiple-dicitionary based ciphers to create Vigenère style ciphers.</p>
<p>I imagine most people reading this will enjoy the simple challenge of solving some encrypted text. I have used this code to make some ciphertext, try and decode it! (extra points for knowing where it is from):</p>
<blockquote><p>
^&#8217;VtuBbtv3vut1u-w.G^&#8217;Vt&#038;vnu-tZBtZnuIvwvtwn<br />
-qbtuB6GN3vutbqBS-qt.BSt&#038;wB~vtV.tqv1wbG}u5t<br />
~nDDv5tVvG}u5tbBwvtVvtbBt@nvIvZG}u5tbqwv6tv<br />
3vw.t@nvIvtnubBt1tUnwvG}Ztbqv.t&#038;Swuv5tnbtqS<br />
wbt&#038;vI1SZvt^t61ZtZBtq1@@.tUBwt.BSm)B6tbqvZv<br />
t@BnubZtBUt51b1tV1~vt1t&#038;v1SbnUSDtDnuvG}u5t6<br />
v&#8217;wvtBSbtBUt&#038;vb1GQv&#8217;wvtwvDv1Znu-tButbnVvG
</p></blockquote>
<p>(note: newlines were placed to make it fit and do not represent a character)</p>
<p>Update: Made code a little cleaner by moving dictionary outside of functions.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stealthcopter.com/blog/2009/12/python-cryptography-substitution-cipher-improving-on-the-caesar-cipher/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Python: Cryptography Caesar shift encryption (shift cipher)</title>
		<link>http://www.stealthcopter.com/blog/2009/12/python-cryptography-caesar-shift-encryption-shift-cipher/</link>
		<comments>http://www.stealthcopter.com/blog/2009/12/python-cryptography-caesar-shift-encryption-shift-cipher/#comments</comments>
		<pubDate>Mon, 21 Dec 2009 18:00:11 +0000</pubDate>
		<dc:creator>mat</dc:creator>
				<category><![CDATA[cryptography]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[computational]]></category>

		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=271</guid>
		<description><![CDATA[I have always had a keen interest in cryptography and rather than give a brief history of cryptography I will recommend reading Simon Singh&#8217;s The code book or for a modern and hands on approach Applied Cryptography by Bruce Schneier (Who also made a brilliant book on security, more of descriptive approach but very interesting [...]]]></description>
			<content:encoded><![CDATA[<p>I have always had a keen interest in cryptography and rather than give a brief history of cryptography I will recommend reading <strong>Simon Singh&#8217;s</strong> <a href="http://www.simonsingh.net/The_Code_Book.html">The code book</a> or for a modern and hands on approach <a href="http://www.schneier.com/book-applied.html">Applied Cryptography</a> by <strong>Bruce Schneier</strong> (Who also made a brilliant book on security, more of descriptive approach but very interesting <a href="http://www.schneier.com/book-sandl.html">Secrets and Lies: Digital Security in a Networked World</a>).</p>
<p>This post aims to detail the creation (in python) of one of the simplest forms of encryption; the simple Caesar shift (or shift cipher). The Caesar shift takes the normal alphabet and maps it to a an identical alphabet with a rotation. The cipher will be written in such a way that it can be easily expanded on to create more complex encryption schemes with little modification.</p>
<div id="attachment_277" class="wp-caption aligncenter" style="width: 330px"><a href="http://en.wikipedia.org/wiki/Caesar_shift"><img src="http://www.stealthcopter.com/blog/wp-content/uploads/2009/12/320px-Caesar3.png" alt="Caesar shift alphabet" title="320px-Caesar3" width="320" height="135" class="size-full wp-image-277" /></a><p class="wp-caption-text">Caesar shift alphabet diagram</p></div>
<p>The above image shows a diagrammatic representation of a Caesar shift of 3 (alphabet transposed onto a rotation of itself with a displacement of 3). Below shows the entire alphabet in plaintext and in ciphertext, followed by a simple sentence in plaintext and in cipher text.</p>
<table>
<tr>
<td>
Plaintext:</td>
<td>abcdefghijklmnopqrstuvwxyz</td>
</tr>
<tr>
<td>
Ciphertext:</td>
<td>defghijklmnopqrstuvwxyzabc</td>
</tr>
</table>
<table>
<tr>
<td>
Plaintext:</td>
<td>the cat sat on the mat</td>
</tr>
<tr>
<td>
Ciphertext:</td>
<td>wkh fdw vdw rq wkh pdw</td>
</tr>
</table>
<p>Below is the code to convert plaintext into ciphertext along with an example of the usage:</p>
<pre name="code" class="python">
def caesar(plaintext,shift):

	alphabet=["a","b","c","d","e","f","g","h","i","j","k","l",
	"m","n","o","p","q","r","s","t","u","v","w","x","y","z"]

	#Create our substitution dictionary
	dic={}
	for i in range(0,len(alphabet)):
		dic[alphabet[i]]=alphabet[(i+shift)%len(alphabet)]

	#Convert each letter of plaintext to the corrsponding
	#encrypted letter in our dictionary creating the cryptext
	ciphertext=""
	for l in plaintext.lower():
		if l in dic:
			l=dic[l]
		ciphertext+=l

	return ciphertext

#Example useage
plaintext="the cat sat on the mat"
print "Plaintext:", plaintext
print "Cipertext:",caesar(plaintext,3)
#This will result in:
#Plaintext: the cat sat on the mat
#Cipertext: wkh fdw vdw rq wkh pdw
</pre>
<p><strong>Explanation</strong><br />
Here we have written a function that is given the plaintext and an arbitary rotation (including negative) and returns a ciphertext. The function creates a dictionary, mapping each letter to another letter that is &#8216;shift&#8217; letters away from it in the alphabet (the modulus (<strong>%</strong>) is used so to wrap the alphabet back to the start if it falls off the end). It then converts each letter of the plain text into the corresponding encrypted letter using the dictionary.</p>
<p><strong>Future</strong><br />
In the next post I will discuss methods to improve the Caesar shift and how to turn it into a full substitution cipher (where the alphabet is shuffled rather than rotated). The Caesar shift is very susceptible to brute forcing and frequency analysis which I shall explain and create a program to defeat these encryptions in a future post.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stealthcopter.com/blog/2009/12/python-cryptography-caesar-shift-encryption-shift-cipher/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Python: Wordwheel / WordCube solver</title>
		<link>http://www.stealthcopter.com/blog/2009/12/python-wordwheel-wordcube-solver/</link>
		<comments>http://www.stealthcopter.com/blog/2009/12/python-wordwheel-wordcube-solver/#comments</comments>
		<pubDate>Sun, 13 Dec 2009 17:36:50 +0000</pubDate>
		<dc:creator>mat</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[strings]]></category>
		<category><![CDATA[website]]></category>
		<category><![CDATA[WordCube]]></category>

		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=240</guid>
		<description><![CDATA[Often in newspapers there is a wordwheel or some variant, whereby you have to find as many words greater than 3 letters long, containing the centre word and using the letters no more than once. I have created a webpage that generates a &#8220;WordCube&#8221; daily for people to peruse at their leisure (www.stealthcopter.com/wordcube). This post [...]]]></description>
			<content:encoded><![CDATA[<p>Often in newspapers there is a wordwheel or some variant, whereby you have to find as many words greater than 3 letters long, containing the centre word and using the letters no more than once. I have created a webpage that generates a &#8220;WordCube&#8221; daily for people to peruse at their leisure (<a href="http://www.stealthcopter.com/wordcube">www.stealthcopter.com/wordcube</a>). This post contains the code and explanation of the solutions to wordcube&#8217;s (and all other word&lt;insert shape here&gt;). </p>
<p><img src="http://stealthcopter.com/wordcube/images/20091212.png" alt="WordCube from http://www.stealthcopter.com/wordcube for 12/12/2009" /><br />
Example WordCube image for the 12th December 2009 from <a href="http://www.stealthcopter.com/wordcube/2009/12/12">www.stealthcopter.com/wordcube/2009/12/12</a></p>
<p>Below is a function I wrote to check if an input was a valid anagram (or partial anagram, as it isn&#8217;t essential to use every letter). The function works by cycling over each letter of word we are testing (word), and checks if the letter is valid (checked against chkword). If the letter is valid then it removes the letter from the original word and moves to the next letter until we run out of letters (returns True) or if the letter is invalid (returns False).</p>
<pre name="code" class="python">
def anagramchk(word,chkword):
	for letter in word:
		if letter in chkword:
			chkword=chkword.replace(letter, '', 1)
		else:
			return False
	return True

f=open('english', 'r')
word=raw_input('Input letters (starting with mandatory letter) :')
minlen=4
count=0
for l in f:
	l=l.strip()
	if len(l)&lt;=len(word) and len(l)&gt;=minlen and word[0] in l and anagramchk(l,word):
		if len(l)==len(word):
			print l,'  &lt;-- Long word'
		else:
			print l
		count+=1
f.close()
print count
</pre>
<p>This will output a list of the possible words, along with a total. The results can be seen for the WordCube in the example above <a href="www.stealthcopter.com/wordcube/2009/12/12?solutions=1">here</a> (To prevent spoiling it if you&#8217;d like to have a go at it yourself).</p>
<p>As always I&#8217;d be interested to see if anyone knows any faster methods or any other general improvements or comments.</p>
<p>The dictionary file can be found here (not perfect):<br />
<a href="http://www.stealthcopter.com/files/english.txt">here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.stealthcopter.com/blog/2009/12/python-wordwheel-wordcube-solver/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Python: Highest common factor of two numbers</title>
		<link>http://www.stealthcopter.com/blog/2009/12/python-highest-common-factor/</link>
		<comments>http://www.stealthcopter.com/blog/2009/12/python-highest-common-factor/#comments</comments>
		<pubDate>Tue, 08 Dec 2009 12:10:42 +0000</pubDate>
		<dc:creator>mat</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[computational]]></category>
		<category><![CDATA[maths]]></category>
		<category><![CDATA[project euler]]></category>

		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=228</guid>
		<description><![CDATA[Finding the highest common factor is useful in mathematics and cryptography. It would be computationally expensive to calculate all the common factors of two numbers and then compare to find the highest factor in common. Instead (as usual) there is a mathematical theory that can be used to speed up this process, in this case [...]]]></description>
			<content:encoded><![CDATA[<p>Finding the highest common factor is useful in mathematics and cryptography. </p>
<p>It would be computationally expensive to calculate all the common factors of two numbers and then compare to find the highest factor in common. Instead (as usual) there is a mathematical theory that can be used to speed up this process, in this case it&#8217;s the <a href="http://en.wikipedia.org/wiki/Euclidean_algorithm">Euclidean algorithm</a>.</p>
<p>The Euclidean algorithm subtracts the smaller number from the larger number, then using this new number and the smaller number repeats this process until the numbers are equal and hence the subtraction goes to zero. This is the highest common factor.</p>
<p><strong>Example:</strong><br />
Find the highest common factor of 252 and 105</p>
<blockquote><p>
252-105=147<br />
147-105=42<br />
105-42=63<br />
63-42=21<br />
42-21=21<br />
21-21=0
</p></blockquote>
<p>Below is the python code to preform this </p>
<pre name="code" class="python">
def hcf(no1,no2):
	while no1!=no2:
		if no1>no2:
			no1-=no2
		elif no2>no1:
			no2-=no1
	return no1
</pre>
<p>This method is quite quick even and scales well for larger numbers. If you know of a faster method or can see any improvements to make then please let me know.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stealthcopter.com/blog/2009/12/python-highest-common-factor/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Python: Anagram solver</title>
		<link>http://www.stealthcopter.com/blog/2009/11/python-anagram-solver/</link>
		<comments>http://www.stealthcopter.com/blog/2009/11/python-anagram-solver/#comments</comments>
		<pubDate>Wed, 18 Nov 2009 17:29:59 +0000</pubDate>
		<dc:creator>mat</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[computational]]></category>
		<category><![CDATA[strings]]></category>

		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=221</guid>
		<description><![CDATA[Finding the solutions to an anagram can be enjoyable and used as a sign of mental agility, with examples from long running television series (countdown) to online gambling skill games. It also makes an interesting tutorial for learning some simple python skills. We start off by writing a function to check if a given word [...]]]></description>
			<content:encoded><![CDATA[<p>Finding the solutions to an anagram can be enjoyable and used as a sign of mental agility, with examples from long running television series (<a href="http://en.wikipedia.org/wiki/Countdown_(game_show)">countdown</a>) to online gambling skill games. It also makes an interesting tutorial for learning some simple python skills.</p>
<p>We start off by writing a function to check if a given word is an anagram of another word. This will be used to check a list of dictionary words against a given word in order to find its anagrams. The function accepts two string inputs, the word we are testing (word), and the given anagram to check it against (chkword).</p>
<p>The function loops over every letter in the word and if it is not present in chkword then we know that it cannot be an anagram of the chkword. Otherwise it is valid and that letter is removed from the chkword so that each letter may only be used once. For example checking &#8220;netting&#8221; with &#8220;testing&#8221; would fail on the second n.</p>
<pre name="code" class="python">
def anagramchk(word,chkword):
	for letter in word:
		if letter in chkword:
			chkword=chkword.replace(letter, '', 1)
		else:
			return 0
	return 1
</pre>
<p>Now we need to get the anagram from the user. The following using raw_input to read what the user types in the console and assign it to a variable, with the optional argument used to print text so the user knows they need to do something.</p>
<pre name="code" class="python">
wordin=raw_input('Enter anagram:\n')
</pre>
<p>We can now join both these together and loop over our dictionary file. We strip each line to remove any extra space and gumph at the end of the lines. If the length of a line is less than 4 then it is ignored,this can be changed to whatever minimum you like, so if you want it to be the same length as the input you could replace it with len(line)==len(wordin). We then use the function already written earlier to check if the word is a valid anagram and print the word if it is. Then we finish by closing the file.</p>
<pre name="code" class="python">
f=open('english.txt', 'r')
for line in f:
	line=line.strip()
	if len(line)>=4:
		if anagramchk(line,wordin):
			print line
f.close()
</pre>
<p>Hopefully this has been helpful to someone. As ever if you have any suggestions or improvements then please leave a comment. Below is the complete version of the code</p>
<pre name="code" class="python">
def anagramchk(word,chkword):
	for letter in word:
		if letter in chkword:
			chkword=chkword.replace(letter, '', 1)
		else:
			return 0
	return 1

wordin=raw_input('Enter anagram:')

f=open('english.txt', 'r')
for line in f:
	line=line.strip()
	if len(line)&gt;=4:
		if anagramchk(line,wordin):
			print line
f.close()
</pre>
<p>The dictionary file I used was one I found in /usr/share/dict/british-english (most linux distro&#8217;s should have this or similar) it can also be found <a href="http://www.stealthcopter.com/files/english.txt">here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.stealthcopter.com/blog/2009/11/python-anagram-solver/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Python: Factors of a number</title>
		<link>http://www.stealthcopter.com/blog/2009/11/python-factors-of-a-number/</link>
		<comments>http://www.stealthcopter.com/blog/2009/11/python-factors-of-a-number/#comments</comments>
		<pubDate>Tue, 10 Nov 2009 11:32:46 +0000</pubDate>
		<dc:creator>mat</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[computational]]></category>
		<category><![CDATA[maths]]></category>
		<category><![CDATA[project euler]]></category>

		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=195</guid>
		<description><![CDATA[Project Euler often requires the factors of a number to be found, here is the function I have written for that purpose. def factors(n): fact=[1,n] check=2 rootn=sqrt(n) while check&#60;rootn: if n%check==0: fact.append(check) fact.append(n/check) check+=1 if rootn==check: fact.append(check) fact.sort() return fact As always any help in speeding up my function would be appreciated. I think it [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://projecteuler.net/">Project Euler</a> often requires the factors of a number to be found, here is the function I have written for that purpose.</p>
<pre name="code" class="python">
def factors(n):
	fact=[1,n]
	check=2
	rootn=sqrt(n)
	while check&lt;rootn:
		if n%check==0:
			fact.append(check)
			fact.append(n/check)
		check+=1
	if rootn==check:
		fact.append(check)
        fact.sort()
	return fact
</pre>
<p>As always any help in speeding up my function would be appreciated. I think it could be improved using a sieve method or by checking if its divisible by 2 first then using check+=2 starting at 3 to improve the speed of the loop by a factor of 2.</p>
<p>Below is a the commented version to help explain what&#8217;s going on if it&#8217;s not clear.</p>
<pre name="code" class="python">
def factors(n):
	# 1 and n are automatically factors of n
	fact=[1,n]
	# starting at 2 as we have already dealt with 1
	check=2
	# calculate the square root of n and use this as the
	# limit when checking if a number is divisible as
	# factors above sqrt(n) will already be calculated as
	# the inverse of a lower factor IE. finding factors of
	# 100 only need go up to 10 (sqrt(100)=10) as factors
	# such as 25 can be found when 5 is found to be a
	# factor 100/5=25
	rootn=sqrt(n)
	while check&lt;rootn:
		if n%check==0:
			fact.append(check)
			fact.append(n/check)
		check+=1
	# this line checks the sqrt of the number to see if
	# it is a factor putting it here prevents it appearing
	# twice in the above while loop and putting it outside
	# the loop should save some time.
	if rootn==check:
		fact.append(check)
	# return an array of factors sorted into numerial order.
        fact.sort()
	return fact
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.stealthcopter.com/blog/2009/11/python-factors-of-a-number/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Evolutionary Algorithms in python, queens example</title>
		<link>http://www.stealthcopter.com/blog/2009/09/evolutionary-algorithms-in-python-queens-example/</link>
		<comments>http://www.stealthcopter.com/blog/2009/09/evolutionary-algorithms-in-python-queens-example/#comments</comments>
		<pubDate>Mon, 14 Sep 2009 21:26:06 +0000</pubDate>
		<dc:creator>mat</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[computational]]></category>
		<category><![CDATA[maths]]></category>

		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=170</guid>
		<description><![CDATA[So a friend of mine introduced me to Evolutionary Algorithms a while back and I got some lecture notes passed onto me explaining the basics and a simple example in pseudo-code. After a bit of work I&#8217;ve written up the queens example as an evolutionary algorithm in python. Here goes: The queens problem The following [...]]]></description>
			<content:encoded><![CDATA[<p>So a friend of mine introduced me to <strong>Evolutionary Algorithms</strong> a while back and I got some lecture notes passed onto me explaining the basics and a simple example in pseudo-code. </p>
<p>After a bit of work I&#8217;ve written up the queens example as an evolutionary algorithm in python. Here goes:</p>
<p><strong>The queens problem</strong><br />
The following is the quote from wikipedia regarding the problem.</p>
<blockquote><p>
The eight queens puzzle is the problem of putting eight chess queens on an 8×8 chessboard such that none of them is able to capture any other using the standard chess queen&#8217;s moves. The queens must be placed in such a way that no two queens would be able to attack each other. Thus, a solution requires that no two queens share the same row, column, or diagonal.
</p></blockquote>
<p><img src="http://www.stealthcopter.com/blog/wp-content/uploads/2009/09/queensdiag.png" alt="queens problem chess maths python" /><br />
The above image is one of the many solutions to the problem. Hint: if you are looking for many solutions once you have found one, all of its 4 rotations (if unique) are solutions too.</p>
<p>Check out the <a href="http://en.wikipedia.org/wiki/Eight_queens_puzzle">wikipedia article</a> for more information </p>
<p><strong>Code</strong><br />
This is the current code with medium amount of comments, I have also provided a heavily commented version for people with less python experience (scroll to bottom).</p>
<pre name="code" class="python">
# -*- coding: utf-8 -*-
##################################################################
##################################################################
# QUEENS EXAMPLE
# - Program Type: Demonstration of Evolutionary alogorithms
# - Authour: Matthew Rollings
# - Date: 14/09/09
##################################################################
##################################################################

# Import randint from the random module
from random import randint

# Creates a array I just use for convenience
board=[1,2,3,4,5,6,7,8]

##################################################################
# FUNCTIONS

# Function to check amount of attacking queens
def check(array):

 # Collisions represent number of attacking queens, starts at zero obv.
 collisions=0
 for i in range(1,9):
  if i not in array:
   print "DUPLICATE NUMBER ERROR - KILLING STUPID GENE" # Never happen <img src='http://www.stealthcopter.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />
   return 0

 # Total Collisions
 # For each queen in the array
 for i in range(0, 8):
  col=0
  # For every other queen in the array
  for j in range(0, 8):
   # avoids checking self vs self
   if i!=j:
    # if queen i is on a diagonal from queen j, then the
    # difference in magnitude x-cord will equal the difference in
    # the magnitude of the y-coord
    if abs(board[i]-board[j])==abs(array[j]-array[i]):
     # Sets a variable to tell the next part that a collision was detected
     col=1
  # If a collision was detected add one to collisions
  if col==1:
   collisions+=1
 # Return 8-colllisions, so that 0 is bad (8 attacking queens) and 8 is good (no attacking queens)
 return 8-collisions

# The reproduce function, takes two arguements, the two parents
def reproduce(array1, array2):

 # FIRST BABY (mother first then father)
 # Takes first half of mothers gene
 baby=array1[0:4]
 # Can't just add two halves as I will get duplicate numbers
 for i in array2:
  # add fathers genes going in order of numbers left
  if i not in baby:
   baby.append(i)
 # Add a little variation by giving a percentage chance of mutation
 if randint(0,100)&gt;20:
  baby=mutate(baby)
 # Add the baby to the population and add its corresponding fitness
 population.append(baby)
 fitness.append(check(baby))

 # SECOND BABY (arrays just swapped around, Father first then mother)
 baby=array2[0:4]
 for i in array1:
  if i not in baby:
   baby.append(i)
 if randint(0,100)&gt;20:
  baby=mutate(baby)
 population.append(baby)
 fitness.append(check(baby)) 

# Mutate the array given to the function
def mutate(array1):
 #Chooses two random places (WARNING CAN BE SAME POSITION)
 a=randint(0,7)
 b=randint(0,7)
 # Swaps the two over (temporary var must be used (c))
 c=array1[a]
 array1[a]=array1[b]
 array1[b]=c
 return array1

# Prints the population and corresponding fitness to the screen
# Only used for debugging
def printpop():
 # Prints the group
 for i in range(0, len(population)):
  print population[i],fitness[i]

##################################################################
# VARIABLES
# The size of the population (how many genes, more = faster convergance but more cpu + memory, fewer = opp.)
popsize=40
# The starting variation of the group because I havn't created a better method
# for starting off. I just add arrays of 1,2,3,4,5,6,7,8 to the population and
# give them the number of mutations between the following two values to make
# psudo random starting group
variation=[4,14]
# How many of the genes to kill each time in percentage, each dead gene will be replaced by a new child <img src='http://www.stealthcopter.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />
die=0.40
# Kill limit is calculated from the above percentage
kill_limit=die*popsize

##################################################################
# MAIN

population=[]
fitness=[]
for i in range(0,popsize):
 population.append([1,2,3,4,5,6,7,8])
 a=0
 while a&lt;randint(variation[0],variation[1]):
  a+=1
  population[i]=mutate(population[i])
 fitness.append(check(population[i]))

maxi=0
generations=1

while maxi!=8:

 # Picks top # in group to be parents, kills rest
 killed=0
 # starting at the lowest fitness (0 and increasing untill kill limit reached)
 x=0
 while killed&lt;kill_limit:
  for i in range(0,popsize):
   # Try is here to catch any errors
   try:
    if fitness[i]==x:
     # This bit removes the crappy gene from the population and from fitness
     population.pop(i)
     fitness.pop(i)
     # increases the kill counter
     killed+=1
    if killed==kill_limit:
     break
   except:
    break
  # increments fitness
  x+=1

 babies=0
 cpop=len(population)-1 #current population
 while babies&lt;killed:
  # produces two babies from two random parents (should prob give fittest parents preference)
  reproduce(population[randint(0,cpop)],population[randint(0,cpop)])
  babies+=2 

 generations+=1

 # Looks for highest fitness in the group and checks if any have won!
 maxi=0
 for i in range(0,popsize):
  if fitness[i]&gt;maxi:
   maxi=fitness[i]
   if maxi==8:
    print population[i]
    break

print "Took",generations,"generations"
</pre>
<p><strong>Quick Analysis</strong><br />
I did a very quick analysis keeping the kill off percentage at 0.4 and varying the population size to see the improvement on generations required before a solution is found. I&#8217;ve also added a link (scroll to bottom) to the .ods file I used to calculate this (and an excel .xls file for the windows chaps). Below is the graph I calculated for different population sizes and it looks like we would expect, decreasing as the population increases. I only did 10 averages for each point and there were some anomalies, which are to be expected with this method of programming as it essentially relies on random numbers so it is a bit noisey:</p>
<p><img src="http://www.stealthcopter.com/blog/wp-content/uploads/2009/09/evolutionary0.4.png" alt="eight queens problem python" /></p>
<p><strong>Links</strong><br />
Note the following might be more desirable to copy and pasting the above as they use tabs rather than spaces.<br />
<a href="http://www.stealthcopter.com/files/python/queens.py">queens.py</a> &#8211; The normal version<br />
<a href="http://www.stealthcopter.com/files/python/queens_commented.py">queens_commented.py</a> &#8211; The heavily commented version.</p>
<p><a href="http://www.stealthcopter.com/files/python/evolutionary.ods">evolutionary.ods</a> &#8211; Analysis in ods format (open office)<br />
<a href="http://www.stealthcopter.com/files/python/queens_commented.py">evolutionary.xls</a> &#8211; Analysis in excel format</p>
<p>If you have any corrections or suggestions please contact me.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stealthcopter.com/blog/2009/09/evolutionary-algorithms-in-python-queens-example/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Python: Making multi depth directories</title>
		<link>http://www.stealthcopter.com/blog/2009/09/python-making-multi-depth-directories/</link>
		<comments>http://www.stealthcopter.com/blog/2009/09/python-making-multi-depth-directories/#comments</comments>
		<pubDate>Fri, 11 Sep 2009 11:26:28 +0000</pubDate>
		<dc:creator>mat</dc:creator>
				<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=164</guid>
		<description><![CDATA[If you try and write a file to something like &#8216;subfolder/file.txt&#8217; in python it wont automatically create the subfolder for you and it will fail with: IOError: [Errno 2] No such file or directory: 'test2/file.txt' I imagine this is deliberate as you don&#8217;t want to be accidentally creating folders. So to make a single folder [...]]]></description>
			<content:encoded><![CDATA[<p>If you try and write a file to something like &#8216;subfolder/file.txt&#8217; in python it wont automatically create the subfolder for you and it will fail with:</p>
<pre>
IOError: [Errno 2] No such file or directory: 'test2/file.txt'
</pre>
<p>I imagine this is deliberate as you don&#8217;t want to be accidentally creating folders. So to make a single folder we import the os module, and use the mkdir command</p>
<pre name="code" class="python">
import os
os.mkdir('newfolder')
</pre>
<p>This is good, but if we try and make a subdirectories using the single command then it will fail as this command will only try and create the last directory in the string you pass. IE: if you pass dir1/dir2/dir3 it will assume dir1 and dir2 already exist and try to make dir3, which will fail with:</p>
<pre>
OSError: [Errno 2] No such file or directory: 'dir1/dir2/dir3'
</pre>
<p>So in order for this to work we would need to make it recursivly create subdirectories, but thankfully someone already went to the trouble for us with &#8216;supermakedir&#8217; or makedirs.</p>
<pre name="code" class="python">
import os
os.makedirs('dir1/dir2/dir3')
</pre>
<p>So this is probably useful enough for most people, but I needed to create folders in order for a file to get dropped in it, however I was only given the full path for the file not seperated into filename and directory path.</p>
<p>So I made the following function that accepts an input such as &#8216;dir1/dir2/dir3/filename.txt&#8217; and cuts it into the folder path using os.path.dirname(filename), and then checks if the folder exists using os.path.exists(folder), and then makes the folders if they don&#8217;t already exist.</p>
<pre name="code" class="python">
def mkdirnotex(filename):
	folder=os.path.dirname(filename)
	if not os.path.exists(folder):
		os.makedirs(folder)
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.stealthcopter.com/blog/2009/09/python-making-multi-depth-directories/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python: Calculating pi using random numbers</title>
		<link>http://www.stealthcopter.com/blog/2009/09/python-calculating-pi-using-random-numbers/</link>
		<comments>http://www.stealthcopter.com/blog/2009/09/python-calculating-pi-using-random-numbers/#comments</comments>
		<pubDate>Thu, 10 Sep 2009 23:01:06 +0000</pubDate>
		<dc:creator>mat</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[maths]]></category>

		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=140</guid>
		<description><![CDATA[During my undergraduate degree I wrote a program in fortran 95 to calculate pi using random numbers. My aim is to rewrite it efficiently in python. I know its a terrible way to calculate pi, and there are much better ways to do it but its fun! First I&#8217;ll explain the maths so you can [...]]]></description>
			<content:encoded><![CDATA[<p>During my undergraduate degree I wrote a program in fortran 95 to calculate pi using random numbers. My aim is to rewrite it efficiently in python. I know its a terrible way to calculate pi, and there are much better ways to do it but its fun!</p>
<p>First I&#8217;ll explain the maths so you can visualise what&#8217;s going on. As we should know <a href="http://www.weebls-stuff.com/wab/pie/">_pi_</a> is the ratio of circle&#8217;s radius to its circumference, which is conveniently the same as the ratio of a circle&#8217;s area to the square of its radius <a href="http://en.wikipedia.org/wiki/Pi">(wiki&#8230;)</a></p>
<p>So what we are going to be doing is picking lots of random coordinates in an x-y grid and calculating if they are within the circle or the square.<br />
<img src="http://www.stealthcopter.com/blog/wp-content/uploads/2009/09/pypi1.png" alt="python-pi-random-numbers-1" /><br />
We will assign the radius to be 1, because that makes it easy to work with. By default a random number in python ( random() ) will return a floating point number between 0 and 1. To test if a point is within a circle we simply use <a href="http://en.wikipedia.org/wiki/Pythagoras">Pythagoras</a>.</p>
<p>So if the sqrt(a**2+b**2)&lt;=1 then the point lies inside the circle&#8217;s radius. In the diagram above we see that point A lies within the circle, and point B lies outside the circle.</p>
<p>We can really don&#8217;t need to use the whole circle as it has symmetry, so we can just take a quartre, which makes the generating of random numbers easier as you only need to use a random number for x and y between 0 and 1, rather than -1 and 1. It will look like the diagram below.</p>
<p><img src="http://www.stealthcopter.com/blog/wp-content/uploads/2009/09/pypi2.png" alt="python-pi-random-numbers-2" /></p>
<p>Now for a confusing bit of maths. We are calculating the ratio of the area of a circle to the area of a square.</p>
<pre>
# Area of circle
A=pi*r**2
# where r = 1
A = pi
# Area of square
A = l ** 2
# in this case (see diagram) our square's length is twice the radius
l=2*r
A=(1+1)**2 = 4

#Therefore our ratio will be pi : 4.
# Which means we must multiply our result by four to get pi.
</pre>
<p><strong>Final version (efficient for using)</strong></p>
<pre name="code" class="python">from random import *
from math import sqrt
inside=0
n=1000
for i in range(0,n):
	x=random()
	y=random()
	if sqrt(x*x+y*y)&lt;=1:
		inside+=1
pi=4*inside/n
print pi</pre>
<p>Below we can see the values it creates </p>
<pre>
n	calc	error
1	4.00000000	0.73686317
10	3.60000000	0.45840735
100	3.24000000	0.09840735
1000	3.06400000	-0.07759265
10000	3.16160000	0.02000735
100000	3.14140000	-0.00019265
1000000	3.14293600	0.00134335
10000000	3.14117920	-0.00041345
</pre>
<p><img src="http://www.stealthcopter.com/blog/wp-content/uploads/2009/09/pierror.png" alt="pi-random-number-error" /></p>
<p>So we can see that the program quickly solves pi to about two decimal places, but it is a terribly inefficient method and will struggle to get much more accuracy than this.</p>
<p>Resources to check out:<br />
<a href="http://wj32.wordpress.com/2007/12/04/ways-to-calculate-pi-in-python/">This blog post</a> &#8211; Solves pi via taylor series expansion<br />
<a href="http://www.xtremesystems.com/superpi/">Super pi</a> &#8211; Program that calculate pi often used for benchmarking</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stealthcopter.com/blog/2009/09/python-calculating-pi-using-random-numbers/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Python: palindrome checking function</title>
		<link>http://www.stealthcopter.com/blog/2009/09/python-palindrome-checking-function/</link>
		<comments>http://www.stealthcopter.com/blog/2009/09/python-palindrome-checking-function/#comments</comments>
		<pubDate>Mon, 07 Sep 2009 18:28:24 +0000</pubDate>
		<dc:creator>mat</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[maths]]></category>
		<category><![CDATA[project euler]]></category>

		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=134</guid>
		<description><![CDATA[I created a reasonable palindrome checking function in python. Method 1 def ispalindrome(num): n=str(num) while len(n)&#62;1: print n if n[0]!=n[-1]: return 0 n=n[1:-1] return 1 I thought that it would be faster avoiding a string conversion, and to somehow use the modulus (modulo) function. However when I came to write it, I found it quite [...]]]></description>
			<content:encoded><![CDATA[<p>I created a reasonable palindrome checking function in python.</p>
<p><strong>Method 1</strong></p>
<pre name="code" class="python">
def ispalindrome(num):
	n=str(num)
	while len(n)&gt;1:
		print n
		if n[0]!=n[-1]:
			return 0
		n=n[1:-1]
	return 1
</pre>
<p>I thought that it would be faster avoiding a string conversion, and to somehow use the modulus (modulo) function. However when I came to write it, I found it quite difficult to code, and I&#8217;m sure there must be a better way.</p>
<p><strong>Method 2</strong></p>
<pre name="code" class="python">
def ispalindrome2(num):
	l=1
	while num/10**l&gt;=1.0:
		l+=1
	r=0
	d=[]
	for i in range(1,l+1):
		p=num%10**i-r
		r+=p
		p=p/10**(i-1)
		d.append(p)

	for i in range(0,l/2):
		if d[i]!=d[-i-1]:
			return 0
	return 1
</pre>
<p>Doing the speed tests show that Method 1 is over 5.2 times faster than Method 2. </p>
<pre>
Method 1
0.355437994003 Seconds elapsed
Method 2
1.85815691948 Seconds elapsed
</pre>
<p><strong>Update: Mike of <a href="http://www.mikemeat.co.uk/blog">mikemeat</a> sent me his method using slices (Method 3) and I adapted it slight (Method 4), then shortly after I realised it could be even more efficient by not needing to differentiate between even and odd strings (Method 5).</strong></p>
<p><strong>Method 3</strong></p>
<pre name="code" class="python">
def ispalindrome3(x):
    z = str(x)
    if len(z)%2 == 0 and z[:len(z)/2]==z[-len(z)/2:][::-1]:
       return 1
    if len(z)%2 != 0 and z[:(len(z)- 1)/2]==z[(-len(z) + 1)/2:][::-1]:
       return 1
    else:
	return 0
</pre>
<p><strong>Method 4</strong></p>
<pre name="code" class="python">
def ispalindrome4(x):
	z = str(x)
	if z[:len( z)/2]==z[len( z)/2+len( z)%2:][::-1]:
		return 1
	return 0
</pre>
<p><strong>Method 5</strong></p>
<pre name="code" class="python">
def ispalindrome5(x):
	z = str(x)
	l=len(z)/2
	if z[:l]==z[-l:][::-1]:
		return 1
	return 0
</pre>
<pre>
Method 1
0.357168912888 Seconds elapsed
Method 2
1.83943104744 Seconds elapsed
Method 3
0.179126977921 Seconds elapsed
Method 4
0.179482936859 Seconds elapsed
Method 5
0.149376153946 Seconds elapsed
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.stealthcopter.com/blog/2009/09/python-palindrome-checking-function/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Python: sum of digits in a string</title>
		<link>http://www.stealthcopter.com/blog/2009/09/python-sum-of-digits-in-a-string/</link>
		<comments>http://www.stealthcopter.com/blog/2009/09/python-sum-of-digits-in-a-string/#comments</comments>
		<pubDate>Fri, 04 Sep 2009 03:29:49 +0000</pubDate>
		<dc:creator>mat</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[project euler]]></category>

		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=127</guid>
		<description><![CDATA[I have a function I wrote for a project euler that calculates the sum of the digits in a number. This is my first attempt which simply converts each letter to an integer and sums them. Method 1: def digitsum(x): total=0 for letter in str(x): total+=int(letter) return total I thought that this could be improved [...]]]></description>
			<content:encoded><![CDATA[<p>I have a function I wrote for a <a href="http://www.projecteuler.net">project euler</a> that calculates the sum of the digits in a number. This is my first attempt which simply converts each letter to an integer and sums them.</p>
<p><strong>Method 1:</strong></p>
<pre name="code" class="python">
def digitsum(x):
	total=0
	for letter in str(x):
		total+=int(letter)
	return total
</pre>
<p>I thought that this could be improved using ord, which converts a letter into its decimal ascii number. Numbers &#8217;0&#8242;, &#8217;1&#8242;, &#8217;2&#8242; &#8230; &#8217;9&#8242; correspond to the ascii values of 48 &#8211; 57 and then took the moduli of this with 48 to give the integer value. I later realised that this was completely nonsensical and should have just subtracted 48, but I decided to include it for the purposes of the speed test.</p>
<p><strong>Method 2:</strong></p>
<pre name="code" class="python">
def digitsum2(x):
	total=0
	for letter in str(x):
		total+=ord(letter)%48
	return total
</pre>
<p><strong>Method 3:</strong></p>
<pre name="code" class="python">
def digitsum3(x):
	total=0
	for letter in str(x):
		total+=ord(letter)-48
	return total
</pre>
<p><strong>Speed Test:</strong><br />
The test uses a long number and one million repetitions for each method.</p>
<pre name="code" class="python">
from time import time

# .. functions go here

# Nice long number to sum
x=981234153134415646571899783156122451653

tic = time()
for i in range(0,1000000):
	digitsum(x)
print time() - tic, 'Seconds elapsed'

tic = time()
for i in range(0,1000000):
	digitsum2(x)
print time() - tic, 'Seconds elapsed'

tic = time()
for i in range(0,1000000):
	digitsum3(x)
print time() - tic, 'Seconds elapsed'
</pre>
<p><strong>Results:</strong></p>
<pre>
#Method 1
29.3496568203 Seconds elapsed
#Method 2
12.185685873 Seconds elapsed
#Method 3
9.59367895126 Seconds elapsed
</pre>
<p>So we can see that the first method is much slower, avoiding the integer conversion by using ord speeds it up the function by ~60% and that using subtraction rather than modulus (a division based operation) saves a further ~20% on top of this.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stealthcopter.com/blog/2009/09/python-sum-of-digits-in-a-string/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Python: crossword solver + dictionary file</title>
		<link>http://www.stealthcopter.com/blog/2009/09/python-crossword-solver-dictionary-file/</link>
		<comments>http://www.stealthcopter.com/blog/2009/09/python-crossword-solver-dictionary-file/#comments</comments>
		<pubDate>Thu, 03 Sep 2009 18:24:18 +0000</pubDate>
		<dc:creator>mat</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[strings]]></category>

		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=113</guid>
		<description><![CDATA[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: line=line.strip() if len(line)==len(word): good=1 pos=0 for letter in word: if not letter=='*': if not letter==line[pos]: good=0 pos+=1 if good==1: print line f.close() Example usage: Crossword Solver use * [...]]]></description>
			<content:encoded><![CDATA[<p>This is a quick and dirty crossword solver that I wrote in python:</p>
<pre name="code" class="python">
word=raw_input('Crossword Solver \nuse * as a wildcard: ')
f=open('dic.txt', 'r')
for line in f:
	line=line.strip()
        if len(line)==len(word):
		good=1
		pos=0
		for letter in word:
			if not letter=='*':
				if not letter==line[pos]:
					good=0
			pos+=1
		if good==1:
			print line
f.close()
</pre>
<p>Example usage:<br />
<code><br />
Crossword Solver<br />
use * as a wildcard: *arn*val<br />
carnival<br />
</code><br />
The dictionary file I used is 608.2Kb with 80,368 english words and avaliable <a href="http://www.stealthcopter.com/files/dic.txt">here</a> </code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.stealthcopter.com/blog/2009/09/python-crossword-solver-dictionary-file/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Python: diskspace</title>
		<link>http://www.stealthcopter.com/blog/2009/09/python-diskspace/</link>
		<comments>http://www.stealthcopter.com/blog/2009/09/python-diskspace/#comments</comments>
		<pubDate>Thu, 03 Sep 2009 18:06:22 +0000</pubDate>
		<dc:creator>mat</dc:creator>
				<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=109</guid>
		<description><![CDATA[I wanted a simple function to use in a program I am writing to ensure that the disk isn&#8217;t getting full, after a quick search I found a blog post on thinkhole.org with a great solution: # os module required import os # retrieves information for the harddrive where root is mounted # in windows [...]]]></description>
			<content:encoded><![CDATA[<p>I wanted a simple function to use in a program I am writing to ensure that the disk isn&#8217;t getting full, after a quick search I found a blog post on <a href="http://thinkhole.org/wp/2006/02/16/using-osstatvfs-to-check-disk-space/">thinkhole.org</a> with a great solution:</p>
<pre name="code" class="python">
# 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
</pre>
<p>You can argue about if they should be KiB or KB if you want, but i take them as 1024 bytes in a <a href="http://en.wikipedia.org/wiki/Kilobyte">kilobyte</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.stealthcopter.com/blog/2009/09/python-diskspace/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Python: Checking if a number is prime</title>
		<link>http://www.stealthcopter.com/blog/2009/09/python-checking-if-a-number-is-prime/</link>
		<comments>http://www.stealthcopter.com/blog/2009/09/python-checking-if-a-number-is-prime/#comments</comments>
		<pubDate>Thu, 03 Sep 2009 11:08:30 +0000</pubDate>
		<dc:creator>mat</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[project euler]]></category>

		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=78</guid>
		<description><![CDATA[I&#8217;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 [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been doing a lot of problem solving on <a href="http://projecteuler.net/">Project Euler</a> 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: <a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes">Sieve of Eratosthenes</a> for a clear explanation / animation). </p>
<p>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</p>
<pre name="code" class="python">
def isprime(number):
	if number&lt;=1:
		return 0
	check=2
	maxneeded=number
	while check&lt;maxneeded+1:
		maxneeded=number/check
		if number%check==0:
			return 0
		check+=1
	return 1
</pre>
<p>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.</p>
<p>Check out my <a href="http://projecteuler.net/index.php?section=profile&#038;profile=gothicbob">profile</a> to see which problems I&#8217;ve completed. If you&#8217;ve completed any I haven&#8217;t please get in contact and give me some tips.</p>
<p><strong>Update:</strong><br />
<a href="http://www.mikemeat.co.uk">Mike</a> 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.</p>
<pre name="code" class="python">
def isprime(number):
	if number&lt;=1 or number%2==0:
		return 0
	check=3
	maxneeded=number
	while check&lt;maxneeded+1:
		maxneeded=number/check
		if number%check==0:
			return 0
		check+=2
	return 1
</pre>
<p>So lets test the speed difference by doing 1,000 checks of the number 982,451,653 (known to be prime from this useful<a href="http://primes.utm.edu/lists/small/millions/">site</a>)</p>
<p>Original Code:   9.99917411804 Seconds elapsed<br />
Improved Code: 5.2977039814 Seconds elapsed</p>
<p>That&#8217;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.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stealthcopter.com/blog/2009/09/python-checking-if-a-number-is-prime/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>
