Skip to content

Instantly share code, notes, and snippets.

@chsanch
Last active October 23, 2017 22:30
Show Gist options
  • Save chsanch/96cd86e0f369a8f59a4b7a7608d49fe9 to your computer and use it in GitHub Desktop.
Save chsanch/96cd86e0f369a8f59a4b7a7608d49fe9 to your computer and use it in GitHub Desktop.
Read a file, and obtain all the words that match a letter combinations list
use v6;
my %words{Str};
%words.push: ( $_.comb.sort.join => $_ ) for "lemario-general-del-espanol.txt".IO.lines;
my @letters = 'o','p','d','e','t','o','r';
# get words for the combinations of different lengths of combinations
my %possible_answers{Int};
%possible_answers.push: ( $_.chars => %words{$_} ) for @letters.combinations(3..7).map(*.sort.join).grep({ %words{$_} });
say %possible_answers;
# Python:
from itertools import combinations
from collections import defaultdict
letters = ['o','p','d','e','t','o','r']
words = defaultdict(list)
with open("lemario-general-del-espanol.txt") as f:
for word in f.readlines():
words["".join(sorted(word.strip()))].append(word.strip())
possible_answers = {i : [words["".join(sorted(c))] for c in combinations(letters, i) if "".join(sorted(c)) in words]
for i in range(7, 2, -1)}
print(possible_answers)
@chsanch
Copy link
Author

chsanch commented Oct 23, 2017

$ perl6 --version
This is Rakudo version 2017.07 built on MoarVM version 2017.07
implementing Perl 6.c.

# text file to read: 

http://chsanch.info/lemario-general-del-espanol.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment