### Author Topic: =={ Challenge }== Decompress String  (Read 11412 times)

0 Members and 1 Guest are viewing this topic.

#### ElpanovEvgeniy

• Water Moccasin
• Posts: 1569
• Moscow (Russia)
##### Re: =={ Challenge }== Decompress String
« Reply #15 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"`

#### Jeff H

• Needs a day job
• Posts: 6142
##### Re: =={ Challenge }== Decompress String
« Reply #16 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.

#### Jeff H

• Needs a day job
• Posts: 6142
##### Re: =={ Challenge }== Decompress String
« Reply #17 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.

#### Kerry

• Mesozoic relic
• Seagull
• Posts: 11654
• class keyThumper<T>:ILazy<T>
##### Re: =={ Challenge }== Decompress String
« Reply #18 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.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

#### ElpanovEvgeniy

• Water Moccasin
• Posts: 1569
• Moscow (Russia)
##### Re: =={ Challenge }== Decompress String
« Reply #19 on: August 29, 2014, 08:32:23 AM »
hi Jeff H!
We have written the same code  :coolsmiley:

#### CAB

• Global Moderator
• Seagull
• Posts: 10401
##### Re: =={ Challenge }== Decompress String
« Reply #20 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.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970

#### Kerry

• Mesozoic relic
• Seagull
• Posts: 11654
• class keyThumper<T>:ILazy<T>
##### Re: =={ Challenge }== Decompress String
« Reply #21 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.

My argument has always been that we need to learn how to ask questions as much as we need to learn to solve problems.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

#### ElpanovEvgeniy

• Water Moccasin
• Posts: 1569
• Moscow (Russia)
##### Re: =={ Challenge }== Decompress String
« Reply #22 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!

#### Kerry

• Mesozoic relic
• Seagull
• Posts: 11654
• class keyThumper<T>:ILazy<T>
##### Re: =={ Challenge }== Decompress String
« Reply #23 on: August 29, 2014, 09:26:37 AM »
Thank you Sir!
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

#### irneb

• Water Moccasin
• Posts: 1794
• ACad R9-2016, Revit Arch 6-2016
##### Re: =={ Challenge }== Decompress String
« Reply #24 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.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

#### Jeff H

• Needs a day job
• Posts: 6142
##### Re: =={ Challenge }== Decompress String
« Reply #25 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.

#### Jeff H

• Needs a day job
• Posts: 6142
##### Re: =={ Challenge }== Decompress String
« Reply #26 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>)))`

#### irneb

• Water Moccasin
• Posts: 1794
• ACad R9-2016, Revit Arch 6-2016
##### Re: =={ Challenge }== Decompress String
« Reply #27 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.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

#### roy_043

• Water Moccasin
• Posts: 1895
##### Re: =={ Challenge }== Decompress String
« Reply #28 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. )

#### Lee Mac

• Seagull
• Posts: 12892
• London, England
##### Re: =={ Challenge }== Decompress String
« Reply #29 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. )