Skip to content

Instantly share code, notes, and snippets.

@bdkosher
Created March 23, 2014 22:32
Show Gist options
  • Save bdkosher/9730876 to your computer and use it in GitHub Desktop.
Save bdkosher/9730876 to your computer and use it in GitHub Desktop.
Provide a pattern to a crossword puzzle, (e.g. T _ A _) and find words matching that pattern.
@groovy.transform.Field String pattern = 'A__S__E'
class XWordCategory {
static boolean matchesPattern(String self, String pattern) {
pattern == null ? false : self ==~ pattern.replace('_', /\w{1}/)
}
}
use(XWordCategory) {
assert 'KEPI'.matchesPattern('____')
assert 'KEPI'.matchesPattern('K___')
assert 'KEPI'.matchesPattern('_E__')
assert 'KEPI'.matchesPattern('__P_')
assert 'KEPI'.matchesPattern('___I')
assert 'KEPI'.matchesPattern('KE__')
assert 'KEPI'.matchesPattern('K_P_')
assert 'KEPI'.matchesPattern('K__I')
assert 'KEPI'.matchesPattern('KEP_')
assert 'KEPI'.matchesPattern('KE_I')
assert 'KEPI'.matchesPattern('_EPI')
assert 'KEPI'.matchesPattern('KEPI')
assert 'KEPI'.matchesPattern('_____') == false
assert 'KEPI'.matchesPattern('___') == false
assert 'KEPI'.matchesPattern('T___') == false
assert 'KEPI'.matchesPattern('_A__') == false
/* d/l http://svn.pietdepsi.com/repos/projects/zyzzyva/trunk/data/words/North-American/OWL.txt as local file */
new File(/C:\Users\jwolf2\Desktop\dictionary.txt/).eachLine { line ->
def word = line.split(' ')[0]
if (word.matchesPattern(pattern)) {
println line
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment