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

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

Към профила на Аделина Рудова

Резултати

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

Код

class Integer
def prime?
return false if self <= 0
self.is_not_divisible_by? 2
end
def is_not_divisible_by?(divisor)
return true if divisor > Math.sqrt(self)
return false if self % divisor == 0
self.is_not_divisible_by? divisor + 1
end
def prime_factors
new_self = self > 0 ? self : -self
make_divisors_array 2, [], new_self
end
def make_divisors_array(divisor, result_array, new_self)
while new_self > 1
while new_self % divisor == 0
result_array << divisor
new_self = new_self / divisor
end
divisor = divisor + 1
end
return result_array
end
def harmonic
(1..self).to_a.map { |n| Rational(1, n) }.inject { |sum, n| sum + n }
end
def digits
new_self = self
digits_array = []
while new_self > 0
digits_array << new_self % 10
new_self /= 10
end
return digits_array.reverse
end
end
class Array
def frequencies
freqs_hash = {}
self.each { |element| freqs_hash[element] = self.count element }
return freqs_hash
end
def average
new_self = self
return Rational(new_self.inject { |sum, n| sum + n}, new_self.count).to_f
end
def drop_every(n)
result_array = []
self.each_index { |i| result_array << self[i] if (i + 1) % n != 0 }
return result_array
end
def combine_with(other)
return self if other == []
return other if self == []
return self.make_combination other, []
end
def make_combination(other, result_array)
result_array << self.shift << other.shift
return result_array += self if other == []
return result_array += other if self == []
return self.make_combination other, result_array
end
end

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

F...FF.......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-e104tk/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 constructs an array containing the digits of a number
     Failure/Error: 0.digits.should      eq [0]
       
       expected: [0]
            got: []
       
       (compared using ==)
     # /tmp/d20131023-4395-e104tk/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)>'

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

  4) Array#combine_with doesn't change the array
     Failure/Error: expect { array.combine_with [1, 2, 3] }.to_not change { array }
       result should not have changed, but did change from [:a, :b, :c] to []
     # /tmp/d20131023-4395-e104tk/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 0.0175 seconds
14 examples, 4 failures

Failed examples:

rspec /tmp/d20131023-4395-e104tk/spec.rb:2 # Integer#prime? checks if a number is prime
rspec /tmp/d20131023-4395-e104tk/spec.rb:43 # Integer#digits constructs an array containing the digits of a number
rspec /tmp/d20131023-4395-e104tk/spec.rb:50 # Integer#digits works with negative numbers
rspec /tmp/d20131023-4395-e104tk/spec.rb:113 # Array#combine_with doesn't change the array

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

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

+class Integer
+
+ def prime?
+ return false if self <= 0
+ self.is_not_divisible_by? 2
+ end
+
+ def is_not_divisible_by?(divisor)
+ return true if divisor > Math.sqrt(self)
+ return false if self % divisor == 0
+ self.is_not_divisible_by? divisor + 1
+ end
+
+ def prime_factors
+ new_self = self > 0 ? self : -self
+ make_divisors_array 2, [], new_self
+ end
+
+ def make_divisors_array(divisor, result_array, new_self)
+ while new_self > 1
+ while new_self % divisor == 0
+ result_array << divisor
+ new_self = new_self / divisor
+ end
+ divisor = divisor + 1
+ end
+ return result_array
+ end
+
+ def harmonic
+ (1..self).to_a.map { |n| Rational(1, n) }.inject { |sum, n| sum + n }
+ end
+
+ def digits
+ new_self = self
+ digits_array = []
+ while new_self > 0
+ digits_array << new_self % 10
+ new_self /= 10
+ end
+ return digits_array.reverse
+ end
+
+end
+
+class Array
+
+ def frequencies
+ freqs_hash = {}
+ self.each { |element| freqs_hash[element] = self.count element }
+ return freqs_hash
+ end
+
+ def average
+ new_self = self
+ return Rational(new_self.inject { |sum, n| sum + n}, new_self.count).to_f
+ end
+
+ def drop_every(n)
+ result_array = []
+ self.each_index { |i| result_array << self[i] if (i + 1) % n != 0 }
+ return result_array
+ end
+
+ def combine_with(other)
+ return self if other == []
+ return other if self == []
+ return self.make_combination(other, [])
+ end
+
+ def make_combination(other, result_array)
+ result_array << self.shift << other.shift
+ return result_array += self if other == []
+ return result_array += other if self == []
+ return self.make_combination(other, result_array)
+ end
+
+end

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

class Integer
def prime?
return false if self <= 0
- self.is_not_divisible_by? 2
+ self.is_not_divisible_by? 2
end
def is_not_divisible_by?(divisor)
- return true if divisor > Math.sqrt(self)
+ return true if divisor > Math.sqrt(self)
return false if self % divisor == 0
- self.is_not_divisible_by? divisor + 1
+ self.is_not_divisible_by? divisor + 1
end
def prime_factors
- new_self = self > 0 ? self : -self
- make_divisors_array 2, [], new_self
+ new_self = self > 0 ? self : -self
+ make_divisors_array 2, [], new_self
end
def make_divisors_array(divisor, result_array, new_self)
- while new_self > 1
- while new_self % divisor == 0
- result_array << divisor
- new_self = new_self / divisor
- end
- divisor = divisor + 1
+ while new_self > 1
+ while new_self % divisor == 0
+ result_array << divisor
+ new_self = new_self / divisor
+ end
+ divisor = divisor + 1
end
return result_array
end
def harmonic
- (1..self).to_a.map { |n| Rational(1, n) }.inject { |sum, n| sum + n }
+ (1..self).to_a.map { |n| Rational(1, n) }.inject { |sum, n| sum + n }
end
def digits
- new_self = self
- digits_array = []
- while new_self > 0
- digits_array << new_self % 10
- new_self /= 10
- end
- return digits_array.reverse
+ new_self = self
+ digits_array = []
+ while new_self > 0
+ digits_array << new_self % 10
+ new_self /= 10
+ end
+ return digits_array.reverse
end
end
class Array
- def frequencies
+ def frequencies
freqs_hash = {}
self.each { |element| freqs_hash[element] = self.count element }
return freqs_hash
- end
+ end
- def average
+ def average
new_self = self
return Rational(new_self.inject { |sum, n| sum + n}, new_self.count).to_f
- end
+ end
- def drop_every(n)
+ def drop_every(n)
result_array = []
self.each_index { |i| result_array << self[i] if (i + 1) % n != 0 }
return result_array
- end
+ end
- def combine_with(other)
+ def combine_with(other)
return self if other == []
return other if self == []
return self.make_combination(other, [])
- end
+ end
def make_combination(other, result_array)
result_array << self.shift << other.shift
return result_array += self if other == []
return result_array += other if self == []
return self.make_combination(other, result_array)
end
end

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

class Integer
def prime?
return false if self <= 0
self.is_not_divisible_by? 2
end
def is_not_divisible_by?(divisor)
return true if divisor > Math.sqrt(self)
return false if self % divisor == 0
self.is_not_divisible_by? divisor + 1
end
def prime_factors
new_self = self > 0 ? self : -self
make_divisors_array 2, [], new_self
end
def make_divisors_array(divisor, result_array, new_self)
while new_self > 1
while new_self % divisor == 0
result_array << divisor
- new_self = new_self / divisor
+ new_self = new_self / divisor
end
divisor = divisor + 1
end
return result_array
end
def harmonic
(1..self).to_a.map { |n| Rational(1, n) }.inject { |sum, n| sum + n }
end
def digits
new_self = self
digits_array = []
while new_self > 0
digits_array << new_self % 10
new_self /= 10
end
return digits_array.reverse
end
end
class Array
def frequencies
freqs_hash = {}
self.each { |element| freqs_hash[element] = self.count element }
return freqs_hash
end
def average
new_self = self
return Rational(new_self.inject { |sum, n| sum + n}, new_self.count).to_f
end
def drop_every(n)
result_array = []
self.each_index { |i| result_array << self[i] if (i + 1) % n != 0 }
return result_array
end
def combine_with(other)
return self if other == []
return other if self == []
- return self.make_combination(other, [])
+ return self.make_combination other, []
end
def make_combination(other, result_array)
result_array << self.shift << other.shift
return result_array += self if other == []
return result_array += other if self == []
- return self.make_combination(other, result_array)
+ return self.make_combination other, result_array
end
end