Skip to content

Instantly share code, notes, and snippets.

@ScottGo
Last active December 15, 2015 20:49
Show Gist options
  • Save ScottGo/5321341 to your computer and use it in GitHub Desktop.
Save ScottGo/5321341 to your computer and use it in GitHub Desktop.
20130305_Fizzbuzzed exercise.rb
def super_fizzbuzz(array)
fizzbuzzed = []
array.each do |num|
if num % 3 == 0 && num % 5 == 0 # return "FizzBuzz"
fizzbuzzed.push("FizzBuzz")
elseif num % 3 == 0 # return "Fizz"
fizzbuzzed.push("Fizz")
elseif num % 5 == 0 # return "Buzz"
fizzbuzzed.push("Buzz")
else #retain original integer
fizzbuzzed.push(num)
end
end
@ScottGo
Copy link
Author

ScottGo commented Apr 5, 2013

This returns an array of the given size containing random powers of base

Since no power of 3 is ever a multiple of 5, this lets us create random arrays

that contain multiples of 3 and no multiples of 5

def random_input_array(base, size)
(1..size).map { |i| base**(1+rand(15)) }
end

describe "super_fizzbuzz" do
it "returns 'Fizz' for multiples of 3" do
super_fizzbuzz(random_input_array(3,100)).should eq ["Fizz"]*100
end

it "returns 'Buzz' for multiples of 5" do
super_fizzbuzz(random_input_array(5,100)).should eq ["Buzz"]*100
end

it "returns 'FizzBuzz' for multiples of 15" do
super_fizzbuzz(random_input_array(15, 100)).should eq ["FizzBuzz"]*100
end

it "works on [1,2,3]" do
super_fizzbuzz([1,2,3]).should eq [1,2,'Fizz']
end

it "works on [15, 5, 3, 1]" do
super_fizzbuzz([15, 5, 3, 1]).should eq ['FizzBuzz', 'Buzz', 'Fizz', 1]
end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment