Решение на Първа задача от Георги Урумов

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

Към профила на Георги Урумов

Резултати

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

Код

class Array
def average
avrg = self.reduce(:+) / self.size.to_f
end
def frequencies
self.each_with_object({}) { |item, hash| hash[item] = self.count item }
end
def drop_every(n)
result_array = []
self.each_slice(n).map { |slice| result_array << slice.take(n-1) }
result_array.flatten
end
def combine_with(other)
if self.size >= other.size
self.zip(other).flatten.compact
else
other.zip(self).flatten.compact
end
end
end
class Integer
def digits
result_array = []
self.to_s.each_char { |char| result_array << char.to_i }
result_array
end
def prime?
return false if self <= 0
(2..Math.sqrt(self)).each do |divisor|
return false if self % divisor == 0 and divisor < self
end
true
end
def harmonic
(1..self).map { |item| 1/item.to_r }.reduce(:+)
end
def prime_factors
n = self.abs
return [] if n <= 1
(2..n).each_with_object([]) do |item, result_array|
if item.prime? and n % item == 0
return result_array.push(item).push((n/item).prime_factors).flatten
end
end
end
end

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

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

Failures:

  1) Integer#prime? checks if a number is prime
     Failure/Error: 1.prime?.should   eq false
       
       expected: false
            got: true
       
       (compared using ==)
     # /tmp/d20131023-4395-18ujdge/spec.rb:5: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]
       
       expected: [3, 3]
            got: [0, 3, 3]
       
       (compared using ==)
     # /tmp/d20131023-4395-18ujdge/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: [1, 2, 3].combine_with([:a, :b, :c, :d, :e]).should eq [1, :a, 2, :b, 3, :c, :d, :e]
       
       expected: [1, :a, 2, :b, 3, :c, :d, :e]
            got: [:a, 1, :b, 2, :c, 3, :d, :e]
       
       (compared using ==)
     # /tmp/d20131023-4395-18ujdge/spec.rb:108: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.02028 seconds
14 examples, 3 failures

Failed examples:

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

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

Георги обнови решението на 16.10.2013 12:26 (преди около 11 години)

+class Array
+ def average
+ avrg = self.reduce(:+) / self.size.to_f
+ end
+
+ def frequencies
+ self.each_with_object({}) { |item, hash| hash[item] = self.count item}
+ end
+
+ def drop_every(n)
+ result_array = []
+ self.each_slice(n).map { |slice| result_array << slice.take(n-1) }
+ result_array.flatten
+ end
+
+end
+
+class Integer
+ def digits
+ result_array = []
+ self.to_s.each_char { |char| result_array << char.to_i }
+ result_array
+ end
+
+ def prime?
+ return false if self <= 0
+ (2..Math.sqrt(self)).each { |divisor|
+ return false if self % divisor == 0 && divisor < self }
+ true
+ end
+
+ def harmonic
+ (1..self).map {|item| 1/item.to_r}.reduce(:+)
+ end
+
+end

Георги обнови решението на 16.10.2013 13:43 (преди около 11 години)

class Array
def average
avrg = self.reduce(:+) / self.size.to_f
end
def frequencies
- self.each_with_object({}) { |item, hash| hash[item] = self.count item}
+ self.each_with_object({}) { |item, hash| hash[item] = self.count item }
end
def drop_every(n)
result_array = []
self.each_slice(n).map { |slice| result_array << slice.take(n-1) }
result_array.flatten
end
+ def combine_with(other)
+ if self.size >= other.size
+ self.zip(other).flatten.compact
+ else
+ other.zip(self).flatten.compact
+ end
+ end
end
class Integer
def digits
result_array = []
self.to_s.each_char { |char| result_array << char.to_i }
result_array
end
def prime?
return false if self <= 0
- (2..Math.sqrt(self)).each { |divisor|
- return false if self % divisor == 0 && divisor < self }
+ (2..Math.sqrt(self)).each do |divisor|
+ return false if self % divisor == 0 and divisor < self
+ end
true
end
def harmonic
- (1..self).map {|item| 1/item.to_r}.reduce(:+)
+ (1..self).map { |item| 1/item.to_r }.reduce(:+)
+ end
+
+ def prime_factors
+ n = self.abs
+ return [] if n <= 1
+ (2..n).each_with_object([]) do |item, result_array|
+ if item.prime? and n % item == 0
+ return result_array.push(item).push((n/item).prime_factors).flatten
+ end
+ end
end
end