Generate balanced Parenthesis - c++

The tried below logic to generate the possible set of balanced parenthesis. but each possible output is getting printed twice.
void balanced_parenthesis(vector<string>& out_vec,string out_str,int is_open,int is_close)
{
if(is_open == 0 && is_close == 0)
{
cout<<out_str<<endl;
out_vec.push_back(out_str);
return;
}
if(is_open != 0)
{
out_str += '(';
is_open = is_open-1;
balanced_parenthesis(out_vec,out_str,is_open,is_close);
}
if( is_close > is_open)
{
out_str += ')';
is_close = is_close-1;
balanced_parenthesis(out_vec,out_str,is_open,is_close);
}
}
however by decrementing is_close and is_open variables in the function call prints the output exactly once.
void balanced_parenthesis(vector<string>& out_vec,string out_str,int is_open,int is_close)
{
if(is_open == 0 && is_close == 0)
{
cout<<out_str<<endl;
out_vec.push_back(out_str);
return;
}
if(is_open != 0)
{
out_str += '(';
balanced_parenthesis(out_vec,out_str,is_open,is_close);
}
if( is_close > is_open)
{
out_str += ')';
balanced_parenthesis(out_vec,out_str,is_open,is_close-1);
}
}
Could anyone explain me behaviour of the first code ?

Related

How figlet fit and smush?

I am creating a figlet like code in c++ and i am currently doing fiting by inserting null at left side character and find min space and remove minspace and null the character,( for understanding take null as '0' )
" __0 "
"| _|0 "
"| |0 _____ "
"| |0 |_____|"
"| |0 $ "
"|__|0 "
(if any space line i insert null at start) Now here min space is 3 so i remove 3 space and null in that and this code works perfect and smushing by inheriting the fitting class and i will pass the right side char by inserting null like
" 0"
" 0"
" _0____ "
"|0_____|"
" $0 "
" 0"
it will give result like
" __ 0"
"| _| 0"
"| | _0____ "
"| ||0_____|"
"| | $0 "
"|__| 0"
Now i will store pos of null and remove it, In next loop check the two character before the null, if any one are HardBlank then i return the function else i smush it in next loop, but above are not smush( not work correctly ) due to HardBlank, so i want to know how figlet actually smush i downloaded the figlet code from here but i did not understand the code.
There is any better algorithm than this or How figlet actually do fitting and smushing ?
All suggestions are welcome,
Thanks in advance.
I asked this question long time ago now, But I answering this question now for future readers I have written algorithm a better than this some time ago that do the following,
Kerning or fitting :
The main part of this thing is trim, so lets create an function that takes two input that figs is left side FIGchar and figc is right side FIGchar. The first thing to do is find the number of space that need to be removed from right side of figs and left side of figc, we can easily find this by counting total space in right size of figs and left side of figc. Finally take minimum of that that is the space count that has to be removed here is that implementation,
/**
* #brief trims in a deep
*
* #param figs fig string
* #param figc fig character
*/
void trim_deep(Figs_type &figs, Figc_type &figc) const
{
std::vector<size_type> elem;
for (size_type i = 0; i < figs.size(); ++i)
{
int lcount = 0, rcount = 0;
for (auto itr = figs[i].rbegin(); itr != figs[i].rend(); ++itr)
{
if (*itr == ' ')
++lcount;
else
break;
}
for (auto itr = figc[i].begin(); itr != figc[i].end(); ++itr)
{
if (*itr == ' ')
++rcount;
else
break;
}
elem.push_back(lcount + rcount);
}
size_type space = *std::min_element(elem.begin(), elem.end());
for (size_type i = 0; i < figs.size(); ++i)
{
size_type siz = space;
while (siz > 0 && figs[i].back() == ' ')
{
figs[i].pop_back();
--siz;
}
figc[i].erase(0, siz);
}
}
Smushing:
This smushing can be done easily by using above function with only smush right most character of figs and left side character of figc if it is smushble here is implementation,
/**
* #brief smush rules
* #param lc left character
* #param rc right character
* #return smushed character
*/
char_type smush_rules(char_type lc, char_type rc) const
{
//()
if (lc == ' ')
{
return rc;
}
if (rc == ' ')
{
return lc;
}
//(Equal character smush)
if (lc == rc)
{
return rc;
}
//(Underscores smush)
if (lc == '_' && this->cvt("|/\\[]{}()<>").find(rc) != string_type::npos)
{
return rc;
}
if (rc == '_' && this->cvt("|/\\[]{}()<>").find(lc) != string_type::npos)
{
return lc;
}
//(Hierarchy Smushing)
auto find_class = [](char_type ch) -> size_type
{
if (ch == '|')
{
return 1;
}
if (ch == '/' || ch == '\\')
{
return 3;
}
if (ch == '[' || ch == ']')
{
return 4;
}
if (ch == '{' || ch == '}')
{
return 5;
}
if (ch == '(' || ch == ')')
{
return 6;
}
return 0;
};
size_type c_lc = find_class(lc);
size_type c_rc = find_class(rc);
if (c_lc > c_rc)
{
return lc;
}
if (c_rc > c_lc)
{
return rc;
}
//(Opposite smush)
if (lc == '[' && rc == ']')
{
return '|';
}
if (lc == ']' && rc == '[')
{
return '|';
}
if (lc == '{' && rc == '}')
{
return '|';
}
if (lc == '}' && rc == '{')
{
return '|';
}
if (lc == '(' && rc == ')')
{
return '|';
}
if (lc == ')' && rc == '(')
{
return '|';
}
//(Big X smush)
if (lc == '/' && rc == '\\')
{
return '|';
}
if (lc == '\\' && rc == '/')
{
return 'Y';
}
if (lc == '>' && rc == '<')
{
return 'X';
}
//(universel smush)
return lc;
}
/**
* #brief smush algoriths on kerned Fig string and character
*
* #param figs
* #param figc
*/
void smush(Figs_type &figs, Figc_type figc, char_type hb) const
{
bool smushble = true;
for (size_type i = 0; i < figs.size(); ++i)
{
if (figs[i].size() == 0 || figc[i].size() == 0)
{
smushble = false;
}
else if ((figs[i].back() == hb) && !(figc[i].front() == hb))
{
smushble = false;
}
}
if (smushble)
{
for (size_type i = 0; i < figs.size(); ++i)
{
char_type val = smush_rules(figs[i].back(), figc[i].front());
figs[i].pop_back();
figc[i].erase(0, 1);
figs[i] += string_type(1, val) + figc[i];
}
}
else
{
for (size_type i = 0; i < figs.size(); ++i)
{
figs[i] += figc[i];
}
}
}
This code is directly copied from this file, So the types can be confusing here is overview Figs_type and Figc_type are just like vector of string and other type are reflects in their name and the repo can be found here.

Get rid of IF statements by using functors

please teach me how to get rid of those if statements by using functors (or any other better methods) inside the following loop:
//Loop over each atom
std::string temp_name ;
float dst;
for (pdb::pdb_vector:: size_type i=0; i < data.size(); ++i)
{
if (type == 0)
{
//choose by residue name
temp_name = data[i].residue_name;
} else {
//choose by atom name
temp_name = data[i].atom_name;
}
//compare the name and extract position if matched
if (temp_name.compare(name) == 0)
{
if (direction.compare("x") == 0)
{
dst = ::atof(data[i].x_coord.c_str());
} else if ((direction.compare("y") == 0)) {
dst = ::atof(data[i].y_coord.c_str());
} else {
dst = ::atof(data[i].z_coord.c_str());
}
}
}
you can replace if(type == 0) with the ternary operator:
// if(type == 0) ...
temp_name = (type == 0 ? data[i].residue_name : data[i].atom_name);
but the rest of your checks seem like they would only be less readable if you tried something similar.

wild card matching in text string

My friend give this wild card(*) matching algorithm . Here is the code .
//This function compares text strings, one of which can have wildcards ('*').
//
BOOL GeneralTextCompare(
char * pTameText, // A string without wildcards
char * pWildText, // A (potentially) corresponding string with wildcards
BOOL bCaseSensitive = FALSE, // By default, match on 'X' vs 'x'
char cAltTerminator = '\0' // For function names, for example, you can stop at the first '('
)
{
BOOL bMatch = TRUE;
char * pAfterLastWild = NULL; // The location after the last '*', if we’ve encountered one
char * pAfterLastTame = NULL; // The location in the tame string, from which we started after last wildcard
char t, w;
// Walk the text strings one character at a time.
while (1)
{
t = *pTameText;
w = *pWildText;
// How do you match a unique text string?
if (!t || t == cAltTerminator)
{
// Easy: unique up on it!
if (!w || w == cAltTerminator)
{
break; // "x" matches "x"
}
else if (w == '*')
{
pWildText++;
continue; // "x*" matches "x" or "xy"
}
else if (pAfterLastTame)
{
if (!(*pAfterLastTame) || *pAfterLastTame == cAltTerminator)
{
bMatch = FALSE;
break;
}
pTameText = pAfterLastTame++;
pWildText = pAfterLastWild;
continue;
}
bMatch = FALSE;
break; // "x" doesn't match "xy"
}
else
{
if (!bCaseSensitive)
{
// Lowercase the characters to be compared.
if (t >= 'A' && t <= 'Z')
{
t += ('a' - 'A');
}
if (w >= 'A' && w <= 'Z')
{
w += ('a' - 'A');
}
}
// How do you match a tame text string?
if (t != w)
{
// The tame way: unique up on it!
if (w == '*')
{
pAfterLastWild = ++pWildText;
pAfterLastTame = pTameText;
w = *pWildText;
if (!w || w == cAltTerminator)
{
break; // "*" matches "x"
}
continue; // "*y" matches "xy"
}
else if (pAfterLastWild)
{
if (pAfterLastWild != pWildText)
{
pWildText = pAfterLastWild;
w = *pWildText;
if (!bCaseSensitive && w >= 'A' && w <= 'Z')
{
w += ('a' - 'A');
}
if (t == w)
{
pWildText++;
}
}
pTameText++;
continue; // "*sip*" matches "mississippi"
}
else
{
bMatch = FALSE;
break; // "x" doesn't match "y"
}
}
}
pTameText++;
pWildText++;
}
return bMatch;
}
This algo works as follow (according to me)
mississippi *sip*
mississippi sip*
ississippi sip*
ssissippi sip*
sissippi ip*
sissippi sip* pAfterLastWild is used to restore the location
issippi ip*
ssippi p*
ssippi sip* again pAfterLastWild is used here.
sippi ip*
sippi sip* here also.
ippi ip*
ppi p*
pi *
i *
I am not able to figure out why pAfterLastTame is needed and what does this piece of code is doing here as i am not able to find use of it .
else if (pAfterLastTame)
{
if (!(*pAfterLastTame) || *pAfterLastTame == cAltTerminator)
{
bMatch = FALSE;
break;
}
pTameText = pAfterLastTame++;
pWildText = pAfterLastWild;
continue;
}
This algo is pretty fast as number of comparisons are equal to size of tameString (correct me i am wrong) .
Does any one know more efficient algorithm than this ??

Shunting Yard algorithm implementation

I've been trying to implement shunting yard algorithm. The code gets an input from the user and after it is evaluated by another function (which is done already), it will be converted to postfix notation and then passed to be computed. The code below is just for the algorithm itself. ie is the vector of tokens made from user's input.
The code makes sense to me but it doesn't compile, yet can't figure out where is not quite right.
double eval_infix_expr(vector<Token> ie, map<string,double> sym_tab)
{
vector<Token> postfix_expr;
static bool IsOperator(const string& token) {
return token == "+" ||
token == "-" ||
token == "*" ||
token == "/" ||
token == "%";
}
static int PrecedenceOf(const string& token) {
if (token == "+" || token == "-") return 0;
if (token == "*" || token == "/" || token == "%") return 1;
throw runtime_error("Unknown operator: " + token);
}
bool expectingOperator = false;
for (size_t i = 0; i < ie.size(); ++i) {
if (IsOperator(ie[i])) {
if (!expectingOperator)
throw runtime_error("Unexpected operator: " + ie[i]);
while (!sym_tab.empty() && IsOperator(sym_tab.top()) &&
PrecedenceOf(sym_tab.top()) >= PrecedenceOf(ie[i])) {
postfix_expr.push_back(sym_tab.top()); sym_tab.pop();
}
sym_tab.push(ie[i]);
expectingOperator = false;
}
else if (ie[i] == "(") {
if (expectingOperator)
throw runtime_error("Expected operator, found (.");
sym_tab.push(ie[i]);
}
else if (ie[i] == ")") {
if (!expectingOperator)
throw runtime_error("Expected value, found ).");
while (!sym_tab.empty() && sym_tab.top() != "(") {
postfix_expr.push_back(sym_tab.top()); sym_tab.pop();
}
if (sym_tab.empty())
throw runtime_error("Imbalanced parentheses.");
sym_tab.pop();
expectingOperator = true;
}
else {
if (expectingOperator)
throw runtime_error("Expecting operator, found " + ie[i]);
postfix_expr.push_back(ie[i]);
expectingOperator = true;
}
}
if (!expectingOperator)
throw runtime_error("Expected value, didn't find one.");
while (!sym_tab.empty()) {
if (sym_tab.top() == "(")
throw runtime_error("Imbalanced parentheses.");
postfix_expr.push_back(sym_tab.top()); sym_tab.pop();
}
}
postfix_Evaluator pe(postfix_expr);
return pe.eval();
}
}

Parsing Parenthesis using stack template class in C++

I have a homework and its just bugging for the last 2 days, I've been doing exactly what the pseudo code and still haven't got it right yet.
For example, if I put in "mike]" or "mike]123", my program will crash, due to the stack is empty...From what I observe, the program will crash when:
- The stack is empty
- And there is a close parenthesis
PS: with the help of us2012, I can fix the crash problem. However, the result is not right.
Instead of printing out "invalid", it outputs "valid"
:(
Here is the pseudo code from my professor:
def parse_parenthesis(str):
stack = create a new empty stack of OpenParen objects
for i from 0 to str.size() - 1:
if str[i] is an open parenthesis
stack.push(new OpenParen(str[i]))
else if str[i] is not a close parenthesis:
# str[i] is not a parenthesis of any kind, so ignore it
continue
# otherwise str[i] must be a close parenthesis, try to
# match it with the most recent open paren, on the top
# of the stack
else if stack is empty
return false;
else if stack.peek() is of the same type as str[i]:
# close properly
stack.pop()
else
return false;
if stack is not empty
return false;
else
return true
and here is what I have so far:
.cpp file
bool ParenMatching(const string& s, unique_ptr<string>& output)
{
unique_ptr<OpenParen> stack(new OpenParen);
bool validOpen, validClose, valid;
bool match; //haveCloseParen;
/*string unMatch = "Unmatch";
string unExpected = "Unexpected close paren";
string noError = "No Error";*/
for (size_t i = 0; i < s.length(); i++)
{
// check if its open parenthesis
validOpen = stack->IsOpenParen(s[i]);
// check if its close parenthesis
validClose = stack->IsCloseParen(s[i]);
// if there is open paren, push into the stack
if(validOpen)
stack->PushObj(s[i]);
else if(!validClose)
{
continue;
}
else if(stack->GetObj().IsEmpty())
valid = false;
else if(match = IsMatchParen(s[i], stack))
stack->PopObj();
else
valid = false;
}
if(!stack->GetObj().IsEmpty())
valid = false;
else
valid = true;
return valid;
}
bool IsMatchParen(const char c, const unique_ptr<OpenParen>& stack)
{
bool valid;
if(c == ')' && stack->PeekObj() == '(')
valid = true;
else if (c == ']' && stack->PeekObj() == '[')
valid = true;
else if (c == '}' && stack->PeekObj() == '{')
valid = true;
else if (c == '>' && stack->PeekObj() == '<')
valid = true;
else
valid = false;
return valid;
}
OpenParen.cpp
// Check if its open paren
bool OpenParen::IsOpenParen(const char c)
{
bool isOpen;
if(c == '(' || c == '[' || c == '{' || c == '<')
isOpen = true;
else
isOpen = false;
return isOpen;
}
// check if its close paren
bool OpenParen::IsCloseParen(const char c)
{
bool isClose;
if(c == ')' || c == ']' || c == '}' || c == '>')
isClose = true;
else
isClose = false;
return isClose;
}
gcc 4.7.3: g++ -Wall -Wextra -std=c++0x parens.cpp
#include <iostream>
#include <stack>
#include <string>
#include <vector>
bool isOpen(char c) {
return c == '(' || c == '[' || c == '{' || c == '<'; }
bool isClose(char c) {
return c == ')' || c == ']' || c == '}' || c == '>'; }
bool isMatch(char c1, char c2) {
return (c1 == '(' && c2 == ')')
|| (c1 == '[' && c2 == ']')
|| (c1 == '{' && c2 == '}')
|| (c1 == '<' && c2 == '>'); }
bool parse(const std::string& s) {
std::stack<std::string::value_type> stk;
for (std::string::size_type i = 0; i < s.size(); ++i) {
if (isOpen(s[i])) { stk.push(s[i]); }
else if (isClose(s[i])) {
if (!stk.empty() && isMatch(stk.top(), s[i])) { stk.pop(); }
else { return false; } } }
return stk.empty(); }
int main() {
std::vector<std::string> ptests = {
"", "()", "()()", "(())", "a(a)a" };
std::vector<std::string> ftests = {
"(", ")", ")(", ")()(", "))((" };
for (const auto& t : ptests) {
if (!parse(t)) { std::cout << "fail: " << t << std::endl; } }
for (const auto& t : ftests) {
if (parse(t)) { std::cout << "fail: " << t << std::endl; } }
}
One important thing you should keep in mind about C++ : Multiple else ifs do not live at the same level. That's because else if is not a single entity, it's an else that belongs to the preceding statement and an ifthat begins a new statement, so
if (cond1)
a();
else if (cond 2)
b();
else if (cond 3)
c();
else
d();
is actually
if (cond1)
a();
else {
if (cond 2)
b();
else {
if (cond 3)
c();
else
d();
}
}
Therefore, your check whether the stack is empty needs to be before the check whether the current close parens matches the top of the stack. Otherwise, your program will try to examine the top of the stack when it's empty, and that results in a crash.
Also, setting valid = false is not the right thing to do when you find a condition that indicates a non-match. The loop will still continue and can reset valid to true in a later iteration. You need to immediately return false, as you can already see in your pseudocode.