Last active
August 4, 2021 18:09
-
-
Save damirka/867bc8acb926c9e6da78a811bff7ac73 to your computer and use it in GitHub Desktop.
Leo Palindrome example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 { | |
const str_len = 20u32; // saving const for convinience | |
// By default we assume that input is a palindrome. | |
let result = true; | |
let processed = 0u8; | |
for start in 0..(str_len / 2) { | |
let start_sym = str[start]; | |
if start_sym != ' ' { | |
let skipped = 0u8; | |
let end_empty = 0u8; | |
let end_sym = ' '; | |
for end in (str_len - 1)..start { | |
if str[end] != ' ' && skipped == processed && end_sym == ' ' { | |
end_sym = str[end]; | |
} else { | |
end_empty = end_empty + 1; | |
if str[end] != ' ' { | |
skipped = skipped + 1; | |
} | |
} | |
} | |
// 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); | |
} | |
processed = processed + 1; | |
} | |
} | |
} | |
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 ")); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The program input for strings/src/main.leo | |
[main] | |
str: [char; 20] = "Borrow or rob "; // char array can be defined as a string | |
[registers] | |
r0: bool = false; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment