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

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

Към профила на Георги Пурнаров

Резултати

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

Код

class Integer
def prime?
return false if self < 0
(2..self/2).each do |factor|
return false if self%factor==0
end
return true
end
def prime_factors(num=self.abs)
list =[]
factor=2
while num>1
list.push factor while num>num =factor?(num,factor)
factor+=1
end
return list
end
def factor? (num,factor)
if num%factor==0
return num/factor
else
return num
end
end
def harmonic
result=0
(1..self).each { |num| result+=Rational(1,num) }
return result
end
def digits
num=self.abs
list = []
while num > 0
list.unshift num%10
num /=10
end
return list
end
end
class Array
def frequencies
list= Hash.new(0)
self.each { |element| list[element]+=1 }
return list
end
def average
average=0.0
self.each { |element| average+=element}
average /=self.size
return average
end
def drop_every(n)
index=n-1
list=self
while index<list.size
list.delete_at(index)
index +=n-1
end
return list
end
def combine_arrays(first, second)
list=[]
0.upto(first.size>second.size ? first.size-1 : second.size-1).each do|index|
list.push(first[index]) if first[index]
list.push(second[index]) if second[index]
end
return list
end
def combine_with(other)
list=[]
if self.empty? or other.empty?
list=self+other
else
list=combine_arrays(self, other)
end
return list
end
end

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

F...F......FF.

Failures:

  1) Integer#prime? checks if a number is prime
     Failure/Error: 0.prime?.should   eq false
       
       expected: false
            got: true
       
       (compared using ==)
     # /tmp/d20131023-4395-17oevwt/spec.rb:4: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-17oevwt/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) Array#drop_every doesn't change the array
     Failure/Error: expect { array.drop_every(3) }.to_not change { array }
       result should not have changed, but did change from [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] to [1, 2, 4, 5, 7, 8, 10]
     # /tmp/d20131023-4395-17oevwt/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)>'

  4) 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-17oevwt/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.01835 seconds
14 examples, 4 failures

Failed examples:

rspec /tmp/d20131023-4395-17oevwt/spec.rb:2 # Integer#prime? checks if a number is prime
rspec /tmp/d20131023-4395-17oevwt/spec.rb:43 # Integer#digits constructs an array containing the digits of a number
rspec /tmp/d20131023-4395-17oevwt/spec.rb:96 # Array#drop_every doesn't change the array
rspec /tmp/d20131023-4395-17oevwt/spec.rb:103 # Array#combine_with combines two arrays by alternatingly taking elements

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

Георги обнови решението на 14.10.2013 23:55 (преди около 11 години)

+class Integer
+ def prime?
+ return false if self < 0
+ (2..self/2).each do |factor|
+ return false if self%factor==0
+ end
+ return true
+ end
+
+ def prime_factors(num=self.abs)
+ list =[]
+ factor=2
+ while num>1
+ list.push factor while num>num =factor?(num,factor)
+ factor+=1
+ end
+ return list
+ end
+
+ def factor? (num,factor)
+ if num%factor==0
+ return num/factor
+ else
+ return num
+ end
+ end
+
+ def harmonic
+ result=0
+ (1..self).each { |num| result+=Rational(1,num) }
+ return result
+ end
+
+ def digits
+ num=self.abs
+ list = []
+ while num > 0
+ list.unshift num%10
+ num /=10
+ end
+ return list
+ end
+end
+
+class Array
+ def frequencies
+ list= Hash.new(0)
+ self.each { |element| list[element]+=1 }
+ return list
+ end
+
+ def average
+ average=0.0
+ self.each { |element| average+=element}
+ average /=self.size
+ return average
+ end
+ def drop_every(n)
+ index=n-1
+ list=self
+ while index<list.size
+ list.delete_at(index)
+ index +=n-1
+ end
+ return list
+ end
+
+ def combine_arrays(first, second)
+ list=[]
+ 0.upto(first.size>second.size ? first.size-1 : second.size-1).each do|index|
+ list.push(first[index]) if first[index]
+ list.push(second[index]) if second[index]
+ end
+ return list
+ end
+
+ def combine_with(other)
+ list=[]
+ if self.empty? or other.empty?
+ list=self+other
+ else
+ list=combine_arrays(self, other)
+ end
+ return list
+ end
+end