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

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

Към профила на Сашо Михайлов

Резултати

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

Код

#1
class Integer
def prime?
i=2
return false if self<0
while(i<self/2)
return false if self%i==0
i+=1
end
return true
end
#2
def prime_factors
numbers = []
i=2
input=self
return prime_factors_help(i,input,numbers)
end
def prime_factors_help(i,input,numbers)
while(i<=input)
if input%i==0
numbers.push i
input=input/i
else
i+=1
end
end
return numbers
end
#3
def harmonic
sum=0/1
i=1
while (i<=self)
sum+=1/i.to_r
i+=1
end
return sum
end
#4
def digits
input=self.abs
number = []
while input>0
number.unshift input%10
input/=10
end
return number
end
end
#5
class Array
def frequencies
input = Hash.new 0
i=0
while i<self.size
input [self [i]]+=1
i+=1
end
return input
end
#6
def average
i=0
sum=0.0
while i<self.size
sum+=self[i]
i+=1
end
return sum/i
end
#7
def drop_every(n)
t=n
input = self
while t<=input.size
input.delete_at(t-1)
t+=n-1
end
return input
end
#8
def combine_with(other)
bufer=[]
return self if other.empty?
return other if self.empty?
return bufer_fill(self,other,bufer)
end
def bufer_fill(self_input,other_input,bufer)
i=0
while i<self_input.size or i<other_input.size
bufer.push self_input[i] if self_input[i]
bufer.push other_input[i] if other_input[i]
i+=1
end
return bufer
end
end

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

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

  4) 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-1299z4c/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)>'

  5) 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-1299z4c/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.02341 seconds
14 examples, 5 failures

Failed examples:

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

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

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

+#1
+class Integer
+ def prime?
+ i=2
+ return false if self<0
+ while(i<self/2)
+ return false if self%i==0
+ i+=1
+ end
+ return true
+ end
+#2
+def prime_factors
+ numbers = []
+ i=2
+ input=self
+ return prime_factors_help(i,input,numbers)
+end
+def prime_factors_help(i,input,numbers)
+ while(i<=input)
+ if input%i==0
+ numbers.push i
+ input=input/i
+ else
+ i+=1
+ end
+ end
+ return numbers
+end
+#3
+def harmonic
+ sum=0/1
+ i=1
+ while (i<=self)
+ sum+=1/i.to_r
+ i+=1
+ end
+ return sum
+end
+#4
+def digits
+ input=self.abs
+ number = []
+ while input>0
+ number.unshift input%10
+ input/=10
+ end
+ return number
+end
+end
+#5
+class Array
+ def frequencies
+ input = Hash.new 0
+ i=0
+ while i<self.size
+ input [self [i]]+=1
+ i+=1
+ end
+ return input
+ end
+#6
+ def average
+ i=0
+ sum=0.0
+ while i<self.size
+ sum+=self[i]
+ i+=1
+ end
+ return sum/i
+ end
+#7
+ def drop_every(n)
+ t=n
+ input = self
+ while t<=input.size
+ input.delete_at(t-1)
+ t+=n-1
+ end
+ return input
+ end
+#8
+ def combine_with(other)
+ bufer=[]
+ return self if other.empty?
+ return other if self.empty?
+ return bufer_fill(self,other,bufer)
+ end
+ def bufer_fill(self_input,other_input,bufer)
+ i=0
+ while i<self_input.size or i<other_input.size
+ bufer.push self_input[i] if self_input[i]
+ bufer.push other_input[i] if other_input[i]
+ i+=1
+ end
+ return bufer
+ end
+end