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
anthonycabreralara
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); // Wrap the result in Some } } None // Return None if no palindrome is found}pub fn is_palindrome(num: i32) -> bool { let string_num = num.to_string(); // Declare variable with let let reversed_string_num: String = string_num.chars().rev().collect(); // Collect into a String string_num == reversed_string_num // Compare strings}
mbergkvist
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}
mbergkvist
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { (start.min(end)..=start.max(end)) .filter_map(|number| { let s = number.to_string(); let s_rev: String = s.chars().rev().collect(); if s == s_rev { Some(number) } else { None } }) .next()}
jimlawton
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here let range = if start > end { end..start+1 } else { start..end+1 }; for i in range { let num_str: String = format!("{i}"); let rev_str: String = num_str.chars().rev().collect(); if num_str == rev_str { return Some(i); } } None}
mehdihmr
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_vec = i.to_string().chars().collect::<Vec<char>>(); let mut is_pal = true; for j in 0..s_vec.len() / 2 { if s_vec[j] != s_vec[s_vec.len() - 1 - j] { is_pal = false; } } if is_pal { return Some(i); } } None}
gauthamk28
use std::cmp::{min, max};pub fn find_first_palindrome(start: u32, end: u32) -> Option<u32> { // TODO: Implement the function here let (start, end) = (min(start, end), max(start, end)); 'numbers_iter: for n in start..=end { let digits = ((n as f64).log10() + 1.0).floor() as u32; for i in 1..(digits / 2) + 1 { if digit(n, i) != digit(n, digits + 1 - i) { continue 'numbers_iter; } } return Some(n); } None}fn digit(number: u32, digit: u32) -> u8 { ((number / 10_u32.pow(digit - 1)) % 10) as u8}
DivineGod
use std::cmp::{min, max};pub fn find_first_palindrome(start: u32, end: u32) -> Option<u32> { // TODO: Implement the function here let (start, end) = (min(start, end), max(start, end)); 'numbers_iter: for n in start..=end { let digits = ((n as f64).log10() + 1.0).floor() as u32; for i in 1..(digits / 2) + 1 { if digit(n, i) != digit(n, digits + 1 - i) { continue 'numbers_iter; } } return Some(n); } None}fn digit(number: u32, digit: u32) -> u8 { ((number / 10_u32.pow(digit - 1)) % 10) as u8}
sroas
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here let (s, e) = if start < end { (start, end) } else { (end, start) }; (s..=e).find(|n| { let nstr = n.to_string(); nstr.chars().collect::<Vec<_>>() == nstr.chars().rev().collect::<Vec<_>>() })}
diegocmsantos
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let (s, e); if start > end { (s, e) = (end, start); } else { (s, e) = (start, end); } (s..=e).find(|&i| is_palindrome(i))}fn is_palindrome(n: i32) -> bool { let s = n.to_string(); s.chars().eq(s.chars().rev())}
qiyuan711
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here let scope = if start > end { end..=start } else { start..=end }; for num in scope { let num_str = num.to_string(); let num_str_rev: String = num_str.chars().rev().collect(); if num_str == num_str_rev { return Some(num) } } None}
tamanishi
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here let (s, e); if start > end { (s, e) = (end, start); } else { (s, e) = (start, end); } for n in s..=e { let org = n.to_string(); let rev = org.chars().rev().collect::<String>(); if org == rev { return Some(n); } } None}
Thymelizabeth
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { (start.min(end)..=end.max(start)).find(|n| { let n = format!("{}", n); n == n.chars().rev().collect::<String>() })}
0xsmarter
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let (start, end) = if end < start { (end, start) } else { (start, end) }; for num in start..=end { if num == reversed_num(num) { return Some(num); } } None}fn reversed_num(mut num: i32) -> i32 { let mut reversed = 0; while num > 0 { reversed *= 10; reversed += num % 10; num /= 10 } reversed}
aynugek
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let (start, end) = if end < start { (end, start) } else { (start, end) }; for num in start..=end { if num == reversed_num(num) { return Some(num); } } None}fn reversed_num(mut num: i32) -> i32 { let mut reversed = 0; while num > 0 { reversed *= 10; reversed += num % 10; num /= 10 } reversed}
cip999
fn is_palindrome(n: i32) -> bool { let n = n.to_string(); let n_rev = n.clone().chars().rev().collect::<String>(); n == n_rev}pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let (start, end) = if start < end { (start, end) } else { (end, start) }; (start..=end).into_iter().find(|&n| is_palindrome(n))}
madeinheaven91
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here let (s, e): (i32, i32) = if start > end { (end, start) }else{ (start, end) }; 'outer: for i in s..=e{ let string = i.to_string(); let max_index = string.len() - 1; let bytes = string.as_bytes(); let mut j = 0; for b in bytes{ if *b != bytes[max_index - j]{ continue 'outer; } j += 1; } return Some(i) } None}
madeinheaven91
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here let (s, e): (i32, i32) = if start > end { (end, start) }else{ (start, end) }; 'outer: for i in s..=e{ let num = if i < 0 { -i } else { i }; let string = num.to_string(); let max_index = string.len() - 1; let bytes = string.as_bytes(); let mut j = 0; for b in bytes{ if *b != bytes[max_index - j]{ continue 'outer; } j += 1; } return Some(i) } None}
harrien22
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here let (start, end) = (start.min(end), start.max(end)); for num in start..=end { let s = num.to_string(); if check_string_palindrome(s) { return Some(num); } } None}fn check_string_palindrome(s: String) -> bool { let s = s.chars(); s.clone().eq(s.rev())}
tsucchinoko
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) }; for x in start..=end { if x.to_string() == x.to_string().chars().rev().collect::<String>() { return Some(x); } } None}
ankeetparikh
use std::cmp;fn is_palindrome(num: i32) -> bool { let mut p = num; let mut rev = 0; while p > 0 { rev = rev * 10 + (p % 10); p /= 10; } return num == rev}pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let st = cmp::min(start, end); let en = cmp::max(start, end); for num in st..=en { if is_palindrome(num) { return Some(num) } } None}
Rolando1994
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) }; for x in start..=end { if x.to_string() == x.to_string().chars().rev().collect::<String>() { return Some(x); } } None}
cloki0610
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) }; for i in start..=end { if i.to_string().chars().rev().collect::<String>() == i.to_string() { return Some(i) } } None}
Bubbet
pub fn find_first_palindrome(mut start: i32, mut end: i32) -> Option<i32> { // TODO: Implement the function here if start > end { let old_end = end; end = start; start = old_end; } for i in start..=end { let s: String = format!("{}", i); let chars = s.chars(); let len = chars.size_hint().0; // i have no idea how this isnt shitting the bed in numbers that are odd in length let first = chars.take(len/2 + 1); let second = s.chars().rev(); if first.zip(second).all(|(first, second)| first == second) { return Some(i) } } None}
whitwulf
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) }; for num in start..=end { if num.to_string() == num.to_string().chars().rev().collect::<String>() { return Some(num); } } None}
maxvi
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 { let s = number.to_string(); if s == s.chars().rev().collect::<String>() { return Some(number); } } None}
senft
use std::cmp::{max, min};pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { for i in min(start, end)..=max(start, end) { let s = i.to_string(); if is_palindrome(&s) { return Some(i); } } None}fn is_palindrome(s: &str) -> bool { let len = s.len(); if len == 1 { return true; } for i in 0..len / 2 { let idx1 = i; let idx2 = len - i - 1; if s.chars().nth(idx1) != s.chars().nth(idx2) { return false; } } true}
senft
use std::cmp::{max, min};pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { for i in min(start, end)..=max(start, end) { let s = i.to_string(); println!("{}", s); if is_palindrome(&s) { return Some(i); } } None}fn is_palindrome(s: &str) -> bool { let len = s.len(); if len == 1 { return true; } println!("len {}", len); for i in 0..len / 2 { let idx1 = i; let idx2 = len - i - 1; println!("idx1 {}", idx1); println!("idx2 {}", idx2); println!("c1 {}", s.chars().nth(idx1).unwrap()); println!("c2 {}", s.chars().nth(idx2).unwrap()); if s.chars().nth(idx1) != s.chars().nth(idx2) { return false; } } true}
shinobu52
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let (start, end) = (std::cmp::min(start, end), std::cmp::max(start, end)); for num in start..=end { let num_str = num.to_string(); let num_chars: Vec<char> = num_str.chars().collect(); let mut flag = true; for i in 0..(num_str.len() / 2) { if num_chars[i] != num_chars[num_chars.len() - 1 - i] { flag = false; break; } } if flag { return Some(num); } } None}
igroomgrim
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { if start > end { return find_first_palindrome(end, start); } for num in start..=end { if is_palindrome(num) { return Some(num); } } None}fn is_palindrome(num: i32) -> bool { let str_num = num.to_string(); str_num == str_num.chars().rev().collect::<String>()}
majesticalcreature
use std::cmp::{min, max};pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here let (start, end) = (min(start, end), max(start, end)); 'numbers_iter: for n in start..=end { let digits = ((n as f64).log10() + 1.0).floor() as i32; for i in 1..(digits / 2) + 1 { if digit(n, i) != digit(n, digits + 1 - i) { continue 'numbers_iter; } } return Some(n); } None}fn digit(number: i32, digit: i32) -> u8 { ((number / pow10(digit - 1)) % 10) as u8}fn pow10(x: i32) -> i32 { let mut result = 1; for _ in 0..x { result *= 10 } result}
StimhackSoftware
fn check_palindrome(inp : i32) -> bool { let check1 = inp.to_string(); let check2 : String = inp.to_string().chars().into_iter().rev().collect(); check1 == check2}pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { if start > end { return find_first_palindrome(end, start) } for e in start..=end { if check_palindrome(e) { return Some(e) } } None}
armed
use std::cmp::{min, max};pub fn is_palindrome_number(mut number: i32) -> bool { if number < 0 { return false; } let original = number; let mut reversed = 0; while number > 0 { let digit = number % 10; reversed = reversed * 10 + digit; number /= 10; } original == reversed}pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let r = min(start, end)..=max(start,end); for x in r { if is_palindrome_number(x) { return Some(x); } } None}
its-me-sv
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here (start.min(end)..=end.max(start)).find(|i| { let no = i.to_string(); let rev_no = no.chars().rev().collect::<String>(); no == rev_no })}
its-me-sv
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here (start.min(end)..=end.max(start)).find(|i| { let no = i.to_string(); let rev_no = no.chars().rev().collect::<String>(); no == rev_no })}
its-me-sv
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here for i in start.min(end)..=end.max(start) { let no = i.to_string(); let rev_no = no.chars().rev().collect::<String>(); if no == rev_no { return Some(i); } } None}
andrey-yu
pub fn find_first_palindrome(mut start: i32, mut end: i32) -> Option<i32> { // TODO: Implement the function here println!("s: {} e: {}", start.to_string(), end.to_string()); if start > end { let end_t = start; start = end; end = end_t; } for n in start..=end { let mut s = Vec::<char>::new(); let mut nn = n; while nn > 0 { let d = ('0' as i32 + nn - nn / 10 * 10) as u8; s.push(d as char); nn = nn/10; } let len = s.len(); println!("s: {:?}", &s); for index in 0..=len / 2 { if index >= len / 2 { return Some(n); } else if s[index] != s[len - 1 - index] { break; } } } None}
raneid
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { for n in start.min(end)..=end.max(start) { let s = n.to_string(); if s == s.chars().rev().collect::<String>() { return Some(n); } } None}
sukman
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here let mut palindrome_range = match start <= end { true => start..=end, false => end..=start }; palindrome_range.find(|o| { o.to_string().chars().eq(o.to_string().chars().rev()) })}
uggla
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here let t = if start > end { (end, start) }else { (start, end) }; (t.0..=t.1).find(|o| { o.to_string().chars().eq(o.to_string().chars().rev()) }) }
ludovicknecht
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let palindrome_range = match start <= end { true => start..=end, false => end..=start }; 'outer: for number in palindrome_range { if number < 0 {continue;} let char_rep: Vec<char> = number.to_string() .chars() .collect(); for index in 0..char_rep.len()/2 { if char_rep[index] != char_rep[char_rep.len()-1-index] { continue 'outer; } } return Some(number); } None}
mrsalo
use std::cmp::Ordering;pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let (start, end) = match start.cmp(&end) { Ordering::Equal => (start, end), Ordering::Less => (start, end), Ordering::Greater => (end, start), }; for i in start..=end { let s = i.to_string(); let s_r = s.chars().rev().collect::<String>(); if s == s_r { return Some(i); } } None}
mrsalo
use std::cmp::Ordering;pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let (start, end) = match start.cmp(&end) { Ordering::Equal => (start, end), Ordering::Less => (start, end), Ordering::Greater => (end, start), }; for i in start..end + 1 { let s = i.to_string(); let s_r = s.chars().rev().collect::<String>(); if s == s_r { return Some(i); } } None}
XiaoPengYouCode
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here fn check_palindrome(number: i32) -> bool { let number_string = number.to_string(); let number_rev = number.to_string().chars().rev().collect::<String>(); println!("number_rev = {}", number_rev); number_string == number_rev } let range = start.min(end)..=start.max(end); range .filter(|&num| num >= 0) .find(|&num| check_palindrome(num))}
XiaoPengYouCode
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here fn check_palindrome(number: i32) -> Option<i32> { let number_string = number.to_string(); let number_rev = number.to_string().chars().rev().collect::<String>(); println!("number_rev = {}", number_rev); if number_string == number_rev { Some(number) } else { None } } let range = if start < end { start..=end } else { end..=start }; for number in range { println!("number = {}", number); match check_palindrome(number) { Some(number) => { return Some(number) }, None => continue } } None}
saukmn
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let range = if start < end { start..=end } else { end..=start }; 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}
wadjoh
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let range = if start < end { start..=end } else { end..=start }; for i in range { if i < 10 || i.to_string() == i.to_string().chars().rev().collect::<String>() { return Some(i); } } None}
kalley
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { // TODO: Implement the function here let mut range = if start < end { start..=end } else { end..=start }; range.find(|x| { let s = x.to_string(); let reversed: String = s.chars().rev().collect(); s.cmp(&reversed) == std::cmp::Ordering::Equal })}
mcgrizzz
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) }; for n in start..=end { let num_string = n.to_string(); let rev_string = num_string.chars().rev().collect::<String>(); if num_string.eq(&rev_string) { return Some(n); } } None}
daves003
fn is_palindrome(n: i32) -> bool { let s = n.to_string(); for (a, b) in s.chars().zip(s.chars().rev()) { if a != b { 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); } for n in start..=end { if is_palindrome(n) { return Some(n); } } None}
querywatson
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { let (a, b) = (start.min(end), start.max(end)); (a..=b) .filter(|&num| num >= 0) .find(|&num| is_palindrome(num))}fn is_palindrome(mut num: i32) -> bool { let original = num; let mut reversed = 0; while num > 0 { reversed = reversed * 10 + (num % 10); num /= 10; } original == reversed}