max

Q&A's, tips, howto's
Locked
eddier
Posts: 289
Joined: Mon Oct 07, 2002 2:48 pm
Location: Blue Mountain College, MS US

max

Post by eddier »

Would be nice if max and min worked with strings or is this a bad idea?

(max "a" "A" "B" "b" "C") => "b"

Eddie

pjot
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Post by pjot »

How would you see this with the following strings:

Code: Select all

(max "lUtz" "edDie" "pEter" "NORman") => ?
Should we add the individual letters? Or is it the first letter which counts?

pjot
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Post by pjot »

Reading my post, it seems rude, but I really did not mean it that way. I was just wondering, how would you consider the example to be "maxed"?

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

I guess Eddie means ASCII sort order as produces when sorting the strings, and don't worry, I don't think anybody is thinking you suggest comparing members on this board ;)

Looking in to the code, I find its a bit expensive. All floating point functions are handled together in one function and would be burdened with an additional type check for 'max' and 'min' on the first argument. Internally there is an compareCells() function for comparing any type and complexity of s-expressions. But the speed penalty would be too big, and I don't want to burden to math functions used relatively often with that speed penalty.

Lutz

pjot
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Post by pjot »

So, if Eddie would mean the ASCII sort order, I can also say:

(max "lUtz" "edDie" "pEter" "NORman") = (sort (list "lUtz" "edDie" "pEter" "NORman") '>)

=> ("pEter" "lUtz" "edDie" "NORman")

Or not? But the intention is probably to use "max" in an analogue way for characters and strings, not only for numbers...

eddier
Posts: 289
Joined: Mon Oct 07, 2002 2:48 pm
Location: Blue Mountain College, MS US

Post by eddier »

I was looking for

Code: Select all

(define-macro (_max)
   (first (sort (args) '>)))
or

Code: Select all

(define-macro(_max)
  (last (sort (args)))
I know Python has this function because I have used it before. I have a list of dates in Y/M/D order as strings and I needed to get the latest. And so, I did the above. I assume the sort is probably quicksort, but I don't know. I know that this should be an O(n) problem but on average the algorithm above is an O(n log n) and O(n * n) in the worst case.

I'll recode to make it O(n).

Thanks.

Eddie

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

Or you could translate the date string into date values and then use max for numbers. This would have linear scaling O(n). It's the best thing anyway to store dates as values for any other date math.

newLISP always uses the qsort() function from the libc library.

Lutz

eddier
Posts: 289
Joined: Mon Oct 07, 2002 2:48 pm
Location: Blue Mountain College, MS US

Post by eddier »

The input file is a tab delimited file generated from ODBC through our database. I have no choice about the date values. There are quite a number of things that I have to apply a rule base or calculate.

Your solution of converting dates to numerical format and then using max is a good one. Then, I have the benefit of more easily translating to the format required by MS Dept of Ed.

Thanks!

Back in the late 90's I wrote the conversion program in Perl. About every year, something the MS Dept of Ed. chages something, add a field, change the way field is calculated, draw from all semesters, draw from 1 semester, etc. Perl seemed to be the way to go back then. But now, the program is getting more difficult to maintain. This time I had to add a field that is more complicated to solve. I thought it might be time to convert it to newLISP.

Eddie

Locked