Based on the comment:
I'm not quite sure if the source code works, and even if it is, I cannot say that I really like it. First of all, based on nesting cycles, it looks like complexity is O (N 2 ). Secondly, I cannot understand what he is doing well enough to be sure that he is really doing what is stated above (he uses the three-parameter equivalentWords overload, which seems to be absent, which complicates its statement).
Some of the Python solutions are much shorter and simpler - to the point that I'm sure they just don't work. The pair seems to be simply printing out unique words that (at least as I interpret it) are not intended at all.
Here is a C ++ version that interprets the requirements:
#include <string> #include <set> #include <vector> #include <algorithm> #include <iostream> #include <map> std::string sort_word(std::string word) { std::sort(word.begin(), word.end()); return word; } namespace std { std::ostream & operator<<(std::ostream &os, std::pair<std::string, std::set<std::string> >const &words) { std::copy(words.second.begin(), words.second.end(), std::ostream_iterator<std::string>(os, "\t")); return os; } } void equivalentWords(std::vector<std::string> const &words, std::ostream &os) { typedef std::map<std::string, std::set<std::string> > word_list_t; word_list_t word_list; for (int i=0; i<words.size(); i++) word_list[sort_word(words[i])].insert(words[i]); std::copy(word_list.begin(), word_list.end(), std::ostream_iterator<word_list_t::value_type>(os, "\n")); } int main() { std::vector<std::string> input; std::copy(std::istream_iterator<std::string>(std::cin), std::istream_iterator<std::string>(), std::back_inserter(input)); equivalentWords(input, std::cout); return 0; }
I think using this as a starting point for a version of Python is more likely to produce clean working results.
Jerry Coffin
source share