-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b46f2aa
commit 9d3f3ea
Showing
5 changed files
with
46 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,47 @@ | ||
mod lib; | ||
use lib::calculate_percentage_change; | ||
use lib::get_input; | ||
use std::env::args; | ||
|
||
fn main() { | ||
let first = get_input(1); | ||
let second = get_input(2); | ||
let percentage_change = calculate_percentage_change(first, second); | ||
println!("{}", percentage_change); | ||
} | ||
|
||
pub fn get_input(index: usize) -> f32 { | ||
let args: Vec<String> = args().collect(); | ||
let result: f32 = match args[index].trim().parse() { | ||
Ok(num) => num, | ||
Err(_) => panic!( | ||
"Failed to read value \"{}\", we were expecting a u32 or f32!", | ||
args[index].trim() | ||
), | ||
}; | ||
result | ||
} | ||
|
||
pub fn calculate_percentage_change(first: f32, second: f32) -> String { | ||
if first < second { | ||
return format!("+{}%", (second - first) / first * 100.0); | ||
} | ||
if first > second { | ||
format!("-{}%", (first - second) / first * 100.0) | ||
} else { | ||
"0%".to_string() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn test_calculate_percentage_change() { | ||
assert_eq!(calculate_percentage_change(1_f32, 2_f32), "+100%"); | ||
assert_eq!(calculate_percentage_change(2_f32, 1_f32), "-50%"); | ||
assert_eq!(calculate_percentage_change(1_f32, 1_f32), "0%"); | ||
assert_eq!(calculate_percentage_change(1.2, 2.4), "+100%"); | ||
assert_eq!(calculate_percentage_change(2.4, 1.2), "-50%"); | ||
assert_eq!(calculate_percentage_change(1.1, 1.1), "0%"); | ||
} | ||
} |