Skip to content

Instantly share code, notes, and snippets.

@Cairnarvon
Created July 30, 2014 19:07
Show Gist options
  • Save Cairnarvon/35054024adde92d30bac to your computer and use it in GitHub Desktop.
Save Cairnarvon/35054024adde92d30bac to your computer and use it in GitHub Desktop.
List all words that can be formed by capitalising letters in a given word.
#!/usr/bin/python3
import sys
DICT = '/usr/share/dict/words'
def subs(s):
for i in range(len(s)):
for s2 in subs(s[i + 1:]):
yield s[i] + s2
yield ''
if __name__ == '__main__':
if len(sys.argv) < 2:
print('Usage: {} NAME [ <DICT ]'.format(sys.argv[0]))
sys.exit(1)
name = ''.join(c for c in sys.argv[1].lower() if c.isalpha())
dic = sys.stdin if not sys.stdin.isatty() else open(DICT)
wordpool = set(l.strip().lower() for l in dic)
dic.close()
for s in sorted([s for s in subs(name) if s in wordpool],
key=len, reverse=True):
print(s)
$ ./kolcaps.py Cairnarvon | head
cairn
cairo
cairn
cairo
cairn
canon
arron
aaron
cain
cain
$ ./kolcaps.py Cairnarvon </usr/share/dict/french | head
canon
cira
cran
car
car
car
con
air
air
ira
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment