bglesias
BAN USER/*
Let me explain how this function works. This function takes a string called s and uses an algorithm to compress is.
(*NOTE: this is slightly different from the questions ex:, but still accomplishes the same goal)
this algorithm will take a string, say 45eeeedfghfffeff62 and transform it into 4_5_4edfgh3fe2f6_2. If there is a number to the left of a character,
it is there to explain how many times that character should be multiplied (ex: eeee: 4e dddddddddd : 10d) The "_" character is there to denote that
if a number is by itself during input,say 6eee2, then the output will be 6_3e2. This is similar to the "*" character in the example. We do not want vagueness.
(*NOTE: this algorithm is to be used with very large strings. This will do a great job of compressing a large string, such as 5eeeeeeeeeedddddfff65ggggeeffssaasddffdsacvffd,
but not smaller strings like 5ee (takes up the same amount of characters.
AUTHOR: BILL GLESIAS
this algorithm is implemented in c++
*/
#include <iostream>
#include <string>
#include <sstream>
std::string charCompress(std::string s){
unsigned i = 0;
std::string newString = "";
while( i < s.length()){
char r = s[i];
if( i == s.length()-1){
newString += s[i];
return newString;
}
else if(s[i] == s[i + 1]){
unsigned sum = 1;
while( i < s.length()- 1 && (s[i] == s[i + 1])){
sum++;
i++;
}
std::ostringstream convert;
convert << sum;
if( i == s.length()-1){
newString += convert.str() + r;
return newString;
}
else{
newString += convert.str() + r;
}
}//end if
else{
newString += s[i];
if(s[i] > 47 && s[i] < 58){
newString += "_";
}
}//end else
i++;
}
}
int main(){
std::string a = charCompress("5eeeeddf55555dcgleeettff");
std::cout << a << std::endl;
a = charCompress("5eeeeeeeeeedddddfff65ggggeeffssaasddffdsacvffd");
std::cout << a << std::endl;
system("PAUSE");
}
//Solution implemented in c++
#include <iostream>
#include <string>
//create a function that returns a string that takes in a string named T and an unsigned integer ( to save memory ) named n.
//Assuming T is the actual LuckyString and not the number of Lucky Strings needing to be executed. (!IMPORTANT)
//the keyword unsigned assumes an unsigned integer in c++
std::string findLuckyString(std::string T, unsigned n){
//IMPLEMENT CONTRAINTS HERE OR IN PARAMETERS
/* Sine contraints for this problem are very vague, I did not implemnent them.
Do they mean storage as in the decimal value or the binary value. This MUST be clarified(!IMPORTANT)
*/
//We loop through the string until we find the proper index.
for (int i = 0; i < T.length(); i++){
//checks the index to see if it is matching
if(n == i){
//if the index is a 5, we will return a string titled "Hacker"
if(T[i] == '5'){
return "Earth";
}//end if
else if(T[i] == '4'){
// if the index is a 4, then the function returns a string titled "Earth"
return "Hacker";
}//end else
}//end if
}//end loop
//if the index is out of bounds OR the string does no consist of all 5s and 4s, then the input is invalid nd will return the string below
return "invalid input! Check string input to make sure '5's and '4's are present and that n is a valid index in the string";
}
int main(){
//test the function with some values. all index's start at 0!
std::cout << findLuckyString("544544", 4) << std::endl;
std::cout << findLuckyString("555554455333", 7) << std::endl;
std::cout << findLuckyString("544544", 10) << std::endl;
std::cout << findLuckyString("67823", 3) << std::endl;
system("PAUSE");
}
I apologize that I did not state in my engineering model that the special character was not meant for input entry and is assumed that it will not be entered. HOWEVER, if you would like to account for the special character, you can do something like the code below. You can compress the string and keep track of the special character through a list ( a vector in my example). That way, if you are interested in decompressing the string, lets say the the example you gave 9___6aab would compress to 9_6_2ab with our vector storing the '_' at indexes 1,2,3. Unravel the compressed string to 96aab and insert the '_' where needed. I hope this helps!
- bglesias February 03, 2014/*
Let me explain how this function works. This function takes a string called s and uses an algorithm to compress is.
(*NOTE: this is slightly different from the questions ex:, but still accomplishes the same goal)
this algorithm will take a string, say 45eeeedfghfffeff62 and transform it into 4_5_4edfgh3fe2f6_2. If there is a number to the left of a character,
it is there to explain how many times that character should be multiplied (ex: eeee: 4e dddddddddd : 10d) The "_" character is there to denote that
if a number is by itself during input,say 6eee2, then the output will be 6_3e2. This is similar to the "*" character in the example. We do not want vagueness.
(*NOTE: this algorithm is to be used with very large strings. This will do a great job of compressing a large string, such as 5eeeeeeeeeedddddfff65ggggeeffssaasddffdsacvffd,
but not smaller strings like 5ee (takes up the same amount of characters.
AUTHOR: BILL GLESIAS
this algorithm is implemented in c++
*/
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
class StringObj{
private: std::string compressedString;
std::vector<unsigned> indexholder;
public:
StringObj::StringObj(std::vector<unsigned>, std::string);
void StringObj::getAndPrintValues() const;
};
StringObj::StringObj(std::vector<unsigned> index, std::string compString){
this->compressedString = compString;
this->indexholder = index;
}
void StringObj::getAndPrintValues() const{
std::cout << this->compressedString << std::endl;
for(unsigned i = 0; i < this->indexholder.size(); i++){
std::cout<< this->indexholder[i] << std::endl;
}
}
StringObj charCompress(std::string s){
unsigned i = 0;
std::vector<unsigned> indexholder;
std::string newString = "";
while( i < s.length()){
char r = s[i];
if(s[i] == '_'){
indexholder.push_back(i);
}
else if( i == s.length()-1){
newString += s[i];
StringObj a = StringObj::StringObj(indexholder,newString);
return a;
}
else if(s[i] == s[i + 1]){
unsigned sum = 1;
while( i < s.length()- 1 && (s[i] == s[i + 1])){
sum++;
i++;
}
std::ostringstream convert;
convert << sum;
if( i == s.length()-1){
newString += convert.str() + r;
StringObj a = StringObj::StringObj(indexholder,newString);
return a;
}
else{
newString += convert.str() + r;
}
}//end if
else{
newString += s[i];
if(s[i] > 47 && s[i] < 58){
newString += "_";
}
}//end else
i++;
}
}
int main(){
StringObj a = charCompress("9___6aab");
a.getAndPrintValues();
a = charCompress("5eeeeeee____eeedddddfff65ggggeeffs__saasddffdsacvffd");
a.getAndPrintValues();
system("PAUSE");
}