TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: pkohut on June 15, 2010, 05:04:19 AM

Title: Determine if string1 is a rotation of string2
Post by: pkohut on June 15, 2010, 05:04:19 AM
Saw this question in StackOverflow and thought it interesting. How about some Autolisp solutions to the problem.

Quote
Given 2 strings s1 and s2 how will you check if s1 is a rotated version of s2?

Example:
if s1 = "stackoverlow" then the following are some of its rotated versions:
"tackoverflows"
"ackoverflowst"
"overflowstack"

where as "stackoverflwo" is not a rotated version.

The solution is at SO also, so don't look just yet  :wink:
Title: Re: Determine if string1 is a rotation of string2
Post by: VovKa on June 15, 2010, 05:43:20 AM
Code: [Select]
(defun test (s1 s2) (or (vl-string-search s1 (strcat s2 s2))))
Title: Re: Determine if string1 is a rotation of string2
Post by: Kerry on June 15, 2010, 05:45:40 AM
(http://www.theswamp.org/screens/index.php?dir=kerry/&file=pinkie.gif)

 :-D
Title: Re: Determine if string1 is a rotation of string2
Post by: Kerry on June 15, 2010, 05:49:41 AM

except for

(test "TIT "TI")
Title: Re: Determine if string1 is a rotation of string2
Post by: pkohut on June 15, 2010, 05:51:49 AM
Code: [Select]
(defun test (s1 s2) (or (vl-string-search s1 (strcat s2 s2))))

Nice. Need to check that length of s1 and s2 are the same, but still nice and compact.
Title: Re: Determine if string1 is a rotation of string2
Post by: pkohut on June 15, 2010, 05:54:24 AM

except for

(test "TIT "TI")

What a boob  :-D
Title: Re: Determine if string1 is a rotation of string2
Post by: Lee Mac on June 15, 2010, 08:07:16 AM
Another two  :-)

Code: [Select]
(defun LM:isStringRotation ( s1 s2 )
  (vl-some
    (function
      (lambda ( s )
        (and (vl-string-search s1 s))
      )
    )
    (
      (lambda ( i )
        (mapcar
          (function
            (lambda ( x )
              (strcat (substr s2 (setq i (1+ i)))
                (substr s2 1 (1- i))
              )
            )
          )
          (vl-string->list s2)
        )
      )
      0
    )
  )
)

Code: [Select]
(defun LM:isStringRotation2 ( s1 s2 / foo )

  (defun foo ( s i )
    (if (< 0 i)
      (if (vl-string-search s1
            (strcat (substr s i) (substr s 1 (1- i)))) t
        (foo s (1- i))
      )
    )
  )

  (foo s2 (strlen s2))
)
Title: Re: Determine if string1 is a rotation of string2
Post by: VovKa on June 17, 2010, 10:44:28 AM

except for

(test "TIT "TI")

yep, my fault
i just can't program and think about tits at the same time :)
Title: Re: Determine if string1 is a rotation of string2
Post by: JohnK on June 17, 2010, 02:14:19 PM
Okay, just for kicks (I had to chew up the rest of my lunch break some how).

No one suggested this method yet so...
Code: [Select]
(setq s1 "stackoverflow"
      s2 "tackoverflows"
      s3 "ackoverflowst"
      s4 "overflowstack"
      is1 (vl-string->list s1)
      is2 (vl-string->list s2)
      is3 (vl-string->list s3)
      is4 (vl-string->list s4))
(mapcar '(lambda ( x )
   (equal
     (vl-sort is1 '<) (vl-sort (eval x) '<)))
'(is2 is3 is4))
Title: Re: Determine if string1 is a rotation of string2
Post by: VovKa on June 17, 2010, 03:03:38 PM
what about these? ;)
Code: [Select]
(setq s1 "stackoverfloww"
      s2 "ttackoverflows"
      s3 "aackoverflowst"
      s4 "overflowstackk"
)
Title: Re: Determine if string1 is a rotation of string2
Post by: JohnK on June 17, 2010, 04:12:32 PM
hey, thats what you get for 10 minutes worth of programing.
:P