233 points · by awa · 16y ago · 57 comments
int putchar ( int ) ;
int main ( void )
{
int z ;
for (z = 0; z < 90; z++)
{
putchar (
z % 9 + z / 9 > 3 &&
z % 9 + z / 9 < 14 &&
z / 9 < z % 9 + 6 &&
z / 9 > z % 9 - 5 ? '*' : ' ' );
if (!(z % 9 - 9 + 1))
putchar ( '\n' );
};
putchar ( '\n' ) ;
return 0;
}
It's too late at night for me to try to simplify that big expression in the middle... x = z % 9;
y = z / 9;
putchar (
x + y > 3 &&
x + y < 14 &&
y < x + 6 &&
y > x - 5 ? '*' : ' ' );
if (x == 8)
putchar ( '\n' );
Wolfram Alpha plots this inequality nicely:http://www.wolframalpha.com/input/?i=x+%2B+y+%3E+3+%26%26+x+...
int putchar ( int ) ;
int main ( void )
{
int z;
int width = 9;
int height = 10;
for (z = 0; z < width * height; z++)
{
int x = z % width;
int y = z / width;
putchar (
x + y > 3 && /* top left */
x + y < 14 && /* bottom right */
y < x + 6 && /* bottom left */
y > x - 5 /* top right */
? '*' : ' ' );
if (x == width - 1)
putchar ( '\n' );
};
putchar ( '\n' ) ;
return 0;
}
It would be clearer to separate the main loop into nested loops for x and y, but I've left it as is to show a closer resemblance to the previous version.The fun part is that you can now play with the various settings. Try increasing the height and width and scaling the equations appropriately. Replace x by 2 * x or x * x for more interesting shapes. You can bound the picture by any equations you like. Example: http://ideone.com/53Qh3
It's a great exercise that challenges CS students every year and does a very good job at teaching nested loops.
Later in the same set of exercises is the four triangles problem that ties increment and decrement operators to the use of nested loops. It's one of my personal favorites.
CS students who legitimately complete these exercises always do well with chapter 4 (Arrays, Simpletron), chapter 5 (pointers) and chapter 6 (structs and classes) and come away with insight and a sense of confidence that echoes for a long time.
It is literally one of those defining problems that separates those who are serious from those who are not.
The responder at comp.lang.c knew that. Kudos to him.
It's up there with FizzBuzz; anyone who can't do it simply isn't up to being a programmer.
I eventually quit the list because it was too much of a time suck, but it's well worth a half a day swimming through the archives. Heathfield's posts are almost always worth a look.
Piqued. Lessee, http://en.wikipedia.org/wiki/Alfred_Edward_Housman ... colleagues were unnerved by his scathing critical attacks ... they "were often savage in the extreme."
Also, for other reasons: A fellow don described him as being "descended from a long line of maiden aunts".
Apparently, after he died, they found notebooks in his office that he had filled with long elaborate, witty, hurtful insults -- aimed at nobody in particular. When he wrote a review, he would tweak the pre-fab insults as needed and voila. A sick, twisted person, but fascinating. (If you're really curious, check out Tom Stoppard's The Invention of Love.)
It's curious that the A.E.H. vinegar does not get wider circulation. My copy of Matthew Parris' Scorn only features a couple of harmless barbs (one the 'malt/Milton' line). There are some scraps over at http://en.wikiquote.org/wiki/Alfred_Edward_Housman , and here http://www.lrb.co.uk/v29/n13/frank-kermode/nothing-for-ever-...
(Also, a passing appreciation by Chesterton, a guide I trust, here http://wikilivres.info/wiki/Autobiography_%28Chesterton%29/C...)
print '\n'.join(['%s%s%s' % (' '*((9-step)/2), '*'*step, ' '*((9-step)/2)) for step in [1, 3, 5, 7, 9, 9, 7, 5, 3, 1]])
Not diamond shaped, but moderately obfuscated.And you hard-coded it in a list. I consider this cheating.
print '\n'.join(max(4-i, i-5)*' ' + (min(i, 9-i)*2+1)*'*' for i in range(0, 10))
Edit: shaved off a few more characters and somehow made it slightly clearer. I would make it diamond shaped, but python will whine because of "invalid indentation". def d(n): return '\n'.join(int(max((n-1)/2-i,i-n/2))*' '+(min(i,n-1-i)*2+1)*'*' for i in xrange(0,n)) print ('\n'
.join(max
(4-i, i-5) *
' ' + (min(i, 9
-i) * 2 + 1) * '*'
for i in range
(0, 10)
))print '\n'.join([" "abs(4-i) + ""(9-abs(8-i2)) for i in range(9)])
C++ != trivially obfuscated C.
If you use it well, it does a lot of heavy lifting, adds a lot of safety and expressiveness (boost::bind is just pure magic) that just wasn't there before, and a lot of the boost libraries are going into the standard - and are already implemented in the TR1 namespace.
for( z = 002354l / 000644l / 000132l / 000322l / 000374l ; ...
has all the hallmarks of machine-generated code.I remember being that kid. Well, not quite so bad that I literally wanted someone to write code for me, but I'm sure someone thought my poorly worded questions were asking people to write code for me. How many kids are we scaring away because we're being assholes to them?
To whatever riposte you might be planning, I would point out that I'm not talking about theory, either. Yeah, sure, everyone deserves care and love and ponies. I'm talking about, in practice, would you still be so caring and loving and pony-dispensing after hundreds of these things? If so, you're better than most. (Too much better for me to believe in, in fact.)
The answer wasn't rude, it was just unhelpful.
For those not familiar with the culture on comp.lang.c and the various related C and C++ groups, it is very simple: people will generally be helpful in dealing with homework questions as long as the asker has made a genuine effort.
On the other hand, anyone who just reposts homework verbatim with no effort whatsoever to solve the problem first is likely to find themselves given a very clever answer that is technically correct but clearly completely implausible for a beginner, thus making it very obvious to the teacher that the person is cheating on their homework. I recall a similar example where someone asked how to work out factorials in C++, and was presented with a highly efficient answer... because it used template metaprogramming and generated that answer at compile time.
This was certainly clear in the related FAQs last time I checked, so anyone who asks a homework question without making any effort to solve it themselves is not only lazy but also incapable of following basic netiquette. I doubt you'd find many regular posters on those groups who had much sympathy, even though some of those same posters give up a great deal of time to help those who are making a genuine effort to improve.
Being deliberately unhelpful is rude.