Determine if a sentence is a pangram. A pangram (Greek: παν γράμμα, pan gramma, "every letter") is a sentence using every letter of the alphabet at least once. The best known English pangram is:
The quick brown fox jumps over the lazy dog.
The alphabet used consists of ASCII letters a
to z
, inclusive, and is case
insensitive. Input may contain non-ASCII symbols, but it is only necessary to
check whether each letter in the English alphabet is present.
Go to the root of your PHP exercise directory, which is <EXERCISM_WORKSPACE>/php
.
To find the Exercism workspace run
% exercism debug | grep Workspace
Get PHPUnit if you don't have it already.
% wget --no-check-certificate https://phar.phpunit.de/phpunit.phar
% chmod +x phpunit.phar
Execute the tests:
% ./phpunit.phar pangram/pangram_test.php
Wikipedia https://en.wikipedia.org/wiki/Pangram
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
<?php
class PangramTest extends PHPUnit\Framework\TestCase
{
public static function setUpBeforeClass() : void
{
require_once 'pangram.php';
}
public function testSentenceEmpty() : void
{
$this->assertFalse(isPangram(''));
}
public function testPangramWithOnlyLowerCase() : void
{
$this->assertTrue(isPangram('the quick brown fox jumps over the lazy dog'));
}
public function testMissingCharacterX() : void
{
$this->assertFalse(isPangram('a quick movement of the enemy will jeopardize five gunboats'));
}
public function testAnotherMissingCharacterX() : void
{
$this->assertFalse(isPangram('the quick brown fish jumps over the lazy dog'));
}
public function testPangramWithUnderscores() : void
{
$this->assertTrue(isPangram('the_quick_brown_fox_jumps_over_the_lazy_dog'));
}
public function testPangramWithNumbers() : void
{
$this->assertTrue(isPangram('the 1 quick brown fox jumps over the 2 lazy dogs'));
}
public function testMissingLettersReplacedByNumbers() : void
{
$this->assertFalse(isPangram('7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog'));
}
public function testPangramWithMixedCaseAndPunctuation() : void
{
$this->assertTrue(isPangram('\Five quacking Zephyrs jolt my wax bed.\\'));
}
public function testPangramWithNonAsciiCharacters() : void
{
$this->assertTrue(isPangram('Victor jagt zwölf Boxkämpfer quer über den großen Sylter Deich.'));
}
public function testMissingLetterReplacedWithUpperCaseCharacter() : void
{
$this->assertFalse(isPangram("Tthe quick brown fo jumps over the lazy dog"));
}
}
<?php
function isPangram($text): bool
{
$text = str_split(strtolower($text));
$alphabets = str_split("abcdefghijklmnopqrstuvwxyz");
foreach($alphabets as $aplhabet){
if(!in_array($aplhabet,$text)){
return false;
}
}
return true;
}
?>
A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.
Here are some questions to help you reflect on this solution and learn the most from it.
Level up your programming skills with 3,122 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments