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
147 changes: 145 additions & 2 deletions RandomWriter/src/RandomWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,152 @@
#include "random.h"
#include "strlib.h"
#include "vector.h"
#include "filelib.h"
#include "simpio.h"
#include "tokenscanner.h"
using namespace std;


/*
The Function is to get the user level model in the form of an integer from 1 -10
param modelLevel - the variable which will contain modelLevel
*/
void getModelLevel(int &modelLevel){
while(true){
modelLevel = getInteger("Enter model level from 1-10: ");
if (modelLevel < 1 || modelLevel >10){
cout<<"You enter the wrong model level!"<<endl;
} else break;
}

}

/*
Function reads the first seed from the file according to the received level model.
param scanner - token scanner for reading characters
param modelLevel - the variable which will contain modelLevel
param seedKey - key first seed to build Map
param nextChar - the next character after seed
*/
void readFirstSeed(TokenScanner &scanner,int &modelLevel,string &seedKey,char &nextChar){

for(int i = 0; i < modelLevel; i++){
nextChar = scanner.getChar();
if(nextChar == EOF )break;
seedKey += nextChar;
}
}

/*Function moves seed by one character
param param seedKey - key first seed to build Map
param nextChar - the next character after seed
param modelLevel - the variable which will contain modelLevel
*/
void readNextSeeds(string &seedKey, char &nextChar,int &modelLevel){

if(seedKey != ""){
seedKey = seedKey.substr(1);
seedKey += nextChar;
}
}


/*
The Function reads the file one character and fills out the Map according to the Markov model
param seedMap - map in which seed are the keys and values are vectors of characters which may be present after seed
param fileForAnalize - which may be present after the oxide
param modelLevel - the variable which will contain modelLevel

*/
void readFileToBuildSeedsMap(Map<string, Vector<char>> &seedMap, ifstream &fileForAnalize, int &modelLevel){
string seedKey;
char nextChar;

TokenScanner scanner(fileForAnalize);

readFirstSeed(scanner,modelLevel,seedKey,nextChar);
while(true){
readNextSeeds(seedKey,nextChar,modelLevel);
nextChar = scanner.getChar();
if(nextChar == EOF){
break;
}
/*At each iteration adding to the map characters which is the current Sid*/
seedMap[seedKey].add(nextChar);
}
fileForAnalize.close();

}


/*The Function of analyzing the map, and choose seed with the highest number of possible characters
param seedMap - map in which seed are the keys and values are vectors of characters which may be present after seed
return seedKey return found the most popular seed
*/
string findMostPopularSeed(Map<string, Vector<char>> &seedMap){
Vector<string> seedKeyCopy = seedMap.keys();
string result = seedKeyCopy[0];
for (string key: seedMap){
if (seedMap[key].size() > seedMap[result].size()){
result = key;
}
}
return result;
}

/*
Function randomly selects the next character of the possible from prepared seed
param seedMap - map in which seed are the keys and values are vectors of characters which may be present after seed
param startSeed - the most popular seed
param modelLevel - the variable which will contain modelLevel
return character the most popular character from prepared seed
*/
char getNextChar(Map<string, Vector<char>> &seedMap,string &startSeed, int &modelLevel){
char result;
Vector<char> nextChar = seedMap[startSeed];
result = nextChar[randomInteger(0, nextChar.size()-1)];
readNextSeeds(startSeed,result,modelLevel);
return result;
}

/*
Function generates a random text length of 2000 characters from the collected data in the map by Markov model
param seedMap - map in which seed are the keys and values are vectors of characters which may be present after seed
*/
void printRandomText(Map<string, Vector<char>> &seedMap,int &modelLevel){
const int MAX_TEXT_SIZE = 2000;
string resultText = "";
string startSeed = findMostPopularSeed(seedMap);

resultText += startSeed;
while (true) {

if (resultText.length() > MAX_TEXT_SIZE) break;

resultText += getNextChar(seedMap, startSeed,modelLevel);
}

cout<<resultText<<endl;
}


int main() {
// TODO: fill in the code
return 0;
ifstream fileForAnalize;
int modelLevel;
Map<string, Vector<char>> seedMap;

while (true) {
promptUserForFile(fileForAnalize, "Please enter the your file name: ");
if (fileForAnalize.is_open()){
break;
}
}

getModelLevel(modelLevel);

readFileToBuildSeedsMap(seedMap,fileForAnalize,modelLevel);

printRandomText(seedMap,modelLevel);

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
108 changes: 106 additions & 2 deletions WordLadder/src/WordLadder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,111 @@
#include "vector.h"
using namespace std;


/*
Function selects all the possible words that are different from the input one character.
param - startWord word to start the search
param allValidWord - a list of all the words
param usedWords - list has previously used the words
return vector all found words
*/
Vector<string> findSimilarWord(string startWord, Lexicon &allValidWord, Lexicon &usedWords){
Vector<string> result;
if(!usedWords.contains(startWord))
usedWords.add(startWord);

for (int i = 0; i < startWord.length(); i++){
string wordCopy = startWord;
for (char c = 'a'; c <= 'z'; c++){
wordCopy.replace(i,1,1,c);
if (allValidWord.contains(wordCopy) & !usedWords.contains(wordCopy)) {
result.add(wordCopy);
usedWords.add(wordCopy);
}
}
}

return result;
}


/*
method adds the word to the queue
param word - the word to be added
validLadders - queue all valid ladders
*/
void addWordToQueue (string word, Queue<Vector<string>> &validLadders){
Vector<string> shortestLadder(1, word);
validLadders.enqueue(shortestLadder);
}


/*
Function displays the shortest ladder
param shortestLadder - a vector that contains the shortest ladder
*/
void printResultLadder(Vector<string> &shortestLadder){
for (int i = 0; i < shortestLadder.size(); i++){
if ( i != shortestLadder.size()-1 ){
cout<<shortestLadder[i]<<"->";
} else{
cout<<shortestLadder[i]<<endl;
}

}

}


/*
Function finds the shortest ladder for two words
param startWord - word to start the search
param destWord - the word which should be built ladder
*/
void findeWordLaddes(string startWord, string destWord){
Lexicon allValidWord("EnglishWords.txt");
Lexicon usedWords;
Queue<Vector<string>> validLadders;
addWordToQueue(startWord, validLadders);
bool result = false;

while (!validLadders.isEmpty()) {
Vector<string> shortestLadder = validLadders.dequeue();
// If the last word of the vector is equal to destWord way to stop searching
if (shortestLadder[shortestLadder.size() -1] == destWord) {
printResultLadder(shortestLadder);
result = true;
break;
}
//displace a search on the last word on the ladder
string lastLadderWord = shortestLadder[shortestLadder.size() -1];

Vector<string> allSimilarWords = findSimilarWord(lastLadderWord, allValidWord, usedWords);

for(int i=0; i < allSimilarWords.size(); i++){
Vector<string> copyShortestLadder = shortestLadder;
copyShortestLadder.add(allSimilarWords[i]);
validLadders.enqueue(copyShortestLadder);
}
//If the ladder is not built stop and display a message
if (shortestLadder.size() == 0) break;

}
if (!result) cout<<"Any ladders could not be found"<<endl;

}


int main() {
// TODO: fill in the code
return 0;
while (true) {
string startWord = getLine("Enter start word: ");
string destWord = getLine("Enter destination word: ");

if ((startWord.length() ==0) || (destWord.length() == 0)){
cout<<"Enter incorrect word!"<<endl;
continue;
} else findeWordLaddes(startWord, destWord);

}
return 0;
}