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.
%
can help determine if a number is even or odd.&&
and ||
will be useful for combining conditions.kylenoteboom
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(); } return "Positive odd".to_string(); } if n % 2 == 0 { return "Negative even".to_string(); } return "Negative odd".to_string()}
martin-unit
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here let mut posneg:bool = true; let mut evenodd:bool = true; if n == 0 { return "Zero".to_string(); } if n < 0 { posneg = false; } if n % 2 != 0 { evenodd = false; } if posneg && evenodd { return "Positive even".to_string(); } else if !posneg && evenodd { return "Negative even".to_string(); } else if posneg && !evenodd { return "Positive odd".to_string(); } else { return "Negative odd".to_string(); }}
martin-unit
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here let mut posneg:bool = true; let mut evenodd:bool = true; if n == 0 { return "Zero".to_string(); } if n < 0 { posneg = false; } if n % 2 != 0 { evenodd = false; } if posneg && evenodd { return "Positive even".to_string(); } else if !posneg && evenodd { return "Negative even".to_string(); } else if posneg && !evenodd { return "Positive odd".to_string(); } else { return "Negative odd".to_string(); }}
jimlawton
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 n % 2 == 0 { String::from("Negative even") } else { String::from("Negative odd") } } else { String::from("Zero") }}
XtebanUy
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here (if n == 0 { "Zero" } else { match (n % 2 == 0, n > 0) { (true, true) => "Positive even", (false, true) => "Positive odd", (true, false) => "Negative even", (false, false) => "Negative odd" } }).to_string() }
XtebanUy
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n == 0 { "Zero".to_string() } else { match (n % 2 == 0, n > 0) { (true, true) => "Positive even".to_string(), (false, true) => "Positive odd".to_string(), (true, false) => "Negative even".to_string(), (false, false) => "Negative odd".to_string() } } }
mbergkvist
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here let s = if n > 0 { if n % 2 == 0 { "Positive even" } else { "Positive odd" } } else if n < 0 { if n % 2 == 0 { "Negative even" } else { "Negative odd" } } else { "Zero" }; String::from(s)}
DivineGod
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here let mut result = String::new(); match n { 0 => return "Zero".to_string(), n if n > 0 => result.push_str("Positive "), _ => result.push_str("Negative "), } match n { n if n % 2 == 0 => result.push_str("even"), _ => result.push_str("odd"), } result}
mehdihmr
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n == 0 { return "Zero".to_string(); } let mut result = String::new(); if n > 0{ result.push_str("Positive "); } else { result.push_str("Negative "); } if n % 2 == 0{ result.push_str("even"); } else { result.push_str("odd"); } return result;}
StimhackSoftware
pub fn describe_number(n: i32) -> String { let res = if n > 0 && n % 2 == 0 { "Positive even" } else if n > 0 { "Positive odd" } else if n == 0 { "Zero" } else if n % 2 == 0 { "Negative even" } else { "Negative odd" }; res.to_string()}
Algorab
use std::cmp::Ordering;pub fn describe_number(n: i32) -> String { match n.cmp(&0) { Ordering::Less => { if n % 2 == 0 { "Negative even".to_string() } else { "Negative odd".to_string() } } Ordering::Equal => "Zero".to_string(), Ordering::Greater => { if n % 2 == 0 { "Positive even".to_string() } else { "Positive odd".to_string() } } }}
sroas
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here let even = n % 2 == 0; let positive = n > 0; if n == 0 { "Zero".to_string() } else if positive && even { "Positive even".to_string() } else if positive && !even { "Positive odd".to_string() } else if !positive && even { "Negative even".to_string() } else { "Negative odd".to_string() }}
jw
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() } } }}
xiuchiliudu
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here let mut result = String::new(); if n == 0 { return "Zero".to_string(); } if n > 0 { result.push_str("Positive "); } else { result.push_str("Negative "); } if n % 2 == 0 { result.push_str("even"); } else { result.push_str("odd"); } result}
yoakemae
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 "Negative even".to_string(); } else { return "Negative odd".to_string(); } } else { if n % 2 == 0 { return "Positive even".to_string(); } else { return "Positive odd".to_string(); } }}
qiyuan711
fn positive_or_negative(n: i32) -> &'static str { if n > 0 { "Positive" } else { "Negative" }}fn even_or_odd(n: i32) -> &'static str { if n % 2 == 0 { "even" } else { "odd" }}pub fn describe_number(n: i32) -> String { match n { 0 => "Zero".to_string(), _ => format!("{} {}", positive_or_negative(n), even_or_odd(n)) }}
qiyuan711
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() } } }
nt2311-vn
pub fn describe_number(n: i32) -> String { if n == 0 { return String::from("Zero") } match n > 0 { true => { if n % 2 == 0 { String::from("Positive even") } else { String::from("Positive odd") } }, false => { if n % 2 == 0 { String::from("Negative even") } else { String::from("Negative odd") } } } }
tinthid
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n > 0 && n % 2 == 0 { return String::from("Positive even"); } if n > 0 && n % 2 == 1 { return String::from("Positive odd"); } if n < 0 && n % 2 == 0 { return String::from("Negative even"); } if n < 0 && n % 2 == -1 { return String::from("Negative odd"); } String::from("Zero")}
devarajang
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here let mut character = String::new(); if n == 0 { character.push_str("Zero"); } else { if n < 0 { character.push_str("Negative "); } else { character.push_str("Positive "); } if n % 2 == 0 { character.push_str("even"); } else { character.push_str("odd"); } } return character;}
maxemontio
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"); } }}
BaroqueEngine
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here match (n.signum(), n % 2 == 0) { (1, true) => "Positive even".to_string(), (1, false) => "Positive odd".to_string(), (-1, true) => "Negative even".to_string(), (-1, false) => "Negative odd".to_string(), _ => "Zero".to_string(), }}
jon-a-miller
pub fn describe_number(n: i32) -> String { if n == 0 { return "Zero".to_string() } let mut result: String ; if n.is_positive() { result = "Positive ".to_string() ; } else if n.is_negative() { result = "Negative ".to_string() ; } else { result = "Zero ".to_string() ; } if n.abs()%2 == 0 { result.push_str("even") ; } else { result.push_str("odd") ; } result}
DV-13
pub fn describe_number(n: i32) -> String { if n == 0 { return "Zero".to_string() } let positivity = if n > 0 { "Positive" } else { "Negative" }; let oddity = if n % 2 == 0 { "even" } else { "odd" }; format!("{} {}", positivity, oddity).to_string()}
gtowers-dukosi
pub fn describe_number(n: i32) -> String { match n { 0 => "Zero".to_string(), x if x > 0 && x % 2 == 0 => "Positive even".to_string(), x if x > 0 && x % 2 != 0 => "Positive odd".to_string(), x if x < 0 && x % 2 == 0 => "Negative even".to_string(), x if x < 0 && x % 2 != 0 => "Negative odd".to_string(), _ => "Undefined".to_string(), }}
nichideropa
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here let sign = ["Positive", "Negative", "Zero"]; let divisible = ["odd", "even"]; let sign = match n { 0 => "Zero".to_string(), _ => if n < 0 { "Negative".to_string() } else { "Positive".to_string() } }; let odd_even = match n { 0 => "".to_string(), _ => if n % 2 == 0 { " even".to_string() } else { " odd".to_string() } }; sign + &odd_even[0..odd_even.len()]}
Thymelizabeth
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here use std::cmp::Ordering; String::from(match (n.cmp(&0), n % 2) { (Ordering::Greater, 0) => "Positive even", (Ordering::Greater, _) => "Positive odd", (Ordering::Less, 0) => "Negative even", (Ordering::Less, _) => "Negative odd", (Ordering::Equal, _) => "Zero", })}
pabiadzinski
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here 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() }}
LauriSarap
pub fn describe_number(n: i32) -> String { match n { 0 => "Zero".to_string(), x if x > 0 && x % 2 == 0 => "Positive even".to_string(), x if x > 0 && x % 2 != 0 => "Positive odd".to_string(), x if x < 0 && x % 2 == 0 => "Negative even".to_string(), x if x < 0 && x % 2 != 0 => "Negative odd".to_string(), _ => "Undefined".to_string() }}
tamanishi
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n == 0 { "Zero".to_string() } else if n % 2 == 0 && n > 0 { "Positive even".to_string() } else if n % 2 == 0 && n < 0 { "Negative even".to_string() } else if n % 2 != 0 && n > 0 { "Positive odd".to_string() } else if n % 2 != 0 && n < 0 { "Negative odd".to_string() } else { "unreachable".to_string() }}
konishu
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n > 0 { if !is_odd(n) { return "Positive even".to_string() } else { return "Positive odd".to_string() } } else if n < 0 { if !is_odd(n) { return "Negative even".to_string() } else { return "Negative odd".to_string() } } else { return "Zero".to_string() }}pub fn is_odd(n: i32) -> bool { if n % 2 == 0 { return false } true}pub fn is_prime(n: i32) -> bool { let mut n = n; if n < 0 { n *= -1 } if n == 1 { return false; // 1は素数ではない } if n == 2 { return true; // 2は素数 } if n % 2 == 0 { return false; // 偶数は素数ではない } let sqrt_n = (n as f64).sqrt() as i32; for i in (3..=sqrt_n).step_by(2) { // 3から奇数のみチェック if n % i == 0 { return false; // 割り切れるなら素数ではない } } true // 素数}
aynugek
pub fn describe_number(n: i32) -> String { match n { ..=-1 => format!("Negative {}", even_or_odd(n)), 0 => String::from("Zero"), 1.. => format!("Positive {}", even_or_odd(n)), }}fn even_or_odd(n: i32) -> &'static str { if n % 2 == 0 { "even" } else { "odd" }}
jeypiti
pub fn describe_number(n: i32) -> String { match n { 0 => "Zero".to_string(), _ => format!( "{} {}", if n > 0 { "Positive" } else { "Negative" }, if n % 2 == 0 { "even" } else { "odd" } ) }}
0xsmarter
pub fn describe_number(n: i32) -> String { if n == 0 { return "Zero".to_string() } if n % 2 == 0 { if n > 0 { return "Positive even".to_string() } else { return "Negative even".to_string() } } else { if n > 0 { return "Positive odd".to_string() } else { return "Negative odd".to_string() } }}
pynhpo
pub fn describe_number(n: i32) -> String { if n == 0 { return "Zero".to_string() } if n % 2 == 0 { if n > 0 { return "Positive even".to_string() } else { return "Negative even".to_string() } } else { if n > 0 { return "Positive odd".to_string() } else { return "Negative odd".to_string() } }}
tsucchinoko
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 != 0 => "Positive odd".to_string(), n if n < 0 && n % 2 == 0 => "Negative even".to_string(), n if n < 0 && n % 2 != 0 => "Negative odd".to_string(), _ => "Zero".to_string(), }}
ankeetparikh
pub fn describe_number(n: i32) -> String { if n == 0 { "Zero".to_string() } else { 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"); } s }}
maxvi
pub fn describe_number(n: i32) -> String { match n { 0 => "Zero", _ if n % 2 == 0 => { if n > 0 { "Positive even" } else { "Negative even" } } _ if n % 2 != 0 => { if n > 0 { "Positive odd" } else { "Negative odd" } } _ => "Invalid input", } .to_string()}
cloki0610
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here match n { 0 => "Zero".to_string(), n if n > 0 && n % 2 == 0 => "Positive even".to_string(), n if n > 0 && n % 2 != 0 => "Positive odd".to_string(), n if n < 0 && n % 2 == 0 => "Negative even".to_string(), n if n < 0 && n % 2 != 0 => "Negative odd".to_string(), _ => "Invalid value".to_string() }}
Brack0
pub fn describe_number(n: i32) -> String { match n { n if n.is_negative() && n % 2 == 0 => "Negative even".to_string(), n if n.is_negative() && n % 2 != 0 => "Negative odd".to_string(), n if n.is_positive() && n % 2 == 0 => "Positive even".to_string(), n if n.is_positive() && n % 2 != 0 => "Positive odd".to_string(), _ => "Zero".to_string(), }}
whitwulf
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here let is_even = n % 2 == 0; let is_positive = n >= 0; let zero = n == 0; match (is_positive, is_even, zero) { (_, _, true) => "Zero".to_string(), (true, true, _) => "Positive even".to_string(), (true, false, _) => "Positive odd".to_string(), (false, true, _) => "Negative even".to_string(), (false, false, _) => "Negative odd".to_string(), }}
whitwulf
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here let is_even = n % 2 == 0; let is_positive = n >= 0; let zero = n == 0; match (is_positive, is_even, zero) { (_, _, true) => "Zero".to_string(), (true, true, _) => "Positive even".to_string(), (true, false, _) => "Positive odd".to_string(), (false, true, _) => "Negative even".to_string(), (false, false, _) => "Negative odd".to_string(), }}
nknknknkn2
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here let mut result = String::from(""); if n == 0 { return "Zero".to_string(); } if n > 0 { result.push_str("Positive "); } else { result.push_str("Negative "); } if n % 2 == 0 { result.push_str("even"); } else { result.push_str("odd"); } return result;}
lucas-heck
pub fn describe_number(n: i32) -> String { match n { n if n == 0 => "Zero".to_string(), n if n > 0 && n % 2 == 0 => "Positive even".to_string(), n if n < 0 && n % 2 == 0 => "Negative even".to_string(), n if n > 0 => "Positive odd".to_string(), _ => "Negative odd".to_string(), }}
lucas-heck
pub fn describe_number(n: i32) -> String { match n { n if n > 0 && n % 2 == 0 => "Positive even".to_string(), n if n > 0 && n % 2 != 0 => "Positive odd".to_string(), n if n < 0 && n % 2 == 0 => "Negative even".to_string(), n if n < 0 && n % 2 != 0 => "Negative odd".to_string(), n if n == 0 => "Zero".to_string(), _ => "".to_string() }}
josschne
pub fn describe_number(n: i32) -> String { match n { 0 => "Zero".to_string(), n if n > 0 && n % 2 == 0 => "Positive even".to_string(), n if n < 0 && n % 2 == 0 => "Negative even".to_string(), n if n > 0 => "Positive odd".to_string(), _ => "Negative odd".to_string() }}
Rolando1994
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n > 0 { if n % 2 == 0 { return "Positive even".to_string(); } else { return "Positive odd".to_string(); } } if n < 0 { if n % 2 == 0 { return "Negative even".to_string(); } else { return "Negative odd".to_string(); } } return "Zero".to_string();}
labike
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n > 0 { if n % 2 == 0 { return "Positive even".to_string(); } else { return "Positive odd".to_string(); } } else if n < 0 { if n % 2 == 0 { return "Negative even".to_string(); } else { return "Negative odd".to_string(); } } else { return "Zero".to_string(); }}
Bottelet
pub fn describe_number(n: i32) -> String { if n >= 0 { if n == 0 { return "Zero".to_string(); } 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(); } }}
bl1ks
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 { "Positive even".to_string() } else { "Positive odd".to_string() } } else { if n % 2 == 0 { "Negative even".to_string() } else { "Negative odd".to_string() } }}