1
\$\begingroup\$

Mixed Duo String Generator Hello again;

Previously my code was critique and I applied this feedback to form my new revised code for the Mixed Duo String Generator. In the code below I revised it to the best of my ability. I removed un-needed comments, keep variables in as small a scope as I could think of, removed un-needed functions, replaced manually typed values with constants, stuck with a consistent indexation, renamed variables with meaningful names, removed camel casing from variables name, nothing is unused, and finally attempted to remove all unnecessary usage of "!= or ==".

I am somewhat nervous but I humbly await judgement of the quality of my code.

#include <map>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>

std::string findPair2(std::string input){   

    if (input.size() <= 4u) {
        return (input.size() == 4u && input[0] == input[2] && input[1] == input[3]) ? input : "";
    }

    const std::string ELEM_1 = "ELEMENT_1";
    const std::string ELEM_2 = "ELEMENT_2";
    const std::string FAILED = "FAILED";


    std::map<char,std::string> potential_second_element_status; /*The key value of the map represents the status of the 
                                                                character being compared to the first element of the string*/
    std::string unique_charaters;
    std::string saved_failures = "";


    for(int i=0; i<input.size(); i++){ //get all the unique characters as they appear in chroniclogical order except the first character
        if((potential_second_element_status.find(input[i]) == potential_second_element_status.end())&&(input[i] != input[0])){
            potential_second_element_status[input[i]] = ELEM_1;
            unique_charaters += input[i];
        }
    }

    for(int i=0; i<input.size(); i++){
        if(saved_failures.size() == unique_charaters.size()){
            break;
        }
        if(input[i] == input[0]){   
            for(int a=0; a<unique_charaters.size(); a++){
                if(potential_second_element_status.find(unique_charaters[a])->second == ELEM_2){
                    saved_failures += unique_charaters[a]; 
                    potential_second_element_status[unique_charaters[a]] = FAILED;
                }       
                if(potential_second_element_status.find(unique_charaters[a])->second == ELEM_1){
                    potential_second_element_status[unique_charaters[a]] = ELEM_2;
                }   
            }
        }       
        else if(potential_second_element_status.find(input[i])->second == ELEM_2){  
            potential_second_element_status[input[i]] = ELEM_1; 
        }
        else if(potential_second_element_status.find(input[i])->second == ELEM_1){  
            saved_failures += input[i]; 
            potential_second_element_status[input[i]] = FAILED;
        }
    }

    if(saved_failures.size() == unique_charaters.size()){
        char temp = input[0];
        input.erase(std::remove(input.begin(), input.end(), temp), input.end()); 
        input = findPair2(input);
    }
    else{
        for(int i=0; i<saved_failures.size(); i++){
            input.erase(std::remove(input.begin(), input.end(), saved_failures[i]), input.end()); 
        }       
    }

    return input;
}
void autoStringRead2(){
    std::ifstream inputs( "pairs-in.txt" );
    std::string line;
    std::string output;
    std::string filename = "pairs-in.txt";
    std::fstream fs(filename, std::ios::in);
    std::ofstream results;

    results.open ("results.txt");

    if(!fs.is_open()){
       return;
    }

    while (inputs >> line) {    
        output = findPair(line);
        results << output <<"\n";
        std::cout<< line << "\n";
        std::cout<< output << "\n";
    }

    //inputs.close();
    //results.close();
}
int main(){ 
    autoStringRead2();
    int read;
    std::cin >> read;
    return 0;
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.