Skip to content

Commit 92e1fa8

Browse files
committed
move lib/compsci.rb code to lib/compsci/util.rb
1 parent 1baf003 commit 92e1fa8

File tree

4 files changed

+18
-17
lines changed

4 files changed

+18
-17
lines changed

lib/compsci.rb

+1-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1 @@
1-
module CompSci
2-
# thanks apeiros
3-
# https://gist.github.com/rickhull/d0b579aa08c85430b0dc82a791ff12d6
4-
def self.power_of?(num, base)
5-
return false if base <= 1
6-
mod = 0
7-
num, mod = num.divmod(base) until (num == 1) || (mod > 0)
8-
mod == 0
9-
end
10-
end
1+
module CompSci; end

lib/compsci/util.rb

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module CompSci
2+
# thanks apeiros
3+
# https://gist.github.com/rickhull/d0b579aa08c85430b0dc82a791ff12d6
4+
def self.power_of?(num, base)
5+
return false if base <= 1
6+
mod = 0
7+
num, mod = num.divmod(base) until (num == 1) || (mod > 0)
8+
mod == 0
9+
end
10+
end

test/bench/complete_tree.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
puts <<EOF
4040
4141
#
42-
# timing CompleteTree#push where N is the count of pushes
42+
# timing CompleteTree#push where N is the count of pushes per batch
4343
#
4444
4545
EOF

test/compsci.rb test/util.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
require 'compsci'
1+
require 'compsci/util'
22
require 'minitest/autorun'
33

44
describe CompSci do
55
parallelize_me!
66

7-
it "must determine if num is a power of base" do
8-
powers = {}
7+
it "determines if num is a power of base" do
8+
powers = Hash.new []
99
basemax = 12
1010
expmax = 10
1111
2.upto(basemax) { |base|
1212
0.upto(expmax) { |exp|
13-
powers[base] ||= []
14-
powers[base] << base**exp
13+
powers[base] += [base**exp]
1514
}
1615
}
1716

1817
# 12k assertions below!
1918
2.upto(basemax) { |base|
2019
1.upto(2**expmax) { |num|
21-
expect(CompSci.power_of?(num, base)).must_equal powers[base].include?(num)
20+
x = powers[base].include?(num)
21+
expect(CompSci.power_of?(num, base)).must_equal x
2222
}
2323
}
2424
end

0 commit comments

Comments
 (0)