<?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; strings</title>
	<atom:link href="http://www.stealthcopter.com/blog/tag/strings/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>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: 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: 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>
	</channel>
</rss>
