libzed 1.9.9
A general-purpose library for quick and simple data manipulation.
 
Loading...
Searching...
No Matches
word_unscramble.cpp
#include <iostream>
#include <z/util/dictionary.hpp>
int main(int argc, char **argv) {
if (argc < 2) {
std::cerr << "This program finds valid dictionary words from a list of scrambled words." << std::endl;
std::cerr << "Usage: " << argv[0] << " <scrambled_word_1> <scrambled_word_2> ..." << std::endl;
return 1;
}
std::cerr << "Loading dictionary... ";
if (dict.read("/usr/share/dict/words")) {
std::cerr << "ERROR: Failed to read dictionary file!" << std::endl;
return 1;
} else {
std::cout << zstring::numberFormat(dict.length()) << " words.\n";
}
for (int i = 1; i < argc; i++) {
// Find all valid permutations of the given scrambled word
// (filter out the trivial word itself if it exists)
const auto validWords = dict.findPermutations(argv[i]).filter([&](const z::core::string<> &word) { return word != argv[i]; });
std::cout << "❬" << argv[i] << "❭ Found " << validWords.length() << " permutation" << (validWords.length() == 1 ? "" : "s") << "." << std::endl;
for (int j = 0; j < validWords.length(); j++) {
std::cout << (j == validWords.length() - 1 ? "└─ " : "├─ ") << validWords[j] << std::endl;
}
}
return 0;
}
A wrapper for std::vector.
Definition array.hpp:72
array filter(std::function< bool(const T &)> lambda) const
Filters the array based on a predicate and returns a new array containing the elements that satisfy t...
Definition array.hpp:1218
int length() const noexcept override
Get the length of the array.
Definition array.hpp:1011
static string numberFormat(double value, int precision=0, bool forcePrecision=false, char decimal='.', char thousands=',') noexcept
Create a string from a number with thousands separators.
A class for performing searches on a dictionary of words.
Definition dictionary.hpp:15
z::core::array< zstring > findPermutations(const zstring &scrambled) const noexcept
Find all permutations of a given scrambled word that are valid words in the dictionary.
bool read(const zstring &filename, const core::timeout &time=-1, bool assumePresorted=false)
Read this dictionary's word list as text from a file.