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.carlos-quantexai
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n == 0 { return format!("Zero"); } if n % 2 == 0 { if n > 0 { return format!("Positive even"); } else { return format!("Negative even"); } } else { if n > 0 { return format!("Positive odd"); } else { return format!("Negative odd"); } }}
jose-bernardo
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) => { return "Positive even".to_string(); }, (true, false) => { return "Positive odd".to_string(); }, (false, true) => { return "Negative even".to_string(); }, (false, false) => { return "Negative odd".to_string(); } }}
Mxn-ptr
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here let mut description = String::new(); if n == 0 { return "Zero".to_string() } if n > 0 { description.push_str("Positive"); } else if n < 0 { description.push_str("Negative"); } if n % 2 == 0 { description.push_str(" even"); } else { description.push_str(" odd"); } description}
yansq
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here let mut res = String::new(); if n > 0 { res.push_str("Positive"); } else if n < 0 { res.push_str("Negative"); } else { return String::from("Zero"); } if n % 2 == 0 { res.push_str(" even"); } else { res.push_str(" odd"); } res}
funny233-github
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n == 0 { return format!("Zero"); } if n % 2 == 0 { if n > 0 { return format!("Positive even"); } else { return format!("Negative even"); } } else { if n > 0 { return format!("Positive odd"); } else { return format!("Negative odd"); } }}
digitalresistor
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 => "Negative even".to_string(), n if n > 0 && n % 2 != 0 => "Positive odd".to_string(), n if n < 0 && n % 2 != 0 => "Negative odd".to_string(), n if n == 0 => "Zero".to_string(), _ => unreachable!() }}
majesticalcreature
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n == 0 { return String::from("Zero"); } let mut out = String::from(match n { n if n > 0 => "Positive ", _ => "Negative " }); out.push_str(match n { n if n%2 == 0 => "even", _ => "odd" }); out}
jhq223
pub fn describe_number(n: i32) -> String { 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(), }}
xbarnett
pub fn describe_number(n: i32) -> String { if n == 0 { return String::from("Zero"); } String::from(match (n > 0, n % 2 == 0) { (true, true) => "Positive even", (true, false) => "Positive odd", (false, true) => "Negative even", (false, false) => "Negative odd", })}
Johnchoi913
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n == 0 { return "Zero".to_string(); } let mut ans: String = Default::default(); match n > 0 { true => ans.push_str("Positive "), _ => ans.push_str("Negative ") } match n % 2 == 0 { true => ans.push_str("even"), _ => ans.push_str("odd") } ans}
Ignition
pub fn describe_number(n: i32) -> String { match n % 2 == 0 { true if n>0 => "Positive even", false if n>0 => "Positive odd", true if n<0 => "Negative even", false if n<0 => "Negative odd", _ => "Zero", }.into()}
matei
pub fn describe_number(n: i32) -> String { if n == 0 { return String::from("Zero"); } let mut result = String::from(""); 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}
sander-b-postnl
pub fn describe_number(n: i32) -> String { match n { 0 => "Zero".to_string(), _ => { let sign = if n > 0 { "Positive" } else { "Negative" }; let parity = if n % 2 == 0 { "even" } else { "odd" }; format!("{} {}", sign, parity) } }}
Ustin
pub fn describe_number(n: i32) -> String { 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(), };}
Ustin
pub fn describe_number(n: i32) -> String { 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(), };}
KhazAkar
pub fn describe_number(n: i32) -> String { if n == 0 { return "Zero".to_string(); } else 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(); } } }
oneopane
pub fn describe_number(n: i32) -> String { let mut result = String::new(); if n > 0 { result.push_str("Positive "); } else if n < 0 { result.push_str("Negative "); } else { result.push_str("Zero"); return result; } if n % 2 == 0 { result.push_str("even"); } else { result.push_str("odd"); } result}
amassare
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n==0{ return String::from("Zero"); } let mut res=String::new(); if n>0{ res.push_str("Positive "); }else if n<0{ res.push_str("Negative "); } if n%2==0{ res.push_str("even"); }else{ res.push_str("odd"); } res}
alexromanov
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here let mut s: String = String::from(""); if n == 0 { s.push_str("Zero"); return s; } 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}
CianciuStyles
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n == 0 { return String::from("Zero") } let mut result = String::new(); result.push_str(if n > 0 { "Positive" } else { "Negative"} ); result.push_str(" "); result.push_str(if n % 2 == 0 { "even" } else { "odd"} ); result}
pbjarterot
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n == 0 { return "Zero".to_string(); } let mut res: String = String::new(); match n > 0 { true => res.push_str("Positive "), false => res.push_str("Negative ") } match n % 2 == 0 { true => res.push_str("even"), false => res.push_str("odd") } return res;}
kyhou
pub fn describe_number(n: i32) -> String { if n == 0 { return "Zero".to_string(); } let mut text = if n > 0 { "Positive ".to_string() } else { "Negative ".to_string() }; text.push_str(if n % 2 == 0 { "even" } else { "odd" }); text}
ayushrawat10
pub fn describe_number(n: i32) -> String { match n { 0 => { String::from("Zero") }, i32::MIN..=-1 => { if n%2 == 0 { return String::from("Negative even"); } return String::from("Negative odd"); }, 1..=i32::MAX => { if n % 2 == 0 { return String::from("Positive even"); } return String::from("Positive odd"); } }}
Aditeya
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n == 0 { return "Zero".into(); } let mut res = String::with_capacity(13); if n < 0 { res.push_str("Negative "); } else { res.push_str("Positive "); } if n % 2 == 0 { res.push_str("even"); } else { res.push_str("odd"); } res}
masteryachty
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here String::from( 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" } )}
damascussteel21
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here String::from( 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" } )}
morigs
pub fn describe_number(n: i32) -> String { if n == 0 { return "Zero".to_string(); } let pn = if n > 0 { "Positive" } else { "Negative" }; let eo = if n % 2 == 0 { "even" } else { "odd" }; format!("{pn} {eo}")}
edgarcnp
pub fn describe_number(n: i32) -> String { if n == 0 { return "Zero".to_string(); } let sign = if n > 0 { "Positive" } else { "Negative" }; let parity = if n % 2 == 0 { "even" } else { "odd" }; format!("{} {}", sign, parity)}
jtruong04
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n == 0 { return "Zero".to_string(); } let sign = if n > 0 {"Positive"} else {"Negative"}; let parity = if n % 2 == 0 {"even"} else {"odd"}; format!("{} {}", sign, parity)}
abhiyan-chhetri
pub fn describe_number(n: i32) -> String { match (n == 0 , n > 0, n % 2 == 0) { (false, true , true ) => "Positive even".into(), (false, true, false) => "Positive odd".into(), (false, false, true) => "Negative even".into(), (false, false, false) => "Negative odd".into(), (true, _, _) => "Zero".into() }}
abhiyan-chhetri
pub fn describe_number(n: i32) -> String { match (n == 0 , n > 0, n % 2 == 0) { (false, true , true ) => "Positive even".into(), (false, true, false) => "Positive odd".into(), (false, false, true) => "Negative even".into(), (false, false, false) => "Negative odd".into(), (true, _, _) => "Zero".into() }}
Sommos
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here let mut s = String::new(); if n == 0 { s.push_str("Zero"); return s } 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}
wischi-chr
pub fn describe_number(n: i32) -> String { let mut s = String::new(); if n == 0 { s.push_str("Zero"); return s; } 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}
tukantje
pub fn describe_number(n: i32) -> String { if n == 0 { return "Zero".to_string(); } let mut result = if n > 0 { "Positive ".to_string() } else { "Negative ".to_string() }; if n % 2 == 0 { result.push_str("even"); } else { result.push_str("odd"); } result}
CarrotTarrot
pub fn describe_number(n: i32) -> String { match n { n if n > 0 && n & 1 == 0 => "Positive even".to_string(), n if n > 0 => "Positive odd".to_string(), n if n < 0 && n & 1 == 0 => "Negative even".to_string(), n if n < 0 => "Negative odd".to_string(), _ => "Zero".to_string() }}
CarrotTarrot
pub fn describe_number(n: i32) -> String { match n { 0 => "Zero".to_string(), n if n > 0 && n & 1 == 0 => "Positive even".to_string(), n if n > 0 => "Positive odd".to_string(), n if n < 0 && n & 1 == 0 => "Negative even".to_string(), n if n < 0 => "Negative odd".to_string(), _ => "NaN".to_string() }}
CarrotTarrot
pub fn describe_number(n: i32) -> String { match n { 0 => "Zero".to_string(), n if n > 0 && n & 1 == 0 => "Positive even".to_string(), n if n > 0 => "Positive odd".to_string(), n if n < 0 && n & 1 == 0 => "Negative even".to_string(), n if n < 0 => "Negative odd".to_string(), _ => "NaN".to_string() }}
CarrotTarrot
pub fn describe_number(n: i32) -> String { match n { 0 => "Zero".to_string(), n if n > 0 && n & 1 == 0 => "Positive even".to_string(), n if n > 0 && n & 1 != 0 => "Positive odd".to_string(), n if n < 0 && n & 1 == 0 => "Negative even".to_string(), n if n < 0 && n & 1 != 0 => "Negative odd".to_string(), _ => "NaN".to_string() }}
dyoo
pub fn describe_number(n: i32) -> String { if n == 0 { return "Zero".to_string(); } let pos = n > 0; let neg = n < 0; let even = n % 2 == 0; let sign = if n > 0 { "Positive" } else { "Negative" }; let suffix = if n % 2 == 0 { "even" } else { "odd"}; format!("{} {}", sign, suffix)}
jaswgreen
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(), }}
hilias
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n == 0 { return "Zero".to_string(); } let mut resp = String::new(); resp.push_str(if n > 0 { "Positive " } else { "Negative " }); resp.push_str(if n % 2 == 0 { "even" } else { "odd" }); resp}
hinphansa
pub fn describe_number(n: i32) -> String { 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(), }}
hinphansa
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n == 0 { return "Zero".to_string(); } 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(); }}
qcabanes-hobby
pub fn describe_number(n: i32) -> String { match (n, n & 1) { (0, 0) => "Zero", (1.., 0) => "Positive even", (1.., 1) => "Positive odd", (_, 0) => "Negative even", _ => "Negative odd", }.to_string()}
TaiPoole
pub fn describe_number(n: i32) -> String { match (n, n % 2) { (0, 0) => "Zero", (1.., 0) => "Positive even", (1.., 1) => "Positive odd", (_, 0) => "Negative even", _ => "Negative odd" }.to_string()}
TomBaston
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here match n { 0 => "Zero".into(), _ => format!( "{} {}", if n > 0 { "Positive" } else { "Negative"}, if n & 1 == 0 { "even" } else { "odd" } ) }}
swandyr
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here if n == 0 { String::from("Zero") } else { format!("{} {}", if n > 0 { "Positive" } else { "Negative" }, if n % 2 == 0 { "even" } else { "odd" } ) }}
0x006E
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here let mut str = String::new(); if n == 0 { return "Zero".to_string(); } else if n < 0 { str.push_str("Negative"); } else { str.push_str("Positive"); } if n % 2 == 0 { str.push_str(" even"); } else { str.push_str(" odd"); } str}
alspadoni
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here match (n > 0, n < 0, n % 2) { (true, _, 0) => "Positive even", (true, _, _) => "Positive odd", (_, true, 0) => "Negative even", (_, true, _) => "Negative odd", (false, false, _) => "Zero", }.to_owned()}
vineelkovvuri
pub fn describe_number(n: i32) -> String { // TODO: Implement the function here let res = 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" } }; res.to_string()}