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 (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(), }}
pub fn describe_number(n: i32) -> String { let mut res = String::new(); if n == 0 { return "Zero".to_string(); } if n > 0 { res.push_str("Positive"); } else { res.push_str("Negative"); } if n % 2 == 0 { res.push_str(" even"); } else { res.push_str(" odd"); } res}
pub fn describe_number(n: i32) -> String { let mut res = String::new(); if n == 0 { return "Zero".to_string(); } if n > 0 { res.push_str("Positive"); } else { res.push_str("Negative"); } if n % 2 == 0 { res.push_str(" even"); } else { res.push_str(" odd"); } res}
pub fn describe_number(n: i32) -> String { if n == 0 { return String::from("Zero"); } let parity = if n > 0 { "Positive" } else { "Negative" }; let evenness = match n % 2 { 0 => "even", _ => "odd" }; format!("{parity} {evenness}").into()}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here match n { ..=-1 => format!("Negative {}", if n % 2 == 0 { "even" } else{ "odd" }), 0 => "Zero".to_string(), 1.. => format!("Positive {}", if n % 2 == 0 { "even" } else{ "odd" }) }}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here match n{ ..=-1 => format!("Negative {}",even_or_odd(n)), 0 => String::from("Zero"), 1.. => format!("Positive {}",even_or_odd(n)), }}pub fn even_or_odd(n: i32) -> String{ match n % 2 == 0 { false => String::from("odd"), true => String::from("even"), }}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n == 0 { return "Zero".to_string(); } else { match (n > 0, n%2==0) { (true, true) => "Positive even".to_string(), (true, false) => "Positive odd".to_string(), (false, true) => "Negative even".to_string(), (false, false) => "Negative odd".to_string(), } }}
pub fn describe_number(n: i32) -> String { let even = n % 2 == 0; let positive = n > 0; let zero = n == 0; if positive && even { "Positive even".to_string() } else if positive && !even { "Positive odd".to_string() } else if !positive && even && !zero { "Negative even".to_string() } else if !positive && !even { "Negative odd".to_string() } else { "Zero".to_string() }}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n == 0 { "Zero".to_string() } else if n > 0 { if n & 1 == 1 { "Positive odd".to_string() } else { "Positive even".to_string() } } else { if n & 1 == 1 { "Negative odd".to_string() } else { "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 even".to_string(); } return "Positive odd".to_string(); } else { if n % 2 == 0 { return "Negative even".to_string(); } return "Negative odd".to_string(); }}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here let mut res = String::new(); match n { _ if n > 0 => res.push_str("Positive "), _ if n < 0 => res.push_str("Negative "), _ if n == 0 => res.push_str("Zero"), _ => (), } if n != 0 { match n % 2 { 0 => res.push_str("even"), _ => res.push_str("odd"), } } res}
pub fn describe_number(n: i32) -> String { if n == 0 { return "Zero".to_string(); } else if n > 0 && n % 2 == 0 { return "Positive even".to_string(); } else if n > 0 && n % 2 != 0 { return "Positive odd".to_string(); } else if n < 0 && n % 2 == 0 { return "Negative even".to_string(); } else { return "Negative odd".to_string(); } }
pub fn describe_number(n: i32) -> String { if n == 0 { "Zero".to_string() } else { match (n > 0, n % 2 == 0) { (true, true) => "Positive even".to_string(), (true, false) => "Positive odd".to_string(), (false, true) => "Negative even".to_string(), (false, false) => "Negative odd".to_string(), } }}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n == 0 { return "Zero".to_string() } let posneg = if n > 0 { "Positive" } else { "Negative" }; let evenoddd = if n % 2 == 0 { "even" } else { "odd" }; [ posneg, evenoddd, ].join(" ")}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n == 0 { "Zero".to_string() } else if n > 0 { if n & 1 == 1 { "Positive odd".to_string() } else { "Positive even".to_string() } } else { if n & 1 == 1 { "Negative odd".to_string() } else { "Negative even".to_string() } }}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here 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 { // TODO: Implement the function here if n == 0 { return "Zero".to_string(); } let even = n % 2 == 0; let positive = n > 0; match (even, positive) { (true, true) => "Positive even".to_string(), (true, false) => "Negative even".to_string(), (false, true) => "Positive odd".to_string(), (false, false) => "Negative odd".to_string(), }}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n == 0 { "Zero" } else if n > 0 { if n % 2 == 0 { "Positive even" } else { "Positive odd" } } else { if n % 2 == 0 { "Negative even" } else { "Negative odd" } }.to_string()}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here let mut res = String::new(); match n { 0 => res.push_str("Zero"), 1..=i32::MAX => res.push_str("Positive "), i32::MIN..=-1 => res.push_str("Negative "), } if n != 0 { match n % 2 { 0 => res.push_str("even"), _ => res.push_str("odd"), } } res}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here let mut res = String::new(); match n { _ if n > 0 => res.push_str("Positive "), _ if n < 0 => res.push_str("Negative "), _ if n == 0 => res.push_str("Zero"), _ => (), } if n != 0 { match n % 2 { 0 => res.push_str("even"), _ => res.push_str("odd"), } } res}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here let mut s = match n { 0 => "Zero".to_string(), 0..=i32::MAX => "Positive".to_string(), i32::MIN..=-1 => "Negative".to_string(), }; if &n % 2 != 0 { s = s + " odd" } else if n != 0 { s = s + " even"} s}
pub fn describe_number(n: i32) -> String { if n == 0 {return "Zero".to_string();} if n > 0 { if n % 2 == 0 {return "Positive even".to_string();} else {return "Positive odd".to_string();} } else { if n % 2 == 0 {return "Negative even".to_string();} else {return "Negative odd".to_string();} }}
pub fn describe_number(n: i32) -> String { if n == 0 {return "Zero".to_string();} let even_or_odd: &str; if n % 2 == 0 {even_or_odd = "even";} else {even_or_odd = "odd";} let sign: &str; if n > 0 {sign = "Positive";} else {sign = "Negative";} format!("{sign} {even_or_odd}")}
pub fn describe_number(n: i32) -> String { if n == 0 {return "Zero".to_string();} let mut output = String::new(); if n > 0 { output.push_str("Positive "); } else { output.push_str("Negative "); } if n % 2 == 0 { output.push_str("even"); } else { output.push_str("odd"); } return output;}
pub fn describe_number(n: i32) -> String { match (n == 0, n.is_positive(), n % 2 == 0){ (true, _, _) => "Zero".to_string(), (false, true, true) => "Positive even".to_string(), (false, true, false) => "Positive odd".to_string(), (false, false, true) => "Negative even".to_string(), (false, false, false) => "Negative odd".to_string() } // TODO: Implement the function here}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here match n { i if i == 0 => "Zero", i if i > 0 && i % 2 == 0 => "Positive even", i if i > 0 && i % 2 != 0 => "Positive odd", i if i < 0 && i % 2 == 0 => "Negative even", i if i < 0 && i % 2 != 0 => "Negative odd", _ => "", }.to_string()}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n == 0 { return "Zero".to_string(); } if n > 0 { if n % 2 == 0 { return "Positive even".to_string(); } else { return "Positive odd".to_string(); } } else { if n % 2 == 0 { return "Negative even".to_string(); } else { return "Negative odd".to_string(); } }}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here let is_positive = n > 0; let is_even = n % 2 == 0; let is_zero = n == 0; if is_zero { return "Zero".to_string(); } if is_positive { if is_even { return "Positive even".to_string(); } else { return "Positive odd".to_string(); } } else { if is_even { return "Negative even".to_string(); } else { return "Negative odd".to_string(); } }}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here let (sign,even,zero) = (n > 0, n % 2 == 0, n == 0); let mut r = String::from("Zero"); if !zero { if sign { r=String::from("Positive"); } else { r=String::from("Negative"); } if even { r+=" even"; }else{ r+=" odd"; } } r}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n == 0 { return "Zero".to_string()} match ( n > 0, n % 2 == 0) { (true, true) => "Positive even".to_string(), (true, false) => "Positive odd".to_string(), (false, true) => "Negative even".to_string(), (false, false) => "Negative odd".to_string(), }}
pub fn describe_number(n: i32) -> String { 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 if n < 0 && n % 2 != 0 { "Negative odd".to_string() } else { "Zero".to_string() }}
pub fn describe_number(n: i32) -> String { if n == 0 { return "Zero".to_string() } let parity = if n % 2 == 0 { "even" } else { "odd" }; let sign = if n > 0 { "Positive" } else { "Negative" }; format!("{} {}", sign, parity)}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n==0{ return "Zero".to_string(); } match (n>0 , n%2 == 0) { (true, false)=> "Positive odd".to_string(), (true, true) => "Positive even".to_string(), (false, true) => "Negative even".to_string(), (false, false) => "Negative odd".to_string(), }}
pub fn describe_number(n: i32) -> String { if n == 0 { return String::from("Zero")} let mut result: String = String::from(""); if n < 0 { result += "Negative"; } else { result += "Positive"; } match n % 2 == 0 { true => result += " even", false => result += " odd" } result}
pub fn describe_number(n: i32) -> String { match (n, n % 2 == 0) { (0, _) => "Zero".to_string(), (x, true) if x > 0 => "Positive even".to_string(), (x, false) if x > 0 => "Positive odd".to_string(), (_, true) => "Negative even".to_string(), (_, false) => "Negative odd".to_string(), }}
pub fn describe_number(n: i32) -> String { if n == 0 { return String::from("Zero"); } if n > 0 { if n % 2 == 0 { return String::from("Positive even"); } else { return String::from("Positive odd"); } } else { if n % 2 == 0 { return String::from("Negative even"); } else { return String::from("Negative odd"); } }}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here match n { ..=-1 => format!("Negative {}", if n % 2 == 0 { "even" } else{ "odd" }), 0 => "Zero".to_string(), 1.. => format!("Positive {}", if n % 2 == 0 { "even" } else{ "odd" }) }}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here match n { n if n > 0 => if n % 2 == 0 {return "Positive even".to_string();} else {return "Positive odd".to_string();}, n if n < 0 => if n % 2 == 0 {return "Negative even".to_string();} else {return "Negative odd".to_string();}, _ => return "Zero".to_string() }}
pub fn describe_number(n: i32) -> String { match n { n if n > 0 => if n % 2 == 0 {return "Positive even".to_string();} else {return "Positive odd".to_string();}, n if n < 0 => if n % 2 == 0 {return "Negative even".to_string();} else {return "Negative odd".to_string();}, _ => return "Zero".to_string() }}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n == 0 { return "Zero".to_string(); } return match (n > 0, n % 2 == 0){ (true, true) => "Positive even".to_string(), (true, false) => "Positive odd".to_string(), (false, true) => "Negative even".to_string(), (false, false) => "Negative odd".to_string() }}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n == 0 { return "Zero".to_string() } let mut string: String = "".to_string(); if n > 0 { string.push_str("Positive") } else { string.push_str("Negative") } if n % 2 == 0 { string.push_str(" even") } else { string.push_str(" odd") } return string.to_string()}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n == 0 { return "Zero".to_string(); } let is_positive = n > 0; let is_even: bool = n % 2 == 0; if is_even && is_positive { return "Positive even".to_string(); } else if is_even && !is_positive { return "Negative even".to_string(); } else if !is_even && is_positive { return "Positive odd".to_string(); } else { return "Negative odd".to_string(); }}
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n == 0 { return "Zero".to_string(); } if n > 0 { if n % 2 == 0 { return "Positive even".to_string(); } else { return "Positive odd".to_string(); } } else { if n % 2 == 0 { return "Negative even".to_string(); } else { return "Negative odd".to_string(); } }}