# 4d47's solution

## to Raindrops in the PHP Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Convert a number to a string, the contents of which depend on the number's factors.

• If the number has 3 as a factor, output 'Pling'.
• If the number has 5 as a factor, output 'Plang'.
• If the number has 7 as a factor, output 'Plong'.
• If the number does not have 3, 5, or 7 as a factor, just pass the number's digits straight through.

## Examples

• 28's factors are 1, 2, 4, 7, 14, 28.
• In raindrop-speak, this would be a simple "Plong".
• 30's factors are 1, 2, 3, 5, 6, 10, 15, 30.
• In raindrop-speak, this would be a "PlingPlang".
• 34 has four factors: 1, 2, 17, and 34.
• In raindrop-speak, this would be "34".

## Running the tests

1. Go to the root of your PHP exercise directory, which is `<EXERCISM_WORKSPACE>/php`. To find the Exercism workspace run

`````` % exercism debug | grep Workspace
``````
2. Get PHPUnit if you don't have it already.

`````` % wget --no-check-certificate https://phar.phpunit.de/phpunit.phar
% chmod +x phpunit.phar
``````
3. Execute the tests:

`````` % ./phpunit.phar raindrops/raindrops_test.php
``````

## Source

A variation on a famous interview question intended to weed out potential candidates. http://jumpstartlab.com

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### raindrops_test.php

``````<?php

require "raindrops.php";

class RaindropsTest extends PHPUnit\Framework\TestCase
{
public function test1()
{
\$this->assertSame("1", raindrops(1));
}

public function test3()
{
\$this->assertSame("Pling", raindrops(3));
}

public function test5()
{
\$this->assertSame("Plang", raindrops(5));
}

public function test7()
{
\$this->assertSame("Plong", raindrops(7));
}

public function test6()
{
\$this->assertSame("Pling", raindrops(6));
}

public function test9()
{
\$this->assertSame("Pling", raindrops(9));
}

public function test10()
{
\$this->assertSame("Plang", raindrops(10));
}

public function test14()
{
\$this->assertSame("Plong", raindrops(14));
}

public function test15()
{
\$this->assertSame("PlingPlang", raindrops(15));
}

public function test21()
{
\$this->assertSame("PlingPlong", raindrops(21));
}

public function test25()
{
\$this->assertSame("Plang", raindrops(25));
}

public function test35()
{
\$this->assertSame("PlangPlong", raindrops(35));
}

public function test49()
{
\$this->assertSame("Plong", raindrops(49));
}

public function test52()
{
\$this->assertSame("52", raindrops(52));
}

public function test105()
{
\$this->assertSame("PlingPlangPlong", raindrops(105));
}

public function test12121()
{
\$this->assertSame("12121", raindrops(12121));
}
}``````
``````<?php

function raindrops(\$num)
{
static \$factors = [
3 => 'Pling',
5 => 'Plang',
7 => 'Plong',
];
\$is_factor = function(\$fac) use (\$num) { return \$num % \$fac === 0; };
\$words = implode(array_filter(\$factors, \$is_factor, ARRAY_FILTER_USE_KEY));
return \$words ?: "\$num";
}``````