mirror of
https://github.com/TheAlgorithms/PHP.git
synced 2025-01-17 15:18:13 +01:00
-
This commit is contained in:
commit
c6603aa280
48
Conversions/SpeedConversion.php
Normal file
48
Conversions/SpeedConversion.php
Normal file
@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This function converts the submitted
|
||||
* speed from one unit to another
|
||||
*
|
||||
* Conversion explanation taken from
|
||||
* https://www.asknumbers.com/SpeedConversion.aspx
|
||||
* Available units for conversion:
|
||||
* mph -> miles per hour
|
||||
* km/h -> kilometers per hour
|
||||
* m/s -> meters per second
|
||||
* ft/s -> feet per second
|
||||
* kn -> 1 knot which is equal to 1 nautical mile (1852 km/h)
|
||||
* The conversion is made using kilometers as base
|
||||
*
|
||||
* @param float $speed
|
||||
* @param string $unitFrom
|
||||
* @param string $unitTo
|
||||
* @return int
|
||||
*/
|
||||
function convertSpeed(float $speed, string $unitFrom, string $unitTo)
|
||||
{
|
||||
$speedUnitsFrom = [
|
||||
'mph' => 1.609344,
|
||||
'km/h' => 1,
|
||||
'm/s'=> 3.6,
|
||||
'ft/s'=> 1.097,
|
||||
'kn' => 1.852,
|
||||
];
|
||||
$speedUnitsTo = [
|
||||
'mph' => 0.6213712,
|
||||
'km/h' => 1,
|
||||
'm/s'=> 0.277778,
|
||||
'ft/s'=> 0.911344,
|
||||
'kn' => 0.539957,
|
||||
];
|
||||
$availableUnits = array_keys($speedUnitsFrom);
|
||||
|
||||
if (!is_numeric($speed)) {
|
||||
throw new \Exception("Please pass a valid speed number for converting it from one unit to another.");
|
||||
}
|
||||
if (!in_array($unitFrom, $availableUnits) || !in_array($unitTo, $availableUnits)) {
|
||||
throw new \Exception("Please pass a valid speed unit.\n\nAvailable units: " . implode(', ', $availableUnits));
|
||||
}
|
||||
|
||||
return round($speed * $speedUnitsFrom[$unitFrom] * $speedUnitsTo[$unitTo], 2);
|
||||
}
|
@ -11,6 +11,10 @@
|
||||
* [Decimaltobinary](./Conversions/DecimalToBinary.php)
|
||||
* [Hexadecimaltodecimal](./Conversions/HexadecimalToDecimal.php)
|
||||
* [Octaltodecimal](./Conversions/OctalToDecimal.php)
|
||||
* [Speedconversion](./Conversions/SpeedConversion.php)
|
||||
|
||||
## Graphs
|
||||
* [Depthfirstsearch](./Graphs/DepthFirstSearch.php)
|
||||
|
||||
## Maths
|
||||
* [Absolutemax](./Maths/AbsoluteMax.php)
|
||||
@ -76,6 +80,8 @@
|
||||
* [Morsecodetest](./tests/Ciphers/MorseCodeTest.php)
|
||||
* Conversions
|
||||
* [Conversionstest](./tests/Conversions/ConversionsTest.php)
|
||||
* Graphs
|
||||
* [Depthfirstsearchtest](./tests/Graphs/DepthFirstSearchTest.php)
|
||||
* Maths
|
||||
* [Mathstest](./tests/Maths/MathsTest.php)
|
||||
* [Projecteulertest](./tests/Maths/ProjectEulerTest.php)
|
||||
|
35
Graphs/DepthFirstSearch.php
Normal file
35
Graphs/DepthFirstSearch.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* The Depth-first Search is an algorithm used for traversing or searching trees or graphs
|
||||
* (https://en.wikipedia.org/wiki/Depth-first_search).
|
||||
*
|
||||
* This is a non recursive implementation.
|
||||
*
|
||||
* References:
|
||||
* https://www.geeksforgeeks.org/depth-first-search-or-dfs-for-a-graph/
|
||||
*
|
||||
* https://the-algorithms.com/algorithm/depth-first-search?lang=python
|
||||
*
|
||||
* @author Andre Alves https://github.com/andremralves
|
||||
* @param array $adjList An array representing the grapth as an Adjacent List
|
||||
* @param int|string $start The starting vertex
|
||||
* @return array The visited vertices
|
||||
*/
|
||||
function dfs($adjList, $start)
|
||||
{
|
||||
$visited = [];
|
||||
$stack = [$start];
|
||||
while (!empty($stack)) {
|
||||
// Removes the ending element from the stack and mark as visited
|
||||
$v = array_pop($stack);
|
||||
$visited[$v] = 1;
|
||||
|
||||
// Checks each adjacent vertex of $v and add to the stack if not visited
|
||||
foreach (array_reverse($adjList[$v]) as $adj) {
|
||||
if (!array_key_exists($adj, $visited))
|
||||
array_push($stack, $adj);
|
||||
}
|
||||
}
|
||||
return array_keys($visited);
|
||||
}
|
@ -9,6 +9,7 @@ require_once __DIR__ . '/../../Conversions/BinaryToDecimal.php';
|
||||
require_once __DIR__ . '/../../Conversions/DecimalToBinary.php';
|
||||
require_once __DIR__ . '/../../Conversions/OctalToDecimal.php';
|
||||
require_once __DIR__ . '/../../Conversions/HexadecimalToDecimal.php';
|
||||
require_once __DIR__ . '/../../Conversions/SpeedConversion.php';
|
||||
|
||||
class ConversionsTest extends TestCase
|
||||
{
|
||||
@ -69,4 +70,20 @@ class ConversionsTest extends TestCase
|
||||
$this->expectExceptionMessage('Please pass a valid Hexadecimal Number for Converting it to a Decimal Number.');
|
||||
hexToDecimal("this is a string");
|
||||
}
|
||||
|
||||
public function testSpeedConversion()
|
||||
{
|
||||
assertEquals(convertSpeed(5, 'm/s', 'mph'), 11.18);
|
||||
assertEquals(convertSpeed(5, 'ft/s', 'km/h'), 5.49);
|
||||
assertEquals(convertSpeed(3, 'km/h', 'km/h'), 3);
|
||||
assertEquals(convertSpeed(7, 'kn', 'km/h'), 12.96);
|
||||
assertEquals(convertSpeed(12, 'mph', 'km/h'), 19.31);
|
||||
assertEquals(convertSpeed(0.514, 'm/s', 'kn'), 1);
|
||||
|
||||
$this->expectException(\Exception::class);
|
||||
convertSpeed('1', 'km/h', 'mph');
|
||||
|
||||
$this->expectException(\Exception::class);
|
||||
convertSpeed(1, 'km/h', 'miles');
|
||||
}
|
||||
}
|
||||
|
48
tests/Graphs/DepthFirstSearchTest.php
Normal file
48
tests/Graphs/DepthFirstSearchTest.php
Normal file
@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . '/../../vendor/autoload.php';
|
||||
require_once __DIR__ . '/../../Graphs/DepthFirstSearch.php';
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class DepthFirstSearchTest extends TestCase
|
||||
{
|
||||
public function testDepthFirstSearch()
|
||||
{
|
||||
$graph = array(
|
||||
"A" => ["B", "C", "D"],
|
||||
"B" => ["A", "D", "E"],
|
||||
"C" => ["A", "F"],
|
||||
"D" => ["B", "D"],
|
||||
"E" => ["B", "F"],
|
||||
"F" => ["C", "E", "G"],
|
||||
"G" => ["F"],
|
||||
);
|
||||
$visited = dfs($graph, "A");
|
||||
$this->assertEquals(["A", "B", "D", "E", "F", "C", "G"], $visited);
|
||||
}
|
||||
public function testDepthFirstSearch2()
|
||||
{
|
||||
$graph = array(
|
||||
[1, 2],
|
||||
[2],
|
||||
[0, 3],
|
||||
[3],
|
||||
);
|
||||
|
||||
$visited = dfs($graph, 2);
|
||||
$this->assertEquals([2, 0, 1, 3], $visited);
|
||||
}
|
||||
public function testDepthFirstSearch3()
|
||||
{
|
||||
$graph = array(
|
||||
[2, 3, 1],
|
||||
[0],
|
||||
[0, 4],
|
||||
[0],
|
||||
[2],
|
||||
);
|
||||
$visited = dfs($graph, 0);
|
||||
$this->assertEquals([0, 2, 4, 3, 1], $visited);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user