leo/compiler/disabled_tests/parser/serialize/palindrome.leo

65 lines
1.9 KiB
Plaintext
Raw Normal View History

2022-01-28 21:34:02 +03:00
/*
namespace: Serialize
expectation: Pass
*/
// This Program takes in any 20-byte low register string and tells
// whether a string is a palindrome ignoring any spaces.
function main(str: [char; 20]) -> bool {
return is_palindrome(str);
}
function is_palindrome(str: [char; 20]) -> bool {
2022-03-07 19:30:03 +03:00
const str_len: u32 = 20u32; // saving const for convenience
2022-01-28 21:34:02 +03:00
// By default we assume that input is a palindrome.
2022-03-07 19:30:03 +03:00
let result: bool = true;
let processed: u8 = 0u8;
2022-01-28 21:34:02 +03:00
2022-03-08 21:20:05 +03:00
for start: u32 in 0u32..(str_len / 2u32) {
2022-03-07 19:30:03 +03:00
let start_sym: char = str[start];
2022-01-28 21:34:02 +03:00
if start_sym != ' ' {
2022-03-07 19:30:03 +03:00
let skipped: u8 = 0u8;
let end_empty: u8 = 0u8;
let end_sym: char = ' ';
2022-01-28 21:34:02 +03:00
2022-03-08 21:20:05 +03:00
for end: u32 in (str_len - 1u32)..start {
2022-01-28 21:34:02 +03:00
if str[end] != ' ' && skipped == processed && end_sym == ' ' {
end_sym = str[end];
} else {
2022-03-08 21:20:05 +03:00
end_empty = end_empty + 1u8;
2022-01-28 21:34:02 +03:00
if str[end] != ' ' {
2022-03-08 21:20:05 +03:00
skipped = skipped + 1u8;
2022-01-28 21:34:02 +03:00
}
}
}
// If there are symbols left to the right from the start.
if end_sym != ' ' {
console.log("Comparing: {} ? {}", start_sym, end_sym);
if result {
result = (start_sym == end_sym);
}
2022-03-08 21:20:05 +03:00
processed = processed + 1u8;
2022-01-28 21:34:02 +03:00
}
}
}
console.log("Result is: {}", result);
return result;
}
@test
function test_is_palindrome() {
console.assert(is_palindrome("a b a "));
console.assert(is_palindrome("😀😀😀😀😀 😀😀😀😀😀"));
console.assert(is_palindrome("borrow or rob "));
console.assert(is_palindrome("bbbb aaaa aaaa bbbb"));
console.assert(is_palindrome("aaaaaaaaaaaaaaaaaaaa"));
console.assert(is_palindrome("taco cat "));
}