DEV Community

Discussion on: Daily Challenge #71 - See you next Happy Year

Collapse
 
anwar_nairi profile image
Anwar

Here is my solution using PHP:

if (!function_exists("hasUniqueDigits")) {
    function hasUniqueDigits(int $number): bool {
        $digits = str_split($number);

        return $digits === array_unique($digits);
    }
}

if (!function_exists("nextHappyYear")) {
    function nextHappyYear(int $year): int {
        $nextYear = $year + 1;

        while(!hasUniqueDigits($nextYear)) {
            $nextYear++;
        }

        return $nextYear;
    }
}

And here is my unit tests:

use PHPUnit\Framework\TestCase;

class NextHappyYearTest extends TestCase {
    public function testShouldReturnNextHappyYear() {
        $this->assertEquals(nextHappyYear(7712), 7801);
    }

    public function testShouldReturnOtherNextHappyYear() {
        $this->assertEquals(nextHappyYear(1001), 1023);
    }

    public function testShouldReturnOtherOtherNextHappyYear() {
        $this->assertEquals(nextHappyYear(2018), 2019);
    }
}

Hope you like it :)