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

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

Към профила на Радина Петкова

Резултати

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

Код

class Integer
def prime?
return false if self < 2
2.upto(self / 2) do |x| return false if self % x == 0 end
true
end
def prime_factors
int = self.abs
return [int] if int.prime?
factors = []
2.upto(int / 2) do |x| (factors.push x and int /= x) while int % x == 0 end
factors
end
def harmonic
sum = 1/1r
return sum if self == 1
2.upto(self) do |x| sum += Rational(1) / x end
sum
end
def digits
int = self.abs
return [int] if int < 10
digits_ls = []
(digits_ls.push (int % 10) and int /= 10) while int > 0
digits_ls.reverse
end
end
class Array
def frequencies
freq_ls = {}
0.upto(self.size - 1) do |x|
n = self.select{ |i| i == self[x] } and freq_ls[self[x]] = n.size end
freq_ls
end
def average
sum = 0.0
0.upto(self.size) do |x| val = self[x] and sum += val end
sum / self.size
end
def drop_every(n)
left_elements = []
0.upto(self.size - 1) do |x|
left_elements.push self[x] if (x + 1) % n != 0 end
left_elements
end
def combine_with(other)
return self if other == []
return other if self == []
comb_ls = []
self.size > other.size ? max_size = self.size : max_size = other.size
0.upto(max_size) do |x| comb_ls.push self[x] and comb_ls.push other[x] end
comb_ls.select{ |x| x !=nil }
end
end

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

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

Failures:

  1) 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-1isqrle/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.01762 seconds
14 examples, 1 failure

Failed examples:

rspec /tmp/d20131023-4395-1isqrle/spec.rb:103 # Array#combine_with combines two arrays by alternatingly taking elements

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

Радина обнови решението на 16.10.2013 03:31 (преди около 11 години)

+class Integer
+ def prime?
+ return false if self < 2
+ 2.upto(self / 2) do |x| return false if self % x == 0 end
+ true
+ end
+ def prime_factors
+ int = self.abs
+ return [int] if int.prime?
+ factors = []
+ 2.upto(int / 2) do |x| (factors.push x and int /= x) while int % x == 0 end
+ factors
+ end
+ def harmonic
+ sum = 1/1r
+ return sum if self == 1
+ 2.upto(self) do |x| sum += Rational(1) / x end
+ sum
+ end
+ def digits
+ int = self.abs
+ return [int] if int < 10
+ digits_ls = []
+ (digits_ls.push (int % 10) and int /= 10) while int > 0
+ digits_ls.reverse
+ end
+ end
+class Array
+ def frequencies
+ freq_ls = {}
+ 0.upto(self.size - 1) do |x|
+ n = self.select{ |i| i == self[x] } and freq_ls[self[x]] = n.size end
+ freq_ls
+ end
+ def average
+ sum = 0.0
+ 0.upto(self.size) do |x| val = self[x] and sum += val end
+ sum / self.size
+ end
+ def drop_every(n)
+ left_elements = []
+ 0.upto(self.size - 1) do |x|
+ left_elements.push self[x] if (x + 1) % n != 0 end
+ left_elements
+ end
+ def combine_with(other)
+ return self if other == []
+ return other if self == []
+ comb_ls = []
+ self.size > other.size ? max_size = self.size : max_size = other.size
+ 0.upto(max_size) do |x| comb_ls.push self[x] and comb_ls.push other[x] end
+ comb_ls.select{ |x| x !=nil }
+ end
+ end