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) {
|
2021-07-19 22:15:11 +02:00
|
|
|
string s2;
|
2021-07-23 12:31:19 +02:00
|
|
|
for (char c : s) {
|
2021-07-19 22:15:11 +02:00
|
|
|
s2.push_back(toupper(c));
|
|
|
|
}
|
|
|
|
return s2;
|
|
|
|
}
|
|
|
|
|
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-07-19 22:15:11 +02:00
|
|
|
string word;
|
|
|
|
};
|
|
|
|
typedef vector<Word> Words;
|
2021-07-23 12:31:19 +02:00
|
|
|
typedef unordered_map<string, Word> WordMap; // hash-table from stl
|
2021-07-19 22:15:11 +02:00
|
|
|
|
2021-08-05 19:29:56 +02:00
|
|
|
// --------------------------------------------------------------------------------
|
|
|
|
//-Library
|
2021-07-23 12:31:19 +02:00
|
|
|
class Library {
|
2021-07-19 22:15:11 +02:00
|
|
|
public:
|
2021-07-23 12:31:19 +02:00
|
|
|
Library() {} // hash-tables are automatically initialized
|
2021-08-05 19:29:56 +02:00
|
|
|
void FindWord(const string& s) const { // references are prefered instead of copies
|
|
|
|
int len = s.length(); // cache the length to avoid multiple calls
|
|
|
|
for (const Word& w : words_) { // references are prefered instead of copies
|
|
|
|
string temp = w.word;
|
|
|
|
if (len == temp.length()) {
|
|
|
|
for (int i=0; i<len; i++) {
|
|
|
|
if (s[i] == '-') {
|
|
|
|
temp[i] = '-';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (s == temp) {
|
|
|
|
cout << w.word << "\n"; // print the original word
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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-19 22:15:11 +02:00
|
|
|
}
|
2021-07-23 12:31:19 +02:00
|
|
|
void ComputeStats() {
|
|
|
|
assert(counts_.empty());
|
|
|
|
counts_.resize(18);
|
|
|
|
for (Word w : words_) {
|
2021-07-19 22:15:11 +02:00
|
|
|
int len = w.word.length();
|
2021-07-23 12:31:19 +02:00
|
|
|
if (len < 18) {
|
|
|
|
counts_[len]++;
|
2021-07-16 19:17:37 +02:00
|
|
|
}
|
|
|
|
}
|
2021-07-19 22:15:11 +02:00
|
|
|
}
|
2021-07-23 12:31:19 +02:00
|
|
|
void PrintStats() const {
|
2021-07-19 22:15:11 +02:00
|
|
|
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-19 22:15:11 +02:00
|
|
|
}
|
2021-07-23 12:31:19 +02:00
|
|
|
string GetWord(int i) const {
|
|
|
|
assert(i >= 0 && i < words_.size());
|
|
|
|
return words_[i].word;
|
2021-07-19 22:15:11 +02:00
|
|
|
}
|
2021-07-23 12:31:19 +02:00
|
|
|
void ReadFromFile(string filename) {
|
2021-07-19 22:15:11 +02:00
|
|
|
ifstream f;
|
|
|
|
f.open(filename);
|
2021-08-05 19:29:56 +02:00
|
|
|
while (f.is_open() && !f.eof()) { // check for the file!
|
2021-07-19 22:15:11 +02:00
|
|
|
string line;
|
|
|
|
getline(f, line);
|
|
|
|
// cout << line << "\n";
|
2021-07-23 12:31:19 +02:00
|
|
|
if (!line.empty()) {
|
2021-07-19 22:15:11 +02:00
|
|
|
line = ToUpper(line);
|
|
|
|
int len = line.length();
|
2021-07-23 12:31:19 +02:00
|
|
|
if (line[len - 1] == '\r') {
|
2021-07-19 22:15:11 +02:00
|
|
|
line = line.substr(0, len - 1);
|
2021-07-16 19:17:37 +02:00
|
|
|
}
|
2021-07-23 12:31:19 +02:00
|
|
|
words_.push_back(Word(line));
|
|
|
|
word_map_[line] = Word(line); // create entry for the hash-table
|
2021-07-16 19:17:37 +02:00
|
|
|
}
|
|
|
|
}
|
2021-07-23 12:31:19 +02:00
|
|
|
cout << "Read " << words_.size() << " words from file '"
|
2021-07-19 22:15:11 +02:00
|
|
|
<< 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-19 22:15:11 +02:00
|
|
|
}
|
|
|
|
}
|
2021-07-16 19:17:37 +02:00
|
|
|
|
2021-07-23 12:31:19 +02:00
|
|
|
private: // _ is used to indicate privacy
|
|
|
|
Words words_;
|
|
|
|
WordMap word_map_;
|
|
|
|
vector<int> counts_;
|
2021-07-19 22:15:11 +02:00
|
|
|
};
|
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";
|
|
|
|
}
|
|
|
|
}
|
2021-07-19 22:15:11 +02:00
|
|
|
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
|
|
|
|
|
|
|
lib.FindWord("D--");
|
2021-07-13 17:27:14 +02:00
|
|
|
|
2021-07-16 19:17:37 +02:00
|
|
|
//Grid grid("MY GRID");
|
|
|
|
//grid.LoadFromFile("test");
|
|
|
|
//grid.Check();
|
|
|
|
//grid.Print();
|
2021-07-11 19:11:55 +02:00
|
|
|
}
|