Author Topic: Challenge (reverse a string)  (Read 5475 times)

0 Members and 1 Guest are viewing this topic.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Challenge (reverse a string)
« on: June 03, 2004, 07:06:26 AM »
Challenge:
How many ways can you reverse a word(string)?
i.e. change "mark" to "kram"

<edit>
the answer needs to be in the form of a function,
(defun func () )
</edit>
TheSwamp.org  (serving the CAD community since 2003)

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Challenge (reverse a string)
« Reply #1 on: June 03, 2004, 07:35:28 AM »
here's a quick one.
Code: [Select]

(defun revw1 (str)
(vl-list->string
 (reverse
(vl-string->list str)
)
 )
)
TheSwamp.org  (serving the CAD community since 2003)

M-dub

  • Guest
Challenge (reverse a string)
« Reply #2 on: June 03, 2004, 07:37:24 AM »
I'd say.......8 or 10 beers.

(Sorry Mark...couldn't help it)

DParcon

  • Guest
Challenge (reverse a string)
« Reply #3 on: June 03, 2004, 08:51:32 AM »
In plain lisp:

Code: [Select]



(defun r2l (str)
  (setq num (strlen str)
        nstr ""
  )
  (repeat num
    (setq nstr (strcat (substr str 1 1) nstr)
          str (substr str 2)
    )
  )
  nstr
)

   

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Challenge (reverse a string)
« Reply #4 on: June 03, 2004, 09:11:36 AM »
Ohh!!? good one!

Im not in Acad yet today (I think i might get a chance to work in Autocad today w00t!) So can i wait to get mine in till then.  (I can disguse my playtime better when im working in autocad. :D)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Challenge (reverse a string)
« Reply #5 on: June 03, 2004, 10:03:45 AM »
Mark did it the way I'm inclined to do it. Here's a quick alternate:
Code: [Select]
(defun StrRev ( str / result i )
   (setq i 0)
   (apply 'strcat
      (repeat (strlen str)
         (setq result
            (cons
               (substr str (setq i (1+ i)) 1)
               result
            )
         )
      )
   )
)
Cheers.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Challenge (reverse a string)
« Reply #6 on: June 03, 2004, 10:28:33 AM »
Mark did it exactly the way I would do it.
 .. well, sort of ..  I format my code 'Closed new line - Outer indentation' usually.


or I might do this if it was Friday.
Code: [Select]

(defun revw1 (str)
  (if str
    (vl-list->string (reverse (vl-string->list str)))
    ""
  )
)
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.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Challenge (reverse a string)
« Reply #7 on: June 03, 2004, 10:35:47 AM »
Or if I wasn't feeling particularly lucky, perhaps something like this :

Code: [Select]

(defun revw1 (str)
  (if (= (type arg) 'str) )
    (vl-list->string (reverse (vl-string->list str)))
    ""
  )
)
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.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Challenge (reverse a string)
« Reply #8 on: June 03, 2004, 10:46:41 AM »
I stopped parameter checking years ago; I put the responsibility for that fully upon the caller. If the caller passes a function an integer or a null when it expects a string it should crash. Also, since I'm generally processing thousands of drawings at a time I try to ilk out all the performance I can; parameter checking / validation just imposes unnecessary delay, which is not insignificant in the context of multiple iterations. However, to each his own; it is a free world thank goodness.

Cheers. :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Challenge (reverse a string)
« Reply #9 on: June 03, 2004, 01:16:20 PM »
Ok, Mark did it the way i would have too, but... heres another way. The same basic theory but i kept the char's  in the string char's so mine dosent convert to ascii numbers it just parses out the sting into a list, reverses it  and then puts it back together again.
Code: [Select]
;;;===================================================================;
;;; String->StrList                                                   ;
;;;-------------------------------------------------------------------;
;;; This procedure will take a given string and parse it out to a     ;
;;; list of charaters.                                                ;
;;;                                                                   ;
;;; Arguments: Str - A charater array -e.g. "This is a test"          ;
;;;                                                                   ;
;;; Rerunts: ("T" "h" "i" "s" " " "i" "s" " " "a" " " "t" "e" "s" "t");
;;;                                                                   ;
;;; Author: John Kaul (Se7en)                                         ;
;;;===================================================================;
(defun String->StrList (str / alst cntr)
  (if (= cntr '()) (setq cntr 1))
  (while (<= cntr (strlen str))
    (setq alst (cons (substr str cntr 1) alst)
          cntr (1+ cntr)))
  (reverse alst)
)
;;;===================================================================;
;;; StrList->String                                                   ;
;;;-------------------------------------------------------------------;
;;; This procedure will take a given list of charaters and paste them ;
;;; together into one string.                                         ;
;;;                                                                   ;
;;; Arguments: StrList - A list of strings.                           ;
;;;     -e.g.: ("T" "e" "s" "t")                                      ;
;;;                                                                   ;
;;; Returns: "Test"                                                   ;
;;;                                                                   ;
;;; Author: John Kaul (Se7en)                                         ;
;;;===================================================================;
(defun StrList->String (StrList / strlst)
  (setq strlst "")
  (mapcar '(lambda (x) (setq strlst (strcat strlst x))) strlist)
 strlst
)


(defun main (str)
  (StrList->String (reverse (String->StrList str))))

;;; Command: (main "This is a test of the national...")
;;; > "...lanoitan eht fo tset a si sihT"
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org