Skip to content

Commit

Permalink
temperature converter
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodesdev committed Jun 3, 2024
1 parent 2a4cece commit bce8bfd
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions challenges/challenges.json
Original file line number Diff line number Diff line change
Expand Up @@ -195,5 +195,17 @@
"tags": [],
"created_at": "2024-06-02T00:00:00Z",
"updated_at": "2024-06-02T00:00:00Z"
},
{
"id": 17,
"title": "Temperature Converter",
"slug": "temperature-converter",
"short_description": "Implement a temperature converter that handles various units and errors.",
"language": "RUST",
"difficulty": "MEDIUM",
"track": "CONTROL_FLOW",
"tags": ["conversion", "error handling", "conditionals"],
"created_at": "2024-06-03T00:00:00Z",
"updated_at": "2024-06-03T00:00:00Z"
}
]
6 changes: 6 additions & 0 deletions challenges/temperature-converter/Cargo.toml
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]
42 changes: 42 additions & 0 deletions challenges/temperature-converter/description.md
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.
13 changes: 13 additions & 0 deletions challenges/temperature-converter/src/lib.rs
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()),
}
}
4 changes: 4 additions & 0 deletions challenges/temperature-converter/src/starter.rs
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...
}
44 changes: 44 additions & 0 deletions challenges/temperature-converter/src/tests.rs
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));
}
}

0 comments on commit bce8bfd

Please sign in to comment.