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
dawwestk
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}
BartMassey
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) }) }
kapaseker
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}
Marco-Normal
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}
rawar089
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}
vikingNR
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}
Algorab
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}
mshutov
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}
josschne
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()) })}
ahmetoozcan
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()) }), }}
Dominicm99
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}
tbali0524
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}
nedrw
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}
zahrajavar
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}
LauriSarap
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;}
aey4
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}
lumio
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}
aey4
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}
aey4
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}
aey4
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}
Yamami1221
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;}
caiocdcs
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)}
RonaldoArayadev
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()))}
hubbardip
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}
vgpetrov
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}
vikyw89
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;}
jon-a-miller
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()))}
JeppeFoldberg
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}
Brack0
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()) })}
GiulianoCTRL
// 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())}
chriswmann
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}
chriswmann
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}
chriswmann
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>()}
chriswmann
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}
pynhpo
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}
gardyna
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}
mgreve
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}
kobapi28
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}
javigar322
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}
martin-unit
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}
XtebanUy
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}
LenRemmerswaal
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}
LenRemmerswaal
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}
danc86
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}
r1kk1s
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}
kennethlove
pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> { if start == end { return Some(start); } let mut s: i32 = start; let mut e: i32 = end; if start > end { e = start; s = end; } for num in s..e { let num_str = num.to_string(); if num_str == num_str.chars().rev().collect::<String>() { return Some(num); } } None}
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}