Prime words: Difference between revisions

Added Python implementation
(Added Python implementation)
Line 740:
meg
q
</pre>
 
=={{header|Python}}==
Given that the set is known and quite small, 52 in length, a bit of pre-processing to determine all prime positioned characters before hand will speed up the task significantly rather than determining it every time for every word in the list.
 
Here is the brute force pre-processor, again this can be done easily manually, but if you are feeling lazy, like me :)
<lang Python>
for i in range(65,123):
check = 1
for j in range(2,i):
if i%j == 0:
check = 0
if check==1:
print(chr(i),end='')
<lang>
 
This produces the following string :
 
<pre>
CGIOSYaegkmq
</pre>
 
It's possible to speed it up even more by dropping capitals since the file only contains lower case characters, but we will give that a pass.....
 
And here's the main program which hardcodes the output of the pre-processor above. As always :
 
Tested on Python 3+, the file download will work only if the link is still active. It is possible that you may be able to fetch the file in your browser but download via code may still fail. Check whether you are connected to a VPN, it works on open networks.
 
<lang Python>
import urllib.request
from collections import Counter
urllib.request.urlretrieve("http://wiki.puzzlers.org/pub/wordlists/unixdict.txt", "unixdict.txt")
dictionary = open("unixdict.txt","r")
wordList = dictionary.read().split('\n')
dictionary.close()
primeSet = set("CGIOSYaegkmq")
 
[print(word) for word in wordList if len(set(word).difference(primeSet))==0]
</lang>
 
And here's the final output :
{{Output}}
<pre>
a
aaa
age
agee
ak
am
ama
e
egg
eke
em
emma
g
ga
gag
gage
gam
game
gamma
ge
gee
gem
gemma
gm
k
keg
m
ma
mae
magma
make
mamma
me
meek
meg
q
 
</pre>
 
503

edits