Author Topic: Determine if string1 is a rotation of string2  (Read 2456 times)

0 Members and 1 Guest are viewing this topic.

pkohut

  • Guest
Determine if string1 is a rotation of string2
« 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:

VovKa

  • Water Moccasin
  • Posts: 1631
  • Ukraine
Re: Determine if string1 is a rotation of string2
« Reply #1 on: June 15, 2010, 05:43:20 AM »
Code: [Select]
(defun test (s1 s2) (or (vl-string-search s1 (strcat s2 s2))))

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Determine if string1 is a rotation of string2
« Reply #2 on: June 15, 2010, 05:45:40 AM »


 :-D
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>
Re: Determine if string1 is a rotation of string2
« Reply #3 on: June 15, 2010, 05:49:41 AM »

except for

(test "TIT "TI")
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.

pkohut

  • Guest
Re: Determine if string1 is a rotation of string2
« Reply #4 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.

pkohut

  • Guest
Re: Determine if string1 is a rotation of string2
« Reply #5 on: June 15, 2010, 05:54:24 AM »

except for

(test "TIT "TI")

What a boob  :-D

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Determine if string1 is a rotation of string2
« Reply #6 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))
)

VovKa

  • Water Moccasin
  • Posts: 1631
  • Ukraine
Re: Determine if string1 is a rotation of string2
« Reply #7 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 :)

JohnK

  • Administrator
  • Seagull
  • Posts: 10634
Re: Determine if string1 is a rotation of string2
« Reply #8 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))
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

VovKa

  • Water Moccasin
  • Posts: 1631
  • Ukraine
Re: Determine if string1 is a rotation of string2
« Reply #9 on: June 17, 2010, 03:03:38 PM »
what about these? ;)
Code: [Select]
(setq s1 "stackoverfloww"
      s2 "ttackoverflows"
      s3 "aackoverflowst"
      s4 "overflowstackk"
)

JohnK

  • Administrator
  • Seagull
  • Posts: 10634
Re: Determine if string1 is a rotation of string2
« Reply #10 on: June 17, 2010, 04:12:32 PM »
hey, thats what you get for 10 minutes worth of programing.
:P
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org