There is one nice property of the real data: it is guaranteed to be a real month - it will be output as part of the UNIX 'date' command. So we don't have to worry about words that are not months. Our function can return any number it likes.
We will encode each letter as a number and add them up the result will be the number of the month.
The fun is in finding a suitable code...
Since we have 19 unknowns and only 12 constraints we can expect there to be several solutions -- there should be 7 letters that we can have any value we like, and then 12 will be determined by our chosen values. There is of course a possibility that no set of values for our variables can possibly satisfy all 12 constraints. If so we will find a variable that has to have two different values.
We will write it in algebraic form with single lower case letters standing for the codes of the corresponding character. So we start by listing the months (on a piece of paper) and then the letters that appear in them:
Now I have several ways to tackle these:
Go an buy a linear algebra package.
Go and hire a mathematician.
Go to the library and look up the algorithms....
. . . . . . . . . ( end of section Alternative Approaches) <<Contents | End>>
A Mistake
What I chose to do included a couch, some music, a non-alcoholic brew,
and a little bit of luck... Then coded up my answers, tested them
and did not notice the bug...
It was only when I came to write up how I found the solutions that I found I had made several errors... including one that should have seen when I tested the code.... But such is the strength of ego that I could not see my own errors....
Learn from this.... it is not enough to test a program, you have to be able to explain why it works.
So here is my reconstruction of how I should have tacked it. Please check for mistakes.
A Better Approach
First I noticed that some of the months where similar enough that taking
the difference between two equations will remove two variables. This is
documented by the explanation:
This works because of one of Euclid's Axioms: subtracting equals from equals gives equals.
More importantly, the original pair of equations can be replaced by a pair where one is much simpler.
Jun and Jul are nearly the same.
Jun and Jan are nearly the same.
Mar and Apr are similar.
Mar and May are almost the same.
So I have a new set of equations that are indeed similarly to the original set:
Now
I have a visual brain - it needs pictures so I sketched out a picture where each equation with 3 unknowns was a triangle and each equation with two unknowns was a line.
I found it easy to place values on this diagram and work out the consequences.
In the code I assumed the following:
These are the values expressed in the code.... [ months.cc ]
I was reviewing your developement of the "months" function, URL
http://www.csci.csusb.edu/dick/samples/months.html
In the final code (beginning with Let{(2.14) ...) I don't see
any value for the letter "j"!
Paul E. Black p.black@ieee.org
Reply: Thank you - I have added [ 2.26 ] above. I took the oportunity to fix a typo in [ 2.17 ] where it used to say n=1 rather than l=1.
Request For Comment
Your comments are welcome to be added here with full
citation etc.... Send them me via
[ ../mailme.html ]
. . . . . . . . . ( end of section Comments by Readers) <<Contents | End>>
Exercise 1
Choose a different set of values for m,r,n,...in step 2.14 and
convince yourself that the same routine generates a set of values
for the rest.
Exercise 2
Set up the equations for the days of the week(m+o+n=1,...). Find a
solution. Hint: s,u, and t are the central variables here.
Exercise 3
Using my code as a basis, and the results of exercise 2 above, produce
a function to decode the days of the week.
Exercise 4
Why can you not do this (unless a miracle has occurred) to recognize
the postal symbols for the states in the USA?
Can you recognize both days or the week and months in a single
function using the dame technique? If so: do it, else, explain why not.
Review Exercise 1
Do this after completing the work on arrays. How can you
use an constant array to replace the int code(char) function neatly?
Do it.
Advanced Exercise 1
Embed the tested functions for months and days in a UNIX command
that echoes the number when given a single argument with a three day
abbreviation.