Skip to content

Instantly share code, notes, and snippets.

@christianp
Last active October 25, 2015 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christianp/759ab9417afc032b0e18 to your computer and use it in GitHub Desktop.
Save christianp/759ab9417afc032b0e18 to your computer and use it in GitHub Desktop.
Venn words
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# load my list of words\n",
"words = set(open('2of12.txt').read().split('\\n')[:-1])"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from itertools import permutations,combinations,groupby\n",
"\n",
"# get words of at most 8 characters\n",
"short_words = [word for word in words if len(word)<=8]\n",
"\n",
"# find pairs of words which form two parts of a bigger word\n",
"pairs = set([(a,b) for a,b in permutations(q,2) if a+b in words])\n",
"\n",
"d = {a:list(b) for a,b in groupby(pairs,key=lambda x:x[0])}"
]
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"south east ward | southeast southward eastward | southeastward\n",
"ad o re | ado ore read | adore\n",
"sh all ow | shall show allow | shallow\n",
"re press ed | repress reed pressed | repressed\n",
"north east ward | northeast northward eastward | northeastward\n",
"pock mark ed | pockmark pocked marked | pockmarked\n",
"dis agree able | disagree disable agreeable | disagreeable\n",
"be fall en | befall been fallen | befallen\n"
]
}
],
"source": [
"from itertools import permutations\n",
"for a,b in pairs: #for every pair of words that combine\n",
" for _,c in d.get(b,[]): # for every third word that combines with b\n",
" if (a,c) in pairs or (c,a) in pairs: # if c combines with b\n",
" for three in [''.join(x) for x in permutations((a,b,c))]: # for every combination of a,b and c\n",
" if three in words: # if a+b+c is a word\n",
" twos = [x+y for x,y in permutations((a,b,c),2) if x+y in words]\n",
" print('{} {} {} | {} | {}'.format(a,b,c,' '.join(twos),three))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment