fizzbuzzer 1.1.0 → 1.1.1

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: 257500b27fdf676a85d3671553510fcde0ec2fa3
4
- data.tar.gz: 69812a749e44213e790167fb8b925391ea85317f
3
+ metadata.gz: 6d65bae35e41b257f6d25062f4962ea9d96cc71f
4
+ data.tar.gz: 91b7bc1964e958ca432e6e45db8710e41573c878
5
5
  SHA512:
6
- metadata.gz: 3eeb4670da60daa491e639286d11804329eee875f11847ee11d94a95a897ea2dffc2c01e625517bbca732e0f0896375e667f0b08daf04a38619d1176feb1a86d
7
- data.tar.gz: 6116a56553ef18c25dda8ef1cd0903bf8206fc340dac31f5fea980824ac1e4e02138a6f91221cd3dccf667cf7376d3e68d031678ee32088e450f65c74eedaeda
6
+ metadata.gz: a619ba0dab2f37830a7a6161d2571789f048baa4973e10581cf2e8ae9d71e7487336a61418a3d1715eaf77707a4f0da5086e6237b5ea85dc07a8eb5886dca4fe
7
+ data.tar.gz: 4690304d9705f82d25695c512ddd9112b420381b7b46c8e3b71434374314cd600dd9ed2a7b3afda30b9ce98600e97db218383acee3bd2b691ec1baae20d37eb5
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # FizzBuzzer
2
2
 
3
3
  fizzbuzzer - 1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, Fizz Buzz, ... -
4
- a collection of algorithms for playing the word game for children that teaches division
4
+ a collection of algorithms for playing the word game for children that teaches division e.g. 7/3=?
5
5
  (one of the four basic arithmetic operations in mathematics)
6
6
  or helps you find the world's best coders in programming job interviews -
7
7
  are you (re)using the fizzbuzzer library? ;-) - don't reinvent the wheel or the feedbuzzer!
@@ -178,7 +178,7 @@ def fizzbuzz
178
178
  end
179
179
  ```
180
180
 
181
- ## Object-Oriented with New Fizznum Class
181
+ ### Object-Oriented with New Fizznum Class
182
182
 
183
183
  ``` ruby
184
184
  class Fizznum
@@ -241,7 +241,7 @@ end
241
241
  ```
242
242
 
243
243
 
244
- ## Enumarator
244
+ ### Enumarator
245
245
 
246
246
  ```ruby
247
247
  module FizzBuzz
@@ -283,7 +283,7 @@ end
283
283
  ```
284
284
 
285
285
 
286
- ## Lazy Enumerator to Infinity
286
+ ### Lazy Enumerator to Infinity
287
287
 
288
288
  ```ruby
289
289
  require "bigdecimal"
@@ -307,7 +307,7 @@ end
307
307
  ```
308
308
 
309
309
 
310
- ## Poor Man's (Do-It-Yourself) Pattern Matching
310
+ ### Poor Man's (Do-It-Yourself) Pattern Matching
311
311
 
312
312
  ``` ruby
313
313
  def fizzbuzz
@@ -323,7 +323,7 @@ end
323
323
  ```
324
324
 
325
325
 
326
- ## Pattern Matching with Noaidi
326
+ ### Pattern Matching with Noaidi
327
327
 
328
328
  ``` ruby
329
329
  require "noaidi"
@@ -341,7 +341,7 @@ end
341
341
  ```
342
342
 
343
343
 
344
- ## Pattern Matching with dry-matcher
344
+ ### Pattern Matching with dry-matcher
345
345
 
346
346
  ``` ruby
347
347
  require "dry-matcher"
@@ -373,32 +373,78 @@ end
373
373
  ```
374
374
 
375
375
 
376
- ## Test Driven Development (TDD)
376
+ ### Test Driven Development (TDD)
377
377
 
378
378
  ``` ruby
379
379
  describe FizzBuzz do
380
- before do
381
- @result = fizzbuzz
382
- end
383
380
 
384
- it "returns 'Fizz' for multiples of 3" do
385
- @result[3-1].must_equal "Fizz"
381
+ describe "number is divisible" do
382
+ it "divisible by" do
383
+ divisible_by?(15,3).must_equal true
384
+ end
385
+ it "divisible by 3" do
386
+ divisible_by_3?(15).must_equal true
387
+ end
388
+ it "divisible by 5" do
389
+ divisible_by_5?(15).must_equal true
390
+ end
386
391
  end
387
392
 
388
- it "returns 'Buzz' for multiples of 5" do
389
- @result[5-1].must_equal "Buzz"
390
- end
393
+ describe "number is fizzbuzz" do
394
+ before do
395
+ @result = fizzbuzz
396
+ end
391
397
 
392
- it "returns 'FizzBuzz' for multiples of 3 and 5" do
393
- @result[15-1].must_equal "FizzBuzz"
394
- end
398
+ it "returns 'Fizz' for multiples of 3" do
399
+ @result[3-1].must_equal "Fizz"
400
+ end
401
+
402
+ it "returns 'Buzz' for multiples of 5" do
403
+ @result[5-1].must_equal "Buzz"
404
+ end
405
+
406
+ it "returns 'FizzBuzz' for multiples of 3 and 5" do
407
+ @result[15-1].must_equal "FizzBuzz"
408
+ end
395
409
 
396
- it "returns the passed number if not a multiple of 3 or 5" do
397
- @result[1-1].must_equal 1
410
+ it "returns the passed number if not a multiple of 3 or 5" do
411
+ @result[1-1].must_equal 1
412
+ end
398
413
  end
399
414
  end
400
415
  ```
401
416
 
417
+ And here's another fizzbuzz algorithm with a classic `for` loop
418
+ and using `divisible_by?` and `case/when/else` clauses:
419
+
420
+ ``` ruby
421
+ def divisible_by?(numerator, denominator)
422
+ numerator % denominator == 0
423
+ end
424
+
425
+ def divisible_by_3?( numerator )
426
+ divisible_by?( numerator, 3 )
427
+ end
428
+
429
+ def divisible_by_5?( numerator )
430
+ divisible_by?( numerator, 5 )
431
+ end
432
+
433
+ def fizzbuzz
434
+ result = []
435
+ for n in 1..100 do
436
+ result << case
437
+ when divisible_by_3?(n) && divisible_by_5?(n) then "FizzBuzz"
438
+ when divisible_by_3?(n) then "Fizz"
439
+ when divisible_by_5?(n) then "Buzz"
440
+ else n
441
+ end
442
+ end
443
+ result
444
+ end
445
+ ```
446
+
447
+
402
448
  Or use asserts for testing:
403
449
 
404
450
  ``` ruby
@@ -427,7 +473,8 @@ class TestFizzBuzz < Minitest::Test
427
473
  end
428
474
  ```
429
475
 
430
- And here's another fizzbuzz algorithm using a classic `for` loop:
476
+ And here's yet another fizzbuzz algorithm with a classic `for` loop
477
+ and classic `if/elsif/else`-clauses:
431
478
 
432
479
  ``` ruby
433
480
  def fizzbuzz
@@ -443,6 +490,7 @@ def fizzbuzz
443
490
  end
444
491
  ```
445
492
 
493
+
446
494
  Or use an all-in-one assert as used
447
495
  for testing all the fizzbuzz algorithms :-) in this repo:
448
496
 
@@ -468,7 +516,7 @@ end
468
516
  ```
469
517
 
470
518
 
471
- ## With Parameterization
519
+ ### With Parameterization
472
520
 
473
521
  ``` ruby
474
522
  def fizzbuzz_engine(range, factors)
@@ -510,6 +558,84 @@ end
510
558
  ```
511
559
 
512
560
 
561
+ ### Use Cycle from Enumerable
562
+
563
+ ``` ruby
564
+ def fizzbuzz
565
+ fizzy = [nil, nil, :Fizz].cycle
566
+ buzzy = [nil, nil, nil, nil, :Buzz].cycle
567
+
568
+ (1..100).map do |n|
569
+ "#{fizzy.next}#{buzzy.next}"[/.+/] || n
570
+ end
571
+ end
572
+ ```
573
+
574
+
575
+
576
+ ### Code Golfing - One-Liners - The Shorter The Better!
577
+
578
+ Tip: Use `<1` for a shorter form of `==0`.
579
+
580
+
581
+ **66 Bytes**
582
+
583
+ ``` ruby
584
+ def fizzbuzz
585
+ (1..100).map{|n|s="";n%3<1&&s+="Fizz";n%5<1&&s+="Buzz";s[/.+/]||n}
586
+ end
587
+ ```
588
+
589
+ **65 Bytes**
590
+
591
+ ``` ruby
592
+ def fizzbuzz
593
+ (1..100).map{|n|n%3<1?(n%5<1?"FizzBuzz":"Fizz"):(n%5<1?"Buzz":n)}
594
+ end
595
+ ```
596
+
597
+ **64 Bytes**
598
+
599
+ ``` ruby
600
+ def fizzbuzz
601
+ (1..100).map{|n|n%3*n%5<1?(n%3<1?"Fizz":"")+(n%5<1?"Buzz":""):n}
602
+ end
603
+ ```
604
+
605
+ **62 Bytes**
606
+
607
+ ``` ruby
608
+ def fizzbuzz
609
+ (1..100).map{|n|n%15<1?"FizzBuzz":n%5<1?"Buzz":n%3<1?"Fizz":n}
610
+ end
611
+ ```
612
+
613
+ **58 Bytes**
614
+
615
+ ``` ruby
616
+ def fizzbuzz
617
+ (1..100).map{|n|"#{[:Fizz][n%3]}#{[:Buzz][n%5]}"[/.+/]||n}
618
+ end
619
+ ```
620
+
621
+ And the winner is...
622
+
623
+ **54 Bytes**
624
+
625
+ ``` ruby
626
+ def fizzbuzz
627
+ (1..100).map{|n|n%3<1&&x="Fizz";n%5<1?"#{x}Buzz":x||n}
628
+ end
629
+ ```
630
+
631
+ or
632
+
633
+ ``` ruby
634
+ def fizzbuzz
635
+ (1..100).map{|n|["%sBuzz"%x=["Fizz"][n%3]][n%5]||x||n}
636
+ end
637
+ ```
638
+
513
639
 
514
640
  ## Install
515
641
 
@@ -78,9 +78,7 @@ end
78
78
  end # module FizzBuzzer
79
79
 
80
80
 
81
-
82
- ## monkey patch for V3 - needs top level namespace
83
- ## todo/fix: use refinements - why? why not?
81
+ ## monkey patch for Fixnum - note: use refinements (in the future) - why? why not?
84
82
  class Fixnum
85
83
  def to_fizzbuzz
86
84
  if self % 3 == 0 && self % 5 == 0
@@ -92,19 +90,18 @@ class Fixnum
92
90
  else
93
91
  self
94
92
  end
95
- end
93
+ end # method to_fizzbuzz
96
94
  end
97
95
 
98
96
 
99
97
  module FizzBuzzer
100
98
  module V3
101
99
  def fizzbuzz
102
- (1..100).map { |n| n.to_fizzbuzz }
100
+ (1..100).map { |n| n.to_fizzbuzz }
103
101
  end
104
102
  end
105
103
 
106
104
 
107
-
108
105
  module V4
109
106
  class Fizznum
110
107
  def initialize(n)
@@ -296,8 +293,7 @@ end
296
293
  end
297
294
 
298
295
 
299
-
300
- module V11
296
+ module V11a
301
297
  def fizzbuzz
302
298
  result = []
303
299
  for n in 1..100 do
@@ -312,6 +308,34 @@ module V11
312
308
  end
313
309
 
314
310
 
311
+ module V11b
312
+ def divisible_by?(numerator, denominator)
313
+ numerator % denominator == 0
314
+ end
315
+
316
+ def divisible_by_3?( numerator )
317
+ divisible_by?( numerator, 3 )
318
+ end
319
+
320
+ def divisible_by_5?( numerator )
321
+ divisible_by?( numerator, 5 )
322
+ end
323
+
324
+ def fizzbuzz
325
+ result = []
326
+ for n in 1..100 do
327
+ result << case
328
+ when divisible_by_3?(n) && divisible_by_5?(n) then "FizzBuzz"
329
+ when divisible_by_3?(n) then "Fizz"
330
+ when divisible_by_5?(n) then "Buzz"
331
+ else n
332
+ end
333
+ end
334
+ result
335
+ end
336
+ end
337
+
338
+
315
339
  module V12a
316
340
  def fizzbuzz_engine(range, factors)
317
341
  range.map do |n|
@@ -350,6 +374,84 @@ end
350
374
  end
351
375
 
352
376
 
377
+ module V13
378
+ def fizzbuzz
379
+ fizzy = [nil, nil, :Fizz].cycle
380
+ buzzy = [nil, nil, nil, nil, :Buzz].cycle
381
+
382
+ (1..100).map do |n|
383
+ "#{fizzy.next}#{buzzy.next}"[/.+/] || n
384
+ end
385
+ end
386
+ end
387
+
388
+
389
+ module Golf
390
+ ## Notes:
391
+ ## use <1 shorter form of ==0
392
+ ##
393
+ ## "Fizz"[/.+/] #=> "Fizz"
394
+ ## "Buzz"[/.+/] #=> "Buzz"
395
+ ## ""[/.+/] #=> nil
396
+ ##
397
+ ## ["Fizz"][0] #=> "Fizz"
398
+ ## ["Fizz"][1] #=> nil
399
+ ## ["Fizz"][2] #=> nil
400
+ ##
401
+ ## ["Buzz"][0] #=> "Buzz"
402
+ ## ["Buzz"][1] #=> "nil
403
+ ##
404
+ ## ["%sBuzz" % "Fizz"] #=> "FizzBuzz"
405
+ ## ["%sBuzz" % nil] #=> "Buzz"
406
+
407
+ module V1 ## 66 bytes
408
+ def fizzbuzz
409
+ (1..100).map{|n|s="";n%3<1&&s+="Fizz";n%5<1&&s+="Buzz";s[/.+/]||n}
410
+ end
411
+ end
412
+
413
+ module V2 ## 65 bytes
414
+ def fizzbuzz
415
+ (1..100).map{|n|n%3<1?(n%5<1?"FizzBuzz":"Fizz"):(n%5<1?"Buzz":n)}
416
+ end
417
+ end
418
+
419
+ module V3 ## 64 bytes
420
+ def fizzbuzz
421
+ (1..100).map{|n|n%3*n%5<1?(n%3<1?"Fizz":"")+(n%5<1?"Buzz":""):n}
422
+ end
423
+ end
424
+
425
+ module V4 ## 62 bytes
426
+ # n%15==0 ? "FizzBuzz" : n%5==0 ? "Buzz" : n%3==0 ? "Fizz" : n
427
+ def fizzbuzz
428
+ (1..100).map{|n|n%15<1?"FizzBuzz":n%5<1?"Buzz":n%3<1?"Fizz":n}
429
+ end
430
+ end
431
+
432
+ module V5 ## 58 bytes
433
+ def fizzbuzz
434
+ (1..100).map{|n|"#{[:Fizz][n%3]}#{[:Buzz][n%5]}"[/.+/]||n}
435
+ end
436
+ end
437
+
438
+ module V6 ## 54 bytes
439
+ # n%3 < 1 && x="Fizz"
440
+ # n%5 < 1 ? "#{x}Buzz" : x || n
441
+ def fizzbuzz
442
+ (1..100).map{|n|n%3<1&&x="Fizz";n%5<1?"#{x}Buzz":x||n}
443
+ end
444
+ end
445
+
446
+ module V7 ## 54 bytes
447
+ def fizzbuzz
448
+ (1..100).map{|n|["%sBuzz"%x=["Fizz"][n%3]][n%5]||x||n}
449
+ end
450
+ end
451
+ end # module Golf
452
+
453
+
454
+
353
455
  module Main ## todo/check: use a class Runner instead (w/ include V1 etc.) - why? why not?
354
456
  extend V1
355
457
  end
@@ -5,7 +5,7 @@ module FizzBuzzer
5
5
 
6
6
  MAJOR = 1
7
7
  MINOR = 1
8
- PATCH = 0
8
+ PATCH = 1
9
9
  VERSION = [MAJOR,MINOR,PATCH].join('.')
10
10
 
11
11
  def self.version
@@ -10,26 +10,40 @@ require 'helper'
10
10
 
11
11
  describe FizzBuzz do
12
12
 
13
- include FizzBuzzer::V11 ## add (include) fizzbuzz method using algorithm V11
14
-
15
- before do
16
- @result = fizzbuzz
13
+ include FizzBuzzer::V11b ## add (include) fizzbuzz method using algorithm V11
14
+
15
+ describe "number is divisible" do
16
+ it "divisible by" do
17
+ divisible_by?(15,3).must_equal true
18
+ end
19
+ it "divisible by 3" do
20
+ divisible_by_3?(15).must_equal true
21
+ end
22
+ it "divisible by 5" do
23
+ divisible_by_5?(15).must_equal true
24
+ end
17
25
  end
18
26
 
19
- it "returns 'Fizz' for multiples of 3" do
20
- @result[3-1].must_equal "Fizz"
21
- end
27
+ describe "number is fizzbuzz" do
28
+ before do ## optimize? use before(:all) - why? why not?
29
+ @result = fizzbuzz
30
+ end
22
31
 
23
- it "returns 'Buzz' for multiples of 5" do
24
- @result[5-1].must_equal "Buzz"
25
- end
32
+ it "returns 'Fizz' for multiples of 3" do
33
+ @result[3-1].must_equal "Fizz"
34
+ end
26
35
 
27
- it "returns 'FizzBuzz' for multiples of 3 and 5" do
28
- @result[15-1].must_equal "FizzBuzz"
29
- end
36
+ it "returns 'Buzz' for multiples of 5" do
37
+ @result[5-1].must_equal "Buzz"
38
+ end
39
+
40
+ it "returns 'FizzBuzz' for multiples of 3 and 5" do
41
+ @result[15-1].must_equal "FizzBuzz"
42
+ end
30
43
 
31
- it "returns the passed number if not a multiple of 3 or 5" do
32
- @result[1-1].must_equal 1
44
+ it "returns the passed number if not a multiple of 3 or 5" do
45
+ @result[1-1].must_equal 1
46
+ end
33
47
  end
34
48
  end
35
49
 
@@ -37,7 +51,7 @@ end
37
51
 
38
52
  class TestFizzBuzz < Minitest::Test
39
53
 
40
- include FizzBuzzer::V11 ## add (include) fizzbuzz method using algorithm V11
54
+ include FizzBuzzer::V11a ## add (include) fizzbuzz method using algorithm V11
41
55
 
42
56
  def setup
43
57
  @result = fizzbuzz
@@ -31,17 +31,32 @@ end
31
31
  end # class TestV1
32
32
 
33
33
 
34
- ## auto-generate test classes (see model/sample above)
35
- ## todo/fix: use a version without eval - possible? why? why not?
36
-
37
- %w[V2a V2b V2c V2d V3 V4 V5 V6a V6b V7 V8 V9 V10 V11 V12a V12b].each do |name|
38
- eval %Q(
39
- class Test#{name} < MiniTest::Test
40
- include FizzBuzzer::#{name}
41
-
42
- def test_fizzbuzz
43
- puts "running fizzbuzz #{name}..."
44
- assert_equal FIZZBUZZES_1_TO_100, fizzbuzz
45
- end
46
- end)
34
+
35
+ module FizzBuzz
36
+
37
+ ## auto-generate test classes (see model/sample above)
38
+ def self.test_class_for( m )
39
+ Class.new( MiniTest::Test ) do
40
+ include m
41
+ define_method :test_fizzbuzz do
42
+ puts "testing #{m.name}..."
43
+ assert_equal FIZZBUZZES_1_TO_100, fizzbuzz
44
+ end
45
+ end
46
+ end
47
+
48
+ ALGOS = [ V1, V2a, V2b, V2c, V2d, V3, V4, V5,
49
+ V6a, V6b, V7, V8, V9, V10, V11a, V11b, V12a, V12b, V13,
50
+ Golf::V1, Golf::V2, Golf::V3, Golf::V4, Golf::V5, Golf::V6, Golf::V7 ]
51
+
52
+ def self.build_tests
53
+ puts " auto-adding (building) test classes..."
54
+ ALGOS.each do |algo|
55
+ puts " adding #{algo.name}..."
56
+ test_class_for( algo )
57
+ end
58
+ end
47
59
  end
60
+
61
+
62
+ FizzBuzz.build_tests
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.1.0
4
+ version: 1.1.1
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-24 00:00:00.000000000 Z
11
+ date: 2018-01-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: noaidi