Author Topic: New Challenge..! Alphabetical string list  (Read 2867 times)

0 Members and 1 Guest are viewing this topic.

Andrea

  • Water Moccasin
  • Posts: 2372
New Challenge..! Alphabetical string list
« on: April 07, 2011, 11:04:43 AM »
Hi all,..
I'm curious to see some code who doing this..


New program:
Code: [Select]
(defun strL (char1 char2)
....
)

The final result need to be a list of alphabetic string.


ex:

Code: [Select]
(strL "A" "F")result= ("A" "B" "C" "D" "E" "F")


Code: [Select]
(strL "G" "B")result= ("B" "C" "D" "E" "F" "G")

Code: [Select]
(strL "X" "AF")result= ("X" "Y" "Z" "AA" "AB" "AC" "AD" "AE" "AF")

Code: [Select]
(strL "BD" "AY")result= ("AY" "AZ" "BA" "BB" "BC" "BD")

Code: [Select]
(strL "ABD" "ACA")result= ("ABD" "ABE" "ABF" "ABG" ....."ABX" "ABY" "ABZ" "ACA")


get set,....ready...go !

:)
Keep smile...

Lee Mac

  • Seagull
  • Posts: 12924
  • London, England
Re: New Challenge..! Alphabetical string list
« Reply #1 on: April 07, 2011, 11:48:38 AM »
Some of mine

http://lee-mac.com/columnreference.html

Code: [Select]
(defun strL ( m n / x l )
  (setq m (LM:Alpha2Num m)
        n (LM:Alpha2Num n) x (1+ (max m n)))
  (repeat (1+ (abs (- m n))) (setq l (cons (LM:Num2Alpha (setq x (1- x))) l)))
)
« Last Edit: April 07, 2011, 12:01:54 PM by Lee Mac »

Andrea

  • Water Moccasin
  • Posts: 2372
Re: New Challenge..! Alphabetical string list
« Reply #2 on: April 07, 2011, 12:19:04 PM »
impressed... :-o

Nice Work Lee..
It work perfect.
Keep smile...

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: New Challenge..! Alphabetical string list
« Reply #3 on: April 07, 2011, 01:18:15 PM »
Hi,

I should have used some of mine (look like Lee's...) or this one.

I'd rather build a specific new one (works only with uppercase strings).

EDIT: there was a mistake in the 'baz' sub
Code: [Select]
(defun strL (s1 s2 / foo bar baz)
  (defun foo (l1 l2)
    (if (equal l1 l2)
      (list (reverse l1))
      (cons (reverse l1) (foo (bar l1) l2))
    )
  )
  (defun bar (l)
    (if (= (car l) 90)
      (cons 65
    (if (cdr l)
      (bar (cdr l))
      '(65)
    )
      )
      (cons (1+ (car l)) (cdr l))
    )
  )
  (defun baz (l1 l2)
    (or (< (length l1) (length l2))
(and (= (length l1) (length l2))
     (or (< (car l1) (car l2))
(and (= (car l1) (car l2)) (cdr l1) (baz (cdr l1) (cdr l2)))
     )
)
    )
  )
  ((lambda (l1 l2 / tmp)
     (or (baz l1 l2)
(setq tmp l1
       l1  l2
       l2  tmp
)
     )
     (mapcar 'vl-list->string (foo (reverse l1) (reverse l2)))
   )
    (vl-string->list s1)
    (vl-string->list s2)
  )
)
« Last Edit: April 07, 2011, 04:13:14 PM by gile »
Speaking English as a French Frog

Andrea

  • Water Moccasin
  • Posts: 2372
Re: New Challenge..! Alphabetical string list
« Reply #4 on: April 07, 2011, 01:29:06 PM »
Hi,

I should have used some of mine (look like Lee's...) or this one.

I'd rather build a specific new one (works only with uppercase strings).

Code: [Select]
(defun strL (s1 s2 / foo bar baz)
  (defun foo (l1 l2)
    (if (equal l1 l2)
      (list (reverse l1))
      (cons (reverse l1) (foo (bar l1) l2))
    )
  )
  (defun bar (l)
    (if (= (car l) 90)
      (cons 65
    (if (cdr l)
      (bar (cdr l))
      '(65)
    )
      )
      (cons (1+ (car l)) (cdr l))
    )
  )
  (defun baz (l1 l2)
    (or
      (< (length l1) (length l2))
      (< (car l1) (car l2))
      (and (= (car l1) (car l2))
   (baz (cdr l1) (cdr l2))
      )
    )
  )
  ((lambda (l1 l2 / tmp)
     (or (baz l1 l2)
(setq tmp l1
       l1  l2
       l2  tmp
)
     )
     (mapcar 'vl-list->string (foo (reverse l1) (reverse l2)))
   )
    (vl-string->list s1)
    (vl-string->list s2)
  )
)

Gile,...

tu es l'un des Guru dont les codes vont au delà de ma compréhension.
Merci, car de mains de maîtres j'apprends.

Mes plus sincères respects.
Keep smile...

Lee Mac

  • Seagull
  • Posts: 12924
  • London, England
Re: New Challenge..! Alphabetical string list
« Reply #5 on: April 07, 2011, 01:56:53 PM »
Nice solution Gile  :-)

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: New Challenge..! Alphabetical string list
« Reply #6 on: April 07, 2011, 02:07:53 PM »
Thanks,

I can try to give some explanations.

First, as we know, LISP is more comfortable (and faster) with lists than with strings and lists are cheaper to compute from the head (car), so the main function converts the 2 strings into 2 ascii codes lists and reverse them before using with the three little local functions (foo bar baz)

baz evaluates if the first list is 'lower' than the second one.
bar 'increments' an ascii codes list by 1.
foo build the list of 'incremted' lists.
Speaking English as a French Frog