fork download
  1. (defun sequence-type (seq)
  2. (etypecase seq
  3. (list 'list)
  4. (string 'string)
  5. (bit-vector 'bit-vector)
  6. (vector 'vector)))
  7.  
  8. (defun insert-between (s fn)
  9. (if (<= (length s) 1)
  10. s
  11. (loop with result-type = (sequence-type s)
  12. for i from 0 below (length s)
  13. for a = (elt s i)
  14. for b = (ignore-errors (elt s (1+ i)))
  15. for (c insert-p) = (when b (multiple-value-list (funcall fn a b)))
  16. if (or c insert-p)
  17. collect (concatenate result-type (list a) c)
  18. into result
  19. else
  20. collect (concatenate result-type (list a))
  21. into result
  22. finally (return (apply #'concatenate result-type result)))))
  23.  
  24.  
  25. (defun odai-22-594-aux (a b)
  26. (let* ((a (digit-char-p a 10))
  27. (b (digit-char-p b 10))
  28. (c (format nil "~D" (+ a b))))
  29. (if (= (length c) 1)
  30. c
  31. (format nil "~A~A~A"
  32. (char c 0)
  33. (odai-22-594-aux (char c 0) (char c 1))
  34. (char c 1)))))
  35.  
  36. (defun odai-22-594 (s n m)
  37. (insert-between s (lambda (a b)
  38. (when (and (char= n a)
  39. (char= m b))
  40. (values (odai-22-594-aux a b) t)))))
  41.  
  42. (defun odai-22-608 (s)
  43. (insert-between s (lambda (a b)
  44. (when (and (char= a b)
  45. (upper-case-p a)
  46. (upper-case-p b))
  47. (values (concatenate 'string (list (char-downcase a))) t)))))
  48.  
  49.  
  50.  
  51. (loop for (args expected) in '((("123456789" #\3 #\4) "1237456789")
  52. (("123456789" #\6 #\7) "123456143789")
  53. (("3141592653589793238462643383279502884197169399375105820974944" #\9 #\7)
  54. "3141592653589176793238462643383279502884191767169399375105820917674944")
  55. (("111" #\1 #\1) "12121")
  56. (("11" #\1 #\1) "121")
  57. (("1" #\1 #\1) "1")
  58. (("" #\1 #\1) ""))
  59. for result = (apply #'odai-22-594 args)
  60. do (format t "~:[❌~;✓~] ~{~A~^ ~} → ~A~%"
  61. (equal result expected) args result))
  62. (terpri)
  63. (loop for (args expected) in '((("abcDDefGG") "abcDdDefGgG")
  64. (("abcDDDfGG") "abcDdDdDfGgG"))
  65. for result = (apply #'odai-22-608 args)
  66. do (format t "~:[❌~;✓~] ~{~A~^ ~} → ~A~%"
  67. (equal result expected) args result))
  68.  
Success #stdin #stdout 0.01s 29416KB
stdin
Standard input is empty
stdout
✓ 123456789 3 4 → 1237456789
✓ 123456789 6 7 → 123456143789
✓ 3141592653589793238462643383279502884197169399375105820974944 9 7 → 3141592653589176793238462643383279502884191767169399375105820917674944
✓ 111 1 1 → 12121
✓ 11 1 1 → 121
✓ 1 1 1 → 1
✓  1 1 → 

✓ abcDDefGG → abcDdDefGgG
✓ abcDDDfGG → abcDdDdDfGgG