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
.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()
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}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here 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 mut reversed = string.clone(); unsafe{ let vec = reversed.as_mut_vec(); vec.reverse(); } string == reversed}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here let (start, end) = (start.min(end), end.max(start)) ; for n in start..=end{ let mut s = n.to_string(); unsafe{ let vec = s.as_mut_vec(); vec.reverse(); } if n.to_string() == s{ return Some(n); }}None}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here let (s, e) = (start.min(end), end.max(start)) ; for n in s..=e{ let mut s = n.to_string(); unsafe{ let vec = s.as_mut_vec(); vec.reverse(); } if n.to_string() == s{ return Some(n); }}None}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let s: i32; let e: i32; if start > end { s = end; e = start; } else { s = start; e = end; } for i in s..=e { if is_palindrome(i.to_string()) { return Some(i); } } None}pub fn is_palindrome(string: String) -> bool { let rev = string.chars().rev().collect::<String>(); return string == rev;}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let (st, en) = (start.min(end), end.max(start)) ; (st..=en).find(|&i| is_palindrome(i))}fn is_palindrome(n: i32) -> bool { let number = n.to_string(); number.chars().zip(number.chars().rev()).all(|(l, r)| l == r)}
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()))}
fn digits(n: u32) -> Vec<u32> { let d = f32::log10(n as f32).floor() as u32 + 1; (1..=d).map(|k| (n / (10 as u32).pow(k - 1)) % 10).collect()}fn vec_is_palin<T: Clone + PartialEq>(v: Vec<T>) -> bool { let mut w = v.clone(); w.reverse(); v.iter().zip(w.iter()).all(|(a, b)| a == b)}pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { if start > end { return find_first_palindrome(end, start); } for i in start..=end { if i < 0 { continue; } if vec_is_palin(digits(i.abs() as u32)) { return Some(i); } } None}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let mut inclusive = start..=end; if start > end { inclusive = end..=start; } for i in inclusive { let i_string = i.to_string(); let mut found = true; for (a, b) in i_string.chars().zip(i_string.chars().rev()) { if a != b { found = false; break; } } if found { return Some(i); } } None}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here // handle start bigger than end let new_start = start.min(end); let new_end = end.max(start); for i in new_start..=new_end { if is_number_palindrome(i) { return Some(i); } } return None;}fn is_number_palindrome(n: i32) -> bool { // 2 pointers let mut start = 0; let mut end = n.to_string().len() - 1; while start < end { if n.to_string().chars().nth(start) != n.to_string().chars().nth(end) { return false; } start += 1; end -= 1; } return true;}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let (st, en) = (start.min(end), end.max(start)) ; (st..=en).find(|&i| i.to_string().chars().rev().eq(i.to_string().chars()))}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { for number in start.min(end)..=start.max(end) { let number_string = number.to_string(); let number_string_reversed = number_string.chars().rev().collect::<String>(); if number_string == number_string_reversed { return Some(number) } } None}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let mut range = if end >= start { start..=end } else { end..=start }; range.find(|&x| { let x_string = x.to_string(); x_string.chars().eq(x_string.chars().rev()) })}
// A bit overkill, but I felt like traits...pub trait Reverse { fn reverse(&self) -> Self;}pub trait Palindrome: Reverse + Sizedwhere Self: PartialEq<Self>,{ fn is_palindrome(self) -> bool { self == self.reverse() }}impl Reverse for i32 { fn reverse(&self) -> Self { let mut n = *self; let radix = 10; let mut reversed = 0; while n > 0 { reversed = reversed * radix + n % radix; n /= radix; } reversed }}impl Palindrome for i32 {}pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { (start.min(end)..=start.max(end)) .find(|&i| i.is_palindrome())}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { (start.min(end)..=start.max(end)) .filter_map(|number| (number == reverse_num(number)) .then_some(number)) .next()}fn reverse_num(mut number: i32) -> i32 { let mut reversed = 0; while number > 0 { reversed *= 10; reversed += number % 10; number /= 10; } reversed}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { (start.min(end)..=start.max(end)) .filter_map(|number| (number == reverse_num(number)) .then_some(number)) .next()}fn reverse_num(mut number: i32) -> i32 { let mut reversed = 0; while number > 0 { reversed *= 10; reversed += number % 10; number /= 10; } reversed}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here for n in (start.min(end)..=end.max(start)).into_iter() { let m = n.to_string(); if is_palindrome(m) { return Some(n); } } None}fn is_palindrome(s: String) -> bool { s == s.chars().rev().collect::<String>()}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here let s = std::cmp::min(start, end); let e = std::cmp::max(start, end); for n in (s..=e).into_iter() { let m = n.to_string(); if m == m.chars().rev().collect::<String>() { return Some(n); } } None}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { for num in start.min(end)..=(end.max(start)) { let num_as_string = num.to_string(); let num_chars: Vec<char> = num_as_string.chars().collect(); let num_len = num_chars.len(); for (i, &c) in num_chars.iter().enumerate() { if num_len == 1 || i > (((num_len - 1) / 2) as usize) { return Some(num); } if c != num_chars[num_len - 1 - i] { break; } } } None}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { for i in start.min(end)..=(end.max(start)) { let a = i.to_string(); let b: String = i.to_string().chars().rev().collect(); if a == b { return Some(i) } } None}
fn is_palindrome(n: i32) -> bool { let s = n.to_string(); let count = s.chars().count(); for i in 0..count/2 { if s.chars().nth(i) != s.chars().nth(count-1-i) { return false; } } true}pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { for n in core::cmp::min(start, end)..=core::cmp::max(start, end) { if is_palindrome(n) { return Some(n); } } None}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let mut min = start; let mut max = end; if min > max { let tmp = min; min = max; max = tmp; } for n in min..=max { let stri: String = n.to_string(); let reversed = stri.chars().rev().collect::<String>(); if stri == reversed { return Some(n); } } None}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here let mut i = start; let mut j = end; if i > j { j = j + i; i = j - i; j = j - i; } while i <= j { let num:String = i.to_string(); let mut left = 0; let mut right = num.len()-1; let mut ans = true; while left<right { if num.chars().nth(left) != num.chars().nth(right) { ans = false; break; } left+=1; right-=1; } if ans { return Some(i); } i+=1; } None}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here let mut i = start; let mut j = end; if i > j { j = j + i; i = j - i; j = j - i; } while i <= j { let num:String = i.to_string(); let mut left = 0; let mut right = num.len()-1; let mut ans = true; while left<right { if num.chars().nth(left) != num.chars().nth(right) { ans = false; break; } left+=1; right-=1; } if ans { return Some(i); } i+=1; } None}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here let (fstart, fend) = if start < end {(start,end)} else {(end,start)}; 'outer: for number in fstart..=fend { let number_str = number.to_string(); for (char, revchar) in number_str.chars().zip(number_str.chars().rev()) { if char != revchar { continue 'outer; } } return Some(number); } None}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { println!("{start}..{end}"); if start <= end { (start..=end).find(|n| is_palindrome(*n)) } else { (end..=start).find(|n| is_palindrome(*n)) } }fn is_palindrome(num: i32) -> bool { let numstr: Vec<_> = num.to_string().chars().collect(); let mut pos = 0; let end = numstr.len() - 1; while pos < end - pos { if numstr[pos] != numstr[end - pos] { return false; } pos += 1; } true}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { println!("{start}..{end}"); if start <= end { (start..=end).find(|n| is_palindrome(*n)) } else { (end..=start).find(|n| is_palindrome(*n)) } }fn is_palindrome(num: i32) -> bool { let numstr: Vec<_> = num.to_string().chars().collect(); let mut pos = 0; let end = numstr.len() - 1; while pos < end { if numstr[pos] != numstr[end - pos] { return false; } pos += 1; } true}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let (start, end) = if start <= end { (start, end) } else { (end, start) }; for n in start..=end { if n.to_string().chars().rev().collect::<String>() == n.to_string() { return Some(n); } } None}
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let mut s = start; let mut e = end; if s > e { e = start; s = end; }; for num in s..=e { let num_string = num.to_string(); if num_string == num_string.chars().rev().collect::<String>() { return Some(num); } } println!("{start}{end}"); None}