Skip to content

Commit de42d6a

Browse files
authored
temperature conversions (#169)
* temperature conversions * perfect number * added unit tests and ran phpcs * unit test * fixed style issue in ConversionsTest.php * fixed comments
1 parent 416caa6 commit de42d6a

File tree

5 files changed

+230
-0
lines changed

5 files changed

+230
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
/**
4+
* This function converts the submitted
5+
* temperature (Celsius) and converts it into
6+
* Fahrenheit.
7+
*
8+
* @author Marco https://github.com/MaarcooC
9+
* @param float $celsius
10+
* @throws \Exception
11+
* @return float
12+
*/
13+
function CelsiusToFahrenheit($celsius)
14+
{
15+
if (!is_numeric($celsius)) {
16+
throw new \Exception("Temperature (Celsius) must be a number");
17+
}
18+
19+
return round(($celsius * 9 / 5) + 32, 1);
20+
}
21+
22+
/**
23+
* This function converts the submitted
24+
* temperature (Fahrenheit) and converts it into
25+
* Celsius.
26+
*
27+
* @author Marco https://github.com/MaarcooC
28+
* @param float $fahrenheit
29+
* @throws \Exception
30+
* @return float
31+
*/
32+
function FahrenheitToCelsius($fahrenheit)
33+
{
34+
if (!is_numeric($fahrenheit)) {
35+
throw new \Exception("Temperature (Fahrenheit) must be a number");
36+
}
37+
38+
return round(($fahrenheit - 32) * 5 / 9, 1);
39+
}
40+
41+
/**
42+
* This function converts the submitted
43+
* temperature (Celsius) and converts it into
44+
* Kelvin.
45+
*
46+
* @author Marco https://github.com/MaarcooC
47+
* @param float $celsius
48+
* @throws \Exception
49+
* @return float
50+
*/
51+
function CelsiusToKelvin($celsius)
52+
{
53+
if (!is_numeric($celsius)) {
54+
throw new \Exception("Temperature (Celsius) must be a number");
55+
}
56+
57+
return round(($celsius + 273.15), 2);
58+
}
59+
60+
/**
61+
* This function converts the submitted
62+
* temperature (Kelvin) and converts it into
63+
* Celsius.
64+
*
65+
* @author Marco https://github.com/MaarcooC
66+
* @param float $kelvin
67+
* @throws \Exception
68+
* @return float
69+
*/
70+
function KelvinToCelsius($kelvin)
71+
{
72+
if (!is_numeric($kelvin)) {
73+
throw new \Exception("Temperature (Kelvin) must be a number");
74+
}
75+
76+
return round(($kelvin - 273.15), 2);
77+
}
78+
79+
/**
80+
* This function converts the submitted
81+
* temperature (Kelvin) and converts it into
82+
* Fahrenheit.
83+
*
84+
* @author Marco https://github.com/MaarcooC
85+
* @param float $kelvin
86+
* @throws \Exception
87+
* @return float
88+
*/
89+
function KelvinToFahrenheit($kelvin)
90+
{
91+
if (!is_numeric($kelvin)) {
92+
throw new \Exception("Temperature (Kelvin) must be a number");
93+
}
94+
95+
return round(($kelvin - 273.15) * 1.8 + 32, 2);
96+
}
97+
98+
/**
99+
* This function converts the submitted
100+
* temperature (Fahrenheit) and converts it into
101+
* kelvin.
102+
*
103+
* @author Marco https://github.com/MaarcooC
104+
* @param float $fahrenheit
105+
* @throws \Exception
106+
* @return float
107+
*/
108+
function FahrenheitToKelvin($fahrenheit)
109+
{
110+
if (!is_numeric($fahrenheit)) {
111+
throw new \Exception("Temperature (Fahrenheit) must be a number");
112+
}
113+
114+
return round(($fahrenheit - 32) * 5 / 9 + 273.15, 2);
115+
}

DIRECTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* [Hexadecimaltodecimal](./Conversions/HexadecimalToDecimal.php)
1616
* [Octaltodecimal](./Conversions/OctalToDecimal.php)
1717
* [Speedconversion](./Conversions/SpeedConversion.php)
18+
* [Temperatureconversions](./Conversions/TemperatureConversions.php)
1819

1920
## Datastructures
2021
* Avltree
@@ -70,6 +71,7 @@
7071
* [Median](./Maths/Median.php)
7172
* [Mode](./Maths/Mode.php)
7273
* [Neonnumber](./Maths/NeonNumber.php)
74+
* [Perfectnumber](./Maths/PerfectNumber.php)
7375
* [Perfectsquare](./Maths/PerfectSquare.php)
7476
* Projecteuler
7577
* [Problem1](./Maths/ProjectEuler/Problem1.php)

Maths/PerfectNumber.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/**
4+
This function returns true
5+
* if the submitted number is perfect,
6+
* false if it is not.
7+
*
8+
* A perfect number is a positive integer that is
9+
* equal to the sum of its positive proper
10+
* divisors, excluding the number itself.
11+
*
12+
* About perfect numbers: https://en.wikipedia.org/wiki/Perfect_number
13+
*
14+
* @author Marco https://github.com/MaarcooC
15+
* @param int $number
16+
* @return bool
17+
*/
18+
function perfect_number($number)
19+
{
20+
/*Input validation*/
21+
if (!is_int($number) || $number <= 1) {
22+
/*Return false for non-integer or non-positive numbers*/
23+
return false;
24+
}
25+
26+
$divisorsSum = 1; /*1 is a common divisor for every number*/
27+
28+
/*Check for divisors up to the square root of the number*/
29+
for ($i = 2; $i * $i <= $number; $i++) {
30+
if ($number % $i == 0) {
31+
$divisorsSum += $i; /*add i to the sum of divisors*/
32+
if ($i != $number / $i) { /*add the complement divisor*/
33+
$divisorsSum += $number / $i;
34+
}
35+
}
36+
}
37+
38+
return $divisorsSum == $number;
39+
}

tests/Conversions/ConversionsTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
require_once __DIR__ . '/../../Conversions/OctalToDecimal.php';
1010
require_once __DIR__ . '/../../Conversions/HexadecimalToDecimal.php';
1111
require_once __DIR__ . '/../../Conversions/SpeedConversion.php';
12+
require_once __DIR__ . '/../../Conversions/TemperatureConversions.php';
1213

1314
class ConversionsTest extends TestCase
1415
{
@@ -85,4 +86,60 @@ public function testSpeedConversion()
8586
$this->expectException(\Exception::class);
8687
convertSpeed(1, 'km/h', 'miles');
8788
}
89+
90+
public function testCelsiusToFahrenheit()
91+
{
92+
$this->assertEquals(32.0, CelsiusToFahrenheit(0));
93+
$this->assertEquals(212.0, CelsiusToFahrenheit(100));
94+
$this->assertEquals(98.6, CelsiusToFahrenheit(37));
95+
$this->expectException(\Exception::class);
96+
$this->expectExceptionMessage('Temperature (Celsius) must be a number');
97+
CelsiusToFahrenheit("non-numeric");
98+
}
99+
100+
public function testFahrenheitToCelsius()
101+
{
102+
$this->assertEquals(0.0, FahrenheitToCelsius(32));
103+
$this->assertEquals(100.0, FahrenheitToCelsius(212));
104+
$this->assertEquals(37.0, FahrenheitToCelsius(98.6));
105+
$this->expectException(\Exception::class);
106+
$this->expectExceptionMessage('Temperature (Fahrenheit) must be a number');
107+
FahrenheitToCelsius("non-numeric");
108+
}
109+
110+
public function testCelsiusToKelvin()
111+
{
112+
$this->assertEquals(273.15, CelsiusToKelvin(0));
113+
$this->assertEquals(373.15, CelsiusToKelvin(100));
114+
$this->expectException(\Exception::class);
115+
$this->expectExceptionMessage('Temperature (Celsius) must be a number');
116+
CelsiusToKelvin("non-numeric");
117+
}
118+
119+
public function testKelvinToCelsius()
120+
{
121+
$this->assertEquals(0.0, KelvinToCelsius(273.15));
122+
$this->assertEquals(100.0, KelvinToCelsius(373.15));
123+
$this->expectException(\Exception::class);
124+
$this->expectExceptionMessage('Temperature (Kelvin) must be a number');
125+
KelvinToCelsius("non-numeric");
126+
}
127+
128+
public function testKelvinToFahrenheit()
129+
{
130+
$this->assertEquals(32.0, KelvinToFahrenheit(273.15));
131+
$this->assertEquals(212.0, KelvinToFahrenheit(373.15));
132+
$this->expectException(\Exception::class);
133+
$this->expectExceptionMessage('Temperature (Kelvin) must be a number');
134+
KelvinToFahrenheit("non-numeric");
135+
}
136+
137+
public function testFahrenheitToKelvin()
138+
{
139+
$this->assertEquals(273.15, FahrenheitToKelvin(32));
140+
$this->assertEquals(373.15, FahrenheitToKelvin(212));
141+
$this->expectException(\Exception::class);
142+
$this->expectExceptionMessage('Temperature (Fahrenheit) must be a number');
143+
FahrenheitToKelvin("non-numeric");
144+
}
88145
}

tests/Maths/MathsTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
require_once __DIR__ . '/../../Maths/Mode.php';
2323
require_once __DIR__ . '/../../Maths/FastInverseSquareRoot.php';
2424
require_once __DIR__ . '/../../Maths/BaseX.php';
25+
require_once __DIR__ . '/../../Maths/PerfectNumber.php';
2526

2627
class MathsTest extends TestCase
2728
{
@@ -185,6 +186,22 @@ public function testGreatestCommonDivisor()
185186
$this->assertEquals(3, gcd(9, 12));
186187
}
187188

189+
public function testPerfectNumber()
190+
{
191+
$this->assertTrue(perfect_number(6));
192+
$this->assertTrue(perfect_number(28));
193+
$this->assertTrue(perfect_number(496));
194+
195+
$this->assertFalse(perfect_number(10));
196+
$this->assertFalse(perfect_number(15));
197+
198+
$this->assertFalse(perfect_number(-6));
199+
$this->assertFalse(perfect_number(0));
200+
$this->assertFalse(perfect_number(1));
201+
$this->assertFalse(perfect_number(2.5));
202+
$this->assertFalse(perfect_number("string"));
203+
}
204+
188205
public function testFastInverseSquareRoot()
189206
{
190207
$this->assertEqualsWithDelta(0.31568579235273, fastInvSqrt(10), 0.00001);

0 commit comments

Comments
 (0)