(defun foo (lst n / l)
(setq l (cons (nth (vl-position n lst) lst)
(vl-remove-if ''((x) (= x n)) (cdr (member n lst)))))
(append (vl-remove-if ''((x) (vl-position x l)) lst) l)
)
maybe slower than iteration due to vl-remove-if (predicate) twice?
_$ (StrRemDupes " This Test space rrrremmmovve" nil)
" This Test space remove"
_$ (StrRemDupes " This Test space rrrremmmovve" " ")
" This Test space rrrremmmovve"
_$ (StrRemDupes " This Test space rrrremmmovve" " r")
" This Test space remmmovve"
_$ (StrRemDupes " This Test space rrrremmmovve" "")
" This Test space remove"
I may not understand the challenge -- just woke up, haven't had coffee, feel like my head was struck with a mallet -- so offered solely for fun:
[code(progn
(defun _CleanStr ( str flag )
( (lambda ( foo lst / r )
(foreach x lst (if (foo x r) (setq r (cons x r))))
(vl-string-trim " " (vl-list->string r))
)
(if flag
(lambda (x r) (/= x (car r)))
(lambda (x r) (not (= x 32 (car r))))
)
(reverse (vl-string->list str))
)
)
(setq str " This Test space rrrremmmovvve ")
(print (_CleanStr str nil))
(print (_CleanStr str t))
(princ)
)
...Thanks! :)
Nice solution Ron :)
...
Lee always wins at the "lets shorten the code" game. :DYou mean "code golf"?
My suggestion ... mostly functional programming instead of procedural.
(defun foo (l n / a b )
(if (cdr l)
(setq a (car l) b
(cons (if (or (and (= a n) (/= a (cadr l)))
(/= a n))
a
)
(foo (cdr l) n)
)
)
(setq b (append b (list (last l))))
)
(vl-remove nil b)
)
OP's list
(foo
'(1 1 2 3 7 7 7 7 7 4 4 4 5 6 6 7 7 7 7 8 8 8 8 8 9 9 9 9 7 7 7 9 9 9 9 9 9 10) 7)
;;;(1 1 2 3 7 4 4 4 5 6 6 7 8 8 8 8 8 9 9 9 9 7 9 9 9 9 9 9 10)
(defun remdupchr (str $ )
(vl-list->string
(foo (vl-string->list str) (ascii $))))
;test
('
(($)
(foreach x '("r" "e" "m" "o" "v" "s" " ")
(terpri)
(prin1 (setq $ (remdupchr $ x)))
)
(textpage)(princ)
)
" Thisss isss Tesst sspace rrrrreemmmooovvvee"
)
" Thisss isss Tesst sspace reemmmooovvvee"
" Thisss isss Tesst sspace remmmooovvve"
" Thisss isss Tesst sspace remooovvve"
" Thisss isss Tesst sspace removvve"
" Thisss isss Tesst sspace remove"
" This is Test space remove"
" This is Test space remove"