fizz-buzz 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +20 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +29 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/lib/fizz-buzz.rb +41 -0
- data/test/helper.rb +18 -0
- data/test/test_fizz-buzz.rb +47 -0
- metadata +108 -0
data/.document
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "shoulda", ">= 0"
|
10
|
+
gem "bundler", "~> 1.0.0"
|
11
|
+
gem "jeweler", "~> 1.6.4"
|
12
|
+
gem "rcov", ">= 0"
|
13
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
git (1.2.5)
|
5
|
+
jeweler (1.6.4)
|
6
|
+
bundler (~> 1.0)
|
7
|
+
git (>= 1.2.5)
|
8
|
+
rake
|
9
|
+
rake (0.9.2.2)
|
10
|
+
rcov (0.9.11)
|
11
|
+
shoulda (2.11.3)
|
12
|
+
|
13
|
+
PLATFORMS
|
14
|
+
ruby
|
15
|
+
|
16
|
+
DEPENDENCIES
|
17
|
+
bundler (~> 1.0.0)
|
18
|
+
jeweler (~> 1.6.4)
|
19
|
+
rcov
|
20
|
+
shoulda
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Kerri Miller
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
= fizz-buzz
|
2
|
+
|
3
|
+
== Why?
|
4
|
+
|
5
|
+
In interviews, I'm constantly asked to whiteboard (or actually code) my solution to the FizzBuzz problem. Its not a terribly challenging question:
|
6
|
+
|
7
|
+
"Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”."
|
8
|
+
|
9
|
+
Most solid programmers who know their chops should be able to write it on paper in under 5 minutes. Lately its become one of the more fashionable interview questions, and while FizzBuzz problems will weed out the utterly lost and clueless, it really should be the first step in evaluating a candidate's potential, not the last.
|
10
|
+
|
11
|
+
With that in mind, and suffer from insomnia, I decided to go a step further:
|
12
|
+
|
13
|
+
* TDD
|
14
|
+
* making it a gem
|
15
|
+
* query methods
|
16
|
+
* putting it on the githubs
|
17
|
+
|
18
|
+
|
19
|
+
More on FizzBuzz problems, and how they fit into the interview process
|
20
|
+
|
21
|
+
* http://c2.com/cgi/wiki?FizzBuzzTest
|
22
|
+
* http://imranontech.com/2007/01/24/using-fizzbuzz-to-find-developers-who-grok-coding/
|
23
|
+
|
24
|
+
|
25
|
+
== Copyright
|
26
|
+
|
27
|
+
Copyright (c) 2011 Kerri Miller. See LICENSE.txt for
|
28
|
+
further details.
|
29
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "fizz-buzz"
|
18
|
+
gem.homepage = "http://github.com/kerrizor/fizz-buzz"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{A basic implementation of Fizz Buzz as a gem.}
|
21
|
+
gem.description = %Q{I got sick of doing this test in interviews, so next time someone asks me to do so, I'm going to instead hand them a business card with a URL for this gem on it.}
|
22
|
+
gem.email = "kerrizor@kerrizor.com"
|
23
|
+
gem.authors = ["Kerri Miller"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rake/testtask'
|
29
|
+
Rake::TestTask.new(:test) do |test|
|
30
|
+
test.libs << 'lib' << 'test'
|
31
|
+
test.pattern = 'test/**/test_*.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
|
35
|
+
require 'rcov/rcovtask'
|
36
|
+
Rcov::RcovTask.new do |test|
|
37
|
+
test.libs << 'test'
|
38
|
+
test.pattern = 'test/**/test_*.rb'
|
39
|
+
test.verbose = true
|
40
|
+
test.rcov_opts << '--exclude "gems/*"'
|
41
|
+
end
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "fizz-buzz #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/lib/fizz-buzz.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
class FizzBuzz
|
2
|
+
|
3
|
+
def crunch
|
4
|
+
return_values = Array.new
|
5
|
+
|
6
|
+
(1..100).each do |n|
|
7
|
+
if (n % 15) == 0
|
8
|
+
return_values << "FizzBuzz"
|
9
|
+
elsif (n % 5) == 0
|
10
|
+
return_values << "Buzz"
|
11
|
+
elsif (n % 3) == 0
|
12
|
+
return_values << "Fizz"
|
13
|
+
else
|
14
|
+
return_values << n
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
return return_values
|
19
|
+
end
|
20
|
+
|
21
|
+
def fizz?(n)
|
22
|
+
if (n % 3) == 0
|
23
|
+
return :true
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
def buzz?(n)
|
29
|
+
if (n % 5) == 0
|
30
|
+
return :true
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
def fizzbuzz?(n)
|
36
|
+
if (n % 15) == 0
|
37
|
+
return :true
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'test/unit'
|
11
|
+
require 'shoulda'
|
12
|
+
|
13
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
14
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
15
|
+
require 'fizz-buzz'
|
16
|
+
|
17
|
+
class Test::Unit::TestCase
|
18
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestFizzBuzz < MiniTest::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
@fizzbuzz = FizzBuzz.new
|
7
|
+
end
|
8
|
+
|
9
|
+
|
10
|
+
def test_new_fizzbuzz
|
11
|
+
assert_instance_of FizzBuzz, FizzBuzz.new
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
def test_crunch
|
16
|
+
assert_respond_to @fizzbuzz, :crunch
|
17
|
+
assert_kind_of Array, @fizzbuzz.crunch
|
18
|
+
|
19
|
+
assert_equal 100, @fizzbuzz.crunch.length
|
20
|
+
assert_equal "Fizz", @fizzbuzz.crunch[2]
|
21
|
+
assert_equal "4", @fizzbuzz.crunch[3].to_s
|
22
|
+
assert_equal "Buzz", @fizzbuzz.crunch[4]
|
23
|
+
assert_equal "FizzBuzz", @fizzbuzz.crunch[14]
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
def test_will_it_fizz
|
28
|
+
assert_respond_to @fizzbuzz, :fizz?
|
29
|
+
assert @fizzbuzz.fizz?(3)
|
30
|
+
assert_nil @fizzbuzz.fizz?(2)
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
def test_will_it_buzz
|
35
|
+
assert_respond_to @fizzbuzz, :buzz?
|
36
|
+
assert @fizzbuzz.buzz?(5)
|
37
|
+
assert_nil @fizzbuzz.buzz?(2)
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
def test_will_it_fizzbuzz
|
42
|
+
assert_respond_to @fizzbuzz, :fizzbuzz?
|
43
|
+
assert @fizzbuzz.fizzbuzz?(30)
|
44
|
+
assert_nil @fizzbuzz.fizzbuzz?(2)
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fizz-buzz
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Kerri Miller
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-11-19 00:00:00.000000000 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: shoulda
|
17
|
+
requirement: &2162558880 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '0'
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *2162558880
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: bundler
|
28
|
+
requirement: &2162558400 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.0.0
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *2162558400
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: jeweler
|
39
|
+
requirement: &2162557920 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ~>
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: 1.6.4
|
45
|
+
type: :development
|
46
|
+
prerelease: false
|
47
|
+
version_requirements: *2162557920
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: rcov
|
50
|
+
requirement: &2162557440 !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
type: :development
|
57
|
+
prerelease: false
|
58
|
+
version_requirements: *2162557440
|
59
|
+
description: I got sick of doing this test in interviews, so next time someone asks
|
60
|
+
me to do so, I'm going to instead hand them a business card with a URL for this
|
61
|
+
gem on it.
|
62
|
+
email: kerrizor@kerrizor.com
|
63
|
+
executables: []
|
64
|
+
extensions: []
|
65
|
+
extra_rdoc_files:
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.rdoc
|
68
|
+
files:
|
69
|
+
- .document
|
70
|
+
- Gemfile
|
71
|
+
- Gemfile.lock
|
72
|
+
- LICENSE.txt
|
73
|
+
- README.rdoc
|
74
|
+
- Rakefile
|
75
|
+
- VERSION
|
76
|
+
- lib/fizz-buzz.rb
|
77
|
+
- test/helper.rb
|
78
|
+
- test/test_fizz-buzz.rb
|
79
|
+
has_rdoc: true
|
80
|
+
homepage: http://github.com/kerrizor/fizz-buzz
|
81
|
+
licenses:
|
82
|
+
- MIT
|
83
|
+
post_install_message:
|
84
|
+
rdoc_options: []
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
none: false
|
89
|
+
requirements:
|
90
|
+
- - ! '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
segments:
|
94
|
+
- 0
|
95
|
+
hash: 4354968780685390581
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
requirements: []
|
103
|
+
rubyforge_project:
|
104
|
+
rubygems_version: 1.6.2
|
105
|
+
signing_key:
|
106
|
+
specification_version: 3
|
107
|
+
summary: A basic implementation of Fizz Buzz as a gem.
|
108
|
+
test_files: []
|