Решение на Първа задача от Томислав Иванов

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

Към профила на Томислав Иванов

Резултати

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

Код

class Integer
def prime
(2..self.abs - 1).each do |smaller_num|
if self % smaller_num == 0 then return false end
end
true
end
def prime_factors
simple_multipliers = if self.prime then [1] else [] end
(2..num_copy = self.abs).each do |multiplier|
until num_copy % multiplier != 0 or num_copy == 0 or !multiplier.prime do
num_copy = num_copy / multiplier
simple_multipliers.push(multiplier)
end
end
simple_multipliers
end
def harmonic
sum = Rational(1,1)
(2..self).each {|elem| sum += Rational(1,elem)}
sum
end
def digits
array_digits = []
num_copy = self.abs
begin
array_digits.push(num_copy % 10)
num_copy = num_copy / 10
end while num_copy != 0
array_digits.reverse
end
end
class Array
def frequencies
array_hash = {}
self.each do |array_elem|
array_hash[array_elem] = if array_hash.has_key?(array_elem)
array_hash[array_elem] + 1
else
1
end
end
array_hash
end
def average
sum = 0.0
self.each {|elem| sum += elem}
return sum / self.length
end
def drop_every(n)
new_array = []
(1..self.length).each do |i|
if i % n != 0 then new_array.push(self[i - 1]) end
end
new_array
end
def combine_with(other)
combined_array = []
(0..self.length + other.length - 1).each do |i|
combined_array.push(self[i]) if self.fetch(i, -1) != -1
combined_array.push(other[i]) if other.fetch(i, -1) != -1
end
combined_array
end
end

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

FFF...........

Failures:

  1) Integer#prime? checks if a number is prime
     Failure/Error: -13.prime?.should eq false
     NoMethodError:
       undefined method `prime?' for -13:Fixnum
     # /tmp/d20131023-4395-1vxdhur/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#prime_factors constructs an array containing the prime factors in ascending order
     Failure/Error: 61.prime_factors.should  eq [61]
       
       expected: [61]
            got: [1, 61]
       
       (compared using ==)
     # /tmp/d20131023-4395-1vxdhur/spec.rb:22: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) Integer#prime_factors works with negative numbers
     Failure/Error: (-61).prime_factors.should  eq [61]
       
       expected: [61]
            got: [1, 61]
       
       (compared using ==)
     # /tmp/d20131023-4395-1vxdhur/spec.rb:28: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 0.0183 seconds
14 examples, 3 failures

Failed examples:

rspec /tmp/d20131023-4395-1vxdhur/spec.rb:2 # Integer#prime? checks if a number is prime
rspec /tmp/d20131023-4395-1vxdhur/spec.rb:18 # Integer#prime_factors constructs an array containing the prime factors in ascending order
rspec /tmp/d20131023-4395-1vxdhur/spec.rb:26 # Integer#prime_factors works with negative numbers

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

Томислав обнови решението на 15.10.2013 13:48 (преди около 11 години)

+class Integer
+ def prime
+ (2..self.abs - 1).each do |smaller_num|
+ if self % smaller_num == 0 then return false end
+ end
+ true
+ end
+
+ def prime_factors
+ simple_multipliers = if self.prime then [1] else [] end
+ (2..num_copy = self.abs).each do |multiplier|
+ until num_copy % multiplier != 0 or num_copy == 0 or !multiplier.prime do
+ num_copy = num_copy / multiplier
+ simple_multipliers.push(multiplier)
+ end
+ end
+ simple_multipliers
+ end
+
+ def harmonic
+ sum = Rational(1,1)
+ (2..self).each {|elem| sum += Rational(1,elem)}
+ sum
+ end
+
+ def digits
+ array_digits = []
+ num_copy = self.abs
+ begin
+ array_digits.push(num_copy % 10)
+ num_copy = num_copy / 10
+ end while num_copy != 0
+ array_digits.reverse
+ end
+end
+
+class Array
+ def frequencies
+ array_hash = {}
+ self.each do |array_elem|
+ array_hash[array_elem] = if array_hash.has_key?(array_elem)
+ array_hash[array_elem] + 1
+ else
+ 1
+ end
+ end
+ array_hash
+ end
+
+ def average
+ sum = 0.0
+ self.each {|elem| sum += elem}
+ return sum / self.length
+ end
+
+ def drop_every(n)
+ new_array = []
+ (1..self.length).each do |i|
+ if i % n != 0 then new_array.push(self[i - 1]) end
+ end
+ new_array
+ end
+
+ def combine_with(other)
+ combined_array = []
+ (0..self.length + other.length - 1).each do |i|
+ combined_array.push(self[i]) if self.fetch(i, -1) != -1
+ combined_array.push(other[i]) if other.fetch(i, -1) != -1
+ end
+ combined_array
+ end
+end