mirror of
https://github.com/TheAlgorithms/PHP.git
synced 2025-01-16 22:58:14 +01:00
temperature conversions (#169)
* temperature conversions * perfect number * added unit tests and ran phpcs * unit test * fixed style issue in ConversionsTest.php * fixed comments
This commit is contained in:
parent
416caa61d5
commit
de42d6ad06
115
Conversions/TemperatureConversions.php
Normal file
115
Conversions/TemperatureConversions.php
Normal file
@ -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);
|
||||
}
|
@ -15,6 +15,7 @@
|
||||
* [Hexadecimaltodecimal](./Conversions/HexadecimalToDecimal.php)
|
||||
* [Octaltodecimal](./Conversions/OctalToDecimal.php)
|
||||
* [Speedconversion](./Conversions/SpeedConversion.php)
|
||||
* [Temperatureconversions](./Conversions/TemperatureConversions.php)
|
||||
|
||||
## Datastructures
|
||||
* Avltree
|
||||
@ -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)
|
||||
|
39
Maths/PerfectNumber.php
Normal file
39
Maths/PerfectNumber.php
Normal file
@ -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;
|
||||
}
|
@ -9,6 +9,7 @@ require_once __DIR__ . '/../../Conversions/DecimalToBinary.php';
|
||||
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
|
||||
{
|
||||
@ -85,4 +86,60 @@ class ConversionsTest extends TestCase
|
||||
$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");
|
||||
}
|
||||
}
|
||||
|
@ -22,6 +22,7 @@ require_once __DIR__ . '/../../Maths/Median.php';
|
||||
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
|
||||
{
|
||||
@ -185,6 +186,22 @@ class MathsTest extends TestCase
|
||||
$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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user