fizzbuzzer 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: da58d7c29be8ab792904f6568b0accce17d2cf3c
4
- data.tar.gz: 414323b0ae95450d79aa2d0da5be1c065470e57e
3
+ metadata.gz: 257500b27fdf676a85d3671553510fcde0ec2fa3
4
+ data.tar.gz: 69812a749e44213e790167fb8b925391ea85317f
5
5
  SHA512:
6
- metadata.gz: 4c75335ebca864f1d72568312fb813b53b16ef46fc79c01f3331bea11ce8b4fed9cc06ee17d10630986f135a919d1e84868c32de8845b5115ba63369b2a9aaf3
7
- data.tar.gz: 86134a7767428e28906ce3539714265b8f7910c3a0ead9928dfa39e16288117e78993e67c499f4b56ee1379b50737561c0f4e1c72f70e1277737bab9e48aaac4
6
+ metadata.gz: 3eeb4670da60daa491e639286d11804329eee875f11847ee11d94a95a897ea2dffc2c01e625517bbca732e0f0896375e667f0b08daf04a38619d1176feb1a86d
7
+ data.tar.gz: 6116a56553ef18c25dda8ef1cd0903bf8206fc340dac31f5fea980824ac1e4e02138a6f91221cd3dccf667cf7376d3e68d031678ee32088e450f65c74eedaeda
data/Manifest.txt CHANGED
@@ -6,5 +6,6 @@ bin/fizzbuzz
6
6
  lib/fizzbuzzer.rb
7
7
  lib/fizzbuzzer/version.rb
8
8
  test/helper.rb
9
+ test/test_driven.rb
9
10
  test/test_fizzbuzz.rb
10
11
  test/test_version.rb
data/README.md CHANGED
@@ -182,28 +182,27 @@ end
182
182
 
183
183
  ``` ruby
184
184
  class Fizznum
185
- attr_accessor :num
186
185
 
187
- def initialize(num)
188
- self.num = num
186
+ def initialize(n)
187
+ @n = n
189
188
  end
190
189
 
191
- def fizzbuzz?() num % 3 == 0 && num % 5 == 0; end
192
- def fizz?() num % 3 == 0; end
193
- def buzz?() num % 5 == 0; end
190
+ def fizzbuzz?() @n % 3 == 0 && @n % 5 == 0; end
191
+ def fizz?() @n % 3 == 0; end
192
+ def buzz?() @n % 5 == 0; end
194
193
 
195
- def to_s
194
+ def val
196
195
  if fizzbuzz? then "FizzBuzz"
197
196
  elsif fizz? then "Fizz"
198
197
  elsif buzz? then "Buzz"
199
- else num.to_s
198
+ else @n
200
199
  end
201
200
  end
202
201
 
203
202
  end
204
203
 
205
204
  def fizzbuzz
206
- (1..100).map{ |n| Fizznum.new(n).to_s }
205
+ (1..100).map{ |n| Fizznum.new(n).val }
207
206
  end
208
207
  ```
209
208
 
@@ -308,34 +307,34 @@ end
308
307
  ```
309
308
 
310
309
 
311
-
312
- ## Pattern Matching with Noaidi
310
+ ## Poor Man's (Do-It-Yourself) Pattern Matching
313
311
 
314
312
  ``` ruby
315
- require "noaidi"
316
-
317
313
  def fizzbuzz
318
314
  (1..100).map do |n|
319
- Noaidi.match [n % 3, n % 5] do |m|
320
- m.(0, 0) { "FizzBuzz" }
321
- m.(0, Fixnum) { "Fizz" }
322
- m.(Fixnum, 0) { "Buzz" }
323
- m.(Fixnum, Fixnum) { n }
315
+ case [n % 3 == 0, n % 5 == 0]
316
+ when [true, true] then "FizzBuzz"
317
+ when [true, false] then "Fizz"
318
+ when [false, true] then "Buzz"
319
+ else n
324
320
  end
325
321
  end
326
322
  end
327
323
  ```
328
324
 
329
- ## Poor Man's (Do-It-Yourself) Pattern Matching
325
+
326
+ ## Pattern Matching with Noaidi
330
327
 
331
328
  ``` ruby
329
+ require "noaidi"
330
+
332
331
  def fizzbuzz
333
332
  (1..100).map do |n|
334
- case [n % 3, n % 5].map {|remainder| remainder.zero? }
335
- when [true, true] then "FizzBuzz"
336
- when [true, false] then "Fizz"
337
- when [false, true] then "Buzz"
338
- else n
333
+ Noaidi.match [n % 3, n % 5] do |m|
334
+ m.(0, 0) { "FizzBuzz" }
335
+ m.(0, Fixnum) { "Fizz" }
336
+ m.(Fixnum, 0) { "Buzz" }
337
+ m.(Fixnum, Fixnum) { n }
339
338
  end
340
339
  end
341
340
  end
@@ -349,21 +348,21 @@ require "dry-matcher"
349
348
 
350
349
  FizzBuzz = Dry::Matcher.new(
351
350
  fizzbuzz: Dry::Matcher::Case.new(
352
- match: -> value { value % 3 == 0 && value % 5 == 0 },
351
+ match: -> value { value[0] == 0 && value[1] == 0 },
353
352
  resolve: -> value { "FizzBuzz" } ),
354
353
  fizz: Dry::Matcher::Case.new(
355
- match: -> value { value % 3 == 0 },
354
+ match: -> value { value[0] == 0 },
356
355
  resolve: -> value { "Fizz" } ),
357
356
  buzz: Dry::Matcher::Case.new(
358
- match: -> value { value % 5 == 0 },
357
+ match: -> value { value[1] == 0 },
359
358
  resolve: -> value { "Buzz" } ),
360
359
  other: Dry::Matcher::Case.new(
361
360
  match: -> value { true },
362
- resolve: -> value { value } ))
361
+ resolve: -> value { value[2] } ))
363
362
 
364
363
  def fizzbuzz
365
364
  (1..100).map do |n|
366
- FizzBuzz.(n) do |m|
365
+ FizzBuzz.([n % 3, n % 5, n]) do |m|
367
366
  m.fizzbuzz { |v| v }
368
367
  m.fizz { |v| v }
369
368
  m.buzz { |v| v }
@@ -374,26 +373,99 @@ end
374
373
  ```
375
374
 
376
375
 
377
- ## Rotate / No Conditionals
376
+ ## Test Driven Development (TDD)
378
377
 
379
378
  ``` ruby
380
- def fizzbuzz
381
- fizzy = [1,0,0]
382
- buzzy = [1,0,0,0,0]
379
+ describe FizzBuzz do
380
+ before do
381
+ @result = fizzbuzz
382
+ end
383
383
 
384
- (1..100).map do |n|
385
- fizzy.rotate!
386
- buzzy.rotate!
387
- result = n.to_s * (1 - (fizzy[0] | buzzy[0]))
388
- result << "Fizz" * fizzy[0]
389
- result << "Buzz" * buzzy[0]
390
- result
384
+ it "returns 'Fizz' for multiples of 3" do
385
+ @result[3-1].must_equal "Fizz"
386
+ end
387
+
388
+ it "returns 'Buzz' for multiples of 5" do
389
+ @result[5-1].must_equal "Buzz"
390
+ end
391
+
392
+ it "returns 'FizzBuzz' for multiples of 3 and 5" do
393
+ @result[15-1].must_equal "FizzBuzz"
394
+ end
395
+
396
+ it "returns the passed number if not a multiple of 3 or 5" do
397
+ @result[1-1].must_equal 1
398
+ end
399
+ end
400
+ ```
401
+
402
+ Or use asserts for testing:
403
+
404
+ ``` ruby
405
+ class TestFizzBuzz < Minitest::Test
406
+
407
+ def setup
408
+ @result = fizzbuzz
409
+ end
410
+
411
+ def test_fizz_multiples_of_3
412
+ assert_equal "Fizz", @result[3-1]
413
+ end
414
+
415
+ def test_buzz_for_multiples_of_5
416
+ assert_equal "Buzz", @result[5-1]
417
+ end
418
+
419
+ def test_fizzbuzz_for_multiples_of_3_and_5
420
+ assert_equal "FizzBuzz", @result[15-1]
421
+ end
422
+
423
+ def test_number_if_not_multiple_of_3_or_5
424
+ assert_equal 1, @result[1-1]
425
+ end
426
+
427
+ end
428
+ ```
429
+
430
+ And here's another fizzbuzz algorithm using a classic `for` loop:
431
+
432
+ ``` ruby
433
+ def fizzbuzz
434
+ result = []
435
+ for n in 1..100 do
436
+ result << if n % 3 == 0 && n % 5 == 0 then "FizzBuzz"
437
+ elsif n % 3 == 0 then "Fizz"
438
+ elsif n % 5 == 0 then "Buzz"
439
+ else n
440
+ end
391
441
  end
442
+ result
392
443
  end
393
444
  ```
394
445
 
395
- Contributed by [John Schank](https://github.com/jschank).
446
+ Or use an all-in-one assert as used
447
+ for testing all the fizzbuzz algorithms :-) in this repo:
396
448
 
449
+ ``` ruby
450
+ FIZZBUZZES_1_TO_100 =
451
+ [1, 2, "Fizz", 4, "Buzz", "Fizz", 7, 8, "Fizz", "Buzz", 11, "Fizz", 13, 14,
452
+ "FizzBuzz", 16, 17, "Fizz", 19, "Buzz", "Fizz", 22, 23, "Fizz", "Buzz", 26,
453
+ "Fizz", 28, 29, "FizzBuzz", 31, 32, "Fizz", 34, "Buzz", "Fizz", 37, 38,
454
+ "Fizz", "Buzz", 41, "Fizz", 43, 44, "FizzBuzz", 46, 47, "Fizz", 49, "Buzz",
455
+ "Fizz", 52, 53, "Fizz", "Buzz", 56, "Fizz", 58, 59, "FizzBuzz", 61, 62,
456
+ "Fizz", 64, "Buzz", "Fizz", 67, 68, "Fizz", "Buzz", 71, "Fizz", 73, 74,
457
+ "FizzBuzz", 76, 77, "Fizz", 79, "Buzz", "Fizz", 82, 83, "Fizz", "Buzz", 86,
458
+ "Fizz", 88, 89, "FizzBuzz", 91, 92, "Fizz", 94, "Buzz", "Fizz", 97, 98,
459
+ "Fizz", "Buzz"]
460
+
461
+ class TestFizzBuzz < Minitest::Test
462
+
463
+ def test_fizzbuzz
464
+ assert_equal FIZZBUZZES_1_TO_100, fizzbuzz
465
+ end
466
+
467
+ end
468
+ ```
397
469
 
398
470
 
399
471
  ## With Parameterization
data/lib/fizzbuzzer.rb CHANGED
@@ -107,27 +107,25 @@ end
107
107
 
108
108
  module V4
109
109
  class Fizznum
110
- attr_accessor :num
111
-
112
- def initialize(num)
113
- self.num = num
110
+ def initialize(n)
111
+ @n = n
114
112
  end
115
113
 
116
- def fizzbuzz?() num % 3 == 0 && num % 5 == 0; end
117
- def fizz?() num % 3 == 0; end
118
- def buzz?() num % 5 == 0; end
114
+ def fizzbuzz?() @n % 3 == 0 && @n % 5 == 0; end
115
+ def fizz?() @n % 3 == 0; end
116
+ def buzz?() @n % 5 == 0; end
119
117
 
120
- def to_s
118
+ def val
121
119
  if fizzbuzz? then "FizzBuzz"
122
120
  elsif fizz? then "Fizz"
123
121
  elsif buzz? then "Buzz"
124
- else num.to_s
122
+ else @n
125
123
  end
126
124
  end
127
125
  end
128
126
 
129
127
  def fizzbuzz
130
- (1..100).map{ |n| Fizznum.new(n).to_s }
128
+ (1..100).map{ |n| Fizznum.new(n).val }
131
129
  end
132
130
  end
133
131
 
@@ -239,6 +237,20 @@ end
239
237
 
240
238
 
241
239
  module V8
240
+ def fizzbuzz
241
+ (1..100).map do |n|
242
+ case [n % 3 == 0, n % 5 == 0]
243
+ when [true, true] then "FizzBuzz"
244
+ when [true, false] then "Fizz"
245
+ when [false, true] then "Buzz"
246
+ else n
247
+ end
248
+ end
249
+ end
250
+ end
251
+
252
+
253
+ module V9
242
254
  require "noaidi"
243
255
 
244
256
  def fizzbuzz
@@ -254,40 +266,26 @@ end
254
266
  end
255
267
 
256
268
 
257
- module V9
258
- def fizzbuzz
259
- (1..100).map do |n|
260
- case [n % 3, n % 5].map {|remainder| remainder.zero? }
261
- when [true, true] then "FizzBuzz"
262
- when [true, false] then "Fizz"
263
- when [false, true] then "Buzz"
264
- else n
265
- end
266
- end
267
- end
268
- end
269
-
270
-
271
269
  module V10
272
270
  require "dry-matcher"
273
271
 
274
272
  FizzBuzz = Dry::Matcher.new(
275
273
  fizzbuzz: Dry::Matcher::Case.new(
276
- match: -> value { value % 3 == 0 && value % 5 == 0 },
274
+ match: -> value { value[0] == 0 && value[1] == 0 },
277
275
  resolve: -> value { "FizzBuzz" } ),
278
276
  fizz: Dry::Matcher::Case.new(
279
- match: -> value { value % 3 == 0 },
277
+ match: -> value { value[0] == 0 },
280
278
  resolve: -> value { "Fizz" } ),
281
279
  buzz: Dry::Matcher::Case.new(
282
- match: -> value { value % 5 == 0 },
280
+ match: -> value { value[1] == 0 },
283
281
  resolve: -> value { "Buzz" } ),
284
282
  other: Dry::Matcher::Case.new(
285
283
  match: -> value { true },
286
- resolve: -> value { value } ))
284
+ resolve: -> value { value[2] } ))
287
285
 
288
286
  def fizzbuzz
289
287
  (1..100).map do |n|
290
- FizzBuzz.(n) do |m|
288
+ FizzBuzz.([n % 3, n % 5, n]) do |m|
291
289
  m.fizzbuzz { |v| v }
292
290
  m.fizz { |v| v }
293
291
  m.buzz { |v| v }
@@ -298,21 +296,20 @@ end
298
296
  end
299
297
 
300
298
 
301
- module V11
302
- def fizzbuzz
303
- fizzy = [1,0,0]
304
- buzzy = [1,0,0,0,0]
305
299
 
306
- (1..100).map do |n|
307
- fizzy.rotate!
308
- buzzy.rotate!
309
- result = n.to_s * (1 - (fizzy[0] | buzzy[0]))
310
- result << "Fizz" * fizzy[0]
311
- result << "Buzz" * buzzy[0]
312
- result
300
+ module V11
301
+ def fizzbuzz
302
+ result = []
303
+ for n in 1..100 do
304
+ result << if n % 3 == 0 && n % 5 == 0 then "FizzBuzz"
305
+ elsif n % 3 == 0 then "Fizz"
306
+ elsif n % 5 == 0 then "Buzz"
307
+ else n
308
+ end
309
+ end
310
+ result
313
311
  end
314
312
  end
315
- end
316
313
 
317
314
 
318
315
  module V12a
@@ -4,7 +4,7 @@
4
4
  module FizzBuzzer
5
5
 
6
6
  MAJOR = 1
7
- MINOR = 0
7
+ MINOR = 1
8
8
  PATCH = 0
9
9
  VERSION = [MAJOR,MINOR,PATCH].join('.')
10
10
 
@@ -0,0 +1,61 @@
1
+ # encoding: utf-8
2
+
3
+ ###
4
+ # to run use
5
+ # ruby -I ./lib -I ./test test/test_driven.rb
6
+
7
+
8
+ require 'helper'
9
+
10
+
11
+ describe FizzBuzz do
12
+
13
+ include FizzBuzzer::V11 ## add (include) fizzbuzz method using algorithm V11
14
+
15
+ before do
16
+ @result = fizzbuzz
17
+ end
18
+
19
+ it "returns 'Fizz' for multiples of 3" do
20
+ @result[3-1].must_equal "Fizz"
21
+ end
22
+
23
+ it "returns 'Buzz' for multiples of 5" do
24
+ @result[5-1].must_equal "Buzz"
25
+ end
26
+
27
+ it "returns 'FizzBuzz' for multiples of 3 and 5" do
28
+ @result[15-1].must_equal "FizzBuzz"
29
+ end
30
+
31
+ it "returns the passed number if not a multiple of 3 or 5" do
32
+ @result[1-1].must_equal 1
33
+ end
34
+ end
35
+
36
+
37
+
38
+ class TestFizzBuzz < Minitest::Test
39
+
40
+ include FizzBuzzer::V11 ## add (include) fizzbuzz method using algorithm V11
41
+
42
+ def setup
43
+ @result = fizzbuzz
44
+ end
45
+
46
+ def test_fizz_multiples_of_3
47
+ assert_equal "Fizz", @result[3-1]
48
+ end
49
+
50
+ def test_buzz_for_multiples_of_5
51
+ assert_equal "Buzz", @result[5-1]
52
+ end
53
+
54
+ def test_fizzbuzz_for_multiples_of_3_and_5
55
+ assert_equal "FizzBuzz", @result[15-1]
56
+ end
57
+
58
+ def test_number_if_not_multiple_of_3_or_5
59
+ assert_equal 1, @result[1-1]
60
+ end
61
+ end
@@ -19,13 +19,11 @@ FIZZBUZZES_1_TO_100 =
19
19
  "Fizz", 88, 89, "FizzBuzz", 91, 92, "Fizz", 94, "Buzz", "Fizz", 97, 98,
20
20
  "Fizz", "Buzz"]
21
21
 
22
- ## all strings version
23
- ## e.g. ["1", "2", "Fizz", ...]
24
- FIZZBUZZES_1_TO_100_V2 = FIZZBUZZES_1_TO_100.map {|value| value.to_s }
25
22
 
26
23
 
27
24
  class TestV1 < MiniTest::Test
28
- include FizzBuzzer::V1
25
+
26
+ include FizzBuzzer::V1 ## add (include) fizzbuzz method using algorithm V1
29
27
 
30
28
  def test_fizzbuzz
31
29
  assert_equal FIZZBUZZES_1_TO_100, fizzbuzz
@@ -43,13 +41,7 @@ end # class TestV1
43
41
 
44
42
  def test_fizzbuzz
45
43
  puts "running fizzbuzz #{name}..."
46
- if %w[V4 V11].include? '#{name}'
47
- # use all string version e.g. ["1", "2", "Fizz", ...]
48
- assert_equal FIZZBUZZES_1_TO_100_V2, fizzbuzz
49
- else
50
- # use "mixed" tuple version e.g. [1, 2, "Fizz", ...]
51
- assert_equal FIZZBUZZES_1_TO_100, fizzbuzz
52
- end
44
+ assert_equal FIZZBUZZES_1_TO_100, fizzbuzz
53
45
  end
54
46
  end)
55
47
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fizzbuzzer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gerald Bauer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-01-23 00:00:00.000000000 Z
11
+ date: 2018-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: noaidi
@@ -88,6 +88,7 @@ files:
88
88
  - lib/fizzbuzzer.rb
89
89
  - lib/fizzbuzzer/version.rb
90
90
  - test/helper.rb
91
+ - test/test_driven.rb
91
92
  - test/test_fizzbuzz.rb
92
93
  - test/test_version.rb
93
94
  homepage: https://github.com/rubylibs/fizzbuzzer