Coding_for_Crosswords_in_Py.../a.c

225 lines
5.8 KiB
C
Raw Normal View History

2021-07-13 17:27:14 +02:00
#include <iostream> // library for printing
#include <string> // support for strings
#include <vector> // support for vectors
2021-07-23 12:31:19 +02:00
#include <unordered_map> // support for hash-tables
2021-07-13 17:27:14 +02:00
#include <assert.h>
2021-07-15 17:45:54 +02:00
#include <fstream> // support for reading files
2021-07-13 17:27:14 +02:00
using namespace std;
2021-07-11 19:11:55 +02:00
// For compiling C++ code
// g++ a.c -o a
2021-07-16 19:17:37 +02:00
2021-07-23 12:31:19 +02:00
string ToUpper(string s) {
string s2;
2021-07-23 12:31:19 +02:00
for (char c : s) {
s2.push_back(toupper(c));
}
return s2;
}
2021-08-09 18:10:52 +02:00
// --------------------------------------------------------------------------------
//-Point
struct Point {
Point() {}
Point(int r, int c) : row(r), col(c) {}
friend ostream& operator<<(ostream& os, const Point& p); // overloading the "<<" op
int row = 0; // defaults value for the constructor
int col = 0;
};
ostream& operator<<(ostream& os, const Point& p) { // for printing
os << "(" << p.row << "," << p.col << ")";
return os;
}
2021-08-09 18:28:29 +02:00
// --------------------------------------------------------------------------------
//-Span
struct Span {
Span(Point p, int l, bool v) : point(p), len(l), vert(v) {}
friend ostream& operator<<(ostream& os, const Span& s);
Point point;
int len;
bool vert;
};
ostream& operator<<(ostream& os, const Span& s) {
os << "[" << s.point << " len=" << s.len << " vert=" << s.vert << "]";
return os;
}
2021-08-05 19:29:56 +02:00
// --------------------------------------------------------------------------------
//-Word
2021-07-23 12:31:19 +02:00
struct Word {
Word() {} // default empty constructor
Word(string s) : word(s) {} // standard way of initialization
2021-08-05 20:23:12 +02:00
int len() const { return word.length(); }
string word;
};
2021-08-05 20:23:12 +02:00
typedef vector<Word*> Words; // will store pointers to Words (will not delete by itself)
typedef unordered_map<string, Words> WordMap; // hash-table from stl
2021-08-05 19:29:56 +02:00
// --------------------------------------------------------------------------------
//-Library
2021-07-23 12:31:19 +02:00
class Library {
public:
2021-07-23 12:31:19 +02:00
Library() {} // hash-tables are automatically initialized
2021-08-05 20:23:12 +02:00
~Library() { // destructor of the pointers (instead of the unique pointer)
for (Word* w : words_) {
delete w;
}
}
2021-08-05 19:29:56 +02:00
void FindWord(const string& s) const { // references are prefered instead of copies
2021-08-05 20:23:12 +02:00
auto it = word_map_.find(s);
if (it != word_map_.end()) {
for (const Word* w : it->second) {
cout << " " << w->word;
2021-08-05 19:29:56 +02:00
}
2021-08-05 20:23:12 +02:00
cout << "\n";
2021-08-05 19:29:56 +02:00
}
}
2021-07-23 12:31:19 +02:00
bool IsWord(string s) const {
auto it = word_map_.find(s); // use iterator
if (it == word_map_.end()) {
return false; // if word is not found on hast-table
} else {
return true;
2021-07-16 19:17:37 +02:00
}
2021-07-23 12:31:19 +02:00
//return word_map_.count(s) > 0; // True if word exists
}
2021-07-23 12:31:19 +02:00
void ComputeStats() {
assert(counts_.empty());
counts_.resize(18);
2021-08-05 20:23:12 +02:00
for (const Word* w : words_) { // Word is a pointer!
int len = w->word.length(); // w is not an actual object (it's a pointer)
2021-07-23 12:31:19 +02:00
if (len < 18) {
counts_[len]++;
2021-07-16 19:17:37 +02:00
}
}
}
2021-07-23 12:31:19 +02:00
void PrintStats() const {
cout << "Here are the counts of each word length:\n";
2021-07-23 12:31:19 +02:00
for (int i = 1; i < counts_.size(); i++) {
cout << "[" << i << "] " << counts_[i] << "\n";
2021-07-16 19:17:37 +02:00
}
}
2021-07-23 12:31:19 +02:00
string GetWord(int i) const {
assert(i >= 0 && i < words_.size());
2021-08-05 20:23:12 +02:00
return words_[i]->word;
}
void CreatePatternHash(Word* w) {
int len = w->len();
2021-08-09 18:10:52 +02:00
if (len > 7) return; // avoid load of long words
2021-08-05 20:23:12 +02:00
int num_patterns = 1 << len; // create 2^len patterns
// cout << "PATTERN HASH on " << w->word << "\n";
for (int i=0; i<num_patterns; i++) {
// cout << " " << i << "\n";
string temp = w->word;
for (int j=0; j<len; j++) {
if ((i >> j) & 1) { // get every bit and check if it's 1
temp[j] = '-';
}
}
// cout << " " << temp << "\n";
word_map_[temp].push_back(w);
}
}
2021-07-23 12:31:19 +02:00
void ReadFromFile(string filename) {
ifstream f;
f.open(filename);
2021-08-05 19:29:56 +02:00
while (f.is_open() && !f.eof()) { // check for the file!
string line;
getline(f, line);
// cout << line << "\n";
2021-07-23 12:31:19 +02:00
if (!line.empty()) {
line = ToUpper(line);
int len = line.length();
2021-07-23 12:31:19 +02:00
if (line[len - 1] == '\r') {
line = line.substr(0, len - 1);
2021-07-16 19:17:37 +02:00
}
2021-08-05 20:23:12 +02:00
Word* w = new Word(line);
words_.push_back(w); // Word would be allocated on the heap
CreatePatternHash(w);
2021-07-16 19:17:37 +02:00
}
}
2021-07-23 12:31:19 +02:00
cout << "Read " << words_.size() << " words from file '"
<< filename << "'\n";
}
2021-07-23 12:31:19 +02:00
void DebugBuckets() const {
for (int i = 0; i < word_map_.bucket_count(); i++) {
cout << "[" << i << "] " << word_map_.bucket_size(i) << "\n";
}
}
2021-07-16 19:17:37 +02:00
2021-07-23 12:31:19 +02:00
private: // _ is used to indicate privacy
2021-08-05 20:23:12 +02:00
Words words_; // master vector of words
WordMap word_map_; // pattern hash
2021-07-23 12:31:19 +02:00
vector<int> counts_;
};
2021-07-16 19:17:37 +02:00
2021-07-23 12:31:19 +02:00
struct Grid {
Grid(string n) {
2021-07-14 21:10:44 +02:00
name = n;
}
int rows() const { return lines.size(); }
2021-07-23 12:31:19 +02:00
int cols() const {
if (lines.empty()) {
2021-07-14 21:10:44 +02:00
return 0;
2021-07-23 12:31:19 +02:00
} else {
2021-07-15 17:45:54 +02:00
return lines[0].size();
}
}
2021-07-23 12:31:19 +02:00
void LoadFromFile(string filename) {
2021-07-15 17:45:54 +02:00
ifstream f;
f.open("test");
2021-08-05 19:29:56 +02:00
while (f.is_open() && !f.eof()) { // check for the file
2021-07-15 17:45:54 +02:00
string line;
getline(f, line);
// cout << line << "\n";
2021-07-23 12:31:19 +02:00
if (!line.empty() && line[0] != '#') {
2021-07-15 17:45:54 +02:00
lines.push_back(line);
}
}
2021-07-14 21:10:44 +02:00
}
2021-07-23 12:31:19 +02:00
void Check() const {
for (string s : lines) {
2021-07-14 21:10:44 +02:00
assert(s.size() == cols());
}
}
2021-07-23 12:31:19 +02:00
void Print() const {
2021-07-14 21:10:44 +02:00
cout << "Grid: " << name
2021-07-15 17:45:54 +02:00
<< " (rows=" << rows()
<< ", cols=" << cols() << ")\n";
2021-07-23 12:31:19 +02:00
for (string s : lines) {
2021-07-14 21:10:44 +02:00
cout << " " << s << "\n";
}
}
string name; // strings are initialized empty
2021-07-14 21:10:44 +02:00
vector<string> lines;
};
2021-07-11 19:11:55 +02:00
2021-07-23 12:31:19 +02:00
int main() {
2021-07-16 19:17:37 +02:00
Library lib;
lib.ReadFromFile("top_12000.txt");
2021-08-05 19:29:56 +02:00
2021-08-09 18:10:52 +02:00
Grid grid("MY GRID");
grid.LoadFromFile("test");
grid.Check();
grid.Print();
Point p1;
Point p2(2,1);
cout << "Point1 is " << p1 << "\n";
cout << "Point2 is " << p2 << "\n";
2021-08-09 18:28:29 +02:00
Span s1(p1, 3, true);
Span s2(p2, 5, false);
cout << "Span1 is " << s1 << "\n";
cout << "Span2 is " << s2 << "\n";
2021-07-11 19:11:55 +02:00
}