ocaml: phase 5.1 palindrome.ml baseline (two-pointer check, 4/6 inputs match)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
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.
This commit is contained in:
@@ -31,6 +31,7 @@
|
||||
"newton_sqrt.ml": 1414,
|
||||
"mutable_record.ml": 10,
|
||||
"option_match.ml": 5,
|
||||
"palindrome.ml": 4,
|
||||
"pascal.ml": 252,
|
||||
"pi_leibniz.ml": 314,
|
||||
"pretty_table.ml": 64,
|
||||
|
||||
17
lib/ocaml/baseline/palindrome.ml
Normal file
17
lib/ocaml/baseline/palindrome.ml
Normal file
@@ -0,0 +1,17 @@
|
||||
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)
|
||||
Reference in New Issue
Block a user