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

0 Members and 1 Guest are viewing this topic.

Andrea

  • Water Moccasin
  • Posts: 2372
Increment Challenge..
« on: September 07, 2016, 05:02:13 PM »
Hi All,..

for any who are interested to this challenge..
The function must increment Alphabetic value with two (2) parameter...

ex: (AphaIncrement  A B)

A= Start Letter as a string
B= Increment value as integer (this can be also negative value)

So the challenge is this...
(AphaIncrement  "A" 1) = "B"
(AphaIncrement  "D" 3) = "G"
(AphaIncrement  "AAP" 1) = "AAQ"
(AphaIncrement  "BAU" 2) = "BAW"
(AphaIncrement  "A" -1) = "Z"
(AphaIncrement  "FFFC" 2) = "FFFE"

the goal is to have,...
a,b,c,d,e,....z,
aa,ab,ac,ad,ae...az,
ba,bc,bd,be,..bz
ca,cb,cd,ce,...cz,..
etc...

$5 for who can make this....

Get ready !...
this challenge will start NOW !

Good luck !  :knuppel2:
Keep smile...

VovKa

  • Water Moccasin
  • Posts: 1632
  • Ukraine
Re: Increment Challenge..
« Reply #1 on: September 07, 2016, 05:20:25 PM »
(AphaIncrement  "A" -1) = "Z"
are you sure about this one?

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: Increment Challenge..
« Reply #2 on: September 07, 2016, 06:04:21 PM »

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Increment Challenge..
« Reply #3 on: September 07, 2016, 06:52:22 PM »
Should've held out for a sammich.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Andrea

  • Water Moccasin
  • Posts: 2372
Re: Increment Challenge..
« Reply #4 on: September 07, 2016, 09:54:31 PM »
(AphaIncrement  "A" -1) = "Z"
are you sure about this one?

You're right....no sense..it must be nil or "A"
+1
« Last Edit: September 07, 2016, 09:58:53 PM by Andrea »
Keep smile...

Andrea

  • Water Moccasin
  • Posts: 2372
Re: Increment Challenge..
« Reply #5 on: September 07, 2016, 09:57:08 PM »
http://lee-mac.com/columnreference.html#incalpha

LEE !!!!!!!!!!!!!!   that's sick !  4 recursive in row !?
you're just amazing.. :uglystupid2:

one thing....your code do not allow negative value..as increment.
Keep smile...

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Increment Challenge..
« Reply #6 on: September 08, 2016, 03:24:08 AM »
(AphaIncrement  "A" -1) = "Z"
are you sure about this one?

You're right....no sense..it must be nil or "A"
+1
You need a representation for zero. If "A" is the first digit it follows that it represents zero. But this is counterintuitive. And if "A" is zero then "AA" is zero as well.

VovKa

  • Water Moccasin
  • Posts: 1632
  • Ukraine
Re: Increment Challenge..
« Reply #7 on: September 08, 2016, 05:12:19 AM »
You need a representation for zero. If "A" is the first digit it follows that it represents zero. But this is counterintuitive. And if "A" is zero then "AA" is zero as well.
i believe Lee's code is written to deal with MS Excel, so it's got no zero

VovKa

  • Water Moccasin
  • Posts: 1632
  • Ukraine
Re: Increment Challenge..
« Reply #8 on: September 08, 2016, 05:42:04 AM »
i have this to work with custom "numeral" systems based on positional notation
no negative numbers
Code: [Select]
(defun vk_AlphaBet2Dec (ABC AlphaBet / Len)
  (setq Len (strlen AlphaBet))
  (if (/= ABC "")
    (+ (* (expt Len (1- (strlen ABC)))
  (vl-string-position (ascii (substr ABC 1 1)) AlphaBet)
       )
       (vk_AlphaBet2Dec (substr ABC 2) AlphaBet)
    )
    0
  )
)
(defun vk_Dec2AlphaBet (Dec AlphaBet / Len)
  (setq Len (strlen AlphaBet))
  (if (< Dec Len)
    (substr AlphaBet (1+ Dec) 1)
    (strcat (vk_Dec2AlphaBet (/ Dec Len) AlphaBet)
    (substr AlphaBet (1+ (rem Dec Len)) 1)
    )
  )
)
Code: [Select]
(vk_Dec2AlphaBet 35 "01")
(vk_Dec2AlphaBet 35 "0123456789")
(vk_Dec2AlphaBet 35 "0123456789ABCDEF")
(vk_Dec2AlphaBet 35 "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
(vk_Dec2AlphaBet 35 "Aa")
(vk_Dec2AlphaBet 35 "theSwamp")

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: Increment Challenge..
« Reply #9 on: September 08, 2016, 07:41:17 AM »
Hi,

Quite a long time since I've been here...

You can see the (quite old too) topic.
Speaking English as a French Frog

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1454
  • Marco
Re: Increment Challenge..
« Reply #10 on: September 08, 2016, 08:17:32 AM »
Code: [Select]
(defun ALE_ZetaStrIncVal (ZetStr IncVal / SttStr SttLen)
  (if (eq "" (setq SttStr (vl-string-right-trim "Z" ZetStr)))
    (strcat (vl-string-translate "Z" "A" ZetStr) "A")
    (strcat
      (substr SttStr 1 (setq SttLen (1- (strlen SttStr))))
      (chr (+ IncVal (vl-string-elt SttStr SttLen)))
      (if (eq ZetStr SttStr)
        ""
        (vl-string-translate "Z" "A" (substr ZetStr (+ 2 SttLen)))
      )
    )
  )
)
Code: [Select]
(ALE_ZetaStrIncVal  "A" 1) "B"
(ALE_ZetaStrIncVal  "D" 3) "G"
(ALE_ZetaStrIncVal  "AAP" 1) "AAQ"
(ALE_ZetaStrIncVal  "BAU" 2) "BAW"
(ALE_ZetaStrIncVal  "A" -1) "@"  <<<<<<<<<<<<<<<<<
(ALE_ZetaStrIncVal  "FFFC" 2) "FFFE"

Andrea

  • Water Moccasin
  • Posts: 2372
Re: Increment Challenge..
« Reply #11 on: September 08, 2016, 10:06:38 AM »
this is my quick attempt..
Working with negative value and Upper or lower case :smitten:

Code: [Select]
(defun AA:setcar (startwith increment / carpref iba_alphabet cval str# startwith2 nextcar newstartwith)
  (setq startwith2 startwith)
  (if (< (ascii (substr startwith 1 1)) 96)
    (setq cval 64)
    (setq cval 96)
  )
  (setq iba_alphabet nil)
  (repeat 26
    (setq iba_alphabet (append iba_alphabet (list (chr (setq cval (1+ cval))))))
  )
  (setq carpref "")
  (if (> (setq str# (strlen startwith2)) 1)
    (progn (setq carpref (substr startwith2 1 (1- str#)))
           (setq startwith2 (substr startwith2 str#))
    )
  )
  (if (not
        (setq nextcar (nth (+ increment (vl-position startwith2 iba_alphabet))
                           iba_alphabet
                      )
        )
      )
    (progn (setq carpref startwith)
           (if (> (setq str# (strlen startwith2)) 1)
             (progn (setq carpref (substr startwith2 1 (1- str#)))
                    (setq startwith2 (substr startwith2 str#))
             )
           )
           (setq nextcar startwith2)
    )
  )
  (setq newstartwith (strcat carpref nextcar))
)

Keep smile...

Andrea

  • Water Moccasin
  • Posts: 2372
Re: Increment Challenge..
« Reply #12 on: September 08, 2016, 10:17:53 AM »
Well,...you guys are just amazing..
many concept, diffrent thinking remind me how human can be intelligent sometime.
Thank you very much for sharing all these samples and knowledge.

Now,..who is the winner of this 5$ ?   :whistling:
Keep smile...

Andrea

  • Water Moccasin
  • Posts: 2372
Re: Increment Challenge..
« Reply #13 on: September 08, 2016, 11:06:51 AM »
Test Made:
Code: [Select]
(ALE_ZetaStrIncVal "aa" -1)   = "a`"   I think it must be "z"  ?
Code: [Select]
(LM:incalpha "a" 1)  = "AH"  ?   lowercase ?
Code: [Select]
(incsuff "aB" 1 7) = work perfectly
Code: [Select]
(aa:setcar "BB" -1)  = "BA"
(aa:setcar "BA" -1) = Error  :)

so,.....GILE is the winner !
Also,..his code allow numeric value and respect CASE sensitive..
nice coding GILE.  merci.
Keep smile...

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: Increment Challenge..
« Reply #14 on: September 08, 2016, 02:29:13 PM »
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.
Speaking English as a French Frog

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? 

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: Increment Challenge..
« Reply #30 on: September 13, 2016, 12:54:39 PM »
 :wink:
Code: [Select]
_$ (ff "a" 25)
"a"
_$ (ff "a" 701)
"{"

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Increment Challenge..
« Reply #31 on: September 13, 2016, 02:59:11 PM »
I've been reluctant to post in this thread because I don't have time to explain these excerpts from my library.

So I offer them "as is" for idea fodder, noting they were written for other purposes, and are only a small subset of a library of 60 related encoding/encryption/bit packing functions (there's other to/from flavors corresponding to other 2^x boundaries).

Anyway ...

Code: [Select]
(defun _Map32 ( )
    ;;  © Michael Puckett
    (setq **map32**
        (vl-string->list
            ;;  Note the omission of letters I,L,O,Q
            "0123456789ABCDEFGHJKMNPRSTUVWXYZ"
        )
    )
    (defun _Map32 ( ) **map32**)
    **map32**
)

Code: [Select]
(defun _ToMap ( i map )
    ;;  © Michael Puckett
    (   (lambda ( mask shift / result )
            (while (not (zerop i))
                (setq
                    result (cons (nth (logand mask i) map) result)
                    i      (lsh i shift)
                )
            )
            (vl-list->string result)
        )
        (1- (length map))
        (- (fix (/ (log (length map)) (log 2))))
    )
)

Code: [Select]
(defun _FromMap ( x map )
    ;;  © Michael Puckett
    (   (lambda ( x map shift result )
            (while x
                (setq result (+ result (length (member (car x) map))))
                (if (setq x (cdr x)) (setq result (lsh result shift)))
            )
            result
        )
        (mapcar 'chr (vl-string->list x))
        (mapcar 'chr (reverse (cdr map)))
        (fix (/ (log (length map)) (log 2)))
        0
    )
)

Code: [Select]
(defun _To32 ( i )
    ;;  © Michael Puckett
    (_ToMap i (_Map32))
)


Code: [Select]
(defun _From32 ( x )
    ;;  © Michael Puckett
    (_FromMap x (_Map32))
)

Code: [Select]
(defun _Inc32 ( x i )
    ;;  © Michael Puckett
    (_To32 (+ i (_From32 x)))
)

The range of values that can be represented by these functions corresponds to the range of integers that AutoCAD can represent:

    (expt 2 31)      >> -2147483648, (_To32 (expt 2 31))      >> "2000000"
    (1- (expt 2 31)) >> +2147483647, (_To32 (1- (expt 2 31))) >> "1ZZZZZZ"

To understand why (expt 2 31) yeilds a negative value the uninitiated may wish to read articles on "two's complement".

Code performs no hand handholding. See _Map32 function for acceptable data, notwithstanding AutoCAD's integer limits. Pass stupid data, crash, get stupid results or an std.
   
Examples:

    (_Inc32 "0" 1)      >> "1"
    (_Inc32 "0" -1)     >> "3ZZZZZZ"
    (_From32 "3ZZZZZZ") >> -1

    (_Inc32 "0" 31)     >> "Z"
    (_Inc32 "0" 32)     >> "10"

    (_Inc32 "Z" 1)      >> "10"
    (_Inc32 "ZZ" 1)     >> "100"

    (_Inc32 "Z" -1)     >> "Y"
    (_Inc32 "ZZ" -1)    >> "ZY"
   
    (_Inc32 "ABC" 1)    >> "ABD"
    (_Inc32 "ABC" 10)   >> "ABP"
    (_Inc32 "ABC" 100)  >> "AEG"
   
    (_Inc32 "ABC" -1)   >> "ABB"
    (_Inc32 "ABC" -10)  >> "AB2"
    (_Inc32 "ABC" -100) >> "A88"


FWIW / Have fun.

Cheers.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Increment Challenge..
« Reply #32 on: September 15, 2016, 09:34:18 AM »
...

FWIW / Have fun.

Cheers.
   
Thank you!

I've never used LOGAND function with negative numbers...
Code - Auto/Visual Lisp: [Select]
  1. (logand 31 -1)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Increment Challenge..
« Reply #33 on: September 15, 2016, 10:17:46 AM »
(logand x -1) will always return x because -1 = 1111111111111111. :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: Increment Challenge..
« Reply #34 on: September 15, 2016, 05:51:28 PM »
I've never used LOGAND function with negative numbers...
Code - Auto/Visual Lisp: [Select]
  1. (logand 31 -1)

FWIW, under such circumstances, I find the use of the bitwise NOT operator easier to understand which bits are being masked - for example, using:
Code - Auto/Visual Lisp: [Select]
  1. (logand 13 (~ 8))

It is clear that we are returning every bit except 8.

This is equivalently:
Code - Auto/Visual Lisp: [Select]
  1. (logand 13 -6)

Which isn't so readable with the two's-compliment mental arithmetic.

For the given example:
Code - Auto/Visual Lisp: [Select]
  1. (logand 13 (~ 0))

i.e. mask nothing, return all.



Thank you for sharing your endeavours MP.
« Last Edit: September 15, 2016, 05:55:36 PM by Lee Mac »

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Increment Challenge..
« Reply #35 on: September 15, 2016, 07:19:26 PM »
<nodding> Agree, the bitwise not is useful and often makes clearer one's intent. Thanks LM.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst