Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion RandomWriter/RandomWriter.pro
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ win32 {
QMAKE_LFLAGS += -Wl,--stack,536870912
LIBS += -lDbghelp
LIBS += -lbfd
LIBS += -liberty
# LIBS += -liberty
LIBS += -limagehlp
}
macx {
Expand Down
80 changes: 80 additions & 0 deletions RandomWriter/res/Ham.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
THE TRAGEDY OF HAMLET, PRINCE OF DENMARK

by William Shakespeare

ACT I. Scene I.
Elsinore. A platform before the Castle.

Enter two Sentinels-[first,] Francisco, [who paces up and down
at his post; then] Bernardo, [who approaches him].

Ber. Who's there.?
Fran. Nay, answer me. Stand and unfold yourself.
Ber. Long live the King!
Fran. Bernardo?
Ber. He.
Fran. You come most carefully upon your hour.
Ber. 'Tis now struck twelve. Get thee to bed, Francisco.
Fran. For this relief much thanks. 'Tis bitter cold,
And I am sick at heart.
Ber. Have you had quiet guard?
Fran. Not a mouse stirring.
Ber. Well, good night.
If you do meet Horatio and Marcellus,
The rivals of my watch, bid them make haste.

Enter Horatio and Marcellus.

Fran. I think I hear them. Stand, ho! Who is there?
Hor. Friends to this ground.
Mar. And liegemen to the Dane.
Fran. Give you good night.
Mar. O, farewell, honest soldier.
Who hath reliev'd you?
Fran. Bernardo hath my place.
Give you good night. Exit.
Mar. Holla, Bernardo!
Ber. Say-
What, is Horatio there ?
Hor. A piece of him.
Ber. Welcome, Horatio. Welcome, good Marcellus.
Mar. What, has this thing appear'd again to-night?
Ber. I have seen nothing.
Mar. Horatio says 'tis but our fantasy,
And will not let belief take hold of him
Touching this dreaded sight, twice seen of us.
Therefore I have entreated him along,
With us to watch the minutes of this night,
That, if again this apparition come,
He may approve our eyes and speak to it.
Hor. Tush, tush, 'twill not appear.
Ber. Sit down awhile,
And let us once again assail your ears,
That are so fortified against our story,
What we two nights have seen.
Hor. Well, sit we down,
And let us hear Bernardo speak of this.
Ber. Last night of all,
When yond same star that's westward from the pole
Had made his course t' illume that part of heaven
Where now it burns, Marcellus and myself,
The bell then beating one-

Enter Ghost.

Mar. Peace! break thee off! Look where it comes again!
Ber. In the same figure, like the King that's dead.
Mar. Thou art a scholar; speak to it, Horatio.
Ber. Looks it not like the King? Mark it, Horatio.
Hor. Most like. It harrows me with fear and wonder.
Ber. It would be spoke to.
Mar. Question it, Horatio.
Hor. What art thou that usurp'st this time of night
Together with that fair and warlike form
In which the majesty of buried Denmark
Did sometimes march? By heaven I charge thee speak!
Mar. It is offended.
Ber. See, it stalks away!
Hor. Stay! Speak, speak! I charge thee speak!

THE END
155 changes: 154 additions & 1 deletion RandomWriter/src/RandomWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,162 @@
#include "random.h"
#include "strlib.h"
#include "vector.h"
#include "filelib.h"
#include "simpio.h"
using namespace std;

/**
* Request the file name and open the file if the name is valid.
*
* @param inputFile - The file for open.
*/
void openFile(ifstream &inputFile) {
promptUserForFile(inputFile, "Enter the file name: ");
}

/**
* Close the file.
*
* @param inputFile - The file for close.
*/
void closeFile(ifstream &inputFile) {
inputFile.close();
}

/**
* Request the model level.
*
* @return - The model level value.
*/
int getModelLevel() {
int modelLevel;
while (true) {
modelLevel = getInteger("Enter model level (1-10): ");
if ((modelLevel >= 1) && (modelLevel <= 10))
break;
cout << "Wrong input data." << endl;
}
return modelLevel;
}

/**
* Read the first seed from the file beginning.
*
* @param inputFile - The file for read.
* @param modelLevel - Set the seed length.
* @param seed - The read seed.
* @param nextChar - The next char after the seed.
* @return - True if the seed and the next char are read, or false in other case.
*/
bool readFirstSeed(ifstream &inputFile, int modelLevel, string &seed, char &nextChar) {
for (int i = 0; i < modelLevel; i ++) {
char ch = inputFile.get();
if (ch == EOF)
return false;
seed += ch;
}
if (!inputFile.get(nextChar))
return false;
return true;
}

/**
* Read the next seed after the current seed.
*
* @param inputFile - The file for read.
* @param seed - The current seed before reading. The next seed after reading.
* @param nextChar - The next char after the seed.
* @return - True if the seed and the next char are read, or false in other case.
*/
bool readNextSeed(ifstream &inputFile, string &seed, char &nextChar) {
seed.erase(0, 1);
seed += nextChar;
if (!inputFile.get(nextChar))
return false;
return true;
}

/**
* Read the all seeds from the file.
*
* @param inputFile - The file for read.
* @param modelLevel - Set the seed length.
* @return - The map of the read seeds and the next chars.
*/
Map<string, Vector<char>> readAllSeeds(ifstream &inputFile, int modelLevel) {
Map<string, Vector<char>> result;
string seed;
char nextChar;
cout << "The file is processed..." << endl;
// Read the first seed. If the first seed exists in the file, add it to result map.
if (readFirstSeed(inputFile, modelLevel, seed, nextChar))
result[seed].add(nextChar);
// Read the next seeds while they exists in the file.
while (true) {
if (!readNextSeed(inputFile, seed, nextChar))
break;
result[seed].add(nextChar);
}
return result;
}

/**
* Find and return the most frequency seed.
*
* @param seeds - The map of the seeds and the next chars.
* @return - The most frequency seed.
*/
string getFirstSeed(Map<string, Vector<char>> &seeds) {
Vector<string> keys = seeds.keys();
string result = keys[0];
// Find the seed with the longest vector of the next chars.
for (string key : seeds)
if (seeds[key].size() > seeds[result].size())
result = key;
return result;
}

/**
* Find the next seed after the current seed by add the next char.
*
* @param seeds - The map of the seeds and the next chars.
* @param seed - The current seed before search. The next seed after search.
* @return - The next char from the current seeds next chars vector.
*/
char getNextSeed(Map<string, Vector<char>> &seeds, string &seed) {
Vector<char> nextChars = seeds[seed];
// Choose the next char from the current seeds next chars vector.
char result = nextChars[randomInteger(0, nextChars.size() - 1)];
// Create the next seed by the current seeds one char shifting left and add the choosed next char.
seed.erase(0, 1);
seed += result;
return result;
}

/**
* Generate randomly english text lenth 2000 chars.
*
* @param seeds - The map of the seeds and the next chars.
* @return - The randomly generated text.
*/
string generateText(Map<string, Vector<char>> &seeds) {
string seed = getFirstSeed(seeds);
string text = seed;
while (true) {
text += getNextSeed(seeds, seed);
if (text.length() > 2000)
break;
}
return text;
}

int main() {
// TODO: fill in the code
ifstream inputFile;
openFile(inputFile);
int modelLevel = getModelLevel();
Map<string, Vector<char>> seeds = readAllSeeds(inputFile, modelLevel);
closeFile(inputFile);
string text = generateText(seeds);
cout << text << endl;
return 0;
}
2 changes: 1 addition & 1 deletion WordLadder/WordLadder.pro
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ win32 {
QMAKE_LFLAGS += -Wl,--stack,536870912
LIBS += -lDbghelp
LIBS += -lbfd
LIBS += -liberty
# LIBS += -liberty
LIBS += -limagehlp
}
macx {
Expand Down
122 changes: 121 additions & 1 deletion WordLadder/src/WordLadder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,127 @@
#include "vector.h"
using namespace std;

/**
* Find and return all "neighbor" words of the current word except the used words.
*
* @param word - The current word.
* @param englishWords - All english words.
* @param usedWords - The used words.
* @return - All "neighbor" words.
*/
Vector<string> getNeighborWords(string word, Lexicon &englishWords, Lexicon &usedWords) {
Vector<string> result;
// Add current word to the list of used words if it is not there.
if (!usedWords.contains(word))
usedWords.add(word);
// Generate new words from the current word by change one char.
for (int i = 0; i < word.length(); i ++) {
string copyWord = word;
for (char c = 'a'; c <= 'z'; c ++){
copyWord.replace(i, 1, 1, c);
// Add new generated word to the list of "neighbor" words and to the list of used words
// if it is a real english word and it is not used earlier.
if ((englishWords.contains(copyWord)) && (!usedWords.contains(copyWord))) {
result.add(copyWord);
usedWords.add(copyWord);
}
}
}
return result;
}

/**
* Put the ladder to the queue.
*
* @param ladder - The ladder.
* @param ladders - The ladders queue.
*/
void putToQueue(Vector<string> ladder, Queue<Vector<string>> &ladders) {
ladders.enqueue(ladder);
}

/**
* Put the word to the queue. Create the first ladder.
*
* @param word - The first word in the queue.
* @param ladders - The ladders queue.
*/
void putToQueue(string word, Queue<Vector<string>> &ladders) {
Vector<string> ladder(1, word);
ladders.enqueue(ladder);
}

/**
* Get the ladder from the queue.
*
* @param ladders - The ladders queue.
* @return - The ladder.
*/
Vector<string> getFromQueue(Queue<Vector<string>> &ladders) {
return ladders.dequeue();
}

/**
* Find and return the shortest ladder from source word to the destination word.
*
* @param sourceWord - The source word.
* @param destWord - The destination word.
* @param englishWords - All english words.
* @return - The found shortest ladder if it exists or empty ladder in other case.
*/
Vector<string> findShortestLadder(string sourceWord, string destWord, Lexicon & englishWords) {
Queue<Vector<string>> ladders;
Lexicon usedWords;
// Put the sourse word to the queue.
putToQueue(sourceWord, ladders);
// Repeat search while ladders queue contains ladders.
while (!ladders.isEmpty()) {
// Get ladder from queue and get its last word.
Vector<string> ladder = getFromQueue(ladders);
string lastWord = ladder[ladder.size() - 1];
// Return the ladder if the last word of the ladder is the destination word.
if (lastWord == destWord)
return ladder;
// Get "neighbor" words of the last word in the ladder except the used words.
Vector<string> neighborWords = getNeighborWords(lastWord, englishWords, usedWords);
// For each "neighbor" word create new ladder by add it word to the end of the ladders copy.
// The new ladder put to the queue.
for (string word : neighborWords) {
Vector<string> copyLadder = ladder;
copyLadder.add(word);
putToQueue(copyLadder, ladders);
}
}
// Return the empty ladder if the shortest ladder is not found.
Vector<string> ladder;
return ladder;
}

/**
* Output the shortest ladder if it is found or info message in other case.
*
* @param shortestLadder - The ladder.
*/
void showResults(Vector<string> ladder) {
if (ladder.size() != 0)
for (int i = 0; i < ladder.size(); i ++)
if (i != ladder.size() - 1)
cout << ladder[i] << " -> ";
else
cout << ladder[i] << endl;
else
cout << "No ladder found." << endl;
}

int main() {
// TODO: fill in the code
Lexicon englishWords("EnglishWords.txt");
while (true) {
string sourceWord = getLine("Enter source word (RETURN to quit): ");
if (sourceWord.length() == 0)
break;
string destWord = getLine("Enter destination word: ");
Vector<string> shortestLadder = findShortestLadder(sourceWord, destWord, englishWords);
showResults(shortestLadder);
}
return 0;
}