In this challenge, you will demonstrate your understanding of control flow in Rust. The task involves finding the first decimal palindrome in a given range.
A decimal palindrome is a number whose decimal (base 10, "normal") digits read the same backward as forward. This exercise will require you to find the numerically least non-negative palindrome in a given range. The easiest way to do this is to iterate through the range, check each number to see if it is a palindrome, and return the first palindrome found. You can use any control flow construct to solve this problem. (There are much more efficient ways to solve this problem, but the calculations get complex quickly.)
Palindromes are fascinating numbers, and finding them within a range will require clear control flow logic to ensure you identify the first one accurately.
You need to write a function, find_first_palindrome(start: i32, end: i32) -> Option<i32>
, that takes two integer arguments start
and end
. The function should return the numerically least non-negative palindrome number within the range.
The range is inclusive: for example, if start == 1
and end == 1
the palindrome 1
is in range.
The range may have start > end
, in which case it is still a valid range: for example, start == 3
and end == 1
contains the values 1, 2, 3
.
If there are no palindromes in the range, the function should return None
.
start
to end
inclusive.None
if no palindromes exist in the range.start
is greater than end
.let result = find_first_palindrome(10, 30);
assert_eq!(result, Some(11)); // 11 is the first palindrome in the range
let result = find_first_palindrome(100, 105);
assert_eq!(result, Some(101)); // 101 is the first palindrome in the range
let result = find_first_palindrome(123, 130);
assert_eq!(result, None); // No palindromes in this range
let result = find_first_palindrome(-130, -1);
assert_eq!(result, None); // No palindromes in this range
let result = find_first_palindrome(100, -105);
assert_eq!(result, Some(0)); // 0 is the first palindrome in the range
Did you know that palindromes are not just limited to numbers? They are found in words, phrases, and even DNA sequences! For example, the word "racecar" is a palindrome, as it reads the same backward and forward. Check out this "Weird Al" video for many many examples.
Palindromes are fascinating in various fields, including mathematics, literature, and biology, where they often have unique properties and significance.
String
and compare it with its reverse.char
s in a String
by using the chars()
method on a String
rev()
method on an iterator. For example, you can get the char
s in a String
s
in reverse order with s.chars().rev()
use std::cmp::{max, min};pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here let (start, end) = (max(min(start, end), 0), max(start, end)); (start..=end).find(|&num| { let num_str: String = num.to_string(); let (forward, backward) = (num_str.chars(), num_str.chars().rev()); forward.zip(backward).all(|c| c.0 == c.1) })}
use std::cmp::{max, min};pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here let (start, end) = (max(min(start, end), 0), max(start, end)); (start..=end).find(|&num| num.to_string().chars().zip(num.to_string().chars().rev()) .all(|c| c.0 == c.1) )}
pub fn find_first_palindrome(mut start: i32, mut end: i32) -> Option<i32> { if start > end { let temp = start; let temp2 = end; start = temp2; end = temp; } let mut palindromes = Vec::new(); for num in start..=end { let num_str = num.to_string(); if num_str == num_str.chars().rev().collect::<String>() { palindromes.push(num); } } return palindromes.first().copied();}
use std::mem;fn is_palindrome(n: i32) -> bool { let s = n.to_string(); let chars = s.chars().collect::<Vec<_>>(); let l = chars.len(); for i in 0..l / 2 { if chars[i] != chars[l - i - 1] { return false; } } return true}pub fn find_first_palindrome(mut start: i32, mut end: i32) -> Option<i32> { if start > end { mem::swap(&mut start, &mut end); } for n in start..=end { if is_palindrome(n) { return Some(n); } } None}
pub fn is_palindrome(num: i32) -> bool { let s = num.to_string(); s == s.chars().rev().collect::<String>()}pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // Create a RangeInclusive local variable in ascending order. let my_iter= if start<=end { start..=end } else { end..=start }; my_iter .filter(|x| *x >= 0) // ignore negative elements. .find(|&n| is_palindrome(n)) // Locate palindrome, return Option<i32>. }
fn check(n: i32) -> bool { let s = n.to_string(); let rs: String = s.chars().rev().collect(); s == rs}pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { if start > 0 && end < 0 { return Some(0); } else if start < 0 && end < 0{ return None; } else if start < 0 && end > 0 { return Some(0); } else { let start1; let end1; if start > end { end1 = start; start1 = end; } else { start1 = start; end1 = end; } for i in start1..=end1 { if check(i) { return Some(i); } } } None}
pub fn is_dec_palindrome(v: i32) -> bool { let mut cpy = v; let mut ans = 0; while cpy > 0 { ans = ans * 10 + cpy % 10; cpy /= 10; } v == ans}pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let mut start = start; let mut end = end; if start > end { std::mem::swap(&mut start, &mut end); } for i in start..=end { if i >= 0 && is_dec_palindrome(i) { return Some(i); } } None}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here if start == end { let forward = start.to_string(); let reverse = forward.chars().rev().collect::<String>(); if forward == reverse { return Some(start); } } if start < end { for v in start..=end { let forward = v.to_string(); let reverse = forward.chars().rev().collect::<String>(); if forward == reverse { return Some(v); } } }else{ for v in end..=start { let forward = v.to_string(); let reverse = forward.chars().rev().collect::<String>(); if forward == reverse { return Some(v); } } } None}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { fn range(start: i32, end: i32) -> std::ops::RangeInclusive<i32> { if start > end { return end..=start; } else { start..=end } } let mut start = start; if start < 0 { start = 0; } for n in range(start, end) { if n.to_string().chars().collect::<String>() == n.to_string().chars().rev().collect::<String>() { return Some(n); } } None}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let from = start.min(end); let to = start.max(end); (from..=to).find(|number| { let forward = number.to_string(); let reverse = forward.chars().rev().collect::<String>(); forward == reverse })}
use core::cmp::{min, max};pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here let i = min(start, end); let j = max(start, end); for k in i..=j { let number = k.to_string(); let reverse_number: String = number.chars().rev().collect(); if number == reverse_number { return Some(k) } } None}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let (bottom, top) = if start > end { (end, start) } else { (start, end) }; for i in bottom..top + 1 { if i.to_string() == i.to_string().chars().rev().collect::<String>() { return Some(i); } } None}
use std::cmp;pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let _start = cmp::min(start, end); let _end = cmp::max(start, end); for v in _start..=_end { let s1 = v.to_string(); let s2: String = v.to_string().chars().rev().collect(); if s1 == s2 { return Some(v); } } None}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let min: i32; let max: i32; if start < end { min = start; max = end; } else { min = end; max = start; } for number in min..=max { let s = number.to_string(); let r: String = s.chars().rev().collect(); if s == r { return Some(number) } } None}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let min: i32; let max: i32; if start < end { min = start; max = end; } else { min = end; max = start; } for number in min..=max { let s = number.to_string(); let r: String = s.chars().rev().collect(); if s == r { return Some(number) } } None}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here let (mut begin, mut finish) = (start, end); if start > end { (begin, finish) = (end, start); } for i in begin..=finish { if i.to_string() == i.to_string().chars().rev().collect::<String>() { return Some(i); } } None}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here let mut start_copy = start; let mut end_copy = end; if start > end { start_copy = end; end_copy = start; } for i in start_copy..=end_copy { let num = i.to_string(); let chars: Vec<char> = num.to_lowercase().chars().collect(); let chars_rev: Vec<char> = num.to_lowercase().chars().collect(); let last_index = chars.len() - 1; for j in 0..chars.len() { if chars[j] != chars_rev[last_index - j] { break; } if j == chars.len() - 1 { return Some(i); } } } None}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let lower = start.min(end); let upper = start.max(end); for n in lower..=upper { if n < 0 { continue; } let s = n.to_string(); let rev: String = s.chars().rev().collect(); if s == rev { return Some(n); } } None}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let (start, end) = if start > end { (end, start) } else { (start, end) }; for num in start..=end { if is_palindrome(num) { return Some(num); } } None }fn is_palindrome(num: i32) -> bool { let num_str = num.to_string(); num_str == num_str.chars().rev().collect::<String>()}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let (low, high) = (start.min(end), start.max(end)); for i in low..=high { let rev = create_rev_number(i); if rev == i { return Some(i); } } None}fn create_rev_number(n: i32) -> i32 { n.abs().to_string().chars().rev().collect::<String>().parse::<i32>().unwrap()}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here if start > end { for num in end..=start { if num > -1 { let num_str = num.to_string(); if num_str == num_str.chars().rev().collect::<String>() { return Some(num) } } } } else { for num in start..=end { if num > -1 { let num_str = num.to_string(); if num_str == num_str.chars().rev().collect::<String>() { return Some(num) } } } } None}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let (min, max) = if start <= end { (start, end ) } else { (end, start) }; (min..=max).find( | &n | n >= 0 && is_palindrome(n))}fn is_palindrome(n: i32) -> bool { let s = n.to_string(); s.chars().eq(s.chars().rev())}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here let (init, fine) = if start > end { (end, start) } else { (start, end) }; for i in init..=fine { let as_str = i.to_string(); if as_str == as_str.chars().rev().collect::<String>() { return Some(i); } } None}
pub fn find_first_palindrome(mut start: i32, mut end: i32) -> Option<i32> { if start > end { let temp = start; start = end; end = temp; } for i in start..=end { if is_palindrome(i) { return Some(i); } } None}fn is_palindrome(num: i32) -> bool { let mut reversed = 0; let mut n = num; while n > 0 { reversed = reversed * 10 + n % 10; n /= 10; } reversed == num}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here let (real_start, real_end) = if start > end { (end, start) } else { (start, end) }; for number in real_start..=real_end { let number_str = number.to_string(); if number_str == number_str.chars().rev().collect::<String>(){ return Some(number); } } None}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here let mut _start: i32 = start; let mut _end: i32 = end; if start > end { _start = end; _end = start; }; for n in _start..=_end { if judge_palindrome(n) { return Some(n); } } None}fn judge_palindrome(num: i32) -> bool { let s = format!("{num}"); s == s.chars().rev().collect::<String>()}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here let mut _start:i32 = start; let mut _end:i32 = end; if start > end { _start = end; _end = start; }; for n in _start..=_end { if judge_palindrome(n) { return Some(n); } } None}fn judge_palindrome(num: i32) -> bool { let s = format!("{num}"); s == s.chars().rev().collect::<String>()}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { if start > end { return find_first_palindrome(end, start); } if end < 0 { return None; } if start < 0 { return find_first_palindrome(0, end); } (start..=end).find(|&i| is_palindrome(i))}fn is_palindrome(num: i32) -> bool { if num == 0 { return true; } let order = (num as f32).log10() as u32; if order == 0 { return true; } let mut check = num; for _ in 0..=order / 2 { let check_order = (check as f32).log10() as u32; if check_order == 0 { break; } let left_part = check / 10i32.pow(check_order); let right_part = check % 10; if left_part != right_part { return false; } check /= 10; check %= 10i32.pow(check_order - 1); } true}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let mut my_start = start; let mut my_end = end; if start > end { my_start = end; my_end = start; } for num in my_start..=my_end { let num_str = num.to_string(); if num_str == num_str.chars().rev().collect::<String>() { return Some(num); } } None}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here let (mut start, mut end) = (start, end); if start > end { let temp = start; start = end; end = temp; } for num in start..=end { let num_str = num.to_string(); let reversed = num_str.chars().rev().collect::<String>(); if num_str == reversed { return Some(num) } } None}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here let (st, en) = (start.min(end), end.max(start)) ; (st..=en).find(|&i| i.to_string().chars().rev().eq(i.to_string().chars()))}
use std::cmp::{min,max};pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let s = min(start, end); let e = max(start, end); for i in s..=e{ let mut num = i; if is_palindrome(&mut num) { return Some(i); } } None}pub fn is_palindrome(num: &mut i32) -> bool { let original_number = *num; let mut reversed_number = 0; while *num > 0 { let digit = *num % 10; reversed_number = reversed_number * 10 + digit; *num /= 10; } original_number == reversed_number}
use std::cmp::{min, max};pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here let s = min(start, end); let e = max(start, end); for i in s..=e { if is_palindrome(i.to_string()) { return Some(i) } } None}fn is_palindrome(s: String) -> bool { s.chars().zip(s.chars().rev()).all(|(c1, c2)| c1 == c2)}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here let range = if start > end { end..=start } else { start..=end }; 'outer: for num in range { let s = num.to_string(); let mut iter = s .chars() .zip(s.chars().rev()); while let Some((a, b)) = iter.next() { if a != b { continue 'outer; } } return Some(num) } None}
fn digits(mut value: i32) -> Vec<u8> { let d = std::iter::from_fn(|| { if value == 0 { None } else { let d = value % 10; value /= 10; Some(d as u8) } }); d.collect()}fn is_palindrome(digits: &[u8]) -> bool { let ndigits = digits.len(); for i in 0..(ndigits + 1) / 2 { if digits[i] != digits[ndigits - i - 1] { return false; } } true}pub fn find_first_palindrome(mut start: i32, mut end: i32) -> Option<i32> { if start > end { std::mem::swap(&mut start, &mut end); } (start..=end).find(|&i| { if i < 0 { return false; } let d = digits(i); is_palindrome(&d) }) }
fn is_palindrome(n: i32) -> bool { let s = n.to_string(); s == s.chars().rev().collect::<String>()}pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { if start <= end { for num in start..=end { if is_palindrome(num) { return Some(num); } } } else { for num in (end..=start) { if is_palindrome(num) { return Some(num); } } } None}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { for num in start.min(end)..=end.max(start) { let num_ = num.to_string(); if num_.chars().zip(num_.chars().rev()).all(|(c1, c2)| c1 == c2) { return Some(num) } } None}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here let mut s= start; let mut e = end; if e < s { (s, e) = (e, s); } for i in s..=e { if i < 0 { continue; } let p = i.to_string().chars().rev().collect::<String>().parse::<i32>().ok().unwrap(); if p == i { return Some(i); } } None}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here if end < start { for i in end..=start{ let i_to_str = i.to_string(); if i_to_str == i_to_str.chars().rev().collect::<String>(){ return Some(i) } } } for i in start..=end{ let i_to_str = i.to_string(); if i_to_str == i_to_str.chars().rev().collect::<String>(){ return Some(i) } } None}
pub fn find_first_palindrome(mut start: i32, mut end: i32) -> Option<i32> { if start > end { let tmp = start; start = end; end = tmp } for num in start..=end { if num.to_string().chars().rev().collect::<String>() == num.to_string() { return Some(num); } } None}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let from: i32; let to: i32; if start <= end { from = start; to = end; } else { from = end; to = start; } for candidate in from..=to { let rev: String = candidate.to_string().chars().rev().collect(); if candidate.to_string() == rev { return Some(candidate); } } None}
use std::cmp::{min, max};pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { (min(start, end)..=max(start, end)) .find(|&x| { let s = x.to_string(); s.chars().eq(s.chars().rev()) })}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here match start < end { true => (start..=end).find(|num| { let s = num.to_string(); s.chars().eq(s.chars().rev()) }), false => (end..=start).find(|num| { let s = num.to_string(); s.chars().eq(s.chars().rev()) }), }}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let mut a = start; let mut b = end; if start > end { b = start; a = end } for i in a..=b { if i.to_string().chars().eq(i.to_string().chars().rev()) { return Some(i); } } None}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here let mut from = start; let mut to = end; if start > end { from = end; to = start; } for num in from..=to { let mut n = num; let mut backward = 0; while n > 0 { let d = n % 10; n /= 10; backward = backward * 10 + d; } if num == backward { return Some(num); } } None}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here let mut s = start; let mut e = end; if start > end { s = end; e = start; } for i in s..=e { let s = i.to_string(); let rs: String = s.chars().rev().collect(); if s == rs { return Some(i); } } None}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here let (start, end) = if start > end { (end, start) } else { (start, end) }; let mut s = String::new(); for num in start..end+1{ s = num.to_string(); if s == s.chars().rev().collect::<String>(){ return Some(num); } } None}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let (start, end) = if start > end { (end, start) } else { (start, end) }; for number in start..=end { if number < 0 { continue; } if number < 10 { return Some(number); } let mut digits: Vec<i32> = vec![]; let mut temp = number; while temp > 0 { digits.push(temp % 10); temp /= 10; } let digits_length = digits.len() as i32; let mut left: i32 = 0; let mut right: i32 = digits_length - 1; let mut is_palindrome = true; while left < right { if digits[left as usize] == digits[right as usize] { left += 1; right -= 1; } else { is_palindrome = false; break; } } if is_palindrome { return Some(number); } } return None;}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let (start, end) = (start.min(end), end.max(start)) ; for n in start..=end{ if is_palindrome(n.to_string()){ return Some(n); }}None}pub fn is_palindrome(string: String) -> bool { let string_reversed:String = string.chars().rev().collect(); string == string_reversed}
use std::cmp;pub fn is_palindrome(num: i32) -> bool { let different_count = num .to_string() .chars() .zip(num.to_string().chars().rev()) .filter(|&(a, b)| a != b) .count(); different_count == 0}pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { for i in cmp::min(start, end)..=cmp::max(start, end) { if is_palindrome(i) { return Some(i); } } None}