Files
Landon Curt Noll ab391454b1 fix use of alt code in 2019/ciura
The shell scripts look for and use bash.
The helper scripts have been made excurable.
2023-03-07 17:13:56 -08:00

41 lines
1.0 KiB
Python
Executable File

#!/usr/bin/python3
"""Reads allowed words and writes perfect pangrams composed of them.
Merges anagrams before passing them to ./prog and expands them
in ./prog's output.
"""
import collections
import subprocess
import sys
def main():
"""Wraps the execution of ./prog"""
if len(sys.argv) < 2:
sys.stderr.write('Usage: {} LETTERS [N]\n'.format(sys.argv[0]))
sys.exit(2)
anagrams = collections.defaultdict(list)
for line in sys.stdin:
word = line.strip()
anagrams[''.join(sorted(word))].append(word)
prog = subprocess.Popen(
['./prog'] + sys.argv[1:],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
try:
for word in anagrams:
prog.stdin.write(bytes(word + '\n', 'utf-8'))
except BrokenPipeError:
sys.exit(1)
prog.stdin.close()
for anagram in prog.stdout:
words = anagram.decode('utf-8').split()
print(' '.join('/'.join(anagrams[w]) for w in words))
if __name__ == '__main__':
main()