- https://www.codewars.com/kata/5855777bb45c01bada0002ac Resistor Color Codes, Part 2.
Started at 17.11.2020 22:10 Tuesday.
Finished at 20.11.2020 22:22 Friday.
<?php | |
# https://www.codewars.com/kata/5855777bb45c01bada0002ac Resistor Color Codes, Part 2. | |
function encodeResistorColors($ohmsString) { | |
$hash = [ | |
0 => "black", | |
1 => "brown", | |
2 => "red", | |
3 => "orange", | |
4 => "yellow", | |
5 => "green", | |
6 => "blue", | |
7 => "violet", | |
8 => "gray", | |
9 => "white", | |
"k" => 3, | |
"M" => 6, | |
"" => 0, | |
]; | |
preg_match('/^(\d+)((\.)(\d+))?(\w)?/',$ohmsString,$matches); | |
var_dump($matches); | |
$digit = $matches[1]; | |
$text = $matches[5]; | |
$digit2 = $matches[4]; | |
$mt = $hash[$text]; | |
if(strlen($digit) >= 2){ | |
$mul = strlen($digit) - 2 + $mt; | |
$first = $hash[intval($digit[0])]; | |
$second = $hash[intval($digit[1])]; | |
$third = $hash[$mul]; | |
return "$first $second $third gold"; | |
} | |
if(1 == strlen($digit) && "" == $digit2){ | |
$first = $hash[intval($digit[0])]; | |
$second = $hash[0]; | |
$third = $hash[$mt-1]; | |
return "$first $second $third gold"; | |
} | |
if(1 == strlen($digit) && "" != $digit2){ | |
$first = $hash[intval($digit[0])]; | |
$second = $hash[intval($digit2[0])];; | |
$third = $hash[$mt-1]; | |
return "$first $second $third gold"; | |
} | |
} | |
class DefaultArgumentsTest extends TestCase { | |
public function testSomeCommonResistorValues() { | |
$this->assertEquals("brown black black gold", encodeResistorColors("10 ohms")); | |
$this->assertEquals("yellow violet black gold", encodeResistorColors("47 ohms")); | |
$this->assertEquals("brown black brown gold", encodeResistorColors("100 ohms")); | |
$this->assertEquals("red red brown gold", encodeResistorColors("220 ohms")); | |
$this->assertEquals("orange orange brown gold", encodeResistorColors("330 ohms")); | |
$this->assertEquals("yellow violet brown gold", encodeResistorColors("470 ohms")); | |
$this->assertEquals("blue gray brown gold", encodeResistorColors("680 ohms")); | |
/** | |
$this->assertEquals("brown black red gold", encodeResistorColors("1k ohms")); | |
$this->assertEquals("yellow violet red gold", encodeResistorColors("4.7k ohms")); | |
$this->assertEquals("brown black orange gold", encodeResistorColors("10k ohms")); | |
$this->assertEquals("red red orange gold", encodeResistorColors("22k ohms")); | |
$this->assertEquals("yellow violet orange gold", encodeResistorColors("47k ohms")); | |
$this->assertEquals("brown black yellow gold", encodeResistorColors("100k ohms")); | |
$this->assertEquals("orange orange yellow gold", encodeResistorColors("330k ohms")); | |
$this->assertEquals("brown black green gold", encodeResistorColors("1M ohms")); | |
$this->assertEquals("red black green gold", encodeResistorColors("2M ohms"));*/ | |
} | |
} |
Started at 17.11.2020 22:10 Tuesday.
Finished at 20.11.2020 22:22 Friday.