In this challenge, you will demonstrate your understanding of control flow in Rust. The task involves finding the first palindrome number in a given range.
A palindrome is a number that reads the same backward as forward. This exercise will require you 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.
Palindromes are fascinating numbers, and finding them within a range will require efficient 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 first palindrome number within the range (inclusive). If there are no palindromes in the range, it should return None
.
start
to end
.None
if no palindromes exist in the range.start
may be 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. Palindromes are fascinating in various fields, including mathematics, literature, and biology, where they often have unique properties and significance.
rev()
method on a char
.char
s in a string by using the chars()
method on a String
ad0x99
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) }; if start > end { return None; } for num in start..=end { let num_str = num.to_string(); let reversed_str = num_str.chars().rev().collect::<String>(); if num_str == reversed_str { return Some(num); } } None}
sander-b-postnl
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(); let reversed: String = num_str.chars().rev().collect(); num_str == reversed}
jose-bernardo
pub fn is_palindrome(n: i32) -> bool { let mut m = n; let mut rev_n = 0; while m > 0 { rev_n = rev_n * 10 + m % 10; m /= 10; } rev_n == n}pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let (start, end) = if start > end { (end, start) } else { (start, end) }; for x in start..=end { if is_palindrome(x) { println!("{}", x); return Some(x) } } None}
mk-comm
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // Ensure the range is valid by swapping if necessary 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(); let reversed: String = num_str.chars().rev().collect(); num_str == reversed}
pickx
fn is_palindrome(n: i32) -> bool { let mut n = n.abs(); let nearest_pow_of_10 = 10_u32.pow(n.ilog10()); let mut nearest_pow_of_10 = i32::try_from(nearest_pow_of_10).unwrap(); while n >= 10 { let msb = n / nearest_pow_of_10; let lsb = n % 10; if msb != lsb { return false } n %= nearest_pow_of_10; // remove msb n /= 10; // remove lsb nearest_pow_of_10 /= 100; } true}pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // weird requirement let mut range = if start <= end { start..=end } else { end..=start }; range.find(|&n| is_palindrome(n))}
carlos-quantexai
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // Ensure the range is valid let (start, end) = if start > end { (end, start) } else { (start, end) }; for number in start..=end { if is_palindrome(number) { return Some(number); // Return the first palindrome found } } None // Return None if no palindromes are found}fn is_palindrome(num: i32) -> bool { let str_num = num.to_string(); // Convert the number to a string str_num == str_num.chars().rev().collect::<String>() // Check if it reads the same forwards and backwards}
digitalresistor
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let range = if start > end { end..=start } else { start..=end }; for n in range { let mystr = n.to_string(); if mystr.chars().rev().eq(mystr.chars()) { return Some(n); } } None}
digitalresistor
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let range = if start > end { end..=start } else { start..=end }; for n in range { let mystr = format!("{n}"); if mystr.chars().rev().eq(mystr.chars()) { return Some(n); } } None}
digitalresistor
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let (start, end) = if start > end { (end, start) } else { (start, end) }; for n in start..=end { let mystr = format!("{n}"); if mystr.chars().rev().eq(mystr.chars()) { return Some(n); } } None}
digitalresistor
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let (start, end) = if start > end { (end, start) } else { (start, end) }; for n in start..=end { let mystr = format!("{n}"); let mystr_rev = mystr.chars().rev(); if mystr_rev.eq(mystr.chars()) { return Some(n); } } None}
Mxn-ptr
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here let mut start = start; let mut end = end; if start > end { let tmp = start; start = end; end = tmp; } for i in start..=end { let s = i.to_string(); if s == s.chars().rev().collect::<String>() { return Some(i); } } None}
Mxn-ptr
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here let mut start = start; let mut end = end; if start > end { let tmp = start; start = end; end = tmp; } for i in start..=end { let s = i.to_string(); if s == s.chars().rev().collect::<String>() { return Some(i); } } None}
hinphansa
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here match start > end { true => return find_first_palindrome(end, start), false => (), } for i in start..=end { if i == reverse_int(i) { return Some(i); } } None}fn reverse_int(n: i32) -> i32 { let mut n = n; let mut reversed = 0; while n != 0 { let digit = n % 10; reversed = reversed * 10 + digit; n /= 10; } reversed}
hinphansa
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here match start > end { true => return find_first_palindrome(end, start), false => (), } for i in start..=end { let n = i.to_string(); let rev = n.chars().rev().collect::<String>(); if n == rev { return Some(i); } } None}
lvyuemeng
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let (start, end) = if start > end { (end, start) } else { (start, end) }; let start_str = start.to_string(); let len = start_str.len(); let first_half = &start_str[..(len + 1) / 2]; let second_half = &start_str[..len / 2].chars().rev().collect::<String>(); let palindrome_str = format!("{}{}", first_half, second_half); let palindrome = palindrome_str.parse::<i32>().unwrap(); if palindrome >= start && palindrome <= end { return Some(palindrome); } None}
lvyuemeng
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let (start, end) = if start > end { (end, start) } else { (start, end) }; let start_str = start.to_string(); let len = start_str.len(); // Create the palindrome by mirroring the first half of the string let first_half = &start_str[..(len + 1) / 2]; let second_half = &start_str[..len / 2].chars().rev().collect::<String>(); let palindrome_str = format!("{}{}", first_half, second_half); // Convert the palindrome string back to an integer let palindrome = palindrome_str.parse::<i32>().unwrap(); // Check if the palindrome is within the bounds if palindrome >= start && palindrome <= end { return Some(palindrome); } None}
funny233-github
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here let mut sp = start; let mut ep = end; if sp > ep { let tmp = ep; ep = sp; sp = tmp; } for i in sp..=ep { let mut s = format!(""); let mut tmp = i; while tmp > 0 { s += (tmp % 10).to_string().as_ref(); tmp /= 10; } if s == s.chars().rev().collect::<String>() { return Some(i); } } None}
dantekelly
use std::mem;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 reverse_int(n) == n { return Some(n); } } None}fn reverse_int(mut num: i32) -> i32 { let radix = 10; let mut n = 123; let mut reversed = 0; while num != 0 { reversed = reversed * radix + num % radix; num /= radix; } return reversed;}
dantekelly
use std::mem;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 check_palindrome(n).is_ok() { return Some(n); } } None}fn check_palindrome(num: i32) -> Result<bool, bool> { if reverse_int(num) == num { return Ok(true); } Err(false)}fn reverse_int(mut num: i32) -> i32 { let radix = 10; let mut n = 123; let mut reversed = 0; while num != 0 { reversed = reversed * radix + num % radix; num /= radix; } return reversed;}
dantekelly
use std::mem;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 check_palindrome(n).is_ok() { return Some(n); } } None}fn check_palindrome(num: i32) -> Result<bool, bool> { let reverse: String = num.to_string().chars().rev().collect(); if reverse == num.to_string() { return Ok(true); } Err(false)}
jtruong04
fn is_palindrome(number: i32) -> bool { let reverse:i32 = number.to_string().chars().rev().collect::<String>().parse().unwrap(); number == reverse}pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here println!("start {}, end {}", start, end); if start < end { for i in start..end+1 { if is_palindrome(i) { return Some(i); } } } else { for i in end..start+1 { if is_palindrome(i) { return Some(i); } } } None}
Aditeya
pub fn find_first_palindrome(mut start: i32, mut end: i32) -> Option<i32> { // TODO: Implement the function here if start > end { std::mem::swap(&mut start, &mut end); } for i in start..=end { let is = i.to_string(); if is == is.chars().rev().collect::<String>() { return Some(i); } } None}
jhq223
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { use std::mem; let mut start = start; let mut end = end; if start > end { mem::swap(&mut start, &mut end); } for i in start..=end { let ichars = i.to_string(); if ichars.chars().eq(ichars.chars().rev()) { return Some(i); } } None}
jhq223
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { use std::mem; let mut start = start; let mut end = end; if start > end { mem::swap(&mut start, &mut end); } for i in start..=end { let ichars = i.to_string(); if ichars.chars().collect::<String>() == ichars.chars().rev().collect::<String>() { return Some(i); } } None}
xbarnett
use std::cmp::{min, max};pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { for i in min(start, end)..=max(start, end) { if i.to_string() == i.to_string().chars().rev().collect::<String>() { return Some(i); } } None}
kyhou
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { if start < 10 && end < 10 { if start != end { return None; } else { return Some(start); } } let mut start = start; let mut end = end; if start > end { std::mem::swap(&mut end, &mut start); } (start..end).find(|x| x.to_string() == x.to_string().chars().rev().collect::<String>())}
CianciuStyles
use std::cmp::{max, min};pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here let (lo, hi) = (min(start, end), max(start, end)); for i in lo..=hi { let s1: String = i.to_string(); let s2: String = s1.chars().rev().collect(); if s1 == s2 { return Some(i); } } None}
masteryachty
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { if start > end { return find_first_palindrome(end, start); } for n in start..=end { let n_str = n.to_string(); let is_palindrome = n_str .chars() .zip(n_str.chars().rev()) .all(|(a, b)| a == b); if is_palindrome { return Some(n); } } None}
TomBaston
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { if start > end { return find_first_palindrome(end, start); } for n in start..=end { let n_str = n.to_string(); let is_palindrome = n_str .chars() .zip(n_str.chars().rev()) .all(|(a, b)| a == b); if is_palindrome { return Some(n); } } None}
elhananby
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let mut range = start.min(end)..=start.max(end); range.find(|&num| { let num_string = num.to_string(); num_string.chars().eq(num_string.chars().rev()) })}
elhananby
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // No need for .into_iter() as Range implements IntoIterator let mut range = start.min(end)..=start.max(end); // Use iter.find() instead of for loop + return range.find(|&num| { let num_string = num.to_string(); num_string.chars().eq(num_string.chars().rev()) })}
elhananby
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let range = if start <= end { start..=end } else { end..=start }.into_iter(); for num in range { let num_string = num.to_string(); let reversed = num_string.chars().rev().collect::<String>(); if num_string == reversed { return Some(num); } } None}
Sommos
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here let (start, end) = if start <= end { (start, end) } else { (end, start) }; // Iterate through the range of numbers from start to end for n in start..=end { // Convert the number to a string let range_string = n.to_string(); // Check if the number is a palindrome if range_string == range_string.chars().rev().collect::<String>() { return Some(n); // Return the first palindrome found } } None }
Burnus
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { (start.min(end)..=start.max(end)) .find(|n| n.to_string() == n.to_string().chars().rev().collect::<String>())}
swandyr
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here if start < end { start..=end } else { end..=start } .skip_while(|&n| n != rev_number(n)).next()}fn rev_number(mut num: i32) -> i32 { let mut rev = 0; let max = (0u32..).take_while(|&e| num / 10i32.pow(e) != 0).last().unwrap(); for e in (0..=max).rev() { let a = num / 10i32.pow(e); rev += a * 10i32.pow(max - e); num -= a * 10i32.pow(e); } rev}
qcabanes-hobby
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let (start, end) = if start < end { (start, end) } else { (end, start )}; (start..=end).find(|n| n.to_string() == n.to_string().chars().rev().collect::<String>())}
bczhc
pub fn find_first_palindrome(mut start: i32, mut end: i32) -> Option<i32> { if start > end { std::mem::swap(&mut start, &mut end); } for i in start..=end { if is_palindrome(i.to_string().as_bytes()) { return Some(i); } } None}fn is_palindrome<T: Eq>(items: &[T]) -> bool { let mid = items.len() / 2; let (a, b) = items.split_at(mid); if items.len() % 2 == 0 { a.iter().zip(b.iter().rev()).all(|x| x.0 == x.1) } else { a.iter().zip(b.iter().skip(1).rev()).all(|x| x.0 == x.1) }}
hilias
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here let (start, end) = if start < end { (start, end) } else { (end, start )}; for num in start..=end { let original = num.to_string(); let reverse = original.chars().rev().collect::<String>(); if original == reverse { return Some(num); } } None}
vineelkovvuri
pub fn is_palindrome(n: i32) -> bool { let mut rev = 0; let mut num = n; while num != 0 { rev = rev * 10 + num % 10; num /= 10; } rev == n}pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let (from, to) = if start < end { (start, end) } else { (end, start) }; for num in from..=to { if is_palindrome(num) { return Some(num); } } None}
TaiPoole
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let range; if start >= end { range = end..=start; } else { range = start..=end; }; for num in range { if num.to_string().chars().collect::<Vec<char>>() == num.to_string().chars().rev().collect::<Vec<char>>(){ return Some(num) } } None}
SRVng
pub fn find_first_palindrome(_start: i32, _end: i32) -> Option<i32> { let mut start = _start; let mut end = _end; if start.gt(&_end) { start = _end; end = _start; } for n in start..(end + 1) { let text = n.to_string(); let rev: String = text.chars().rev().collect(); if text.eq(&rev) { return Some(n); } } None}
kavu16
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here fn is_palindrome(num: i32) -> bool { let num_string = num.to_string(); let reversed: String = num_string.chars().rev().collect(); num_string == reversed } if start < end { for n in start..=end { if is_palindrome(n) { return Some(n) } } } else { for n in end..=start { if is_palindrome(n) { return Some(n) } } } None}
dmbtechdev
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here let (min,max); if start > end { min = end; max = start; } else { min = start; max = end; } for num in min..=max { let num_str = num.to_string(); let reversed_str = num_str.chars().rev().collect::<String>(); if num_str == reversed_str { return Some(num); } } None}
Herrgrim0
pub fn find_first_palindrome(mut start: i32, mut end: i32) -> Option<i32> { // TODO: Implement the function here if start > end { std::mem::swap(&mut start, &mut end); } for i in start..=end { let x = i.to_string(); let y = x.chars().rev().collect::<String>(); if x == y { return Some(i); } } None}
jenny07007
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let (start, end) = if start > end { (end, start) } else { (start, end) }; for n in start..=end { let n_str = n.to_string(); let reversed: String = n_str.chars().rev().collect(); if n_str == reversed { return Some(n); } } None}
Abinesh-Mathivanan
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let (start, end) = if start > end { (end, start) } else { (start, end) }; for i in start..=end { if i.to_string() == i.to_string().chars().rev().collect::<String>() { return Some(i); } } None }
leemars
use std::cmp::min;use std::cmp::max;pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here for i in min(start, end)..=max(start, end) { let origin = i.to_string(); let reversed = i.to_string().chars().rev().collect::<String>(); if origin == reversed { return Some(i); } } return None;}
leemars
use std::cmp::min;use std::cmp::max;pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here for i in min(start, end)..=max(start, end) { let origin = i.to_string(); let reversed = i.to_string().chars().rev().collect::<String>(); if origin == reversed { return Some(i); } } return None;}
A-ndrey
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let (start, end) = if start > end { (end, start) } else { (start, end) }; for p in start..=end { if p == rev(p) { return Some(p); } } None}fn rev(p: i32) -> i32 { let mut p = p; let mut r = 0; while p > 0 { r = r * 10 + p % 10; p /= 10; } r}
suraboy
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) }; (start..=end).find(|&x| x.to_string() == x.to_string().chars().rev().collect::<String>())}