Author Topic: Increment Challenge..  (Read 8298 times)

0 Members and 1 Guest are viewing this topic.

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1454
  • Marco
Re: Increment Challenge..
« Reply #15 on: September 08, 2016, 04:16:49 PM »
this is my quick attempt..
Working with negative value and Upper or lower case :smitten:
Comando: (AA:SETCAR "A" -1)
Traccia all'indietro:
[0.50] (VL-BT)
[1.46] (ERRDUMP "valore dell'argomento errato: non negativo: -1")
[2.41] (_call-err-hook #<SUBR @000000002bff3f48 ERRDUMP> "valore dell'argomento errato: non negativo: -1")
[3.35] (sys-error "valore dell'argomento errato: non negativo: -1")
:ERROR-BREAK.30 nil
[4.27] (NTH -1 ("A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"))
[5.21] (AA:SETCAR "A" -1)
[6.15] (#<SUBR @000000002bf8f2a0 -rts_top->)
[7.12] (#<SUBR @00000000353c8700 veval-str-body> "(AA:SETCAR \"A\" -1)" T #<FILE internal>)
:CALLBACK-ENTRY.6 (:CALLBACK-ENTRY)
:ARQ-SUBR-CALLBACK.3 (nil 0)

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1454
  • Marco
Re: Increment Challenge..
« Reply #16 on: September 08, 2016, 04:18:55 PM »
Test Made:
Code: [Select]
(ALE_ZetaStrIncVal "aa" -1)   = "a`"   I think it must be "z"  ?
Why? See your Reply #4 on: Today at 03:54:31 »

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1454
  • Marco
Re: Increment Challenge..
« Reply #17 on: September 08, 2016, 04:29:42 PM »
Code: [Select]
Comando: (ALE_ZetaStrIncVal "aa" -1) > "a`"
Comando: (incsuff "aa" -1 7)              > "a`"

Comando: (ALE_ZetaStrIncVal "aB" 1)  > "aC"
Comando: (incsuff "aB" 1 7)            > "aC"

Andrea

  • Water Moccasin
  • Posts: 2372
Re: Increment Challenge..
« Reply #18 on: September 12, 2016, 01:32:21 PM »
Test Made:
Code: [Select]
(ALE_ZetaStrIncVal "aa" -1)   = "a`"   I think it must be "z"  ?
Why? See your Reply #4 on: Today at 03:54:31 »

..x...y...z...aa...ab...ac...ad  ?
so (aa - 1) must be "z"  ?


forse mi sbaglio ?
Keep smile...

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: Increment Challenge..
« Reply #19 on: September 12, 2016, 06:35:42 PM »
I found a bug with my function  :oops: :
Code - Auto/Visual Lisp: [Select]
  1. _$ (LM:incalpha "Y" 1)
  2. "@"

The following should fix it and allow for lowercase:
Code - Auto/Visual Lisp: [Select]
  1. ;; Increment Alpha  -  Lee Mac
  2. ;; Increments an uppercase alphabetical string by a given number of units, e.g. AX + 4 = BB
  3. ;; a - [str] alphabetical string
  4. ;; i - [int] integer increment
  5.  
  6. (defun LM:incalpha ( a i / c n )
  7.     (cond
  8.         (   (zerop i) a)
  9.         (   (zerop (setq n (strlen a)))
  10.             (LM:incalpha "A" (1- i))
  11.         )
  12.         (   (= 90 (logand 90 (setq c (ascii (substr a n)))))
  13.             (LM:incalpha (strcat (LM:incalpha (substr a 1 (1- n)) 1) (chr (boole 6 27 c))) (1- i))
  14.         )
  15.         (   (setq i (+ i (boole 4 97 c)))
  16.             (strcat (LM:incalpha (substr a 1 (1- n)) (/ i 26)) (chr (boole 7 (boole 1 96 c) (1+ (rem i 26)))))
  17.         )
  18.     )
  19. )

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Increment Challenge..
« Reply #20 on: September 12, 2016, 07:17:51 PM »

Can I borrow a dollar gile?

Merci Andrea,

This routine has been used as base routine in the "Increment" LISP suite which is the ancestor of the quite popular .NET "Increment" plugin on Exchange Apps.
Use it and a great tool

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1454
  • Marco
Re: Increment Challenge..
« Reply #21 on: September 13, 2016, 02:48:59 AM »
Test Made:
Code: [Select]
(ALE_ZetaStrIncVal "aa" -1)   = "a`"   I think it must be "z"  ?
Why? See your Reply #4 on: Today at 03:54:31 »

..x...y...z...aa...ab...ac...ad  ?
so (aa - 1) must be "z"  ?


forse mi sbaglio ?
Non so se ti sbagli ma tutte queste funzioni danno lo stesso risultato:
Code: [Select]
(LM:incalpha "aa" -1)       => "a`"
(ALE_ZetaStrIncVal "aa" -1) => "a`"
(incsuff "aa" -1 7)         => "a`"

VovKa

  • Water Moccasin
  • Posts: 1632
  • Ukraine
Re: Increment Challenge..
« Reply #22 on: September 13, 2016, 04:56:25 AM »
i think that doing math on the system that has no "zero" is not a good idea

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Increment Challenge..
« Reply #23 on: September 13, 2016, 07:27:17 AM »
I found a bug with my function  :oops: :
Code - Auto/Visual Lisp: [Select]
  1. _$ (LM:incalpha "Y" 1)
  2. "@"

The following should fix it and allow for lowercase:
Code - Auto/Visual Lisp: [Select]
  1. ;; Increment Alpha  -  Lee Mac
  2. ;; Increments an uppercase alphabetical string by a given number of units, e.g. AX + 4 = BB
  3. ;; a - [str] alphabetical string
  4. ;; i - [int] integer increment
  5.  
  6. (defun LM:incalpha ( a i / c n )
  7.     (cond
  8.         (   (zerop i) a)
  9.         (   (zerop (setq n (strlen a)))
  10.             (LM:incalpha "A" (1- i))
  11.         )
  12.         (   (= 90 (logand 90 (setq c (ascii (substr a n)))))
  13.             (LM:incalpha (strcat (LM:incalpha (substr a 1 (1- n)) 1) (chr (boole 6 27 c))) (1- i))
  14.         )
  15.         (   (setq i (+ i (boole 4 97 c)))
  16.             (strcat (LM:incalpha (substr a 1 (1- n)) (/ i 26)) (chr (boole 7 (boole 1 96 c) (1+ (rem i 26)))))
  17.         )
  18.     )
  19. )

Code: [Select]
(LM:incalpha  "D" 1);==>>"F"
(LM:incalpha  "d" 1);==>>"f"

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: Increment Challenge..
« Reply #24 on: September 13, 2016, 08:14:48 AM »
Code: [Select]
(LM:incalpha  "D" 1);==>>"F"
(LM:incalpha  "d" 1);==>>"f"

Thanks - I had taken the binary manipulation a step too far  :oops:

Corrected (hopefully!):
Code - Auto/Visual Lisp: [Select]
  1. ;; Increment Alpha  -  Lee Mac
  2. ;; Increments an uppercase alphabetical string by a given number of units, e.g. AX + 4 = BB
  3. ;; a - [str] alphabetical string
  4. ;; i - [int] integer increment
  5.  
  6. (defun LM:incalpha ( a i / c n )
  7.     (cond
  8.         (   (zerop i) a)
  9.         (   (zerop (setq n (strlen a)))
  10.             (LM:incalpha "A" (1- i))
  11.         )
  12.         (   (= 90 (logand 90 (setq c (ascii (substr a n)))))
  13.             (LM:incalpha (strcat (LM:incalpha (substr a 1 (1- n)) 1) (chr (boole 6 27 c))) (1- i))
  14.         )
  15.         (   (setq i (+ i -1 (boole 4 96 c)))
  16.             (strcat (LM:incalpha (substr a 1 (1- n)) (/ i 26)) (chr (boole 7 (boole 1 96 c) (1+ (rem i 26)))))
  17.         )
  18.     )
  19. )

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: Increment Challenge..
« Reply #25 on: September 13, 2016, 08:20:20 AM »
I may be speaking too soon, but I think the function could actually be condensed to:
Code - Auto/Visual Lisp: [Select]
  1. ;; Increment Alpha  -  Lee Mac
  2. ;; Increments an uppercase alphabetical string by a given number of units, e.g. AX + 4 = BB
  3. ;; a - [str] alphabetical string
  4. ;; i - [int] integer increment
  5.  
  6. (defun LM:incalpha ( a i / c n )
  7.     (cond
  8.         (   (zerop i) a)
  9.         (   (zerop (setq n (strlen a))) (LM:incalpha "A" (1- i)))
  10.         (   (setq c (ascii (substr a n))
  11.                   i (+ i -1 (boole 4 96 c))
  12.             )
  13.             (strcat (LM:incalpha (substr a 1 (1- n)) (/ i 26)) (chr (boole 7 (boole 1 96 c) (1+ (rem i 26)))))
  14.         )
  15.     )
  16. )

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Increment Challenge..
« Reply #26 on: September 13, 2016, 09:00:18 AM »
my version:
Code - Auto/Visual Lisp: [Select]
  1. (defun ai (s i / A B L)
  2.   (setq l (strlen s)
  3.             a (ascii (substr s l))
  4.             b (cond ((< 64 a 91) 64)
  5.                     ((< 96 a 123) 96)
  6.               )
  7.       )
  8.   (and (minusp i)(<(- a (abs i) b)1) (setq i (+ 26  i (/ i -26))))
  9.   (if b
  10.     (strcat (substr s 1 (1- l))
  11.             (cond ((< (+ b 26) (+ i a)) (chr (+ b (/ (- (+ i a) b) 26))))
  12.                   ("")
  13.             )
  14.             (chr (+ b ((lambda(c)(if (zerop c) 26 c)) (rem  (- (+ i a) b) 26))))
  15.     )
  16.   )
  17. )
test:
Code: [Select]
(ai  "A" 1) ;;>>> "B"
(ai  "D" 3) ;;>>> "G"
(ai  "AAP" 1) ;;>>> "AAQ"
(ai  "BAU" 2) ;;>>> "BAW"
(ai  "A" -1) ;;>>> "Z"
(ai  "FFFC" 2) ;;>>> "FFFE"
(ai  "Z" 1) ;;>>> "AA"
(ai  "Z" -1) ;;>>> "Y"
(ai  "a" 1) ;;>>> "b"
(ai  "d" 3) ;;>>> "g"
(ai  "aap" 1) ;;>>> "aaq"
(ai  "bau" 2) ;;>>> "baw"
(ai  "a" -1) ;;>>> "z"
(ai  "fffc" 2) ;;>>> "fffe"
(ai  "z" 1) ;;>>> "aa"
(ai  "z" -1) ;;>>> "y"
(ai  "y" -1) ;;>>> "x"

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Increment Challenge..
« Reply #27 on: September 13, 2016, 09:03:36 AM »
:)
Code: [Select]
(ai  "AA" -1) ;;>>> "AZ"
(ai  "zz" 1) ;;>>> "zaa"
« Last Edit: September 13, 2016, 09:11:22 AM by ElpanovEvgeniy »

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Increment Challenge..
« Reply #28 on: September 13, 2016, 12:07:38 PM »
new variant:

Code - Auto/Visual Lisp: [Select]
  1. (defun ff (s i / b f)
  2.   (defun f (l i)
  3.     (cond ((= 0 i) l)
  4.           ((not l) (f (list i) 0))
  5.           ((cons (rem (+ (car l) i) 26) (f (cdr l) (/ (+ (car l) i) 26))))
  6.     )
  7.   )
  8.   (if (setq b (cond ((< 64 (ascii s) 91) 64)
  9.                     ((< 96 (ascii s) 123) 96)
  10.               )
  11.       )
  12.            (mapcar (function (lambda (a)
  13.                                (cond ((< a 1) "")
  14.                                      ((chr (+ a b)))
  15.                                )
  16.                              )
  17.                    )
  18.                    (reverse (f (reverse (mapcar (function (lambda (a) (- a 96))) (vl-string->list (strcase s t)))) i))
  19.            )
  20.     )
  21.   )
  22. )

test:
Code: [Select]
(ff "aa" -200);;>>> ""
(ff "aa" 200);;>>> "hs"
(ff "aa" 2000);;>>> "byy"
(ff "aa" -2000);;>>> ""
(ff "AA" -200) ;;>>> ""
(ff "AA" 200);;>>> "HS"
(ff "aA" -200) ;;>>> ""
(ff "aA" 200);;>>> "hs"
(ff "Aa" -200) ;;>>> ""
(ff "Aa" 200);;>>> "HS"
(ff  "AA" -1) ;;>>> "A"
(ff  "A" 1) ;;>>> "B"
(ff  "A" -1) ;;>>> ""
(ff  "A" 1) ;;>>> "B"
(ff  "D" 3) ;;>>> "G"
(ff  "AAP" 1) ;;>>> "AAQ"
(ff  "BAU" 2) ;;>>> "BAW"
(ff  "A" -1) ;;>>> ""
(ff  "FFFC" 2) ;;>>> "FFFE"
(ff  "Z" 1) ;;>>> "AA"
(ff  "Z" -1) ;;>>> "Y"
(ff  "a" 1) ;;>>> "b"
(ff  "d" 3) ;;>>> "g"
(ff  "aap" 1) ;;>>> "aaq"
(ff  "bau" 2) ;;>>> "baw"
(ff  "a" -1) ;;>>> ""
(ff  "fffc" 2) ;;>>> "fffe"
(ff  "z" 1) ;;>>> "aa"
(ff  "z" -1) ;;>>> "y"
(ff  "y" -1) ;;>>> "x"

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1454
  • Marco
Re: Increment Challenge..
« Reply #29 on: September 13, 2016, 12:51:06 PM »
 :-) IM(very)HO is a good solution... but the new question:
Code: [Select]
(ff "b"   -1) =>  "a"
(ff "aa"  -1) => "a"

Is it acceptable the same result on two strings? Perhaps VovKa said something right?