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

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

Към профила на Петя Делчева

Резултати

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

Код

class Integer
def prime?
if self<0 then check_prime=false else check_prime=true end
(2..self-1).each{ |x| if self%x == 0 then check_prime = false end}
check_prime
end
def prime_factors
prime_digits,input = [] , self
(2..input).each{ |x| if input%x==0 then input/=x and prime_digits.push(x) end}
prime_digits
end
def harmonic
sum = 0
if self<0 then self.abs end
(1..self).each{|x| sum+=1/(x.to_r)}
sum
end
def digits
array_digits = [] and number = self and i=0
while number > 0
array_digits[i]=number%10 and number = number/10 and i=i+1
end
array_digits.reverse
end
end
class Array
def frequencies
uniq_arr,output_hash=self.uniq,Hash.new
output_hash.default=0
self.each{|x| if uniq_arr.include?(x) then output_hash[x]+=1 end}
output_hash
end
def average
input_array=self and sum=0
input_array.each{|x| sum=sum+x}
((sum.to_f/(input_array.length)))
end
def drop_every(p)
inputarr=self and outputarr = []
inputarr.each_index{|x| if (x-1)%p!=0 then outputarr.push(inputarr[x]) end}
outputarr
end
def combine_with(second_arr)
combined_arr,i,p=[], 0 , 0
while self.length>=i or second_arr.length>=p
combined_arr.push(self[i]).push(second_arr[p]) and i+=1 and p+=1
end
combined_arr=combined_arr.compact
combined_arr
end
end

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

FFF.FF....F.F.

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-sjeyk3/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 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: [2, 3, 4, 5]
       
       (compared using ==)
     # /tmp/d20131023-4395-sjeyk3/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: []
       
       (compared using ==)
     # /tmp/d20131023-4395-sjeyk3/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#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-sjeyk3/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)>'

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

  6) 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, 3, 4, 6, 7, 9, 10]
       
       (compared using ==)
     # /tmp/d20131023-4395-sjeyk3/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)>'

  7) 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-sjeyk3/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.02378 seconds
14 examples, 7 failures

Failed examples:

rspec /tmp/d20131023-4395-sjeyk3/spec.rb:2 # Integer#prime? checks if a number is prime
rspec /tmp/d20131023-4395-sjeyk3/spec.rb:18 # Integer#prime_factors constructs an array containing the prime factors in ascending order
rspec /tmp/d20131023-4395-sjeyk3/spec.rb:26 # Integer#prime_factors works with negative numbers
rspec /tmp/d20131023-4395-sjeyk3/spec.rb:43 # Integer#digits constructs an array containing the digits of a number
rspec /tmp/d20131023-4395-sjeyk3/spec.rb:50 # Integer#digits works with negative numbers
rspec /tmp/d20131023-4395-sjeyk3/spec.rb:87 # Array#drop_every drops every n-th element from an array.
rspec /tmp/d20131023-4395-sjeyk3/spec.rb:103 # Array#combine_with combines two arrays by alternatingly taking elements

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

Петя обнови решението на 14.10.2013 10:10 (преди над 10 години)

+class Integer
+
+ def prime?
+ if self<0 then check_prime=false else check_prime=true end
+ i=self-1
+ while i<self and i>=2 do
+ if self%i==0 then check_prime=false end
+ i=i-1
+ end
+ return check_prime
+ end
+ def harmonic
+ sum=0 and i=0
+ if self<0 then self.abs end
+ while i<self and i>=0
+ i=(i+1).to_r
+ sum=sum+1/i
+ end
+ return sum
+ end
+
+ def digits
+ array_digits = [] and number = self and i=0
+ while number > 0
+ array_digits[i]=number%10 and number = number/10 and i=i+1
+ end
+ return array_digits.reverse
+ end
+
+ # tova e moe tvorenie minava testa no go razgledai nz kolko e vqrno
+ # mai be6e kazano 4e trqbva da raboti i za otricatelni 4isla...
+ def prime_factors
+ prime_digits = [] and number = self and count = 0
+ for i in 2..number
+ while number%i == 0
+ prime_digits[count] = i and count = count + 1 and number = number/i
+ end
+ end
+ return prime_digits
+ end
+
+end
+class Array
+
+ def average
+ input_array=self and sum=0
+ input_array.each{|x| sum=sum+x}
+ return ((sum.to_f/(input_array.length)))
+ end
+
+ def drop_every(p)
+ inputarr=self and outputarr = []
+ inputarr.each_index{|x| if (x-1)%p!=0 then outputarr.push(inputarr[x]) end}
+ return outputarr
+ end
+end

Петя обнови решението на 14.10.2013 10:15 (преди над 10 години)

class Integer
def prime?
if self<0 then check_prime=false else check_prime=true end
i=self-1
while i<self and i>=2 do
if self%i==0 then check_prime=false end
i=i-1
end
return check_prime
end
def harmonic
sum=0 and i=0
if self<0 then self.abs end
while i<self and i>=0
i=(i+1).to_r
sum=sum+1/i
end
return sum
end
def digits
array_digits = [] and number = self and i=0
while number > 0
array_digits[i]=number%10 and number = number/10 and i=i+1
end
return array_digits.reverse
end
- # tova e moe tvorenie minava testa no go razgledai nz kolko e vqrno
- # mai be6e kazano 4e trqbva da raboti i za otricatelni 4isla...
def prime_factors
prime_digits = [] and number = self and count = 0
for i in 2..number
while number%i == 0
prime_digits[count] = i and count = count + 1 and number = number/i
end
end
return prime_digits
end
end
class Array
def average
input_array=self and sum=0
input_array.each{|x| sum=sum+x}
return ((sum.to_f/(input_array.length)))
end
def drop_every(p)
inputarr=self and outputarr = []
inputarr.each_index{|x| if (x-1)%p!=0 then outputarr.push(inputarr[x]) end}
return outputarr
end
end

Петя обнови решението на 15.10.2013 00:10 (преди над 10 години)

class Integer
-
def prime?
if self<0 then check_prime=false else check_prime=true end
- i=self-1
- while i<self and i>=2 do
- if self%i==0 then check_prime=false end
- i=i-1
- end
- return check_prime
+ (2..self-1).each{ |x| if self%x == 0 then check_prime = false end}
+ check_prime
end
+ def prime_factors
+ prime_digits,input = [] , self
+ (2..input).each{ |x| if input%x==0 then input/=x and prime_digits.push(x) end}
+ prime_digits
+ end
def harmonic
- sum=0 and i=0
- if self<0 then self.abs end
- while i<self and i>=0
- i=(i+1).to_r
- sum=sum+1/i
- end
- return sum
+ sum = 0
+ if self<0 then self.abs end
+ (1..self).each{|x| sum+=1/(x.to_r)}
+ sum
end
-
def digits
array_digits = [] and number = self and i=0
while number > 0
array_digits[i]=number%10 and number = number/10 and i=i+1
end
- return array_digits.reverse
+ array_digits.reverse
end
-
- def prime_factors
- prime_digits = [] and number = self and count = 0
- for i in 2..number
- while number%i == 0
- prime_digits[count] = i and count = count + 1 and number = number/i
- end
- end
- return prime_digits
- end
-
end
class Array
+ def frequencies
+ uniq_arr,output_hash=self.uniq,Hash.new
+ output_hash.default=0
+ self.each{|x| if uniq_arr.include?(x) then output_hash[x]+=1 end}
+ output_hash
+ end
def average
input_array=self and sum=0
input_array.each{|x| sum=sum+x}
- return ((sum.to_f/(input_array.length)))
+ ((sum.to_f/(input_array.length)))
end
def drop_every(p)
inputarr=self and outputarr = []
inputarr.each_index{|x| if (x-1)%p!=0 then outputarr.push(inputarr[x]) end}
- return outputarr
+ outputarr
end
end

Петя обнови решението на 15.10.2013 23:47 (преди над 10 години)

class Integer
def prime?
if self<0 then check_prime=false else check_prime=true end
(2..self-1).each{ |x| if self%x == 0 then check_prime = false end}
check_prime
end
def prime_factors
prime_digits,input = [] , self
(2..input).each{ |x| if input%x==0 then input/=x and prime_digits.push(x) end}
prime_digits
end
def harmonic
sum = 0
if self<0 then self.abs end
(1..self).each{|x| sum+=1/(x.to_r)}
sum
end
def digits
array_digits = [] and number = self and i=0
while number > 0
array_digits[i]=number%10 and number = number/10 and i=i+1
end
array_digits.reverse
end
end
class Array
def frequencies
uniq_arr,output_hash=self.uniq,Hash.new
output_hash.default=0
self.each{|x| if uniq_arr.include?(x) then output_hash[x]+=1 end}
output_hash
end
def average
input_array=self and sum=0
input_array.each{|x| sum=sum+x}
((sum.to_f/(input_array.length)))
end
def drop_every(p)
inputarr=self and outputarr = []
inputarr.each_index{|x| if (x-1)%p!=0 then outputarr.push(inputarr[x]) end}
outputarr
end
+ def combine_with(second_arr)
+ combined_arr,i,p=[], 0 , 0
+ while self.length>=i or second_arr.length>=p
+ combined_arr.push(self[i]).push(second_arr[p]) and i+=1 and p+=1
+ end
+ combined_arr=combined_arr.compact
+ combined_arr
+ end
end