Skip to content

Instantly share code, notes, and snippets.

@AbstractBeliefs
Created May 5, 2014 15:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AbstractBeliefs/6bb0e8ae113ee2ce9bd0 to your computer and use it in GitHub Desktop.
Save AbstractBeliefs/6bb0e8ae113ee2ce9bd0 to your computer and use it in GitHub Desktop.

Raindrops

Write a program that converts a number to a string, the contents of which depends on the number's prime factors.

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

Examples

  • 28's prime-factorization is 2, 2, 7.
    • In raindrop-speak, this would be a simple "Plong".
  • 1755 prime-factorization is 3, 3, 3, 5, 13.
    • In raindrop-speak, this would be a "PlingPlang".
  • The prime factors of 34 are 2 and 17.
    • Raindrop-speak doesn't know what to make of that, so it just goes with the straightforward "34".

Source

A variation on a famous interview question intended to weed out the obviously incompetent. view source

package raindrops
import "testing"
var tests = []struct {
input int
expected string
}{
{1, "1"},
{3, "Pling"},
{5, "Plang"},
{7, "Plong"},
{6, "Pling"},
{9, "Pling"},
{10, "Plang"},
{14, "Plong"},
{15, "PlingPlang"},
{21, "PlingPlong"},
{25, "Plang"},
{35, "PlangPlong"},
{49, "Plong"},
{52, "52"},
{105, "PlingPlangPlong"},
{12121, "12121"},
}
func TestConvert(t *testing.T) {
for _, test := range tests {
if actual := Convert(test.input); actual != test.expected {
t.Errorf("Convert(%d) expected %q, Actual %q", test.input, test.expected, actual)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment