fizzbuzz 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/fizzbuzz.rb +40 -1
- data/lib/fizzbuzz/version.rb +1 -1
- metadata +2 -2
data/lib/fizzbuzz.rb
CHANGED
@@ -1,3 +1,10 @@
|
|
1
|
+
# A note on originality: FizzBuzz has been solved in hundreds of ways by
|
2
|
+
# thousands of people. Most of the solutions found here have been rediscovered
|
3
|
+
# time and again by various authors. I will endeavor to give credit for
|
4
|
+
# original solutions and new combinations of existing solutions. If you feel
|
5
|
+
# you deserve shared credit for one of these solutions, by all means please
|
6
|
+
# let me know.
|
7
|
+
|
1
8
|
$:.unshift File.dirname(__FILE__)
|
2
9
|
|
3
10
|
# Prints numbers from 1 to 100. If the number is divisible by 3, it prints
|
@@ -7,6 +14,20 @@ def fizzbuzz
|
|
7
14
|
fizzbuzz_map_with_ternary_modulo_15_3_5
|
8
15
|
end
|
9
16
|
|
17
|
+
# The gold standard. Executes in constant time, also has a constant amount of
|
18
|
+
# elegance (where k is quite small)
|
19
|
+
def fizzbuzz_hardcode
|
20
|
+
[1, 2, "Fizz", 4, "Buzz", "Fizz", 7, 8, "Fizz", "Buzz", 11, "Fizz", 13, 14,
|
21
|
+
"FizzBuzz", 16, 17, "Fizz", 19, "Buzz", "Fizz", 22, 23, "Fizz", "Buzz", 26,
|
22
|
+
"Fizz", 28, 29, "FizzBuzz", 31, 32, "Fizz", 34, "Buzz", "Fizz", 37, 38,
|
23
|
+
"Fizz", "Buzz", 41, "Fizz", 43, 44, "FizzBuzz", 46, 47, "Fizz", 49, "Buzz",
|
24
|
+
"Fizz", 52, 53, "Fizz", "Buzz", 56, "Fizz", 58, 59, "FizzBuzz", 61, 62,
|
25
|
+
"Fizz", 64, "Buzz", "Fizz", 67, 68, "Fizz", "Buzz", 71, "Fizz", 73, 74,
|
26
|
+
"FizzBuzz", 76, 77, "Fizz", 79, "Buzz", "Fizz", 82, 83, "Fizz", "Buzz", 86,
|
27
|
+
"Fizz", 88, 89, "FizzBuzz", 91, 92, "Fizz", 94, "Buzz", "Fizz", 97, 98,
|
28
|
+
"Fizz", "Buzz"]
|
29
|
+
end
|
30
|
+
|
10
31
|
# Uses ternary evaluation to choose element; uses modulo 15 to represent the
|
11
32
|
# combination of moduli 3 and 5.
|
12
33
|
def fizzbuzz_map_with_ternary_modulo_15_3_5
|
@@ -18,6 +39,21 @@ def fizzbuzz_map_with_ternary_modulo_15_3_5
|
|
18
39
|
}
|
19
40
|
end
|
20
41
|
|
42
|
+
# From: Dan Uznanski
|
43
|
+
def fizzbuzz_mod15_index
|
44
|
+
(1..100).map {|i| ['fizzbuzz', nil, nil, 'fizz', nil, 'buzz', 'fizz', nil, nil, 'fizz', 'buzz', nil, 'fizz', nil, nil][i % 15] || i }
|
45
|
+
end
|
46
|
+
|
47
|
+
# From: Arnar Birgisson
|
48
|
+
def fizzbuzz_indirect_index
|
49
|
+
(1..100).map {|i| ['fizzbuzz', 'fizz', 'buzz', i][[i%15,i%3,i%5,0].index(0)] }
|
50
|
+
end
|
51
|
+
|
52
|
+
# From: Paulo Bonzini
|
53
|
+
def fizzbuzz_25bit_encoding
|
54
|
+
(1..100).map {|a| [a, "Fizz", "Buzz", "FizzBuzz"][(0x1241843 >> ((a % 15) * 2)) & 3] }
|
55
|
+
end
|
56
|
+
|
21
57
|
# Uses the fact that seed 1781773465 in Ruby's rand will generate the 15-digit
|
22
58
|
# sequence that repeats in the FizzBuzz progression. The premise here is that
|
23
59
|
# we want to cleverly trick rand into delivering a predictable sequence. (It
|
@@ -26,8 +62,9 @@ end
|
|
26
62
|
# stored in a 30-bit number. Since 1781773465 requires 31 bits of storage, our
|
27
63
|
# cleverness has actually cost us a bit of storage efficiency. BUT THAT'S NOT
|
28
64
|
# THE POINT!)
|
65
|
+
#
|
66
|
+
# From: David Brady
|
29
67
|
#--
|
30
|
-
|
31
68
|
# This sequence adheres to the specific ordering of 0=int, 1=Fizz, 2=Buzz,
|
32
69
|
# 3=FizzBuzz. It may be possible to find a smaller key if the ordering is
|
33
70
|
# changed. There are 24 possible permutations. If we assume that the
|
@@ -46,6 +83,7 @@ end
|
|
46
83
|
|
47
84
|
|
48
85
|
# Here's a 26-bit seed
|
86
|
+
# From: David Brady
|
49
87
|
def fizzbuzz_rand_sequence15_3201
|
50
88
|
(0..99).map {|i| srand(46308667) if (i%15).zero?; ["FizzBuzz", "Buzz", i+1, "Fizz"][rand(4)]}
|
51
89
|
end
|
@@ -57,6 +95,7 @@ end
|
|
57
95
|
# Uses the fact that the FizzBuzz sequence is a repeating 15-digit sequence,
|
58
96
|
# and that the 15-digit can further be broken down into a, a', b, where b is
|
59
97
|
# "FizzBuzz" and a' is the reverse of a.
|
98
|
+
# From: David Brady
|
60
99
|
def fizzbuzz_sequence7_reverse
|
61
100
|
i=0;
|
62
101
|
stack = [0, 0, "Fizz", 0, "Buzz", "Fizz", 0]
|
data/lib/fizzbuzz/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fizzbuzz
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- FIXME full name
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-02-
|
12
|
+
date: 2008-02-26 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|