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

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

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

Резултати

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

Код

class Integer
def prime?
(2..self.abs - 1).each do |i|
return false if (self.abs % i == 0)
end
true
end
def prime_factors
number = self.abs and prime_factors_array = []
(2..number).each do |i|
while number % i == 0
prime_factors_array << i
number = number/i
end
end
prime_factors_array
end
def harmonic
harmonic_sum = 0
(1..self).each do |i|
harmonic_sum = harmonic_sum.to_r + 1.to_r / i.to_r
end
harmonic_sum.to_r
end
def digits
self < 0 ? length = self.to_s.length - 2 : length = self.to_s.length - 1
array_of_digits = [] and number = self.abs
while length >= 0
array_of_digits << number / (10 ** length)
number = number % (10 ** length) and length = length - 1
end
array_of_digits
end
end
class Array
def frequencies
frequencies_hash = {}
self.each do |item|
frequence_number = self.find_all { |current_item| current_item == item }
frequencies_hash[item] = frequence_number.length
end
frequencies_hash
end
def average
average_sum = 0.0
self.each { |i| average_sum = average_sum + i }
average_sum/self.length
end
def drop_every(n)
array = []
(0..self.length - 1).each do |i|
array << self[i] if (i % n == 0)
end
array
end
def combine_with(other)
combined_array = [] and i = 0
other.each do |item|
combined_array << self[i] << item if i < self.length
i+=1
end
(i..self.length - 1).each { |item| combined_array << self[item] }
combined_array
end
end

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

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

Failures:

  1) Integer#prime? checks if a number is prime
     Failure/Error: -13.prime?.should eq false
       
       expected: false
            got: true
       
       (compared using ==)
     # /tmp/d20131023-4395-f0sale/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) 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-f0sale/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)>'

  3) Array#combine_with combines two arrays by alternatingly taking elements
     Failure/Error: [].combine_with([1, 2, 3]).should                   eq [1, 2, 3]
       
       expected: [1, 2, 3]
            got: []
       
       (compared using ==)
     # /tmp/d20131023-4395-f0sale/spec.rb:105: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.01911 seconds
14 examples, 3 failures

Failed examples:

rspec /tmp/d20131023-4395-f0sale/spec.rb:2 # Integer#prime? checks if a number is prime
rspec /tmp/d20131023-4395-f0sale/spec.rb:87 # Array#drop_every drops every n-th element from an array.
rspec /tmp/d20131023-4395-f0sale/spec.rb:103 # Array#combine_with combines two arrays by alternatingly taking elements

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

Сияна обнови решението на 15.10.2013 21:57 (преди около 11 години)

+class Integer
+ def prime?
+ (2..self.abs - 1).each do |i|
+ return false if (self.abs % i == 0)
+ end
+ true
+ end
+
+ def prime_factors
+ number = self.abs and prime_factors_array = []
+ (2..number).each do |i|
+ while number % i == 0
+ prime_factors_array << i
+ number = number/i
+ end
+ end
+ prime_factors_array
+ end
+
+ def harmonic
+ harmonic_sum = 0
+ (1..self).each do |i|
+ harmonic_sum = harmonic_sum.to_r + 1.to_r / i.to_r
+ end
+ harmonic_sum.to_r
+ end
+
+ def digits
+ self < 0 ? length = self.to_s.length - 2 : length = self.to_s.length - 1
+ array_of_digits = [] and number = self.abs
+ while length >= 0
+ array_of_digits << number / (10 ** length)
+ number = number % (10 ** length) and length = length - 1
+ end
+ array_of_digits
+ end
+end
+class Array
+ def frequencies
+ frequencies_hash = {}
+ self.each do |element|
+ frequence_number = self.find_all { |current_item| current_item == element }
+ frequencies_hash[element] = frequence_number.length
+ end
+ frequencies_hash
+ end
+
+ def average
+ average_sum = 0.0
+ self.each { |i| average_sum = average_sum + i }
+ average_sum/self.length
+ end
+
+ def drop_every(n)
+ array = []
+ (0..self.length - 1).each do |i|
+ array << self[i] if (i % n == 0)
+ end
+ array
+ end
+
+ def combine_with(other)
+ combined_array = [] and i = 0
+ other.each do |item|
+ combined_array << self[i] << item if i < self.length
+ i+=1
+ end
+ (i..self.length - 1).each { |item| combined_array << self[item] }
+ combined_array
+ end
+end

Сияна обнови решението на 16.10.2013 10:30 (преди около 11 години)

class Integer
+
def prime?
(2..self.abs - 1).each do |i|
return false if (self.abs % i == 0)
end
true
end
def prime_factors
- number = self.abs and prime_factors_array = []
- (2..number).each do |i|
- while number % i == 0
- prime_factors_array << i
- number = number/i
- end
- end
- prime_factors_array
+ number = self.abs and prime_factors_array = []
+ (2..number).each do |i|
+ while number % i == 0
+ prime_factors_array << i
+ number = number/i
+ end
+ end
+ prime_factors_array
end
def harmonic
harmonic_sum = 0
(1..self).each do |i|
harmonic_sum = harmonic_sum.to_r + 1.to_r / i.to_r
end
harmonic_sum.to_r
end
def digits
self < 0 ? length = self.to_s.length - 2 : length = self.to_s.length - 1
- array_of_digits = [] and number = self.abs
- while length >= 0
- array_of_digits << number / (10 ** length)
- number = number % (10 ** length) and length = length - 1
- end
- array_of_digits
+ array_of_digits = [] and number = self.abs
+ while length >= 0
+ array_of_digits << number / (10 ** length)
+ number = number % (10 ** length) and length = length - 1
+ end
+ array_of_digits
end
+
end
class Array
+
def frequencies
frequencies_hash = {}
- self.each do |element|
- frequence_number = self.find_all { |current_item| current_item == element }
- frequencies_hash[element] = frequence_number.length
- end
- frequencies_hash
+ self.each do |item|
+ frequence_number = self.find_all { |current_item| current_item == item }
+ frequencies_hash[item] = frequence_number.length
+ end
+ frequencies_hash
end
def average
average_sum = 0.0
- self.each { |i| average_sum = average_sum + i }
- average_sum/self.length
+ self.each { |i| average_sum = average_sum + i }
+ average_sum/self.length
end
def drop_every(n)
- array = []
- (0..self.length - 1).each do |i|
- array << self[i] if (i % n == 0)
- end
- array
+ array = []
+ (0..self.length - 1).each do |i|
+ array << self[i] if (i % n == 0)
+ end
+ array
end
def combine_with(other)
combined_array = [] and i = 0
other.each do |item|
combined_array << self[i] << item if i < self.length
i+=1
end
(i..self.length - 1).each { |item| combined_array << self[item] }
combined_array
end
-end
+
+end