4d47's solution

to Raindrops in the Perl 6 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".

Resources

Remember to check out the Perl 6 documentation and resources pages for information, tips, and examples if you get stuck.

Running the tests

There is a test suite and module included with the exercise. The test suite (a file with the extension `.t`) will attempt to run routines from the module (a file with the extension `.pm6`). Add/modify routines in the module so that the tests will pass! You can view the test data by executing the command `perl6 --doc *.t` (* being the name of the test suite), and run the test suite for the exercise by executing the command `prove6 .` in the exercise directory.

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.t

``````#!/usr/bin/env perl6
use v6;
use Test;
use JSON::Fast;
use lib \$?FILE.IO.dirname;
use Raindrops;
plan 18;

my \$c-data = from-json \$=pod.pop.contents;
for @(\$c-data<cases>) {
subtest {
plan 2;
is .<input><number>.&raindrop, |.<expected description>;
isa-ok .<input><number>.&raindrop, Str;
}
}

=begin code
{
"exercise": "raindrops",
"version": "1.1.0",
"cases": [
{
"description": "the sound for 1 is 1",
"property": "convert",
"input": {
"number": 1
},
"expected": "1"
},
{
"description": "the sound for 3 is Pling",
"property": "convert",
"input": {
"number": 3
},
"expected": "Pling"
},
{
"description": "the sound for 5 is Plang",
"property": "convert",
"input": {
"number": 5
},
"expected": "Plang"
},
{
"description": "the sound for 7 is Plong",
"property": "convert",
"input": {
"number": 7
},
"expected": "Plong"
},
{
"description": "the sound for 6 is Pling as it has a factor 3",
"property": "convert",
"input": {
"number": 6
},
"expected": "Pling"
},
{
"description": "2 to the power 3 does not make a raindrop sound as 3 is the exponent not the base",
"property": "convert",
"input": {
"number": 8
},
"expected": "8"
},
{
"description": "the sound for 9 is Pling as it has a factor 3",
"property": "convert",
"input": {
"number": 9
},
"expected": "Pling"
},
{
"description": "the sound for 10 is Plang as it has a factor 5",
"property": "convert",
"input": {
"number": 10
},
"expected": "Plang"
},
{
"description": "the sound for 14 is Plong as it has a factor of 7",
"property": "convert",
"input": {
"number": 14
},
"expected": "Plong"
},
{
"description": "the sound for 15 is PlingPlang as it has factors 3 and 5",
"property": "convert",
"input": {
"number": 15
},
"expected": "PlingPlang"
},
{
"description": "the sound for 21 is PlingPlong as it has factors 3 and 7",
"property": "convert",
"input": {
"number": 21
},
"expected": "PlingPlong"
},
{
"description": "the sound for 25 is Plang as it has a factor 5",
"property": "convert",
"input": {
"number": 25
},
"expected": "Plang"
},
{
"description": "the sound for 27 is Pling as it has a factor 3",
"property": "convert",
"input": {
"number": 27
},
"expected": "Pling"
},
{
"description": "the sound for 35 is PlangPlong as it has factors 5 and 7",
"property": "convert",
"input": {
"number": 35
},
"expected": "PlangPlong"
},
{
"description": "the sound for 49 is Plong as it has a factor 7",
"property": "convert",
"input": {
"number": 49
},
"expected": "Plong"
},
{
"description": "the sound for 52 is 52",
"property": "convert",
"input": {
"number": 52
},
"expected": "52"
},
{
"description": "the sound for 105 is PlingPlangPlong as it has factors 3, 5 and 7",
"property": "convert",
"input": {
"number": 105
},
"expected": "PlingPlangPlong"
},
{
"description": "the sound for 3125 is Plang as it has a factor 5",
"property": "convert",
"input": {
"number": 3125
},
"expected": "Plang"
}
]
}
=end code``````
``````unit class Raindrops;

method convert(UInt \$n --> Cool) {
# 'is cached' on methods not yet implemented.
my \$output = '';
\$output ~= 'Pling' if \$n %% 3;
\$output ~= 'Plang' if \$n %% 5;
\$output ~= 'Plong' if \$n %% 7;
\$output || \$n
}``````