Skip to content

Commit

Permalink
temperature conversions (#169)
Browse files Browse the repository at this point in the history
* temperature conversions

* perfect number

* added unit tests and ran phpcs

* unit test

* fixed style issue in ConversionsTest.php

* fixed comments
  • Loading branch information
MaarcooC authored Oct 19, 2024
1 parent 416caa6 commit de42d6a
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 0 deletions.
115 changes: 115 additions & 0 deletions Conversions/TemperatureConversions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

/**
* This function converts the submitted
* temperature (Celsius) and converts it into
* Fahrenheit.
*
* @author Marco https://github.com/MaarcooC
* @param float $celsius
* @throws \Exception
* @return float
*/
function CelsiusToFahrenheit($celsius)
{
if (!is_numeric($celsius)) {
throw new \Exception("Temperature (Celsius) must be a number");
}

return round(($celsius * 9 / 5) + 32, 1);
}

/**
* This function converts the submitted
* temperature (Fahrenheit) and converts it into
* Celsius.
*
* @author Marco https://github.com/MaarcooC
* @param float $fahrenheit
* @throws \Exception
* @return float
*/
function FahrenheitToCelsius($fahrenheit)
{
if (!is_numeric($fahrenheit)) {
throw new \Exception("Temperature (Fahrenheit) must be a number");
}

return round(($fahrenheit - 32) * 5 / 9, 1);
}

/**
* This function converts the submitted
* temperature (Celsius) and converts it into
* Kelvin.
*
* @author Marco https://github.com/MaarcooC
* @param float $celsius
* @throws \Exception
* @return float
*/
function CelsiusToKelvin($celsius)
{
if (!is_numeric($celsius)) {
throw new \Exception("Temperature (Celsius) must be a number");
}

return round(($celsius + 273.15), 2);
}

/**
* This function converts the submitted
* temperature (Kelvin) and converts it into
* Celsius.
*
* @author Marco https://github.com/MaarcooC
* @param float $kelvin
* @throws \Exception
* @return float
*/
function KelvinToCelsius($kelvin)
{
if (!is_numeric($kelvin)) {
throw new \Exception("Temperature (Kelvin) must be a number");
}

return round(($kelvin - 273.15), 2);
}

/**
* This function converts the submitted
* temperature (Kelvin) and converts it into
* Fahrenheit.
*
* @author Marco https://github.com/MaarcooC
* @param float $kelvin
* @throws \Exception
* @return float
*/
function KelvinToFahrenheit($kelvin)
{
if (!is_numeric($kelvin)) {
throw new \Exception("Temperature (Kelvin) must be a number");
}

return round(($kelvin - 273.15) * 1.8 + 32, 2);
}

/**
* This function converts the submitted
* temperature (Fahrenheit) and converts it into
* kelvin.
*
* @author Marco https://github.com/MaarcooC
* @param float $fahrenheit
* @throws \Exception
* @return float
*/
function FahrenheitToKelvin($fahrenheit)
{
if (!is_numeric($fahrenheit)) {
throw new \Exception("Temperature (Fahrenheit) must be a number");
}

return round(($fahrenheit - 32) * 5 / 9 + 273.15, 2);
}
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* [Hexadecimaltodecimal](./Conversions/HexadecimalToDecimal.php)
* [Octaltodecimal](./Conversions/OctalToDecimal.php)
* [Speedconversion](./Conversions/SpeedConversion.php)
* [Temperatureconversions](./Conversions/TemperatureConversions.php)

## Datastructures
* Avltree
Expand Down Expand Up @@ -70,6 +71,7 @@
* [Median](./Maths/Median.php)
* [Mode](./Maths/Mode.php)
* [Neonnumber](./Maths/NeonNumber.php)
* [Perfectnumber](./Maths/PerfectNumber.php)
* [Perfectsquare](./Maths/PerfectSquare.php)
* Projecteuler
* [Problem1](./Maths/ProjectEuler/Problem1.php)
Expand Down
39 changes: 39 additions & 0 deletions Maths/PerfectNumber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/**
This function returns true
* if the submitted number is perfect,
* false if it is not.
*
* A perfect number is a positive integer that is
* equal to the sum of its positive proper
* divisors, excluding the number itself.
*
* About perfect numbers: https://en.wikipedia.org/wiki/Perfect_number
*
* @author Marco https://github.com/MaarcooC
* @param int $number
* @return bool
*/
function perfect_number($number)
{
/*Input validation*/
if (!is_int($number) || $number <= 1) {
/*Return false for non-integer or non-positive numbers*/
return false;
}

$divisorsSum = 1; /*1 is a common divisor for every number*/

/*Check for divisors up to the square root of the number*/
for ($i = 2; $i * $i <= $number; $i++) {
if ($number % $i == 0) {
$divisorsSum += $i; /*add i to the sum of divisors*/
if ($i != $number / $i) { /*add the complement divisor*/
$divisorsSum += $number / $i;
}
}
}

return $divisorsSum == $number;
}
57 changes: 57 additions & 0 deletions tests/Conversions/ConversionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require_once __DIR__ . '/../../Conversions/OctalToDecimal.php';
require_once __DIR__ . '/../../Conversions/HexadecimalToDecimal.php';
require_once __DIR__ . '/../../Conversions/SpeedConversion.php';
require_once __DIR__ . '/../../Conversions/TemperatureConversions.php';

class ConversionsTest extends TestCase
{
Expand Down Expand Up @@ -85,4 +86,60 @@ public function testSpeedConversion()
$this->expectException(\Exception::class);
convertSpeed(1, 'km/h', 'miles');
}

public function testCelsiusToFahrenheit()
{
$this->assertEquals(32.0, CelsiusToFahrenheit(0));
$this->assertEquals(212.0, CelsiusToFahrenheit(100));
$this->assertEquals(98.6, CelsiusToFahrenheit(37));
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Temperature (Celsius) must be a number');
CelsiusToFahrenheit("non-numeric");
}

public function testFahrenheitToCelsius()
{
$this->assertEquals(0.0, FahrenheitToCelsius(32));
$this->assertEquals(100.0, FahrenheitToCelsius(212));
$this->assertEquals(37.0, FahrenheitToCelsius(98.6));
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Temperature (Fahrenheit) must be a number');
FahrenheitToCelsius("non-numeric");
}

public function testCelsiusToKelvin()
{
$this->assertEquals(273.15, CelsiusToKelvin(0));
$this->assertEquals(373.15, CelsiusToKelvin(100));
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Temperature (Celsius) must be a number');
CelsiusToKelvin("non-numeric");
}

public function testKelvinToCelsius()
{
$this->assertEquals(0.0, KelvinToCelsius(273.15));
$this->assertEquals(100.0, KelvinToCelsius(373.15));
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Temperature (Kelvin) must be a number');
KelvinToCelsius("non-numeric");
}

public function testKelvinToFahrenheit()
{
$this->assertEquals(32.0, KelvinToFahrenheit(273.15));
$this->assertEquals(212.0, KelvinToFahrenheit(373.15));
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Temperature (Kelvin) must be a number');
KelvinToFahrenheit("non-numeric");
}

public function testFahrenheitToKelvin()
{
$this->assertEquals(273.15, FahrenheitToKelvin(32));
$this->assertEquals(373.15, FahrenheitToKelvin(212));
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Temperature (Fahrenheit) must be a number');
FahrenheitToKelvin("non-numeric");
}
}
17 changes: 17 additions & 0 deletions tests/Maths/MathsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
require_once __DIR__ . '/../../Maths/Mode.php';
require_once __DIR__ . '/../../Maths/FastInverseSquareRoot.php';
require_once __DIR__ . '/../../Maths/BaseX.php';
require_once __DIR__ . '/../../Maths/PerfectNumber.php';

class MathsTest extends TestCase
{
Expand Down Expand Up @@ -185,6 +186,22 @@ public function testGreatestCommonDivisor()
$this->assertEquals(3, gcd(9, 12));
}

public function testPerfectNumber()
{
$this->assertTrue(perfect_number(6));
$this->assertTrue(perfect_number(28));
$this->assertTrue(perfect_number(496));

$this->assertFalse(perfect_number(10));
$this->assertFalse(perfect_number(15));

$this->assertFalse(perfect_number(-6));
$this->assertFalse(perfect_number(0));
$this->assertFalse(perfect_number(1));
$this->assertFalse(perfect_number(2.5));
$this->assertFalse(perfect_number("string"));
}

public function testFastInverseSquareRoot()
{
$this->assertEqualsWithDelta(0.31568579235273, fastInvSqrt(10), 0.00001);
Expand Down

0 comments on commit de42d6a

Please sign in to comment.