Let me preface this with a disclaimer; I don't mean to pick on you in
particular, since the style you've used seems to be pretty common.
See below for the actual content.
On Nov 4, 2006, at 6:28 PM, Steve wrote:
Well due to a connectivity issue I only got to do the first question.
I did it in c++ and it basically looked like this (I've been working
with it a little to clean it up since then)
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str;
char charopen = '(';
char charclose = ')';
while(getline(cin,str))
{
if(str.empty())
{
return 0;
}
int counter;
int c_open = 0;
int c_close = 0;
int depth = 0;
counter = str.length();
Why, oh why, do you use so much darned extra space in this code?
It's an incredibly inefficient use of both vertical and horizontal
space. What is the point of putting curly braces on lines all by
themselves? There's not actually a lot of code, but I have to scroll
down just to read the whole thing. And this is in a darned simple
function; how long would a complex one get? Compare with this
version, which fits the whole main function in an 80x25 terminal:
#include <iostream>
#include <string>
using namespace std;
int main() {
string str;
char charopen = '(', charclose = ')';
while (getline(cin,str)) {
if (str.empty()) { return 0; }
int counter, c_open, c_close, depth;
c_open = c_close = depth = 0;
counter = str.length();
for (int x = 0; x <= counter; x++) {
if (str[x] == charopen) {
c_open++;
depth++;
if (str[x+1] == charclose) { depth--; }
}
if (str[x] == charclose) { c_close++; }
}
if (c_close != c_open) { cout << 0 << endl; }
else { cout << depth+1 << endl; }
}
}
Is there some reason I'm missing to prefer the extremely long version
over the shortened one?
Again, I don't mean this as a personal attack, just an honest
bafflement at a programming style I've seen used fairly frequently.
--Levi
/*
PLUG: http://plug.org, #utah on irc.freenode.net
Unsubscribe: http://plug.org/mailman/options/plug
Don't fear the penguin.
*/