Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
Two-pointer palindrome check:
let is_palindrome s =
let n = String.length s in
let rec check i j =
if i >= j then true
else if s.[i] <> s.[j] then false
else check (i + 1) (j - 1)
in
check 0 (n - 1)
Tests on six strings:
racecar = true
hello = false
abba = true
'' = true (vacuously, i >= j on entry)
'a' = true
'ab' = false
Sum = 4.
Uses s.[i] <> s.[j] (string-get + structural inequality), recursive
2-arg pointer advancement, and a multi-clause if/else if/else for
the three cases.
48 baseline programs total.
18 lines
439 B
OCaml
18 lines
439 B
OCaml
let is_palindrome s =
|
|
let n = String.length s in
|
|
let rec check i j =
|
|
if i >= j then true
|
|
else if s.[i] <> s.[j] then false
|
|
else check (i + 1) (j - 1)
|
|
in
|
|
check 0 (n - 1)
|
|
|
|
;;
|
|
|
|
(if is_palindrome "racecar" then 1 else 0) +
|
|
(if is_palindrome "hello" then 1 else 0) +
|
|
(if is_palindrome "abba" then 1 else 0) +
|
|
(if is_palindrome "" then 1 else 0) +
|
|
(if is_palindrome "a" then 1 else 0) +
|
|
(if is_palindrome "ab" then 1 else 0)
|