Avatar of JaeHyoLee

JaeHyoLee's solution

to Raindrops in the Lua Track

Published at Jul 13 2018 · 2 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

To run the tests, run the command busted from within the exercise directory.

Further information

For more detailed information about the Lua track, including how to get help if you're having trouble, please visit the exercism.io Lua language page.

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_spec.lua

local raindrops = require('raindrops')

describe('raindrops', function()
  it('the sound for 1 is 1', function()
    assert.equal('1', raindrops(1))
  end)

  it('the sound for 3 is Pling', function()
    assert.equal('Pling', raindrops(3))
  end)

  it('the sound for 5 is Plang', function()
    assert.equal('Plang', raindrops(5))
  end)

  it('the sound for 7 is Plong', function()
    assert.equal('Plong', raindrops(7))
  end)

  it('the sound for 6 is Pling as it has a factor 3', function()
    assert.equal('Pling', raindrops(6))
  end)

  it('2 to the power 3 does not make a raindrop sound as 3 is the exponent not the base', function()
    assert.equal('8', raindrops(8))
  end)

  it('the sound for 9 is Pling as it has a factor 3', function()
    assert.equal('Pling', raindrops(9))
  end)

  it('the sound for 10 is Plang as it has a factor 5', function()
    assert.equal('Plang', raindrops(10))
  end)

  it('the sound for 14 is Plong as it has a factor 7', function()
    assert.equal('Plong', raindrops(7))
  end)

  it('the sound for 15 is PlingPlang as it has a factor 3 and 5', function()
    assert.equal('PlingPlang', raindrops(15))
  end)

  it('the sound for 21 is PlingPlong as it has factors 3 and 7', function()
    assert.equal('PlingPlong', raindrops(21))
  end)

  it('the sound for 25 is Plang as it has a factor 5', function()
    assert.equal('Plang', raindrops(25))
  end)

  it('the sound for 27 is Pling as it has a factor 3', function()
    assert.equal('Pling', raindrops(27))
  end)

  it('the sound for 35 is PlangPlong as it has factors 5 and 7', function()
    assert.equal('PlangPlong', raindrops(35))
  end)

  it('the sound for 49 is Plong as it has a factor 7', function()
    assert.equal('Plong', raindrops(49))
  end)

  it('the sound for 52 is 52', function()
    assert.equal('52', raindrops(52))
  end)

  it('the sound for 105 is PlingPlangPlong as it has factors 3, 5 and 7', function()
    assert.equal('PlingPlangPlong', raindrops(105))
  end)

  it('the sound for 3125 is Plang as it has a factor 5', function()
    assert.equal('Plang', raindrops(3125))
  end)
end)
return function(number)
  local result
  result = (number%3 == 0 and 'Pling' or '') .. (number%5 == 0 and 'Plang' or '') .. (number%7 == 0 and 'Plong' or '')
  return #result == 0 and tostring(number) or result
end

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of schecky

parens around the anded components may have helped me out with the intended logic bug I figured it out. Interesting approach!

Avatar of ryanplusplus

@schecky I agree that the foo and bar or baz pattern looks a bit weird, but after a while you stop trying to parse out the logic and just recognize the pattern as a rough ternary operator.

What can you learn from this solution?

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.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?