Решение на Първа задача от Стефан Ангелов

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

Към профила на Стефан Ангелов

Резултати

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

Код

class Integer
def prime?
(2..Math.sqrt(self)).each do |n|
return false if self % n == 0
end
true
end
end
class Integer
def prime_factors
primes = (2..Math.sqrt(self).floor).select { |p| p.prime?}
# Не успях да измисля нищо, което
# се вписва в ограниченията...
end
end
class Array
def average
total_sum = 0.0
self.each {|n| total_sum += n}
total_sum / self.size
end
end
class Integer
def harmonic
nth_harmonic = 0
(1..self).each do |n|
nth_harmonic += Rational(1, n)
end
nth_harmonic
end
end
class Array
def drop_every(n)
new_array = []
(0..self.size-1).each do |i|
new_array << self[i] unless (i+1) % n == 0
end
new_array
end
end
class Array
def combine_with(other)
combined_array = []
greater_size = if self.size > other.size then self.size else other.size end
(0..greater_size-1).each do |i|
combined_array << self[i]
combined_array << other[i]
end
combined_array.compact
end
end
class Integer
def digits
number_of_digits = self.to_s.length
integer_to_array = []
(0..number_of_digits-1).each do |n|
integer_to_array << self / 10**n % 10
end
integer_to_array.reverse
end
end
class Array
def frequencies
array_to_hash = Hash.new(0)
self.each { |n| array_to_hash[n] += 1 }
array_to_hash
end
end

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

FFF..F......F.

Failures:

  1) Integer#prime? checks if a number is prime
     Failure/Error: -13.prime?.should eq false
     Math::DomainError:
       Numerical argument is out of domain - "sqrt"
     # /tmp/d20131023-4395-106ak3f/solution.rb:3:in `sqrt'
     # /tmp/d20131023-4395-106ak3f/solution.rb:3:in `prime?'
     # /tmp/d20131023-4395-106ak3f/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: 360.prime_factors.should eq [2, 2, 2, 3, 3, 5]
       
       expected: [2, 2, 2, 3, 3, 5]
            got: [2, 3, 5, 7, 11, 13, 17]
       
       (compared using ==)
     # /tmp/d20131023-4395-106ak3f/spec.rb:19: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: (-4).prime_factors.should   eq [2, 2]
     Math::DomainError:
       Numerical argument is out of domain - "sqrt"
     # /tmp/d20131023-4395-106ak3f/solution.rb:13:in `sqrt'
     # /tmp/d20131023-4395-106ak3f/solution.rb:13:in `prime_factors'
     # /tmp/d20131023-4395-106ak3f/spec.rb:27: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)>'

  4) Integer#digits works with negative numbers
     Failure/Error: (-33).digits.should     eq [3, 3]
       
       expected: [3, 3]
            got: [9, 6, 7]
       
       (compared using ==)
     # /tmp/d20131023-4395-106ak3f/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)>'

  5) 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-106ak3f/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 0.01783 seconds
14 examples, 5 failures

Failed examples:

rspec /tmp/d20131023-4395-106ak3f/spec.rb:2 # Integer#prime? checks if a number is prime
rspec /tmp/d20131023-4395-106ak3f/spec.rb:18 # Integer#prime_factors constructs an array containing the prime factors in ascending order
rspec /tmp/d20131023-4395-106ak3f/spec.rb:26 # Integer#prime_factors works with negative numbers
rspec /tmp/d20131023-4395-106ak3f/spec.rb:50 # Integer#digits works with negative numbers
rspec /tmp/d20131023-4395-106ak3f/spec.rb:103 # Array#combine_with combines two arrays by alternatingly taking elements

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

Стефан обнови решението на 16.10.2013 11:04 (преди над 10 години)

+class Integer
+ def prime?
+ (2..Math.sqrt(self)).each do |n|
+ return false if self % n == 0
+ end
+ true
+ end
+end
+
+
+class Integer
+ def prime_factors
+ primes = (2..Math.sqrt(self).floor).select { |p| p.prime?}
+ # Не успях да измисля нищо, което
+ # се вписва в ограниченията...
+ end
+end
+
+
+class Array
+ def average
+ total_sum = 0.0
+ self.each {|n| total_sum += n}
+ total_sum / self.size
+ end
+end
+
+
+class Integer
+ def harmonic
+ nth_harmonic = 0
+ (1..self).each do |n|
+ nth_harmonic += Rational(1, n)
+ end
+ nth_harmonic
+ end
+end
+
+
+class Array
+ def drop_every(n)
+ new_array = []
+ (0..self.size-1).each do |i|
+ new_array << self[i] unless (i+1) % n == 0
+ end
+ new_array
+ end
+end
+
+
+class Array
+ def combine_with(other)
+ combined_array = []
+ greater_size = if self.size > other.size then self.size else other.size end
+ (0..greater_size-1).each do |i|
+ combined_array << self[i]
+ combined_array << other[i]
+ end
+ combined_array.compact
+ end
+end
+
+
+class Integer
+ def digits
+ number_of_digits = self.to_s.length
+ integer_to_array = []
+ (0..number_of_digits-1).each do |n|
+ integer_to_array << self / 10**n % 10
+ end
+ integer_to_array.reverse
+ end
+end
+
+
+class Array
+ def frequencies
+ array_to_hash = Hash.new(0)
+ self.each { |n| array_to_hash[n] += 1 }
+ array_to_hash
+ end
+end