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 rangeDid 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.chars in a String by using the chars() method on a Stringrev() method on an iterator. For example, you can get the chars in a String s in reverse order with s.chars().rev()pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> {    let is_palindrome = |v: i32| {        let s = format!("{}", v);        let s_rev: String = s.chars().rev().collect();        s == s_rev    };    let range = if start < end {        start..=end    } else {        end..=start    };    for i in range {        if i >= 0 && is_palindrome(i) {            return Some(i);        }    }    None}use core::ops::RangeInclusive;pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> {    let range: RangeInclusive<i32> = if end < start { end..=start } else { start..=end };    for num in range {        let num_string: String = num.to_string();        if num_string == num_string.chars().rev().collect::<String>(){            return Some(num);        }    }    None}pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> {    if start == end {        return Some(start);    }    let points = if start > end {        (end, start)    } else {        (start, end)    };    for i in points.0..points.1 {        if is_palindrome(i) {            return Some(i);        }    }    None}fn is_palindrome(num: i32) -> bool {    let str = num.to_string();    let mut points = (0, str.len() - 1);    while points.0 < points.1 {        if str.chars().nth(points.0).unwrap() != str.chars().nth(points.1).unwrap() {            return false;        }        points = (points.0 + 1, points.1 - 1);    }    true}use core::ops::RangeInclusive;pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> {    let range: RangeInclusive<i32> = if end < start { end..=start } else { start..=end };    for num in range {        let num_string: String = num.to_string();        if num_string == num_string.chars().rev().collect::<String>(){            return Some(num);        }    }    None}use std::cmp::{max, min};pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> {    let lower_val = min(start, end);    let upper_val = max(start, end);    for i in lower_val..=upper_val {        if i < 0 {            continue;        }        let mut original = i;        let mut reverted = 0;        while original > 0 {            let digit = original % 10;            original = original / 10;            reverted = (reverted * 10) + digit;        }        if i == reverted {            return Some(i);        }    }    None}pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> {    let lower_bound;    let upper_bound;    if start > end {        lower_bound = end;        upper_bound = start;    } else {        lower_bound = start;        upper_bound = end;    }    for i in lower_bound..=upper_bound {        if i < 0 {            continue;        }        let mut original = i;        let mut reverted = 0;        while original > 0 {            let digit = original % 10;            original = original / 10;            reverted = (reverted * 10) + digit;        }        if i == reverted {            return Some(i);        }    }    None}use::std::cmp;pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> {    let inner_start = cmp::min(start, end);    let inner_end = cmp::max(start, end);    for num in inner_start..=inner_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> {    // TODO: Implement the function here    for num in std::cmp::min(start, end)..=std::cmp::max(start, end) {        let num_string = num.to_string();        if num_string.chars().rev().collect::<String>() == num_string {            return Some(num);        }    }    None}pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> {    // TODO: Implement the function here    if start < 0 && end < 0{        return None;    } else{        if start < end{            for i in start..=end{                if is_palindrome(i){                    return Some(i);                }            }        }else{            for i in end..=start{                if is_palindrome(i){                    return Some(i);                }            }        }    }    None}fn is_palindrome(num : i32) -> bool{    if num < 0{        return false;    }    let num_str = num.to_string();    num_str.chars().eq(num_str.chars().rev())}pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> {    let min = match start.is_negative() {        true => 0,        false => std::cmp::min(start, end),    };    let max = std::cmp::max(start, end);    let is_palindrome = |no: &i32| -> bool {        let mut reversed = 0;        let mut n = no.clone();        while n != 0 {            reversed = reversed * 10 + n % 10;            n /= 10;        }        reversed == *no    };    (min..=max).find(is_palindrome)}use std::cmp::max;use std::cmp::min;fn is_palindrom(num : i32) -> bool {    let repr = num.to_string();    repr == repr.chars().rev().collect::<String>()}pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> {        for i in min(start, end)..=max(start, end) {        if is_palindrom(i) {            return Some(i);        }    }        None}use std::cmp::max;use std::cmp::min;fn is_palindrom(num : i32) -> bool {    let repr = num.to_string();    repr == repr.chars().rev().collect::<String>()}pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> {        for i in min(start, end)..=max(start, end) {        if is_palindrom(i) {            return Some(i);        }    }        None}pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> {    let mut start = start;    let mut end = end;    if start >= 0 && end >= 0 && start > end {        (start, end) = (end, start);    }    if start < 0 && end >= 0 {        return Some(0);    }    if start < 0 && end < 0 {        return None;    }    for i in start..=end {        let s = i.to_string();        let rev: String = s.chars().rev().collect();        if s == rev {            return Some(i);        }    }    None}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 buf = start;        start = end;        end = buf;    }    for i in start..=end {        if i >= 0 {            let digits = decimal_digits(i as u32);            if digits.iter().eq(digits.iter().rev()) {                return Some(i);            }        }    }    None}fn decimal_digits(mut num: u32) -> Vec<u32> {    let mut digits = Vec::new();        if num == 0 {        digits.push(0);        return digits;    }    while num > 0 {        digits.push(num % 10);        num /= 10;    }        digits.reverse();    digits}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 s > e {        s = s+e;        e = s-e;        s = s-e;    }    for i in s..=e {        let mut num = i;        let mut rev = 0;        while num > 0 {            rev = 10*rev + num%10;            num /= 10;        }        if rev as u32 == i as u32 {            return Some(i);        }    }    None}pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> {    let (start, end) = if start > end {        (end.max(0), start.max(0))    } else {        (start.max(0), end.max(0))    };    for n in start..=end {        let s1 = format!("{n}");        let s2 = format!("{}", s1.chars().rev().collect::<String>());        if s1 == s2 {            return Some(n);        }    }    None}pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> {    let mut s = start;    let mut e = end;    if start > end {        s = end;        e = start;    }    if e < 0 {        return None;    }    (s..=e).find(|x| {        let chars: Vec<char> = x.to_string().chars().collect();        let len = chars.len();        for i in 0..len / 2 {            if chars[i] != chars[len - i - 1] {                return false;            }        }        true    })}struct Digits {    n: usize,    divisor: usize,}impl Digits {    fn new(n: usize) -> Self {        let mut divisor = 1;        while n >= divisor * 10 {            divisor *= 10;        }        Digits {            n: n,            divisor: divisor,        }    }}impl Iterator for Digits {    type Item = u8;    fn next(&mut self) -> Option<Self::Item> {        if self.divisor == 0 {            None        } else {            let v = Some((self.n / self.divisor) as u8);            self.n %= self.divisor;            self.divisor /= 10;            v         }    }}fn palindrome(digits: Vec<u8>) -> bool {    let len = digits.len();    for i in 0..len / 2 {        if digits[i] != digits[len - 1 - i] {            return false;        }    }    true}pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> {    let mut n = start.max(0);    let step = if n <= end { 1 } else { -1 };    let mut p = None;    loop {        let digits: Vec<u8> = Digits::new(n as usize).collect();        if palindrome(digits) {            if n <= end {                return Some(n);            }             p = Some(n);        }        n += step;        if n == end { break; }    }    p}pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> {    let (low, high) = if start <= end { (start, end) } else { (end, start) };    for i in low.max(0)..=high {        let s = i.to_string();        if s.chars().eq(s.chars().rev()) {            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)};    for n in start..=end {        let n_string = n.to_string();        if n_string.chars().rev().collect::<String>() == n_string {            return Some(n);        }    }    None}use std::cmp::Ordering;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 };    range        .map(|number| number.to_string())        .find(|str| str.chars().cmp(str.chars().rev()) == Ordering::Equal)        .map(|val| Some(val.parse::<i32>().unwrap()))?}pub fn is_palindrome(num:i32) -> bool{    let string = num.abs().to_string();    let bytes = string.as_bytes();    let len:usize = string.len();    let mut i:usize = len as usize;    if len == 1{        return true;    }    while i > 0{        if bytes[i-1] != bytes[len-i]{            return false;        }        i=i-1;    }    return true;}pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> {    let (mut start,mut end) = (start,end);    if end < start{        (start,end) = (end,start);    }    if start < 0 {        start = 0;    }    while start <= end{        if is_palindrome(start){            return Some(start)        }        start=start+1;    }    return 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 end < start {        (start, end) = (end, start);    };    for index in start..=end{        if index<0 {            continue;        }        let s = index.to_string();        let rev_s: String = s.chars().rev().collect();        if s==rev_s{            return Some(index)        }    }    None}fn is_pal(n: i32) -> bool {    let s = format!("{n}").chars().collect::<Vec<_>>();    let mut r = s.clone();    r.reverse();    r == s}pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> {    // TODO: Implement the function here    let (a, b) = if end < start {        (end, start)    } else {        (start, end)    };    for n in a..=b {        if is_pal(n) {            return Some(n);        }    }    None}fn is_pal(n: i32) -> bool {    let s = format!("{n}").chars().collect::<Vec<_>>();    let mut r = s.clone();    r.reverse();    r == s}pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> {    // TODO: Implement the function here    let (a, b) = if end < start {        (end, start)    } else {        (start, end)    };    for n in a..=b {        if is_pal(n) {            return Some(n);        }    }    None}use std::mem::swap;pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> {    let mut l = start;    let mut r = end;        if start > end{        swap(&mut l, &mut r);    }    for num in l..=r{        let st_num = num.to_string();        for (idx, ch) in st_num.chars().rev().enumerate(){            if ch != st_num.chars().nth(idx).unwrap(){                break;            }            if idx + 1  == st_num.chars().count(){                return Some(num);            }        }    }    None}pub fn nbpos(num: i32) -> i8 {    let mut a = num;    let mut nbpos = 1;    while a >= 10 {        a = a/10;        nbpos += 1;    }    nbpos}pub fn is_palindrome(num: i32) -> bool {        let nbpos = nbpos(num);    if nbpos == 1 {        true    } else {        let middle_pos = nbpos/2;        let mut vec: Vec<i32> = vec![];        let mut a = num;        let mut pal = true;        for _ in 1..=middle_pos {            vec.push(a%10);            a /= 10;        }        if nbpos%2==1 {            a/=10;        }                while let Some(digit) = vec.pop() {            if digit != a%10 {                pal = false;                break;            }            a/=10;        }        pal    }}pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> {    if start<0 && end<0{        return None;    }    if start<0 || end <0{        return Some(0);    }    let (low, high) = if start <= end { (start, end) } else { (end, start) };    (low..=high).find(|num| is_palindrome(*num))}fn is_pal(n: i32) -> bool {    let s = format!("{n}").chars().collect::<Vec<_>>();    let mut r = s.clone();    r.reverse();    r == s}pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> {    // TODO: Implement the function here    let (a, b) = if end < start {        (end, start)    } else {        (start, end)    };    for n in a..=b {        if is_pal(n) {            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 number in start..=end {        let s = number.to_string();        if s == s.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 deb: i32;    let fin: i32;    if start > end {        deb = end;        fin = start;    } else {        deb = start;        fin = end;    }    for i in deb..=fin {        let chain = format!("{i}");          if chain == chain.chars().rev().collect::<String>() {            return Some(i);        }    }     None}pub fn is_pal (num :i32)->bool{    if num.to_string() == num.to_string().chars().rev().collect::<String>(){        return true;    }    false}pub fn find_first_palindrome(mut start: i32, mut end: i32) -> Option<i32> {      if end < start {        (start, end) = (end, start);    } // TODO: Implement the function here    if start<0 && end<0{        return None;    }    if start<0 || end <0{        return Some(0);    }         for i in start..=end{        if is_pal(i){            return Some(i);        }    }    None}fn is_palindrome(number: i32) -> bool {    let chars: Vec<char> = number.to_string().chars().collect();    let len = chars.len();    let mid = len / 2;    chars[..mid].iter().eq(chars[len - mid..].iter().rev())}pub fn find_first_palindrome(mut start: i32, mut end: i32) -> Option<i32> {    if end < start {        (start, end) = (end, start);    }    for i in start..=end {        if is_palindrome(i) {            return Some(i)        }    }    None}pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> {    let mut min_val = start.min(end);    let max_val = start.max(end);    min_val = min_val.max(0);    (min_val..=max_val).find(|i| is_palindrome(*i))}pub 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}pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> {    let min_val = start.min(end);    let max_val = start.max(end);    let search_start = min_val.max(0);    let search_end = max_val.max(0);        if search_end < 0 {        return None;    }    for num in search_start..=search_end {        if is_palindrome(num) {            return Some(num);        }    }    None}pub 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}pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> {    // TODO: Implement the function here    let mut start_corrected = start;    let mut end_corrected = end;    if start >= end {        start_corrected = end;        end_corrected = start;    }    for i in start_corrected..=end_corrected {        println!("NUMBER: {i}");        let num_str = i.to_string().chars().collect::<Vec<char>>();        for j in 0..num_str.len() {            let first = j;            let last = num_str.len() - j - 1;            if num_str[first] != num_str[last] {                println!("{} != {}", num_str[first], num_str[last]);                break;            }            println!("{} {}", num_str[first], num_str[last]);            return Some(i);        }    }    None}pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> {    let min_val = start.min(end);    let max_val = start.max(end);    let search_start = min_val.max(0);    let search_end = max_val.max(0);    // if range is entirely negative, no palindromes    if search_end < 0 {        return None;    }    for num in search_start..=search_end {        if is_palindrome(num) {            return Some(num);        }    }    None}pub 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}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 {        (start, end) = (end, start);    }    for num in start..=end {        if is_palindrome(num) {            return Some(num);        }    }    None}fn is_palindrome(number: i32) -> bool {    let mut num = number;    let mut rev = 0;    while num > 0 {        rev = rev * 10 + num % 10;        num /= 10;    }    rev == number}pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> {    let is_palindrom = |n| {        if n < 9 {            return true;        }        let mut rest = n;        let mut rev = 0;        while rest > 0 {            rev = rev * 10 + rest % 10;            rest /= 10;        }        n == rev    };    let start = 0.max(start);    let end = 0.max(end);    let range = if start <= end {        start..=end    } else {        end..=start    };    for n in range {        if is_palindrom(n) {            return Some(n);        }    }    None}pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> {    let (min, max) = if start <= end {        (start.max(0), end)    } else {        (end.max(0), start)    };    (min..=max).find(|&num| is_palindrome(num))}fn is_palindrome(mut value: i32) -> bool {    if value < 0 {        return false;    }    let original = value;    let mut reversed = 0;    while value > 0 {        reversed = reversed * 10 + value % 10;        value /= 10;    }    original == reversed}pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> {    let (min, max) = if start <= end {        (start.max(0), end)    } else {        (end.max(0), start)    };    (min..=max).find(|&num| is_palindrome(num))}fn is_palindrome(mut value: i32) -> bool {    if value < 0 {        return false;    }    let original = value;    let mut reversed = 0;    while value > 0 {        reversed = reversed * 10 + value % 10;        value /= 10;    }    original == reversed}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 end < start {        let t = end;        end = start;        start = t;    }    if start < 0 {        start = 0;    }    while start <= end {        let num = start.to_string();        if num.chars().zip(num.chars().rev()).all(|(x,y)| x == y) {            return Some(start);        }        start += 1;    }    None}pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> {    println!("start: {}, end: {}", start, end);    let mut s = start;    let mut e = end;    if start > end {        s = end;        e = start;    }    if start == end && start == 1 {        return Some(1);    }    for num in s..e {        let fwd = num.to_string();        let rev = num.to_string().chars().rev().collect::<String>();        if fwd == rev {            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 i in start..=end {        let num_str = i.to_string();        if num_str.chars().eq(num_str.chars().rev()) {            return Some(i)        }    }    None}pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> {        let (s,e) = if start > end {        (end, start)    } else {        (start, end)    };    for number in s..=e {        let number_str = number.to_string();        let chars = number_str.chars();        let mut chars_reversed = chars.clone().into_iter().collect::<Vec<char>>();        chars_reversed.reverse();        if chars.zip(chars_reversed).all(|(a,b)| a == b) {            return Some(number)        }    }    None}pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> {    let mut end_ = end;    let mut start_ = start;    if start > end {        let third = end;        end_ = start;        start_ = third;    }        for i in start_..=end_ {        let chars = i.to_string();        if chars.chars().eq(chars.chars().rev()) {            return Some(i);        }    }    None}fn is_palindrome(mut n: i32) -> bool {    if n < 0 {        return false;    }    let original = n;    let mut reversed = 0;    while n > 0 {        let digit = n % 10;        reversed = reversed * 10 + digit;        n /= 10;    }    original == reversed}pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> {    let a = start.min(end);    let b = start.max(end);    (a..=b).find(|&n| is_palindrome(n))}pub fn find_first_palindrome(start: i32, end: i32) -> Option<i32> {    let a = if start > end {end} else {start};    let b = if start > end {start} else {end};    (a..=b).into_iter().filter(|n| {        let a = n.to_string();        a == a.chars().rev().collect::<String>()    }).next()}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)    };    let mut i: i32 = start;    while i <= end {        let x: String = i.to_string();        let y: String = x.chars().rev().collect();        if x == y {            return Some(i);        }        i += 1;    }    None}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 i in start..=end {        let st1 = i.to_string();        let st2 : String = st1.chars().rev().collect();        if st1 == st2 {            return Some(i);        }    }    None}fn is_a_palindrome(i: i32) -> bool {    let s = format!("{i}");    let reversed_s = s.chars().rev().collect::<String>();    s == reversed_s}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 is_a_palindrome(i) {            return Some(i);        }    }    None}