Author Topic: [challenge] A31 : Midverse  (Read 1450 times)

0 Members and 1 Guest are viewing this topic.

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1314
  • Marco
Re: [challenge] A31 : Midverse
« Reply #15 on: March 08, 2022, 08:52:46 AM »
Another Vanilla:
Code: [Select]
(defun Midverse_A4 (s / L c e)
  (cond
    ( (zerop (setq L (strlen s))) "" )
    ( (= 1 L)                      s )
    ( (setq c 1   e "")
      (repeat (- L 2)
        (setq e (strcat (substr s (setq c (1+ c)) 1) e))
      )
      (strcat (substr s 1 1) e  (substr s L 1))
    )
  )
)
Code: [Select]
(setq aString "A")
(midverse_a4 aString) => "A"
(midverse-kirby aString) => "A"
(midverse-rjp aString) => "AA"
(midverse-gile aString) => "AA"
(midverse-rjp2 aString) => "AA"
(midverse-gile2 aString) => "AA"
(midverse-7 aString) => "AA"
Code: [Select]
(midverse_a4 "") => ""
(midverse-rjp "") => error
(midverse-gile "") => error
(midverse-rjp2 "") => error
(midverse-kirby "") => error
(midverse-gile2 "") => error
(midverse-7 "") => error

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1314
  • Marco
Re: [challenge] A31 : Midverse
« Reply #16 on: March 08, 2022, 08:53:49 AM »
It looks like my idea was already taken. I like it, Marc'Antonio.
Grazie  :-)

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1314
  • Marco
Re: [challenge] A31 : Midverse
« Reply #17 on: March 08, 2022, 09:16:38 AM »
Vl version:
Code: [Select]
(defun Midverse_A5 (s / L) ; similar
  (cond
    ( (not (setq L (vl-string->list s))) "" )
    ( (cdr L)
      (vl-list->string (append (list (car L)) (cdr (reverse (cdr L))) (list (last L))))
    )
    ( s )
  )
)

kirby

  • Newt
  • Posts: 110
Re: [challenge] A31 : Midverse
« Reply #18 on: March 08, 2022, 09:23:20 AM »
Wanted to check out the speed loss in my version due to 2 external function calls.
(and notwithstanding coding time reduction by using library functions)

Also took the opportunity to correct null string problem noted by Marc'Antonio

Code - Auto/Visual Lisp: [Select]
  1. (defun MidVerseV2-kirby (MyString / OutVal L FirstChar LastChar CNT)   
  2. ; [challenge] A31 : Midverse -  Reverse the middle letters of a word.  Version 2
  3. ; Input:
  4. ;       MyString - (string) string to be modified
  5. ; Returns:
  6. ;       modfied string
  7. ; Uses custom functions:
  8. ;       none!
  9.  
  10. (setq OutVal "")
  11.  
  12. (if (eq (type MyString) 'STR)
  13.   (progn
  14.        
  15.         (setq L (strlen MyString))
  16.         (if (> L 0)
  17.           (progn
  18.                 (setq FirstChar (substr MyString 1 1))
  19.                 (setq LastChar (substr MyString L 1))
  20.        
  21.                 (setq CNT L)
  22.                 (repeat (- L 2)
  23.                         (setq CNT (1- CNT))     ; work backwards
  24.                         (setq OutVal (strcat OutVal (substr MyString CNT 1)))  
  25.                 ) ; close repeat
  26.                
  27.                 (setq OutVal (strcat FirstChar OutVal LastChar))
  28.           )
  29.         ) ; close if
  30.   )    
  31. ) ; close if
  32.  
  33. OutVal
  34. )
  35.  

Re-running RJP's benchmark analysis:

Elapsed milliseconds / relative speed for 8192 iteration(s):
    (MIDVERSE-RJP "retinues").........1531 / 3.45 <fastest>
    (MIDVERSE-GILE "retinues")........1610 / 3.28
    (MIDVERSE_A2 "retinues")..........1875 / 2.82
    (MIDVERSE-KIRBYV2 "retinues").....1922 / 2.75
    (MIDVERSE_A4 "retinues")..........1953 / 2.70
    (MIDVERSE_A "retinues")...........2157 / 2.45
    (MIDVERSE-KIRBY "retinues").......2266 / 2.33
    (MIDVERSE-RJP2 "retinues")........3110 / 1.70
    (MIDVERSE-7 "retinues")...........4922 / 1.07
    (MIDVERSE-GILE2 "retinues").......5281 / 1.00 <slowest>

So in my case about a 15% speed increase from eliminating the function calls, but results vary a bit each time you run the Benchmark.   

And noting that I many not be using the latest version of MP's benchmark function, found here:
http://www.theswamp.org/index.php?topic=3952.0


JohnK

  • Administrator
  • Seagull
  • Posts: 10140
Re: [challenge] A31 : Midverse
« Reply #19 on: March 08, 2022, 09:34:01 AM »
@Kirby I use the same one (version). I don't know if anyone has another. I, myself, could never really modify MP's code all that much (I always found it difficult reading a program 10 lines at a time--because that's all that will fit on my screen at one time--). :p
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1314
  • Marco
Re: [challenge] A31 : Midverse
« Reply #20 on: March 08, 2022, 10:30:26 AM »
@Kirby my tests
Code: [Select]
>>>>>  (MIDVERSEV2-KIRBY "a") = "aa"
(setq aString "retinues")
--- Benchmark utility: In memory of Michael Puckett ---
Elapsed milliseconds / relative speed for 65536 iteration(s):
    (MIDVERSE-GILE ASTRING)........1141 / 1.37 <fastest>
    (MIDVERSE-RJP ASTRING).........1172 / 1.33
    (MIDVERSE_A5 ASTRING)..........1187 / 1.32
    (MIDVERSE_A4 ASTRING)..........1515 / 1.03
    (MIDVERSEV2-KIRBY ASTRING).....1562 / 1 <slowest>

Elapsed milliseconds / relative speed for 65536 iteration(s):
    (MIDVERSE-RJP ASTRING).........1125 / 1.32 <fastest>
    (MIDVERSE_A5 ASTRING)..........1141 / 1.3
    (MIDVERSE-GILE ASTRING)........1141 / 1.3
    (MIDVERSE_A4 ASTRING)..........1484 / 1
    (MIDVERSEV2-KIRBY ASTRING).....1484 / 1 <slowest>


kirby

  • Newt
  • Posts: 110
Re: [challenge] A31 : Midverse
« Reply #21 on: March 08, 2022, 10:44:40 AM »
@JohnK - thanks. I guess that some folks have added headers attributing and honouring MP to their versions.  Reviewed the code, and yikes.  My aged and wee brain finds it hard to read and understand MP's code without adding lots of notes.

@Marc'Antonio - thanks. Looks like the versions that convert Strings to Lists and use vl- functions are the quickest.  And obviously the shortest code helps.

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1314
  • Marco
Re: [challenge] A31 : Midverse
« Reply #22 on: March 08, 2022, 12:25:44 PM »
... I guess that some folks have added headers attributing and honouring MP to their versions.  ...
Yes it is.


>>>>>  (MIDVERSEV2-KIRBY "a") = "aa"

kirby

  • Newt
  • Posts: 110
Re: [challenge] A31 : Midverse
« Reply #23 on: March 08, 2022, 05:29:23 PM »
Awww

Code - Auto/Visual Lisp: [Select]
  1. (defun MidVerseV2-kirby (MyString / OutVal L FirstChar LastChar CNT)   
  2. ; [challenge] A31 : Midverse -  Reverse the middle letters of a word.  Version 2
  3. ; Input:
  4. ;       MyString - (string) string to be modified
  5. ; Returns:
  6. ;       modfied string
  7. ; Uses custom functions:
  8. ;       none!
  9.  
  10. (setq OutVal "")
  11.  
  12. (if (eq (type MyString) 'STR)
  13.   (progn
  14.        
  15.         (setq L (strlen MyString))
  16.         (if (> L 2)
  17.           (progn
  18.                 (setq FirstChar (substr MyString 1 1))
  19.                 (setq LastChar (substr MyString L 1))
  20.        
  21.                 (setq CNT L)
  22.                 (repeat (- L 2)
  23.                         (setq CNT (1- CNT))     ; work backwards
  24.                         (setq OutVal (strcat OutVal (substr MyString CNT 1)))  
  25.                 ) ; close repeat
  26.                
  27.                 (setq OutVal (strcat FirstChar OutVal LastChar))
  28.           )
  29.           (progn
  30.                 ; catch short string
  31.                 (setq OutVal MyString)
  32.           )    
  33.         ) ; close if
  34.   )    
  35. ) ; close if
  36.  
  37. OutVal
  38. )
  39.  

pBe

  • Bull Frog
  • Posts: 401
Re: [challenge] A31 : Midverse
« Reply #24 on: March 09, 2022, 11:57:39 AM »
Most likely already taken
Code - Auto/Visual Lisp: [Select]
  1. (defun midverse (str / n s)
  2.   (setq n 1 s "" )
  3.   (strcat (substr str 1 1)
  4.           (repeat (- (strlen str) 2)
  5.             (setq s (strcat (substr str (setq n (1+ n)) 1) s))
  6.             s
  7.           )
  8.           (substr str (1+ n))
  9.   )
  10. )

"", "A" ,"AA"
Code - Auto/Visual Lisp: [Select]
  1. (defun midverse (str / sl n s)
  2.   (cond
  3.     ((<= (setq sl (strlen str)) 2) str)
  4.     ((setq n 1
  5.            s ""
  6.      )
  7.      (strcat (substr str 1 1)
  8.              (repeat (- sl 2)
  9.                (setq s (strcat (substr str (setq n (1+ n)) 1) s))
  10.                s
  11.              )
  12.              (substr str (1+ n))
  13.      )
  14.     )
  15.   )
  16. )
« Last Edit: March 09, 2022, 12:11:08 PM by pBe »