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

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

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

Резултати

  • 1 точка от тестове
  • 0 бонус точки
  • 1 точка общо
  • 2 успешни тест(а)
  • 12 неуспешни тест(а)

Код

#!/usr/bin/ruby
# Add a new methods (function) to class Integer.
class Integer
def remainder (quotient, divisor)
if quotient % divisor == 0
return false
else
return true
end
end
def has_divisor_except_itself_and_one (number)
divisor = number - 1
while divisor > 1 do
return false if not remainder number, divisor
divisor -= 1
end
return true
end
def finds_first_prime_divisor list
num = 2
while num <= list[-1].abs
pr_div = list[-1].abs / num
return list+[num,pr_div] if num.prime? and not remainder list[-1].abs,num
num += 1
end
end
def prime?
if self <= 1
return false
elsif self > 1
has_divisor_except_itself_and_one self
end
end
def prime_factors
list_of_prime_factors = [self]
while not list_of_prime_factors[-1].abs.prime?
list_of_prime_factors = finds_first_prime_divisor list_of_prime_factors
list_of_prime_factors.delete_at(-3)
end
puts list_of_prime_factors.sort
end
def digits
list = []
number = self
while number != 0
list.push number % 10
number /= 10
end
puts list.reverse
end
end
class Array
def average
sum = 0
self.each { |n| sum += n }
puts sum / self.length
end
end

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

.2
2
2
3
3
5
F2
2
FFFFFF42
F5
.FFFF

Failures:

  1) 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: nil
       
       (compared using ==)
     # /tmp/d20131023-4395-lc00sv/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)>'

  2) Integer#prime_factors works with negative numbers
     Failure/Error: (-4).prime_factors.should   eq [2, 2]
       
       expected: [2, 2]
            got: nil
       
       (compared using ==)
     # /tmp/d20131023-4395-lc00sv/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)>'

  3) Integer#harmonic returns the n-th harmonic number
     Failure/Error: 1.harmonic.should  eq 1
     NoMethodError:
       undefined method `harmonic' for 1:Fixnum
     # /tmp/d20131023-4395-lc00sv/spec.rb:35: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 constructs an array containing the digits of a number
     Failure/Error: 0.digits.should      eq [0]
       
       expected: [0]
            got: nil
       
       (compared using ==)
     # /tmp/d20131023-4395-lc00sv/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)>'

  5) Integer#digits works with negative numbers
     Failure/Error: (-33).digits.should     eq [3, 3]
     Timeout::Error:
       execution expired
     # /tmp/d20131023-4395-lc00sv/solution.rb:47:in `digits'
     # /tmp/d20131023-4395-lc00sv/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)>'

  6) Array#frequencies returns a map from distinct items to the number of times they appear
     Failure/Error: [].frequencies.should                    == {}
     NoMethodError:
       undefined method `frequencies' for []:Array
     # /tmp/d20131023-4395-lc00sv/spec.rb:59: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#frequencies doesn't change the array
     Failure/Error: expect { array.frequencies }.to_not change { array }
     NoMethodError:
       undefined method `frequencies' for [1, 2, :c, 1, 1]:Array
     # /tmp/d20131023-4395-lc00sv/spec.rb:68:in `block (3 levels) in <top (required)>'
     # /tmp/d20131023-4395-lc00sv/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)>'

  8) Array#average calculates the average of the numbers in the array
     Failure/Error: [42].average.should                   eq 42
       
       expected: 42
            got: nil
       
       (compared using ==)
     # /tmp/d20131023-4395-lc00sv/spec.rb:74: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)>'

  9) Array#drop_every drops every n-th element from an array.
     Failure/Error: [].drop_every(2).should eq []
     NoMethodError:
       undefined method `drop_every' for []:Array
     # /tmp/d20131023-4395-lc00sv/spec.rb:88: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)>'

  10) Array#drop_every doesn't change the array
     Failure/Error: expect { array.drop_every(3) }.to_not change { array }
     NoMethodError:
       undefined method `drop_every' for [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:Array
     # /tmp/d20131023-4395-lc00sv/spec.rb:98:in `block (3 levels) in <top (required)>'
     # /tmp/d20131023-4395-lc00sv/spec.rb:98: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)>'

  11) Array#combine_with combines two arrays by alternatingly taking elements
     Failure/Error: [].combine_with([]).should                          eq []
     NoMethodError:
       undefined method `combine_with' for []:Array
     # /tmp/d20131023-4395-lc00sv/spec.rb:104: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)>'

  12) Array#combine_with doesn't change the array
     Failure/Error: expect { array.combine_with [1, 2, 3] }.to_not change { array }
     NoMethodError:
       undefined method `combine_with' for [:a, :b, :c]:Array
     # /tmp/d20131023-4395-lc00sv/spec.rb:115:in `block (3 levels) in <top (required)>'
     # /tmp/d20131023-4395-lc00sv/spec.rb:115: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.33 seconds
14 examples, 12 failures

Failed examples:

rspec /tmp/d20131023-4395-lc00sv/spec.rb:18 # Integer#prime_factors constructs an array containing the prime factors in ascending order
rspec /tmp/d20131023-4395-lc00sv/spec.rb:26 # Integer#prime_factors works with negative numbers
rspec /tmp/d20131023-4395-lc00sv/spec.rb:34 # Integer#harmonic returns the n-th harmonic number
rspec /tmp/d20131023-4395-lc00sv/spec.rb:43 # Integer#digits constructs an array containing the digits of a number
rspec /tmp/d20131023-4395-lc00sv/spec.rb:50 # Integer#digits works with negative numbers
rspec /tmp/d20131023-4395-lc00sv/spec.rb:58 # Array#frequencies returns a map from distinct items to the number of times they appear
rspec /tmp/d20131023-4395-lc00sv/spec.rb:66 # Array#frequencies doesn't change the array
rspec /tmp/d20131023-4395-lc00sv/spec.rb:73 # Array#average calculates the average of the numbers in the array
rspec /tmp/d20131023-4395-lc00sv/spec.rb:87 # Array#drop_every drops every n-th element from an array.
rspec /tmp/d20131023-4395-lc00sv/spec.rb:96 # Array#drop_every doesn't change the array
rspec /tmp/d20131023-4395-lc00sv/spec.rb:103 # Array#combine_with combines two arrays by alternatingly taking elements
rspec /tmp/d20131023-4395-lc00sv/spec.rb:113 # Array#combine_with doesn't change the array

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

Десислав обнови решението на 16.10.2013 15:50 (преди над 10 години)

+class Integer
+ def remainder (quotient, divisor)
+ if quotient % divisor == 0
+ return false
+ else
+ return true
+ end
+ end
+ def has_divisor_except_itself_and_one (number)
+ divisor = number - 1
+ while divisor > 1 do
+ return false if not remainder number, divisor
+ divisor -= 1
+ end
+ return true
+ end
+ def finds_first_prime_divisor list
+ num = 2
+ while num <= list[-1].abs
+ pr_div = list[-1].abs / num
+ return list+[num,pr_div] if num.prime? and not remainder list[-1].abs,num
+ num += 1
+ end
+ end
+ def prime?
+ if self <= 1
+ return false
+ elsif self > 1
+ has_divisor_except_itself_and_one self
+ end
+ end
+ def prime_factors
+ list_of_prime_factors = [self]
+ while not list_of_prime_factors[-1].abs.prime?
+ list_of_prime_factors = finds_first_prime_divisor list_of_prime_factors
+ list_of_prime_factors.delete_at(-3)
+ end
+ puts list_of_prime_factors.sort
+ end
+ def digits
+ list = []
+ number = self
+ while number != 0
+ list.push number % 10
+ number /= 10
+ end
+ puts list.reverse
+ end
+end

Десислав обнови решението на 16.10.2013 17:28 (преди над 10 години)

+#!/usr/bin/ruby
+
+# Add a new methods (function) to class Integer.
+
class Integer
def remainder (quotient, divisor)
if quotient % divisor == 0
return false
else
return true
end
end
def has_divisor_except_itself_and_one (number)
divisor = number - 1
while divisor > 1 do
return false if not remainder number, divisor
divisor -= 1
end
return true
end
def finds_first_prime_divisor list
num = 2
while num <= list[-1].abs
pr_div = list[-1].abs / num
return list+[num,pr_div] if num.prime? and not remainder list[-1].abs,num
num += 1
end
end
def prime?
if self <= 1
return false
elsif self > 1
has_divisor_except_itself_and_one self
end
end
def prime_factors
list_of_prime_factors = [self]
while not list_of_prime_factors[-1].abs.prime?
list_of_prime_factors = finds_first_prime_divisor list_of_prime_factors
list_of_prime_factors.delete_at(-3)
end
puts list_of_prime_factors.sort
end
def digits
list = []
number = self
while number != 0
list.push number % 10
number /= 10
end
puts list.reverse
end
-end
+end
+
+class Array
+ def average
+ sum = 0
+ self.each { |n| sum += n }
+ puts sum / self.length
+ end
+end