In this challenge, you will create a function that determines the characteristics of a given number. Specifically, the function will evaluate whether a number is positive negative, or zero and also determine if it is even or odd.
Rust's powerful control flow mechanisms, including if
, else if
, and else
statements, make it easy to handle multiple conditions. By combining these conditions with logical operators, you can create a function that provides comprehensive information about the number.
You are given a function describe_number(n: i32) -> String
that takes an integer as input and returns a string description of the number's characteristics. The description should include whether the number is positive, negative, or zero and whether it is even or odd.
For example:
Complete the function by implementing the necessary conditions and logical checks.
let result = describe_number(10);
assert_eq!(result, "Positive even");
let result = describe_number(-3);
assert_eq!(result, "Negative odd");
let result = describe_number(0);
assert_eq!(result, "Zero");
%
can help determine if a number is even or odd.&&
and ||
will be useful for combining conditions.pub fn describe_number(n: i32) -> String { match n { 0 => String::from("Zero"), n if n > 0 && n % 2 == 0 => String::from("Positive even"), n if n > 0 && n % 2 != 0 => String::from("Positive odd"), n if n < 0 && n % 2 == 0 => String::from("Negative even"), n if n < 0 && n % 2 != 0 => String::from("Negative odd"), _ => panic!("unknown case") }}
pub fn describe_number(number: i32) -> String { if number != 0 && number % 2 ==0 { if number > 0 { "Positive even".to_string() }else{ "Negative even".to_string() } }else if number != 0 && number % 2 != 0 { if number > 0 { "Positive odd".to_string() }else{ "Negative odd".to_string() } }else{ "Zero".to_string() }}
pub fn describe_number(n: i32) -> String { const DESCRIPTION: [&str; 5] = ["Zero", "Negative odd", "Negative even", "Positive odd", "Positive even"]; let idx = (2 * (n > 0) as usize + ((n % 2) == 0) as usize + 1) * (n != 0) as usize; return DESCRIPTION[idx].to_string()}
const EVEN_STR: &'static str = "even";const ODD_STR: &'static str = "odd";const POSITIVE_STR: &'static str = "Positive";const NEGATIVE_STR: &'static str = "Negative";pub fn describe_number(n: i32) -> String { if n == 0 { return "Zero".into(); } let sign = if n > 0 { POSITIVE_STR } else { NEGATIVE_STR }; let parity = if n % 2 == 0 { EVEN_STR } else { ODD_STR }; format!("{sign} {parity}")}
pub fn describe_number(n: i32) -> String { if n == 0 { return String::from("Zero"); } let sign: &str = if n > 0 { "Positive" } else { "Negative" }; let div: &str = if n % 2 == 0 { "even" } else { "odd" }; return format!("{sign} {div}");}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n == 0 { return "Zero".to_string() } let even = n % 2 == 0; let positive = n > 0; if positive { if even { return "Positive even".to_string() } else { return "Positive odd".to_string() } } else { if even { return "Negative even".to_string() } else { return "Negative odd".to_string() } }}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n == 0 { "Zero".to_string() } else if n > 0 && n % 2 == 0 { "Positive even".to_string() } else if n > 0 && n % 2 != 0 { "Positive odd".to_string() } else if n < 0 && n % 2 == 0 { "Negative even".to_string() } else { "Negative odd".to_string() }}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n == 0 { "Zero".to_string() } else { let mut list = vec![]; if n < 0 { list.push("Negative") } else { list.push("Positive") } if n % 2 == 0 { list.push("even") } else { list.push("odd") } list.join(" ") }}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if is_zero(n) { return "Zero".to_string() } match (is_positive(n), is_even(n)) { (true, true) => "Positive even".to_string(), (true, false) => "Positive odd".to_string(), (false, true) => "Negative even".to_string(), (false, false) => "Negative odd".to_string(), }}fn is_positive(n: i32) -> bool { n > 0}fn is_even(n: i32) -> bool { n % 2 == 0}fn is_zero(n: i32) -> bool { n == 0}
pub fn describe_number(n: i32) -> String { if n == 0 { return "Zero".to_string(); } let mut result = if n.abs() == n { "Positive ".to_string() } else { "Negative ".to_string() }; if n % 2 == 0 { result.push_str("even"); } else { result.push_str("odd"); } result}
use std::cmp::Ordering;pub fn describe_number(n: i32) -> String { // TODO: Implement the function here let is_even = n & 1 == 0; match (n.cmp(&0), is_even) { (Ordering::Equal, _) => "Zero".to_string(), (Ordering::Greater, true) => "Positive even".to_string(), (Ordering::Greater, false) => "Positive odd".to_string(), (Ordering::Less, true) => "Negative even".to_string(), (Ordering::Less, false) => "Negative odd".to_string(), }}
pub fn describe_number(n: i32) -> String { if n == 0{return "Zero".to_string()} else {return String::from(if n > 0 {"Positive "} else {"Negative "}) + (if n % 2 == 0 {"even"} else {"odd"})}}
use std::cmp::Ordering;pub fn describe_number(n: i32) -> String { // TODO: Implement the function here return match n.cmp(&0) { Ordering::Less => match n % 2 { 0 => "Negative even", _ => "Negative odd", }, Ordering::Greater => match n % 2 { 0 => "Positive even", _ => "Positive odd", }, _ => "Zero" }.to_string();}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here // Check a tuple of (is_zero, is_positive, is_even) match (n==0, n>0, n%2==0) { (true, _, _) => return String::from("Zero"), (_, true, true) => return String::from("Positive even"), (_, true, false) => return String::from("Positive odd"), (_, false, true) => return String::from("Negative even"), (_, false, false) => return String::from("Negative odd"), }}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here match n { n if n > 0 && n % 2 == 0 => "Positive even".to_string(), n if n > 0 && n % 2 == 1 => "Positive odd".to_string(), n if n < 0 && n % 2 == 0 => "Negative even".to_string(), n if n < 0 && n % 2 == -1 => "Negative odd".to_string(), _ => "Zero".to_string() }}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here match n { n if n > 0 => String::from(format!("Positive {}", if n % 2 == 0{"even"} else {"odd"})), n if n < 0 => String::from(format!("Negative {}", if n % 2 == 0{"even"} else {"odd"})), _ => String::from("Zero"), }}
pub fn describe_number(n: i32) -> String { let even: bool = n % 2 == 0; if n > 0 { "Positive ".to_string() + if even {"even"} else {"odd"} } else if n < 0 { "Negative ".to_string() + if even {"even"} else {"odd"} } else { "Zero".to_string() } // TODO: Implement the function here}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here match n { 0 => "Zero".to_string(), w if w > 0 => { if w % 2 == 0 { "Positive even".to_string() } else { "Positive odd".to_string() } }, w if w < 0 => { if w % 2 == 0 { "Negative even".to_string() } else { "Negative odd".to_string() } }, _ => "no reach".to_string(), }}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n > 0{ if n % 2 == 0 { return String::from("Positive even"); } else { return String::from("Positive odd"); } } else if n < 0 { if n % 2 == 0 { return String::from("Negative even"); } else { return String::from("Negative odd"); } } else { return String::from("Zero"); }}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n == 0 { return String::from("Zero") } let mut returnstr = String::new(); if n > 0 { returnstr.push_str("Positive "); } else { returnstr.push_str("Negative "); } if n % 2 == 0 { returnstr.push_str("even"); } else { returnstr.push_str("odd"); } returnstr}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n > 0 { if n % 2 == 0 { String::from("Positive even") } else { String::from("Positive odd") } } else if n < 0 { if (-1*n) % 2 == 0 { String::from("Negative even") } else { String::from("Negative odd") } } else { String::from("Zero") }}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here match n { n if n > 0 => String::from(format!("Positive {}", if n % 2 == 0{"even"} else {"odd"})), n if n < 0 => String::from(format!("Negative {}", if n % 2 == 0{"even"} else {"odd"})), _ => String::from("Zero"), }}
pub fn describe_number(n: i32) -> String { if n > 0{ if n % 2 == 0 { return String::from("Positive even"); } else { return String::from("Positive odd"); } } else if n < 0 { if n % 2 == 0 { return String::from("Negative even"); } else { return String::from("Negative odd"); } } else { return String::from("Zero"); } }
pub fn describe_number(n: i32) -> String { if n == 0 { return "Zero".into(); } let s1 = if n < 0 { "Negative" } else { "Positive" }; let s2 = if n % 2 == 0 { "even" } else { "odd" }; format!("{s1} {s2}")}
pub fn describe_number(n: i32) -> String { if n == 0 { return "Zero".into(); } let s1 = if n < 0 { "Negative" } else { "Positive" }; let s2 = if n % 2 == 0 { "even" } else { "odd" }; format!("{s1} {s2}")}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here match (n, n < 0, n % 2 == 0) { (0, _, _) => "Zero".to_string(), (_, false, true) => "Positive even".to_string(), (_, false, false) => "Positive odd".to_string(), (_, true, true) => "Negative even".to_string(), (_, true, false) => "Negative odd".to_string(), }}
pub fn describe_number(n: i32) -> String { if n == 0 { return String::from("Zero"); } let sign = match n >> 31 { 0 => "Positive", -1 => "Negative", _ => unreachable!(), }; let odd_or_even = match n & 1 { 0 => "even", 1 => "odd", _ => unreachable!(), }; format!("{} {}", sign, odd_or_even)}
pub fn describe_number(n: i32) -> String { let sign = if n > 0 { "Positive"} else if n < 0 { "Negative" } else { "Zero" }; let odd_or_even = if n == 0 { "" } else if n % 2 != 0 { " odd" } else { " even" }; format!("{}{}", sign, odd_or_even)}
pub fn describe_number(n: i32) -> String { match n { x if x > 0 && x % 2 == 0 => String::from("Positive even"), x if x < 0 && x % 2 == 0 => String::from("Negative even"), x if x > 0 && x % 2 != 0 => String::from("Positive odd"), x if x < 0 && x % 2 != 0 => String::from("Negative odd"), _ => String::from("Zero"), }}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n == 0 { return "Zero".to_string(); } else if n > 0 { if n % 2 != 0 { return "Positive odd".to_string(); } else { return "Positive even".to_string(); } } else { if n % 2 != 0 { return "Negative odd".to_string(); } else { return "Negative even".to_string(); } }}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n == 0 { return "Zero".to_string(); } else if n > 0 { if n % 2 != 0 { return "Positive odd".to_string(); } else { return "Positive even".to_string(); } } else { if n % 2 != 0 { return "Negative odd".to_string(); } else { return "Negative even".to_string(); } }}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n == 0 { "Zero".to_string() } else { match (n > 0, n % 2) { (true, 0) => "Positive even".to_string(), (true, _) => "Positive odd".to_string(), (false, 0) => "Negative even".to_string(), (false, _) => "Negative odd".to_string(), } }}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n == 0 { return "Zero".to_string(); } match n > 0 { true => match n % 2 == 0 { true => "Positive even".to_string(), false => "Positive odd".to_string(), }, false => match n % 2 == 0 { true => "Negative even".to_string(), false => "Negative odd".to_string(), } }}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n == 0 { return String::from("Zero"); } else if n % 2 == 0 { if n > 0 { return String::from("Positive even"); } return String::from("Negative even"); } if n > 0 { return String::from("Positive odd"); } return String::from("Negative odd");}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n == 0 { return String::from("Zero"); } let mut s = String::from(""); if n > 0 { s.push_str("Positive"); } else { s.push_str("Negative"); } if n % 2 == 0 { s.push_str(" even"); } else { s.push_str(" odd"); } return s;}
pub fn describe_number(n: i32) -> String { if n == 0 { "Zero".to_string() } else if n > 0 { if n % 2 == 0 { "Positive even".to_string() } else { "Positive odd".to_string() } } else { if n % 2 == 0 { "Negative even".to_string() } else { "Negative odd".to_string() } }}
pub fn describe_number(n: i32) -> String { if n == 0 { return "Zero".to_string(); } let sign = if n > 0 { "Positive" } else { "Negative" }; let pair = if n % 2 == 0 { "even" } else { "odd" }; format!("{} {}", sign, pair)}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n == 0 { return "Zero".to_string() } match (n>0, n%2) { (true, 0) => "Positive even".to_string(), (true, 1) => "Positive odd".to_string(), (false, 0) => "Negative even".to_string(), (false, -1) => "Negative odd".to_string(), _ => "Invalid".to_string() }}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n > 0 { if n % 2 == 0 { return String::from("Positive even") } else { return String::from("Positive odd") } } if n < 0 { if n % 2 == 0 { return String::from("Negative even") } else { return String::from("Negative odd") } } String::from("Zero")}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n == 0 { return String::from("Zero"); } else if n < 0 { if n % 2 == 0 { return String::from("Negative even"); } return String::from("Negative odd"); } else { if n % 2 == 0 { return String::from("Positive even"); } return String::from("Positive odd"); }}
pub fn describe_number(n: i32) -> String { if n == 0 { "Zero".into() } else if n > 0 { if n % 2 == 0 { "Positive even".into() } else { "Positive odd".into() } } else { if n % 2 == 0 { "Negative even".into() } else { "Negative odd".into() } }}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here match n { n if n > 0 && n % 2 == 0 => "Positive even".into(), n if n > 0 && n % 2 != 0 => "Positive odd".into(), n if n < 0 && n % 2 == 0 => "Negative even".into(), n if n < 0 && n % 2 != 0 => "Negative odd".into(), _ => "Zero".into(), }}
pub fn describe_number(n: i32) -> String { match (0 == n, n < 0, 0 == n % 2) { (true, _, _) => "Zero".to_string(), (_, true, false) => "Negative odd".to_string(), (_, true, true) => "Negative even".to_string(), (_, false, false) => "Positive odd".to_string(), (_, false, true) => "Positive even".to_string() }}
pub fn describe_number(n: i32) -> String { match n { n if n > 0 => { if even(n) {return String::from("Positive even");} return String::from("Positive odd"); }, n if n < 0 => { if even(n) {return String::from("Negative even");} return String::from("Negative odd"); }, _ => String::from("Zero"), }} fn even(n: i32) -> bool { if n % 2 == 0 { return true; } false }
pub fn describe_number(n: i32) -> String { match n { n if n > 0 => { if even(n) {return String::from("Positive even");} return String::from("Positive odd"); }, n if n < 0 => { if even(n) {return String::from("Negative even");} return String::from("Negative odd"); }, _ => String::from("Zero"), }} fn even(n: i32) -> bool { if n % 2 == 0 { return true; } false }
pub fn describe_number(n: i32) -> String { match n { 0 => String::from("Zero"), n if n % 2 == 0 => { if n > 0 {return String::from("Positive even");} return String::from("Negative even"); }, _ => { if n > 0 {return String::from("Positive odd");} return String::from("Negative odd"); } }}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here match n { n if n > 0 => String::from(format!("Positive {}", if even_or_odd(n){"even"} else {"odd"})), n if n < 0 => String::from(format!("Negative {}", if even_or_odd(n){"even"} else {"odd"})), _ => String::from("Zero"), }}fn even_or_odd(number : i32)-> bool { if number % 2 == 0 { return true } false }
pub fn describe_number(n: i32) -> String { if n>0 { if n % 2 == 0 { "Positive even".to_string() } else { "Positive odd".to_string() } } else if n<0 { if n % 2 == 0 { "Negative even".to_string() } else { "Negative odd".to_string() } } else { "Zero".to_string() }}
pub fn describe_number(n: i32) -> String { match (0 == n, n < 0, 0 == n % 2) { (true, _, _) => "Zero".to_string(), (_, true, false) => "Negative odd".to_string(), (_, true, true) => "Negative even".to_string(), (_, false, false) => "Positive odd".to_string(), (_, false, true) => "Positive even".to_string() }}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here match n { x if x > 0 && x % 2 == 0 => "Positive even".to_owned(), x if x > 0 && x % 2 != 0 => "Positive odd".to_owned(), x if x < 0 && x % 2 == 0 => "Negative even".to_owned(), x if x < 0 && x % 2 != 0 => "Negative odd".to_owned(), _ => "Zero".to_owned(), }}