-
Notifications
You must be signed in to change notification settings - Fork 18
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
Showing
7 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[package] | ||
name = "temperature-converter" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] |
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
In many real-world applications, converting temperatures between different units is a common task. For example, meteorologists, scientists, and engineers often need to convert temperatures from Celsius to Fahrenheit or Kelvin. In this challenge, you'll implement a function that **converts temperatures between these units using logical operators and conditional statements**. | ||
|
||
## Your task | ||
|
||
Your task is to write a function `convert_temperature` that takes three parameters: a **float** representing the temperature value, a **string** representing the **unit** of the input temperature, and another **string** representing the desired unit for the output temperature. The function should return a `Result` type with either the converted temperature as a float or an error message if the input is invalid. | ||
|
||
## Requirements | ||
|
||
- The function should correctly convert temperatures between **Celsius**, **Fahrenheit**, and **Kelvin**. | ||
- If the input unit or the desired output unit is not one of **"C", "F", or "K"**, the function should return an error message: `"Invalid unit"`. | ||
- If the conversion is successful, the function should return the converted temperature as a **float** in the `Ok` variant of the `Result`. | ||
|
||
## Did you know? | ||
|
||
The Celsius and Fahrenheit scales are named after the scientists **Anders Celsius** and **Daniel Gabriel Fahrenheit**, respectively. The **Kelvin scale**, on the other hand, is named after **Lord Kelvin**, a British physicist. Unlike the **Celsius** and **Fahrenheit** scales, which are based on the freezing and boiling points of water, the **Kelvin** scale is an absolute temperature scale based on the concept of absolute zero, the lowest possible temperature where all molecular motion ceases. | ||
|
||
## Example | ||
|
||
```rust | ||
let result = convert_temperature(100.0, "C", "F"); | ||
assert_eq!(result, Ok(212.0)); | ||
|
||
let result = convert_temperature(32.0, "F", "C"); | ||
assert_eq!(result, Ok(0.0)); | ||
|
||
let result = convert_temperature(0.0, "C", "K"); | ||
assert_eq!(result, Ok(273.15)); | ||
|
||
let result = convert_temperature(300.0, "K", "C"); | ||
assert_eq!(result, Ok(26.85)); | ||
|
||
let result = convert_temperature(100.0, "C", "X"); | ||
assert_eq!(result, Err("Invalid unit".to_string())); | ||
``` | ||
|
||
## Hints | ||
|
||
- To convert Celsius to Fahrenheit: \(F = C \times \frac{9}{5} + 32\) | ||
- To convert Fahrenheit to Celsius: \(C = (F - 32) \times \frac{5}{9}\) | ||
- To convert Celsius to Kelvin: \(K = C + 273.15\) | ||
- To convert Kelvin to Celsius: \(C = K - 273.15\) | ||
- Remember to handle invalid units with proper error messages. |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
mod tests; | ||
|
||
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { | ||
match (from_unit, to_unit) { | ||
("C", "F") => Ok(value * 9.0 / 5.0 + 32.0), | ||
("F", "C") => Ok((value - 32.0) * 5.0 / 9.0), | ||
("C", "K") => Ok(value + 273.15), | ||
("K", "C") => Ok(value - 273.15), | ||
("F", "K") => Ok((value - 32.0) * 5.0 / 9.0 + 273.15), | ||
("K", "F") => Ok((value - 273.15) * 9.0 / 5.0 + 32.0), | ||
_ => Err("Invalid unit".to_string()), | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pub fn convert_temperature(value: f64, from_unit: &str, to_unit: &str) -> Result<f64, String> { | ||
// TODO: Implement the function here | ||
// Your code here... | ||
} |
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#[cfg(test)] | ||
mod tests { | ||
use crate::convert_temperature; | ||
|
||
#[test] | ||
fn test_convert_celsius_to_fahrenheit() { | ||
assert_eq!(convert_temperature(100.0, "C", "F"), Ok(212.0)); | ||
} | ||
|
||
#[test] | ||
fn test_convert_fahrenheit_to_celsius() { | ||
assert_eq!(convert_temperature(32.0, "F", "C"), Ok(0.0)); | ||
} | ||
|
||
#[test] | ||
fn test_convert_celsius_to_kelvin() { | ||
assert_eq!(convert_temperature(0.0, "C", "K"), Ok(273.15)); | ||
} | ||
|
||
#[test] | ||
fn test_convert_kelvin_to_celsius() { | ||
let result = convert_temperature(300.0, "K", "C").unwrap(); | ||
let rounded_result = (result * 100.0).round() / 100.0; | ||
assert_eq!(rounded_result, 26.85); | ||
} | ||
|
||
#[test] | ||
fn test_invalid_unit() { | ||
assert_eq!( | ||
convert_temperature(100.0, "C", "X"), | ||
Err("Invalid unit".to_string()) | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_convert_fahrenheit_to_kelvin() { | ||
assert_eq!(convert_temperature(32.0, "F", "K"), Ok(273.15)); | ||
} | ||
|
||
#[test] | ||
fn test_convert_kelvin_to_fahrenheit() { | ||
assert_eq!(convert_temperature(273.15, "K", "F"), Ok(32.0)); | ||
} | ||
} |