Programming Pearls Clmn 3

Data Structures Programs [Array Only?]


Okay… From 3.1 to 3.6, I only see some suggestions about use array to program instead of a lot of variables.

Is that what the author really want to say to our readers?


In 3.6, there is a principle: The more general problem may be easier to solve. In order to solve a 23-case problem directly, writing a general n-case version and applying it to n = 23 is much easier.

Well, I might direct shoot the author. You want to prove 4 is a sum of two primes. A easier and more general version is

Every even integer greater than 2 can be expressed as a sum of two primes.

If you solve this general version problem, CONGRATULATIONS, then you solved Goldbach’s conjecture right?


Let’s solve the problems in 3.7

2. Find a1 to am in a easy way.

This remind me of optimization of some dynamic programming problem. The easiest version is Fibonacci series or sequence whatever…

F[n] = F[n-1] + F[n-2]

It is clear that we only need the last 2 numbers in the sequence. So, instead of storing the whole list, we are supposed to keep 2 variables, one is x, the other one is y.

y, x = x+y, y

Easy right?

This general version question is the same. We use k variables to store the value you need currently.

4. Date and week

Zeller’s congruence

I knew this equation or congruence or whatever since high school. I was solving a OI problem. It gave you a date, and you need to tell him what day in a week is that day. So I just used this congruence.

But I am not clear about this principle.

the months are numbered from 3 for March to 14 for February

So the 29th Feb for any leap year will be the last day for this year. And, you can take it as 0 day of next year. Quite brilliant right?

There are a lot of ways to calculate dates and days, look here

5. Find suffix.

I suggest built a Trie for all suffixes, of course, in a reverse order.

8. SSD — Seven Segment Display. [Are you from EEE? or are you a computer engineer?]

Look here.

Please join NTU! You ganna learn SSD for at least 3 years. LoL…


Write Great Code… Structure your program and structure your software.

Email me when Programmers Don’t Read Books publishes stories