Решение на Първа задача от Диан Николов

Обратно към всички решения

Към профила на Диан Николов

Резултати

  • 5 точки от тестове
  • 0 бонус точки
  • 5 точки общо
  • 11 успешни тест(а)
  • 3 неуспешни тест(а)

Код

class Integer
def prime?
2.upto(abs - 1).all? { |a| abs.remainder(a).nonzero? }
end
def prime_factors
arr = []
temp = abs
2.upto(abs).each do |a| while temp.remainder(a) == 0
arr << a
temp /= a
end
end
arr
end
def harmonic
sum = Rational(0)
1.upto(self).each { |a| sum += Rational(1, a) }
sum
end
def digits
arr = []
temp = self
begin
arr << temp.remainder(10)
temp /= 10
end while temp != 0
arr.reverse
end
end
class Array
def frequencies
h = Hash.new()
each do |a|
if nil == h[a] then h[a] = 1 else h[a] += 1 end
end
h
end
def average
result = 0
each { |a| result += a }
result / Float(length)
end
def drop_every(n)
newArray = []
0.upto(length - 1).each do |i|
if (i + 1).remainder(n) != 0 then newArray << self[i] end
end
newArray
end
def combine_with(other)
newArray = []
for i in 0..[length, other.length].max
if nil != self[i] then newArray << self[i] end
if nil != other[i] then newArray << other[i] end
end
newArray
end
end

Лог от изпълнението

F....F......F.

Failures:

  1) Integer#prime? checks if a number is prime
     Failure/Error: -13.prime?.should eq false
       
       expected: false
            got: true
       
       (compared using ==)
     # /tmp/d20131023-4395-1xlut8x/spec.rb:3:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  2) Integer#digits works with negative numbers
     Failure/Error: (-33).digits.should     eq [3, 3]
     Timeout::Error:
       execution expired
     # /tmp/d20131023-4395-1xlut8x/solution.rb:27:in `remainder'
     # /tmp/d20131023-4395-1xlut8x/solution.rb:27:in `digits'
     # /tmp/d20131023-4395-1xlut8x/spec.rb:51:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  3) Array#combine_with combines two arrays by alternatingly taking elements
     Failure/Error: [:a, :b, :c].combine_with([1, nil, 3]).should       eq [:a, 1, :b, nil, :c, 3]
       
       expected: [:a, 1, :b, nil, :c, 3]
            got: [:a, 1, :b, :c, 3]
       
       (compared using ==)
     # /tmp/d20131023-4395-1xlut8x/spec.rb:110:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 7.19 seconds
14 examples, 3 failures

Failed examples:

rspec /tmp/d20131023-4395-1xlut8x/spec.rb:2 # Integer#prime? checks if a number is prime
rspec /tmp/d20131023-4395-1xlut8x/spec.rb:50 # Integer#digits works with negative numbers
rspec /tmp/d20131023-4395-1xlut8x/spec.rb:103 # Array#combine_with combines two arrays by alternatingly taking elements

История (1 версия и 0 коментара)

Диан обнови решението на 15.10.2013 20:02 (преди около 11 години)

+class Integer
+ def prime?
+ 2.upto(abs - 1).all? { |a| abs.remainder(a).nonzero? }
+ end
+
+ def prime_factors
+ arr = []
+ temp = abs
+ 2.upto(abs).each do |a| while temp.remainder(a) == 0
+ arr << a
+ temp /= a
+ end
+ end
+ arr
+ end
+
+ def harmonic
+ sum = Rational(0)
+ 1.upto(self).each { |a| sum += Rational(1, a) }
+ sum
+ end
+
+ def digits
+ arr = []
+ temp = self
+ begin
+ arr << temp.remainder(10)
+ temp /= 10
+ end while temp != 0
+ arr.reverse
+ end
+end
+
+class Array
+ def frequencies
+ h = Hash.new()
+ each do |a|
+ if nil == h[a] then h[a] = 1 else h[a] += 1 end
+ end
+ h
+ end
+
+ def average
+ result = 0
+ each { |a| result += a }
+ result / Float(length)
+ end
+
+ def drop_every(n)
+ newArray = []
+ 0.upto(length - 1).each do |i|
+ if (i + 1).remainder(n) != 0 then newArray << self[i] end
+ end
+ newArray
+ end
+
+ def combine_with(other)
+ newArray = []
+ for i in 0..[length, other.length].max
+ if nil != self[i] then newArray << self[i] end
+ if nil != other[i] then newArray << other[i] end
+ end
+ newArray
+ end
+end