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

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

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

Резултати

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

Код

class Integer
def prime?
prime = false
(2..Math.sqrt(self)).each do |number|
if (self % number == 0)
return false
else
return true
end
end
end
def prime_factors
return calc_prime_factors(self.abs)
end
def harmonic
num = self
harmonic = Rational(1, num)
(2..num).each do |n|
harmonic += Rational(n, num)
end
return harmonic.to_r
end
def digits
digits = []
given_number = self.abs
while given_number != 0 do
given_number, last_digit = given_number.divmod(10)
digits << last_digit
end
return digits.reverse
end
end
class Array
def frequencies
return_hash = {}
if self != nil
self.each { |element| return_hash[element] += 1}
end
return return_hash
end
def average
sum = 0.to_f
number_of_elements = self.length.to_f
self.each do |element|
sum += element
end
averageResult = sum/number_of_elements
return averageResult
end
def drop_every(n)
return_array = []
(0..self.length - 1).step(n) do |element|
return_array.push self[element]
end
return return_array
end
def combine_with(other)
if self.length>other.length
return combine(self, self,other)
else
return combine(other, self, other)
end
end
def combine(item, caller, other)
combined_array = []
(0..item.length).each do |element|
if caller[element] then combined_array.push caller[element] end
if other[element] then combined_array.push other[element] end
end
return combined_array
end
end
def calc_prime_factors(n)
return [] if n == 1
factor = (2..n).find {|x| n % x == 0}
[factor] + calc_prime_factors(n / factor)
end

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

F..FF.FF..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-yjeruf/solution.rb:4:in `sqrt'
     # /tmp/d20131023-4395-yjeruf/solution.rb:4:in `prime?'
     # /tmp/d20131023-4395-yjeruf/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#harmonic returns the n-th harmonic number
     Failure/Error: 10.harmonic.should eq 7381/2520r
       
       expected: (7381/2520)
            got: (11/2)
       
       (compared using ==)
     # /tmp/d20131023-4395-yjeruf/spec.rb:37: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#digits constructs an array containing the digits of a number
     Failure/Error: 0.digits.should      eq [0]
       
       expected: [0]
            got: []
       
       (compared using ==)
     # /tmp/d20131023-4395-yjeruf/spec.rb:44: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) Array#frequencies returns a map from distinct items to the number of times they appear
     Failure/Error: [:a, :a, :a].frequencies.should          == { :a => 3 }
     NoMethodError:
       undefined method `+' for nil:NilClass
     # /tmp/d20131023-4395-yjeruf/solution.rb:43:in `block in frequencies'
     # /tmp/d20131023-4395-yjeruf/solution.rb:43:in `each'
     # /tmp/d20131023-4395-yjeruf/solution.rb:43:in `frequencies'
     # /tmp/d20131023-4395-yjeruf/spec.rb:60: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#frequencies doesn't change the array
     Failure/Error: expect { array.frequencies }.to_not change { array }
     NoMethodError:
       undefined method `+' for nil:NilClass
     # /tmp/d20131023-4395-yjeruf/solution.rb:43:in `block in frequencies'
     # /tmp/d20131023-4395-yjeruf/solution.rb:43:in `each'
     # /tmp/d20131023-4395-yjeruf/solution.rb:43:in `frequencies'
     # /tmp/d20131023-4395-yjeruf/spec.rb:68:in `block (3 levels) in <top (required)>'
     # /tmp/d20131023-4395-yjeruf/spec.rb:68: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)>'

  6) Array#drop_every drops every n-th element from an array.
     Failure/Error: (1..10).to_a.drop_every(3).should eq [1, 2, 4, 5, 7, 8, 10]
       
       expected: [1, 2, 4, 5, 7, 8, 10]
            got: [1, 4, 7, 10]
       
       (compared using ==)
     # /tmp/d20131023-4395-yjeruf/spec.rb:91: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)>'

  7) 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-yjeruf/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.01728 seconds
14 examples, 7 failures

Failed examples:

rspec /tmp/d20131023-4395-yjeruf/spec.rb:2 # Integer#prime? checks if a number is prime
rspec /tmp/d20131023-4395-yjeruf/spec.rb:34 # Integer#harmonic returns the n-th harmonic number
rspec /tmp/d20131023-4395-yjeruf/spec.rb:43 # Integer#digits constructs an array containing the digits of a number
rspec /tmp/d20131023-4395-yjeruf/spec.rb:58 # Array#frequencies returns a map from distinct items to the number of times they appear
rspec /tmp/d20131023-4395-yjeruf/spec.rb:66 # Array#frequencies doesn't change the array
rspec /tmp/d20131023-4395-yjeruf/spec.rb:87 # Array#drop_every drops every n-th element from an array.
rspec /tmp/d20131023-4395-yjeruf/spec.rb:103 # Array#combine_with combines two arrays by alternatingly taking elements

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

Иван обнови решението на 16.10.2013 01:44 (преди около 11 години)

+class Integer
+ def prime?
+ prime = false
+ (2..Math.sqrt(self)).each do |number|
+ if (self % number == 0)
+ return false
+ else
+ return true
+ end
+ end
+ end
+
+ def prime_factor
+ return calc_prime_factors(self.abs)
+ end
+
+ def harmonic
+ num = self
+ harmonic = Rational(1, num)
+ (2..num).each do |n|
+ harmonic += Rational(n, num)
+ end
+ return harmonic.to_r
+ end
+
+ def digits
+ digits = []
+ given_number = self.abs
+
+ while given_number != 0 do
+ given_number, last_digit = given_number.divmod(10)
+ digits << last_digit
+ end
+
+ return digits.reverse
+ end
+end
+
+class Array
+ def frequencies
+ return_hash = {}
+
+ self.each { |element| return_hash[element] += 1}
+
+ return return_hash
+ end
+
+ def average
+ average = 0
+ number_of_elements = self.length
+ self.each do |element|
+ average += element
+ end
+
+ return average/number_of_elements
+ end
+
+ def drop_every(n)
+ return_array = []
+
+ (0..self.length - 1).step(n) do |element|
+ return_array.push self[element]
+ end
+
+ return return_array
+ end
+
+ def combine_with(other)
+ if self.length>other.length
+ return combine(self, self,other)
+ else
+ return combine(other, self, other)
+ end
+ end
+
+ def combine(item, caller, other)
+ combined_array = []
+ (0..item.length).each do |element|
+ if caller[element] then combined_array.push caller[element] end
+ if other[element] then combined_array.push other[element] end
+ end
+ return combined_array
+ end
+end
+def calc_prime_factors(n)
+ return [] if n == 1
+ factor = (2..n).find {|x| n % x == 0}
+ [factor] + calc_prime_factors(n / factor)
+end

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

class Integer
def prime?
prime = false
(2..Math.sqrt(self)).each do |number|
if (self % number == 0)
return false
else
return true
end
end
end
- def prime_factor
+ def prime_factors
return calc_prime_factors(self.abs)
end
def harmonic
num = self
harmonic = Rational(1, num)
(2..num).each do |n|
harmonic += Rational(n, num)
end
return harmonic.to_r
end
def digits
digits = []
given_number = self.abs
while given_number != 0 do
given_number, last_digit = given_number.divmod(10)
digits << last_digit
end
return digits.reverse
end
end
class Array
def frequencies
return_hash = {}
-
- self.each { |element| return_hash[element] += 1}
-
+ if self != nil
+ self.each { |element| return_hash[element] += 1}
+ end
return return_hash
end
def average
- average = 0
- number_of_elements = self.length
+ sum = 0.to_f
+ number_of_elements = self.length.to_f
self.each do |element|
- average += element
+ sum += element
end
-
- return average/number_of_elements
+ averageResult = sum/number_of_elements
+ return averageResult
end
def drop_every(n)
return_array = []
(0..self.length - 1).step(n) do |element|
return_array.push self[element]
end
return return_array
end
def combine_with(other)
if self.length>other.length
return combine(self, self,other)
else
return combine(other, self, other)
end
end
def combine(item, caller, other)
combined_array = []
(0..item.length).each do |element|
if caller[element] then combined_array.push caller[element] end
if other[element] then combined_array.push other[element] end
end
return combined_array
end
end
+
def calc_prime_factors(n)
return [] if n == 1
factor = (2..n).find {|x| n % x == 0}
[factor] + calc_prime_factors(n / factor)
end