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

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

Към профила на Емануил Иванов

Резултати

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

Код

class Integer
def prime?
divisor=2
prime=true
while(prime and (divisor<=Math.sqrt(self))) do
if self%divisor==0 then prime=false
end
divisor+=1
end
puts prime
end
def prime_factors
end
def harmonic
sum=0
1.upto(self) {|i| sum+=1.0/i}
sum
end
def digits
digitsArray=[]
number=self
while(number>0) do
digitsArray.push(number%10)
number=number/10
end
digitsArray.reverse
end
end
class Array
def frequencies
frequenciesHash= {}
count=1
self.each do |i|
if frequenciesHash.has_key?(i) then count+=1
else
frequenciesHash={i=>count}
end
print frequenciesHash
end
end
def average
count=0
sum=0.0
self.each do |i|
sum+=i
count+=1
end
puts sum/count
end
def drop_every(n)
position=0
inputArray=self.collect { |x| x**1}
inputArray.each do |i|
intChecker=(position+1)/n.to_r
if intChecker.floor==intChecker then self.delete_at(position)
end
position+=1
end
end
def combine_with (other)
end
end

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

FFFFFFF{1=>1}{2=>1}{:c=>1}{1=>1}{1=>1}.42.0
F5.5
.FFF.

Failures:

  1) Integer#prime? checks if a number is prime
     Failure/Error: -13.prime?.should eq false
     Math::DomainError:
       Numerical argument is out of domain - "sqrt"
     # /tmp/d20131023-4395-1mwojac/solution.rb:5:in `sqrt'
     # /tmp/d20131023-4395-1mwojac/solution.rb:5:in `prime?'
     # /tmp/d20131023-4395-1mwojac/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) Integer#prime_factors constructs an array containing the prime factors in ascending order
     Failure/Error: 360.prime_factors.should eq [2, 2, 2, 3, 3, 5]
       
       expected: [2, 2, 2, 3, 3, 5]
            got: nil
       
       (compared using ==)
     # /tmp/d20131023-4395-1mwojac/spec.rb:19: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#prime_factors works with negative numbers
     Failure/Error: (-4).prime_factors.should   eq [2, 2]
       
       expected: [2, 2]
            got: nil
       
       (compared using ==)
     # /tmp/d20131023-4395-1mwojac/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)>'

  4) Integer#harmonic returns the n-th harmonic number
     Failure/Error: 42.harmonic.should eq 12309312989335019/2844937529085600r
       
       expected: (12309312989335019/2844937529085600)
            got: 4.326742806648339
       
       (compared using ==)
     # /tmp/d20131023-4395-1mwojac/spec.rb:38: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) 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-1mwojac/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)>'

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

  7) Array#frequencies returns a map from distinct items to the number of times they appear
     Failure/Error: [].frequencies.should                    == {}
       expected: {}
            got: [] (using ==)
       Diff:
       @@ -1 +1,2 @@
       +[]
     # /tmp/d20131023-4395-1mwojac/spec.rb:59: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)>'

  8) Array#average calculates the average of the numbers in the array
     Failure/Error: [42].average.should                   eq 42
       
       expected: 42
            got: nil
       
       (compared using ==)
     # /tmp/d20131023-4395-1mwojac/spec.rb:74: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)>'

  9) Array#drop_every drops every n-th element from an array.
     Failure/Error: (1..10).to_a.drop_every(2).should eq [1, 3, 5, 7, 9]
       
       expected: [1, 3, 5, 7, 9]
            got: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
       
       (compared using ==)
     # /tmp/d20131023-4395-1mwojac/spec.rb:90: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)>'

  10) 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, 6, 8, 9, 10]
     # /tmp/d20131023-4395-1mwojac/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)>'

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

Failed examples:

rspec /tmp/d20131023-4395-1mwojac/spec.rb:2 # Integer#prime? checks if a number is prime
rspec /tmp/d20131023-4395-1mwojac/spec.rb:18 # Integer#prime_factors constructs an array containing the prime factors in ascending order
rspec /tmp/d20131023-4395-1mwojac/spec.rb:26 # Integer#prime_factors works with negative numbers
rspec /tmp/d20131023-4395-1mwojac/spec.rb:34 # Integer#harmonic returns the n-th harmonic number
rspec /tmp/d20131023-4395-1mwojac/spec.rb:43 # Integer#digits constructs an array containing the digits of a number
rspec /tmp/d20131023-4395-1mwojac/spec.rb:50 # Integer#digits works with negative numbers
rspec /tmp/d20131023-4395-1mwojac/spec.rb:58 # Array#frequencies returns a map from distinct items to the number of times they appear
rspec /tmp/d20131023-4395-1mwojac/spec.rb:73 # Array#average calculates the average of the numbers in the array
rspec /tmp/d20131023-4395-1mwojac/spec.rb:87 # Array#drop_every drops every n-th element from an array.
rspec /tmp/d20131023-4395-1mwojac/spec.rb:96 # Array#drop_every doesn't change the array
rspec /tmp/d20131023-4395-1mwojac/spec.rb:103 # Array#combine_with combines two arrays by alternatingly taking elements

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

Емануил обнови решението на 16.10.2013 15:34 (преди над 10 години)

+class Integer
+ def prime?
+ divisor=2
+ prime=true
+ while(prime and (divisor<=Math.sqrt(self))) do
+ if self%divisor==0 then prime=false
+ end
+ divisor+=1
+ end
+ puts prime
+ end
+
+ def prime_factors
+ end
+
+ def harmonic
+ sum=0
+ 1.upto(self) {|i| sum+=1.0/i}
+
+ sum
+ end
+
+ def digits
+ digitsArray=[]
+ number=self
+ while(number>0) do
+ digitsArray.push(number%10)
+ number=number/10
+ end
+ digitsArray.reverse
+ end
+
+end
+class Array
+ def frequencies
+ frequenciesHash= {}
+ count=1
+ self.each do |i|
+ if frequenciesHash.has_key?(i) then count+=1
+ else
+ frequenciesHash={i=>count}
+ end
+ print frequenciesHash
+ end
+
+ end
+
+ def average
+ count=0
+ sum=0.0
+ self.each do |i|
+ sum+=i
+ count+=1
+ end
+ puts sum/count
+ end
+
+ def drop_every(n)
+ position=0
+ inputArray=self.collect { |x| x**1}
+ inputArray.each do |i|
+ intChecker=(position+1)/n.to_r
+if intChecker.floor==intChecker then self.delete_at(position)
+ end
+ position+=1
+ end
+
+ end
+ def combine_with (other)
+
+ end
+
+end