TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started 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.
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:
-
(defun test (s1 s2) (or (vl-string-search s1 (strcat s2 s2))))
-
(http://www.theswamp.org/screens/index.php?dir=kerry/&file=pinkie.gif)
:-D
-
except for
(test "TIT "TI")
-
(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.
-
except for
(test "TIT "TI")
What a boob :-D
-
Another two :-)
(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
)
)
)
(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))
)
-
except for
(test "TIT "TI")
yep, my fault
i just can't program and think about tits at the same time :)
-
Okay, just for kicks (I had to chew up the rest of my lunch break some how).
No one suggested this method yet so...
(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))
-
what about these? ;)
(setq s1 "stackoverfloww"
s2 "ttackoverflows"
s3 "aackoverflowst"
s4 "overflowstackk"
)
-
hey, thats what you get for 10 minutes worth of programing.
:P