Skip to content

Instantly share code, notes, and snippets.

@hakobyansen
Last active June 5, 2020 22:22
Show Gist options
  • Select an option

  • Save hakobyansen/1fcf8671e713b00b95acb0e19ba4cb9b to your computer and use it in GitHub Desktop.

Select an option

Save hakobyansen/1fcf8671e713b00b95acb0e19ba4cb9b to your computer and use it in GitHub Desktop.
<?php
$converter = new Converter('ZZ');
echo $converter->getResult() . PHP_EOL;
class Converter
{
private $letters;
private $result = 0;
public function __construct(string $letters)
{
$this->letters = str_split($letters);
$this->setResult();
}
public function setResult()
{
$alphabet = $this->getAlphabet();
$alphabetLength = count($alphabet);
$lettersLength = count($this->letters);
if(!$lettersLength) {
return $this->result; // supposed to be 0
}
if($lettersLength === 1) {
return $alphabet[$this->letters[0]] + 1;
}
for($i = 0; $i < count($this->letters); $i++)
{
$lengthAtCurrentPosition = $alphabet[$this->letters[$i]] + 1;
$lettersLength -= 1;
$this->result += (pow($alphabetLength, $lettersLength) * $lengthAtCurrentPosition);
}
}
public function getAlphabet(): array
{
return array_flip(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']);
}
public function getResult(): int
{
return $this->result;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment