Update all new Tasks
This commit is contained in:
parent
00a190b0a6
commit
91df62d461
5697 changed files with 93386 additions and 804 deletions
28
Task/String-matching/Common-Lisp/string-matching.lisp
Normal file
28
Task/String-matching/Common-Lisp/string-matching.lisp
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
(defun starts-with-p (str1 str2)
|
||||
"Determine whether `str1` starts with `str2`"
|
||||
(let ((p (search str2 str1)))
|
||||
(and p (= 0 p))))
|
||||
|
||||
(print (starts-with-p "foobar" "foo")) ; T
|
||||
(print (starts-with-p "foobar" "bar")) ; NIL
|
||||
|
||||
(defun ends-with-p (str1 str2)
|
||||
"Determine whether `str1` ends with `str2`"
|
||||
(let ((p (mismatch str2 str1 :from-end T)))
|
||||
(or (not p) (= 0 p))))
|
||||
|
||||
(print (ends-with-p "foobar" "foo")) ; NIL
|
||||
(print (ends-with-p "foobar" "bar")) ; T
|
||||
|
||||
(defun containsp (str1 str2)
|
||||
"Determine whether `str1` contains `str2`.
|
||||
Instead of just returning T, return a list of starting locations
|
||||
for every occurence of `str2` in `str1`"
|
||||
(unless (string-equal str2 "")
|
||||
(loop for p = (search str2 str1) then (search str2 str1 :start2 (1+ p))
|
||||
while p
|
||||
collect p)))
|
||||
|
||||
(print (containsp "foobar" "oba")) ; (2)
|
||||
(print (containsp "ababaBa" "ba")) ; (1 3)
|
||||
(print (containsp "foobar" "x")) ; NIL
|
||||
Loading…
Add table
Add a link
Reference in a new issue