-
StringsMathematics
Code def math(s): return eval(s)
Test Cases test.assert_equals(math("2 + 2"), 4)
Output:
-
Code fn math(e: &str) -> i32 {let mut ope = '\0';let mut first = 0;let mut secon = 0;for c in e.chars() {match c {'+' => {ope = '+';},'-' => {ope = '-';},'*' => {ope = '*';},'/' => {ope = '/';},'=' => {ope = '='}'0'..='9' => {if ope == '\0' {first *= 10;first += c as i32 - '0' as i32;} else {secon *= 10;secon += c as i32 - '0' as i32;}}' ' => {},_ => {panic!("wrong value");}}}return match ope {'+' => first + secon,'-' => first - secon,'*' => first * secon,'/' => first / secon,'=' => (first == secon) as i32,_ => panic!("no operator")}}- def math(s):
- return eval(s)
Preloaded Code fn math(e: String) -> i32 {}
Test Cases #[cfg(test)]mod tests {use super::*;#[test]fn test_add() {assert_eq!(math("1 + 2"), 3);assert_eq!(math("12+ 3456789"), 3456801);assert_eq!(math("50 + 10 0"), 150);}#[test]fn test_sub() {assert_eq!(math("1-3"), -2);assert_eq!(math("500 - 150"), 350);assert_eq!(math("0-2"), -2);}#[test]fn test_multiply() {assert_eq!(math("1* 30"), 30);assert_eq!(math("20 *5"), 100);}#[test]fn test_divide() {assert_eq!(math("10/5"), 2);assert_eq!(math("500 / 2"), 250);}#[test]fn test_equal() {assert_eq!(math("10 = 10"), 1);assert_eq!(math("2 =50"), 0);assert_eq!(math("0=0"), 1);}}- test.assert_equals(math("2 + 2"), 4)
- All
- {{group.name}} ({{group.count}})
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}