Image by Louish
Chrestomathy means literally ‘useful to know’. Rather broad, yes, so in practice it means a collection of good literary passages, like a Readers Digest I guess. What I’m more interested in is program chrestomathy, this is a collection of programs written in different programming language. It lets you see how different computer languages solve the same problem. Here are some examples.
The problem here is something called FizzBuzz. It just prints all the numbers from 1-100. For multiples of three it prints “Fizz”, for the multiples of five it print “Buzz”, for multiples of both it prints “FizzBuzz”. I include this problem because I just did it as part of my learning at Codecademy.
Javascript
Here is a solution in Javascript:
for (var i = 1; i <= 100; i++) {
if (i % 15 == 0) {
print("FizzBuzz");
} else if (i % 3 == 0) {
print("Fizz");
} else if (i % 5 == 0) {
print("Buzz");
} else {
print(i);
}
}
Basically this takes the variable i and counts from 1 to 100. The % sign is a modulo (also a fun word) which gives you the remainder when you divide (ie, 3/2 leaves a remainder of 1). If the remainder when divided by 15 is (==, though I would write ===) then you know it's divisible by both. You then exit there, which is a good idea now that I think about it, no overlap. Otherwise if the remainder from 3, 5, etc. Otherwise (else) just print the number.
C
As you can see, the implementation in C is almost exactly the same.
#include
int main (void)
{
int i;
for (i = 1; i <= 100; i++)
{
if (!(i % 15))
printf ("FizzBuzz");
else if (!(i % 3))
printf ("Fizz");
else if (!(i % 5))
printf ("Buzz");
else
printf ("%d", i);
printf("n");
}
return 0;
}
As an aside, I took an intro to C class in University, at the very end of the dark ages. The horrible professor showed us programming on a chalkboard and we took notes. The final exams were, I shit you not, hand-written. We hand wrote code and handed it in. I got like 20% in that class, but thankfully the highest grade was like 50.
Ruby
I've never used Ruby, but this looks pretty sexy.
1.upto(100) do |n|
print "Fizz" if a = (n % 3).zero?
print "Buzz" if b = (n % 5).zero?
print n unless (a || b)
print "n"
end
It's printing 'Fizz' and then just printing 'Buzz' right after it to account for something being divisible by both. There seems to be very little bracketing ({ code here }) which seems parsimonious and cool. It's very easy to mess up code by forgetting brackets, and you can end up with hundreds of them.
SQL
Finally, here's an example from SQL, which is a database language, like the database which runs this blog, but no one would really use it for solving such a problem.
DROP TABLE fizzbuzz;
CREATE TABLE fizzbuzz(i INT, fizz string, buzz string);
INSERT INTO fizzbuzz VALUES(1,"","");
INSERT INTO fizzbuzz SELECT i + (SELECT MAX(i) FROM fizzbuzz), fizz, buzz FROM fizzbuzz;
INSERT INTO fizzbuzz SELECT i + (SELECT MAX(i) FROM fizzbuzz), fizz, buzz FROM fizzbuzz;
INSERT INTO fizzbuzz SELECT i + (SELECT MAX(i) FROM fizzbuzz), fizz, buzz FROM fizzbuzz;
INSERT INTO fizzbuzz SELECT i + (SELECT MAX(i) FROM fizzbuzz), fizz, buzz FROM fizzbuzz;
INSERT INTO fizzbuzz SELECT i + (SELECT MAX(i) FROM fizzbuzz), fizz, buzz FROM fizzbuzz;
INSERT INTO fizzbuzz SELECT i + (SELECT MAX(i) FROM fizzbuzz), fizz, buzz FROM fizzbuzz;
INSERT INTO fizzbuzz SELECT i + (SELECT MAX(i) FROM fizzbuzz), fizz, buzz FROM fizzbuzz;
DROP TABLE lookup;
CREATE TABLE lookup (fizzy, buzzy, rem);
INSERT INTO lookup VALUES("fizz", "buzz", 1);
SELECT
(SELECT i FROM lookup WHERE rem = (i%3<>0)&(i%5<>0)),
(SELECT fizzy FROM lookup WHERE rem = (i%3=0)),
(SELECT buzzy FROM lookup WHERE rem = (i%5=0))
FROM fizzbuzz WHERE i <= 100;
So, that's a chrestomathy. A collection of texts, in this case from programming languages. This post was a chrestomathy.
For a minute there I thought I had clicked on the wrong link. In case you’re interested, here’s the source for the ‘fizzbuzz’ problem – http://imranontech.com/2007/01/24/using-fizzbuzz-to-find-developers-who-grok-coding/
I actually asked this question during a few interviews I held for a junior position at my company. The candidates either had no programming experience or very little, but all had degrees and diplomas and what not. Out of the four candidates I spoke to that day, only one answered the question and even then it wasn’t complete.
>> It’s very easy to mess up code by forgetting brackets, and you can end up with hundreds of them.
Actually, if you want you can write the Ruby example entirely without brackets.
1.upto 100 do |n|
print “Fizz” if a = n % 3 == 0
print “Buzz” if b = n % 5 == 0
print n unless a or b
print “\n”
end
However, choice of brackets depends more on the personal taste and level of clarity you ought to maintain in the code.