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

0 Members and 1 Guest are viewing this topic.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: =={ Challenge }== Decompress String
« Reply #45 on: August 29, 2014, 12:25:49 PM »
as correctly?
Code: [Select]
(decompress "a") => "" ; as "a0"
or
(decompress "a") => "a" ; as "a1"

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: =={ Challenge }== Decompress String
« Reply #46 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  :-)
« Last Edit: August 29, 2014, 12:32:06 PM by Lee Mac »

Kerry

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

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: =={ Challenge }== Decompress String
« Reply #48 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. )
« Last Edit: August 29, 2014, 03:44:57 PM by Lee Mac »

VovKa

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

irneb

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

Kerry

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

Stefan

  • Bull Frog
  • Posts: 319
  • The most I miss IRL is the Undo button
Re: =={ Challenge }== Decompress String
« Reply #52 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. )

pBe

  • Bull Frog
  • Posts: 402
Re: =={ Challenge }== Decompress String
« Reply #53 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.