TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: irneb on August 29, 2014, 04:38:49 AM

Title: =={ Challenge }== Decompress String
Post by: irneb on August 29, 2014, 04:38:49 AM
Since the previous FizzBuzz challenge was seen as "nice", I thought I'd add another one.

Here's the request: The compression algorithm created a string where each numeral digit means the uncompressed string preceding it is repeated that many times. You must write the decompress function. Assume the compressed string can only contain characters 0 through 9 and a through z, while the decompressed can only contain a through z.

E.g.
Code: [Select]
_$ (Decompress "ab2c3")
"ababcababcababc"
_$ (Decompress "ab4c3d2")
"ababababcababababcababababcdababababcababababcababababcd"

Edited to add content range of string.
Title: Re: =={ Challenge }== Decompress String
Post by: roy_043 on August 29, 2014, 05:13:56 AM
My first solution:
Code - Auto/Visual Lisp: [Select]
  1. (defun Roy_Decompress (str / out tmp)
  2.   (setq out "")
  3.   (foreach int (vl-string->list str)
  4.     (cond
  5.       ((= int 48) ; 0
  6.         (setq out "")
  7.       )
  8.       ((= int 49) ; 1
  9.         out
  10.       )
  11.       ((<= 50 int 57) ; 2-9
  12.         (setq tmp out)
  13.         (repeat (- int 49)
  14.           (setq out (strcat out tmp))
  15.         )
  16.       )
  17.       (T
  18.         (setq out (strcat out (chr int)))
  19.       )
  20.     )
  21.   )
  22. )
Title: Re: =={ Challenge }== Decompress String
Post by: Kerry on August 29, 2014, 05:17:51 AM
Design query :

 :|
Title: Re: =={ Challenge }== Decompress String
Post by: Kerry on August 29, 2014, 05:20:47 AM
Design query :
does a string "A23"  mean repeat "A" 23 times or repeat "A"  2 times then 3 times ??

I assume  repeat "A"  2 times then 3 times  :|
Title: Re: =={ Challenge }== Decompress String
Post by: roy_043 on August 29, 2014, 05:31:17 AM
I assume  repeat "A"  2 times then 3 times  :|
That has been my assumption. And I have assumed a non-empty string.

Another interesting issue: What if the input string does not end in a digit?
Title: Re: =={ Challenge }== Decompress String
Post by: irneb on August 29, 2014, 05:33:36 AM
Design query :
  • assume the string is not empty and contains printable characters.
  • Assume the first element is not numeric.

 :|

Design query :
does a string "A23"  mean repeat "A" 23 times or repeat "A"  2 times then 3 times ??

I assume  repeat "A"  2 times then 3 times  :|
Your assumption is correct:
...each numeral digit means ...

Another interesting issue: What if the input string does not end in a digit?
Really? Please explain?
Title: Re: =={ Challenge }== Decompress String
Post by: Kerry on August 29, 2014, 05:34:17 AM
< .. >

Another interesting issue: What if the input string does not end in a digit?

I had assumed the last element would just be appended to the result string.
Title: Re: =={ Challenge }== Decompress String
Post by: irneb on August 29, 2014, 05:37:30 AM
As some more samples to show these questions:
Code: [Select]
_$ (Decompress "")
""
_$ (Decompress "6")
""
_$ (Decompress "3a2b")
"aab"
_$ (Decompress "a2b")
"aab"
Title: Re: =={ Challenge }== Decompress String
Post by: Kerry on August 29, 2014, 05:37:56 AM
< ..>
No, first element could be numeric as well ... does it make any difference in the result?]
< ..>

Only in so much as the possibility must be allowed for.
Title: Re: =={ Challenge }== Decompress String
Post by: Kerry on August 29, 2014, 05:41:14 AM

  • Assume string only contains characters of 0-9 and a-z


So, no A-Z ?
Title: Re: =={ Challenge }== Decompress String
Post by: irneb on August 29, 2014, 05:45:37 AM

  • Assume string only contains characters of 0-9 and a-z


So, no A-Z ?
If you really want to you can do those too, but it's not a big issue. Actually the way I've written mine means everything except for 0-9 will be possible in the result.
Title: Re: =={ Challenge }== Decompress String
Post by: roy_043 on August 29, 2014, 05:46:38 AM
Another interesting issue: What if the input string does not end in a digit?
Really? Please explain?

Code: [Select]
_$ (Decompress "a2b")
"aab"

So (Decompress "a2b") should return the same result as (Decompress "a2b1").
Seems interesting enough to me...
Title: Re: =={ Challenge }== Decompress String
Post by: Kerry on August 29, 2014, 05:50:11 AM


Ahhh, I see you've changed the 'specs' to clarify ...
Title: Re: =={ Challenge }== Decompress String
Post by: roy_043 on August 29, 2014, 05:53:06 AM
Revised code to account for empty string input.
Code - Auto/Visual Lisp: [Select]
  1. (defun Roy_Decompress_2 (str / out tmp)
  2.   (setq out "")
  3.   (foreach int (vl-string->list str)
  4.     (cond
  5.       ((<= 50 int 57) ; 2-9
  6.         (setq tmp out)
  7.         (repeat (- int 49)
  8.           (setq out (strcat out tmp))
  9.         )
  10.       )
  11.       ((= int 48) ; 0
  12.         (setq out "")
  13.       )
  14.       ((/= int 49) ; 1
  15.         (setq out (strcat out (chr int)))
  16.       )
  17.     )
  18.   )
  19.   out
  20. )
Title: Re: =={ Challenge }== Decompress String
Post by: irneb on August 29, 2014, 06:04:02 AM
Revised code to account for empty string input.
Awesome! Very close to mine, most notable difference is mine works with lists instead of strings.
Code - Auto/Visual Lisp: [Select]
  1. (defun Decompress (string / result temp)
  2.   (setq string (vl-string->list string))
  3.   (while string
  4.     (cond ((= (car string) 48) (setq result nil))
  5.           ((< 48 (car string) 58)
  6.            (setq temp result)
  7.            (repeat (- (car string) 49)
  8.              (setq result (append result temp))))
  9.           ((setq result (append result (list (car string))))))
  10.     (setq string (cdr string)))
  11.   (vl-list->string result))
Title: Re: =={ Challenge }== Decompress String
Post by: ElpanovEvgeniy on August 29, 2014, 08:06:00 AM
recursion:
Code - Auto/Visual Lisp: [Select]
  1. (defun f (s a / b)
  2.   (cond ((= s "") a)
  3.         ((= (atoi s) 0) (f (substr s 2) (strcat a (substr s 1 1))))
  4.         ((f (substr s 2) (progn (setq b "") (repeat (atoi (substr s 1 1)) (setq b (strcat a b))))))
  5.   )
  6. )
test:
Code: [Select]
(f "ab2c3" "")
"ababcababcababc"
(f "ab4c3d2" "")
"ababababcababababcababababcdababababcababababcababababcd"
(f "a23" "")
"aaaaaa"
Title: Re: =={ Challenge }== Decompress String
Post by: Jeff H on August 29, 2014, 08:13:49 AM
Just starting to mess with AutoLisp so total noob here
 
Code - Auto/Visual Lisp: [Select]
  1. (defun Decompress (string)
  2.  (recursiveDecompress "" string)
  3. )
  4.  
  5. (defun recursiveDecompress (string1 string2 / str3)
  6.  (setq str3 "")
  7.  (cond
  8.    ((equal "" string2) string1)
  9.    ((repeat (atoi string2) (setq str3 (strcat str3 string1)))
  10.       (recursiveDecompress str3 (substr string2 2)))
  11.    (T (recursiveDecompress (strcat string1 (substr string2 1 1)) (substr string2 2)))
  12.   )
  13. )
  14.  
  15.  
Title: Re: =={ Challenge }== Decompress String
Post by: Jeff H on August 29, 2014, 08:27:06 AM
That's what I get for being noob  this might be correct depending on if this is correct
Code: [Select]
_$ (Decompress "a23")
"aaaaaa"

Does (Decompress "a23") =  "aaaaaa" or "aaa"?
 
 
 
Code - Auto/Visual Lisp: [Select]
  1. (defun recursiveDecompress (string1 string2 / str3)
  2.  (setq str3 "")
  3.  (cond
  4.    ((equal "" string2) string1)
  5.    ((repeat (atoi (substr string2 1 1)) (setq str3 (strcat str3 string1)))
  6.       (recursiveDecompress str3 (substr string2 2)))
  7.    (T (recursiveDecompress (strcat string1 (substr string2 1 1)) (substr string2 2)))
  8.   )
  9. )
  10.  
  11.  
Title: Re: =={ Challenge }== Decompress String
Post by: Kerry on August 29, 2014, 08:31:49 AM
That's what I get for being noob  this might be correct depending on if this is correct
Code: [Select]
_$ (Decompress "a23")
"aaaaaa"

Does (Decompress "a23") =  "aaaaaa" or "aaa"?
 
 

Yes.
Title: Re: =={ Challenge }== Decompress String
Post by: ElpanovEvgeniy on August 29, 2014, 08:32:23 AM
hi Jeff H!
We have written the same code  :coolsmiley:
Title: Re: =={ Challenge }== Decompress String
Post by: CAB on August 29, 2014, 08:38:36 AM
Ahhh, I see you've changed the 'specs' to clarify ...

That's how it works in real life. The specs have a way of evolving to accommodate the unforeseen. It helps to have several perspectives.  8)
Title: Re: =={ Challenge }== Decompress String
Post by: Kerry on August 29, 2014, 08:41:40 AM
Ahhh, I see you've changed the 'specs' to clarify ...

That's how it works in real life. The specs have a way of evolving to accommodate the unforeseen. It helps to have several perspectives.  8)

:-)
My argument has always been that we need to learn how to ask questions as much as we need to learn to solve problems.
Title: Re: =={ Challenge }== Decompress String
Post by: ElpanovEvgeniy on August 29, 2014, 08:52:42 AM
My argument has always been that we need to learn how to ask questions as much as we need to learn to solve problems.

good words!  :-)
Title: Re: =={ Challenge }== Decompress String
Post by: Kerry on August 29, 2014, 09:26:37 AM
Thank you Sir!
Title: Re: =={ Challenge }== Decompress String
Post by: irneb on August 29, 2014, 09:40:36 AM
My argument has always been that we need to learn how to ask questions as much as we need to learn to solve problems.
+1 I agree with you on this, unfortunately I didn't foresee the possibilities. In my mind some of them didn't matter - seeing as they didn't apply to the results (only to the implementation).

My thought on changing the original was that instead of someone having to read all the following posts - the queries asked later were to be incorporated. At least those where I felt I failed to mention in the OP. Sorry guys.
Title: Re: =={ Challenge }== Decompress String
Post by: Jeff H on August 29, 2014, 09:42:25 AM
hi Jeff H!
We have written the same code  :coolsmiley:
Either I got lucky or you screwed up.
 :-D

Title: Re: =={ Challenge }== Decompress String
Post by: Jeff H on August 29, 2014, 09:47:33 AM
hi Jeff H!
We have written the same code  :coolsmiley:

Hi Elpanov,

I do not have much experience with Lisp but can most problems be solved in Lisp  with

Code: [Select]
   (DEFUN <RECURSIVE-FUNCTION> (<PARAM1> ... <PARAMN>)
       (COND (<TERMINATING-CONDITION1> <RESULT1>)
.
.
             (<TERMINATING-CONDITIONN> <RESULTN>)
             (<RECURSIVE-CONDITION1> <RECURSIVE-CALL1>)
.
.
             (<RECURSIVE-CONDITIONM> <RECURSIVE-CALLM>)))
Title: Re: =={ Challenge }== Decompress String
Post by: irneb on August 29, 2014, 09:52:44 AM
I do not have much experience with Lisp but can most problems be solved in Lisp  with
Yes "most" ... though unlike some other Lisps (e.g. Scheme) AutoLisp doesn't have tail-call-elimination, thus recursion would cause stack overflows if the call stack becomes too deep. So generally be careful when you're processing something like a list longer than around 10k to 20k.

Actually if you program in something like Scheme / Clojure you're encouraged to use recursion instead of iterative loops. But that's not always the case, e.g. Common Lisp tends to make use of iterative looping instead.
Title: Re: =={ Challenge }== Decompress String
Post by: roy_043 on August 29, 2014, 10:05:59 AM
Two more from me.

Roy_Decompress_3 is similar to Roy_Decompress_2 but probably faster because it relies less on string operations.
Roy_Decompress_4 is my version with recursion.

Code - Auto/Visual Lisp: [Select]
  1. (defun Roy_Decompress_3 (str / out tmp)
  2.   (foreach int (vl-string->list str)
  3.     (cond
  4.       ((<= 50 int 57) ; 2-9
  5.         (setq tmp out)
  6.         (repeat (- int 49)
  7.           (setq out (append tmp out))
  8.         )
  9.       )
  10.       ((= int 48) ; 0
  11.         (setq out nil)
  12.       )
  13.       ((/= int 49) ; 1
  14.         (setq out (append out (list int)))
  15.       )
  16.     )
  17.   )
  18.   (vl-list->string out)
  19. )

Code - Auto/Visual Lisp: [Select]
  1. (defun Roy_Decompress_4 (str / N_Decode)
  2.   (defun N_Decode (lst / out tmp)
  3.     (cond
  4.       ((not lst)
  5.         nil
  6.       )
  7.       ((= (car lst) 48) ; 0
  8.         nil
  9.       )
  10.       ((<= 49 (car lst) 57) ; 1-9
  11.         (setq tmp (N_Decode (cdr lst)))
  12.         (repeat (- (car lst) 48)
  13.           (setq out (append tmp out))
  14.         )
  15.       )
  16.       (T
  17.         (cons (car lst) (N_Decode (cdr lst)))
  18.       )
  19.     )
  20.   )
  21.   (vl-list->string (reverse (N_Decode (reverse (vl-string->list str)))))
  22. )
Title: Re: =={ Challenge }== Decompress String
Post by: Lee Mac on August 29, 2014, 10:06:06 AM
Without looking at anything already posted:

Code - Auto/Visual Lisp: [Select]
  1. (defun decompress ( s / f )
  2.     (defun f ( s x r )
  3.         (cond
  4.             (   (= "" s) x)
  5.             (   (wcmatch s "#*")
  6.                 (if (< 0 (atoi s))
  7.                     (f (strcat (itoa (1- (atoi s))) (substr s 2)) x (strcat r x))
  8.                     (f (substr s 2) r "")
  9.                 )
  10.             )
  11.             (   (f (substr s 2) (strcat x (substr s 1 1)) r))
  12.         )
  13.     )
  14.     (f s "" "")
  15. )
Title: Re: =={ Challenge }== Decompress String
Post by: roy_043 on August 29, 2014, 10:09:15 AM
@ ElpanovEvgeniy:
(f "ab4c3d0" "") => "ababababcababababcababababcd0"
Title: Re: =={ Challenge }== Decompress String
Post by: Kerry on August 29, 2014, 10:17:40 AM

Roy, yes, a few of these routines will fail if they contain a zero.
Title: Re: =={ Challenge }== Decompress String
Post by: roy_043 on August 29, 2014, 10:36:38 AM
@ Lee:
(decompress "ab4c3d22") => ...
Title: Re: =={ Challenge }== Decompress String
Post by: Kerry on August 29, 2014, 10:51:26 AM
Just for the fun of it  ...

Code - Auto/Visual Lisp: [Select]
  1. ;;;------------------------------------------------------------------
  2. ;;;
  3. ;;; Spit the Dummy if 'testStatement evaluates to nil
  4. ;;; argument must be quoted
  5. ;;; kwb 20020715
  6.  
  7. ;;;   (setq var 0)
  8. ;;;   (KDUB:assert 'var "Symbol 'VAR' has no value assigned.")
  9. ;;;   (KDUB:assert '(not(zerop var)) "Symbol 'VAR' evaluates to 0 ... this is illegal ... go to jail")
  10. ;;;   (KDUB:assert '(> var 0) nil)
  11. ;;;   (KDUB:assert '(< var 1) nil)
  12. (defun kdub:assert (teststatement message)
  13.   (if (not (eval teststatement))
  14.     (progn (kdub:display-message
  15.              " **** Assertion Failure"
  16.              (if message
  17.                message
  18.                (list (vl-prin1-to-string teststatement) " ==>> nil/null")
  19.              )
  20.            )
  21.            ;; decide what to do here later ?
  22.            (if kglobal:debug_on
  23.              (princ "  ")
  24.              (exit)
  25.            )
  26.     )
  27.   )
  28.   (princ)
  29. )
  30. ;;;------------------------------------------------------------------
  31. ;;;
  32. ;;; (KDUB:display_formatted_message prefix mess )
  33. ;;; kwb 20020715
  34. ;|
  35. prefix : String or nil
  36. mess   : atom or list comprising STRing, INT, REAL,
  37.         SYMbol, ENAME, VLA-OBJECT, FILEid etc
  38. |;
  39. ;;;
  40. ;;; Print a formatted message to the command line
  41. ;;; (kdub:display-message "Ooops" "DUH")
  42. ;;; (kdub:display-message "Ooops" '( (> 1 2)))
  43.  
  44. (defun kdub:display-message (prefix mess)
  45.           (list "\n"
  46.                 (if (= (type prefix) 'str)
  47.                   prefix
  48.                   "; **** ERROR"
  49.                 )
  50.                 ": "
  51.           )
  52.   )
  53.   (if (vl-consp mess)
  54.     (mapcar 'princ mess)
  55.     (princ mess)
  56.   )
  57.   (princ)
  58. )
  59.  
  60. ;;;------------------------------------------------------------------
  61. ;;;
  62. ;;; Force closure for Dummy-Spit situation.
  63. ;;; Prints the error message and exits
  64. ;;; Forcing an error to be handled by the error handler.
  65. (defun kdub:dummyspit (mess)
  66.   (kdub:display-message nil mess)
  67.   (princ "  ")
  68.   (exit)                                ; let the current error handler clean up  
  69. )
  70.  
  71.  
  72. ;;;------------------------------------------------------------------
  73.  
  74.  
  75.  

Code - Auto/Visual Lisp: [Select]
  1. (kdub:assert '(equal (irneb1_decompress "") "") nil)
  2. (kdub:assert '(equal (irneb1_decompress "0") "") nil)
  3. (kdub:assert '(equal (irneb1_decompress "a2") "aa") nil)
  4. (kdub:assert '(equal (irneb1_decompress "ab2c3") "ababcababcababc") nil)
  5. (kdub:assert '(equal (irneb1_decompress "1ab2c3") "ababcababcababc") nil)
  6. (kdub:assert '(equal (irneb1_decompress "ab12c3") "ababcababcababc") nil)
  7. (kdub:assert '(equal (irneb1_decompress "ab2c30") "") nil) ;;<<== result revised
  8.  

**** Assertion Failure: (EQUAL (IRNEB1_DECOMPRESS "ab2c30") "ababcababcababc") ==>> nil/null

Revise test suite to remove complete current result when digit 0 encountered
Title: Re: =={ Challenge }== Decompress String
Post by: Kerry on August 29, 2014, 11:06:24 AM
DESIGN QUERY.

Is this design correct ??
a Zero in the string only ignores the characters preceding it since the previous digit value. ??

Code - Auto/Visual Lisp: [Select]
  1. (kdub:assert '(equal (irneb1_decompress "ab0c3") "ccc") nil)
  2. ;; (kdub:assert '(equal (irneb1_decompress "ab1c0d2") "abdabd") nil)
  3.  (kdub:assert '(equal (irneb1_decompress "ab1c0d2") "dd") nil) ;<<== result revised
  4.  


Revise test suite to remove complete current result when digit 0 encountered
Title: Re: =={ Challenge }== Decompress String
Post by: roy_043 on August 29, 2014, 11:12:48 AM
DESIGN QUERY.

Is this design correct ??
a Zero in the string only ignores the characters preceding it since the previous digit value. ??

Code - Auto/Visual Lisp: [Select]
  1. (kdub:assert '(equal (irneb1_decompress "ab0c3") "ccc") nil)
  2. (kdub:assert '(equal (irneb1_decompress "ab1c0d2") "abdabd") nil)
  3.  

From the original challenge I would conclude that a zero in the strings means ignore all previous characters.
So:
(irneb1_decompress "ab1c0d2") => "dd"
Title: Re: =={ Challenge }== Decompress String
Post by: Kerry on August 29, 2014, 11:14:53 AM
That's why I'm asking Roy ... People are certain to treat the condition differently, and have different expectations.

This is why having a testing suite is critical.
Title: Re: =={ Challenge }== Decompress String
Post by: ElpanovEvgeniy on August 29, 2014, 11:33:41 AM
maybe:
Code - Auto/Visual Lisp: [Select]
  1. (decompress "ab2c30") => ""
Title: Re: =={ Challenge }== Decompress String
Post by: ElpanovEvgeniy on August 29, 2014, 11:45:49 AM
Code - Auto/Visual Lisp: [Select]
  1. (defun eea1 (s)
  2.   (defun f+ (s i a) (repeat (atoi i) (setq a (strcat s a))) a)
  3.   (defun f (s i)
  4.     (cond ((= s "") s)
  5.           ((wcmatch s "*#") (f+ (f (substr s 1 (1- i)) (1- i)) (substr s i) ""))
  6.           ((strcat (f (substr s 1 (1- i)) (1- i)) (substr s i)))
  7.     )
  8.   )
  9.   (f s (strlen s))
  10. )
Code: [Select]
(equal (eea "") "") => T
(equal (eea "0") "") => T
(equal (eea "a2") "aa") => T
(equal (eea "ab2c3") "ababcababcababc") => T
(equal (eea "1ab2c3") "ababcababcababc") => T
(equal (eea "ab12c3") "ababcababcababc") => T
(equal (eea "ab2c30") "") => T
(equal (eea "ab0c3") "ccc") => T
Title: Re: =={ Challenge }== Decompress String
Post by: Kerry on August 29, 2014, 12:05:14 PM
I'm not sure Evgeniy,
With a zero in the string ..

Quote
< .. > The compression algorithm created a string where each numeral digit means the uncompressed string preceding it is repeated that many times.

could be read that the result string is repeated zero times in this instance ie not repeated.

I really hate the word assume ..
give me 2 choices and I have a 90% chance of being incorrect :-)


Title: Re: =={ Challenge }== Decompress String
Post by: ElpanovEvgeniy on August 29, 2014, 12:08:40 PM
"ab2c30"
(((("ab" * 2) + "c") * 3) * 0) = ""

Title: Re: =={ Challenge }== Decompress String
Post by: ElpanovEvgeniy on August 29, 2014, 12:10:20 PM
give me 2 choices and I have a 90% chance of being incorrect :-)

red and blue pill?  :-D
Title: Re: =={ Challenge }== Decompress String
Post by: Kerry on August 29, 2014, 12:11:49 PM
"ab2c30"
(((("ab" * 2) + "c") * 3) * 0) = ""


Yes, that would be the rational way to consider it.

Title: Re: =={ Challenge }== Decompress String
Post by: Kerry on August 29, 2014, 12:14:29 PM
give me 2 choices and I have a 90% chance of being incorrect :-)

red and blue pill?  :-D

Yep  :-D

https://www.youtube.com/watch?v=RhlXqYiTz2Q
Title: Re: =={ Challenge }== Decompress String
Post by: Lee Mac on August 29, 2014, 12:20:07 PM
Another:
Code - Auto/Visual Lisp: [Select]
  1. (defun LM:decompress2 ( s / f )
  2.     (defun f ( l a / b c )
  3.         (if (setq l (vl-member-if '(lambda ( x ) (setq c (cons x c)) (< 47 x 58)) l))
  4.             (f (cdr l) (progn (setq a (append (cdr c) a)) (repeat (- (car l) 48) (setq b (append a b)))))
  5.             (vl-list->string (reverse a))
  6.         )
  7.     )
  8.     (f (vl-string->list s) nil)
  9. )
Title: Re: =={ Challenge }== Decompress String
Post by: ElpanovEvgeniy on August 29, 2014, 12:25:49 PM
as correctly?
Code: [Select]
(decompress "a") => "" ; as "a0"
or
(decompress "a") => "a" ; as "a1"
Title: Re: =={ Challenge }== Decompress String
Post by: Lee Mac on August 29, 2014, 12:27:19 PM
@ Lee:
(decompress "ab4c3d22") => ...

This should hopefully fix it:
Code - Auto/Visual Lisp: [Select]
  1. (defun LM:decompress1 ( s / f )
  2.     (defun f ( s x r / n )
  3.         (cond
  4.             (   (= "" s) x)
  5.             (   (wcmatch s "#*")
  6.                 (if (< 0 (setq n (atoi (substr s 1 1))))
  7.                     (f (strcat (itoa (1- n)) (substr s 2)) x (strcat r x))
  8.                     (f (substr s 2) r "")
  9.                 )
  10.             )
  11.             (   (f (substr s 2) (strcat x (substr s 1 1)) r))
  12.         )
  13.     )
  14.     (f s "" "")
  15. )

Thanks roy  :-)
Title: Re: =={ Challenge }== Decompress String
Post by: Kerry on August 29, 2014, 12:32:20 PM
as correctly?
Code: [Select]
(decompress "a") => "" ; as "a0"
or
(decompress "a") => "a" ; as "a1"

my guess :
Code - Auto/Visual Lisp: [Select]
  1. (decompress "a")   => "a" ; as "a1"  
Title: Re: =={ Challenge }== Decompress String
Post by: Lee Mac on August 29, 2014, 03:36:18 PM
Another, just aiming for something different:

Code - Auto/Visual Lisp: [Select]
  1. (defun LM:decompress3 ( s / c p r x )
  2.     (setq r "")
  3.     (if (setq p (vl-string-position 48 s))
  4.         (setq s (substr s (+ 2 p)))
  5.     )
  6.     (repeat (strlen s)
  7.         (if (< 47 (setq c (ascii s)) 58)
  8.             (progn(setq x r) (repeat (- c 49) (setq r (strcat r x))))
  9.             (setq r (strcat r (chr c)))
  10.         )
  11.         (setq s (substr s 2))
  12.     )
  13.     r
  14. )
Title: Re: =={ Challenge }== Decompress String
Post by: VovKa on August 29, 2014, 04:06:00 PM
recursive
one defun
one argument
not suitable for benchmarking
Code: [Select]
(defun decompress (l / a b c)
  (if (setq b (vl-member-if
(function (lambda (x) (setq a (cons x a)) (< 47 x 58)))
(vl-string->list l)
      )
      )
    (decompress
      (vl-list->string
(append (reverse (repeat (- (car b) 48) (setq c (append c (cdr a))))) (cdr b))
      )
    )
    l
  )
)
Title: Re: =={ Challenge }== Decompress String
Post by: irneb on August 30, 2014, 04:49:44 AM
Sorry guys - got an issue with my Windows VM at home (need to re-install for some stupid reason). So can't test just now.

About the 0 or 1 idea ... yes the 0 should "clear" everything before it and the 1 is similar to having no number in that position (i.e. just leave it as is thus far) - at least that's how I thought it should go.

Why those assertions failed on my routine I'm not too sure ... will have to test to find out where it's going "wrong".
Title: Re: =={ Challenge }== Decompress String
Post by: Kerry on August 30, 2014, 05:17:08 AM
irne, I've changed the test suite assertions to be in line with the 0 digit requirement.
Title: Re: =={ Challenge }== Decompress String
Post by: Stefan on August 30, 2014, 06:46:10 AM
2 variants, nothing new in the second one
Code - Auto/Visual Lisp: [Select]
  1. (defun ph:ds1 (str / z ls ln r n)
  2.   (if (setq z (vl-string-position 48 str nil T)) (setq str (substr str (+ 2 z))))
  3.   (while (wcmatch str "#*") (setq str (substr str 2)))
  4.   (if (wcmatch str "*@") (setq str (strcat str "1")))
  5.   (if (eq str "") str
  6.     (progn
  7.       (setq ls (read (strcat "(" (vl-string-translate "0123456789" "          " str) ")"))
  8.             ln (read (strcat "(" (vl-string-translate "abcdefghijklmnopqrstuvxyzw" "                          " str) ")"))
  9.             r "")
  10.       (foreach x ls
  11.         (setq x (strcat r (vl-symbol-name x))
  12.               r x
  13.               n  (car ln)
  14.               ln (cdr ln)
  15.         )
  16.         (while (< 0 n)
  17.           (repeat (1- (rem n 10))
  18.             (setq r (strcat r x))
  19.           )
  20.           (setq n (/ n 10)
  21.                 x r
  22.           )
  23.         )
  24.       )
  25.       (strcase r T)
  26.     )
  27.   )
  28. )
Code - Auto/Visual Lisp: [Select]
  1. (defun ph:ds2 (str / z a r)
  2.   (if (setq z (vl-string-position 48 str nil T)) (setq str (substr str (+ 2 z))))
  3.   (while (wcmatch str "#*") (setq str (substr str 2)))
  4.   (if
  5.     (eq str (setq r "")) str
  6.     (foreach x (vl-remove 49 (vl-string->list str))
  7.       (if (> x 96)
  8.         (setq r (strcat r (chr x)))
  9.         (progn
  10.           (setq a r)
  11.           (repeat (- x 49)
  12.             (setq r (strcat r a))
  13.           )
  14.           r
  15.         )
  16.       )
  17.     )
  18.   )
  19. )
Title: Re: =={ Challenge }== Decompress String
Post by: pBe on August 31, 2014, 03:28:51 AM
With more than one way to approach a problem, inevitably, some of us will write the same code.
Code - Auto/Visual Lisp: [Select]
  1. (defun pbe:Decompress (Str / _s lst)
  2.   (setq _s "")
  3.   (Foreach n (vl-string->list str)
  4.      (setq _s
  5.             (if (< 47 n 58)
  6.               (apply 'strcat
  7.                      (repeat (atoi (chr n))
  8.                        (setq lst (cons _s lst))
  9.                      )
  10.               )
  11.               (strcat _s (chr n))
  12.             )  
  13.      )
  14.     (Setq lst nil))
  15.       _s
  16. )
  17.  


I must say , i have not really understand the criteria for this challenge. it took a while to sink in.