The main point here is to show how some of the more advanced C++ features map to Python code. The point is not what the code does (thus no comments), but how it does it. The example codes should be easily comparable, due to same variable and function names.
#include <string> #include <algorithm> #include <iostream> #include <functional> #include <fstream> #include <map> #include <vector> #include <cctype> using namespace std; typedef multimap<string, string> DicMap; typedef vector<string> Answers; char toNumber(char letter) { static string letter2digit[] = { "E", "JNQ", "RWX", "DSY", "FT", "AM", "CIV", "BKU", "LOP", "GHZ" }; for (int i=0; i<10; ++i) if (letter2digit[i].find(letter) != string::npos) return '0' + i; return '0'; } void findMatch(const string& answer, const string& line, const DicMap& dic, bool allowNumber, Answers& answers) { if (line.empty()) { answers.push_back(answer); return; } for (size_t i = 1; i <= line.length(); ++i) { pair<DicMap::const_iterator, DicMap::const_iterator> range = dic.equal_range(line.substr(0, i)); for (DicMap::const_iterator e = range.first; e != range.second; ++e) { allowNumber = false; findMatch(answer + " " + e->second, line.substr(e->first.length(), line.length()), dic, true, answers); } } if (allowNumber) findMatch(answer + " " + line[0], line.substr(1, line.length()), dic, false, answers); } bool readDic(DicMap& dic) { ifstream in("dic.txt"); if (!in) return false; string line; while (!in.eof()) { getline(in, line); string number = line; number.erase( remove_if(number.begin(), number.end(), not1(ptr_fun(isalpha))), number.end()); transform(number.begin(), number.end(), number.begin(), toupper); transform(number.begin(), number.end(), number.begin(), toNumber); dic.insert(make_pair(number, line)); } return true; } int main() { DicMap dic; if (!readDic(dic)) return 1; ifstream in("ph.txt"); if (!in) return 1; string line; while (!in.eof()) { getline(in, line); string line_real = line; line_real.erase( remove_if(line_real.begin(), line_real.end(), not1(ptr_fun(isdigit))), line_real.end()); Answers answers; findMatch("", line_real, dic, true, answers); for (Answers::iterator i = answers.begin(); i != answers.end(); ++i) cout << line << ":" << *i << endl; } }
import string letter2digit = "E", "JNQ", "RWX", "DSY", "FT", "AM", "CIV", "BKU", "LOP", "GHZ" def toNumber(letter): for i in range(len(letter2digit)): if letter in letter2digit[i]: return chr(ord('0') + i) def findMatch(answer, line, dic, allowNumber, answers): if line: answers.append(answer) return for i in range(1, len(line) + 1): if dic.has_key(line[:i]): allowNumber = False for e in dic[line[:i]]: findMatch(answer + ' ' + e, line[i:], dic, True, answers) if allowNumber: findMatch(answer + ' ' + line[0], line[1:], dic, False, answers) def readDic(): dic = {} for line in file('dic.txt'): line = line.strip() number = filter(lambda x: x in string.ascii_letters, line).upper() number = "".join(map(toNumber, number)) dict[number] = dic.get(number, []) + [line] def main(): dic = readDic() for line in file('ph.txt'): line = line.strip() line_real = filter(lambda x: x in string.digits, line) answers = [] findMatch("", line_real, dic, True, answers) for answer in answers: print line, ':', answer main()In Python sample, the "main"-block was moved into a function for convenience; It need not be there.
Search Encyclopedia
|
Featured Article
|