Решение на Първа задача от Станимир Килявков

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

Към профила на Станимир Килявков

Резултати

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

Код

class Integer
def prime?
flag_prime=self>0 ? true : false
(2..self-1).each do |i|
flag_prime=false if self%i==0
end
return flag_prime
end
def prime_factors
source_array,prime_factors_array,i=self,[],2
while source_array>1
if source_array%i!=0 then i+=1 else
source_array/=i and prime_factors_array.push(i) end
end
return prime_factors_array
end
def harmonic
sum,i=0,0
(0..self-1).each do |i|
i=(i+1).to_r
sum+=1/i
end
return sum
end
def digits
number_digits,i,source_number=[],0,self
while source_number>0
number_digits[i]=source_number%10 and source_number/=10
i+=1
end
return number_digits.reverse
end
end
class Array
def frequencies
source_array,node_frequency=self.uniq,{}
node_frequency.default=0
self.each{|x| if source_array.include?(x) then node_frequency[x]+=1 end}
return node_frequency
end
def average
source_array,sum=self,0
source_array.each{|x| sum+=x}
return (sum.to_f/(source_array.length))
end
def drop_every(p)
source_array,merged_arrays,i=self,[],p
source_array.each_index do |x|
if x!=i-1 then
merged_arrays.push(source_array[x]) else i+=p end
end
return merged_arrays
end
def combine_with(source_parameter_array)
source_array,i,p=[],0,0
while self.length>=i or source_parameter_array.length>=p
source_array.push(self[i]) and i+=1
source_array.push(source_parameter_array[p]) and p+=1
end
return source_array.compact
end
end

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

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

Failures:

  1) Integer#prime? checks if a number is prime
     Failure/Error: 1.prime?.should   eq false
       
       expected: false
            got: true
       
       (compared using ==)
     # /tmp/d20131023-4395-xrt4e4/spec.rb:5: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-xrt4e4/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-xrt4e4/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) Integer#digits works with negative numbers
     Failure/Error: (-33).digits.should     eq [3, 3]
       
       expected: [3, 3]
            got: []
       
       (compared using ==)
     # /tmp/d20131023-4395-xrt4e4/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)>'

  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-xrt4e4/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.01862 seconds
14 examples, 5 failures

Failed examples:

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

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

Станимир обнови решението на 14.10.2013 15:09 (преди около 11 години)

+class Integer
+ def prime?
+ i,flag=self-1,false
+ flag=true if self>0
+ while i>=2
+ flag=false if self%i==0
+ i-=1 end
+ return flag
+ end
+
+ def prime_factors
+ tmp,arr,i=self,[],2
+ while tmp>1
+ if tmp%i==0 then tmp=tmp/i and arr.push(i)
+ else i+=1 end
+ end
+ return arr
+ end
+
+ def harmonic
+ sum,i=0,0
+ while i<self
+ i=(i+1).to_r
+ sum+=1/i
+ end
+ return sum
+ end
+
+ def digits
+ arr,i,tmp=[],0,self
+ while tmp>0
+ arr[i]=tmp%10 and tmp/=10
+ i+=1
+ end
+ return arr.reverse
+ end
+end
+
+class Array
+ def frequencies
+ tmp,myhsh=self.uniq,Hash.new
+ myhsh.default=0
+ self.each{|x| if tmp.include?(x) then myhsh[x]+=1 end}
+ return myhsh
+ end
+
+ def average
+ arr,sum=self,0
+ arr.each{|x| sum+=x}
+ return (sum.to_f/(arr.length))
+ end
+
+ def drop_every(p)
+ arr,newarr,i=self,[],p
+ arr.each_index{|x|
+ if x!=i-1 then newarr.push(arr[x]) else i+=p end}
+ return newarr
+ end
+
+ def combine_with(a)
+ tmp,i,p=[],0,0
+ while self.length>=i or a.length>=p
+ tmp.push(self[i]) and i+=1
+ tmp.push(a[p]) and p+=1
+ end
+ tmp=tmp.compact
+ return tmp
+ end
+end

Станимир обнови решението на 14.10.2013 15:19 (преди около 11 години)

class Integer
def prime?
i,flag=self-1,false
- flag=true if self>0
+ flag=true if self>0
while i>=2
flag=false if self%i==0
- i-=1 end
+ i-=1 end
return flag
end
def prime_factors
tmp,arr,i=self,[],2
while tmp>1
- if tmp%i==0 then tmp=tmp/i and arr.push(i)
+ if tmp%i==0 then tmp=tmp/i and arr.push(i)
else i+=1 end
end
return arr
end
def harmonic
- sum,i=0,0
- while i<self
- i=(i+1).to_r
- sum+=1/i
- end
- return sum
+ sum,i=0,0
+ while i<self
+ i=(i+1).to_r
+ sum+=1/i
+ end
+ return sum
end
def digits
arr,i,tmp=[],0,self
while tmp>0
arr[i]=tmp%10 and tmp/=10
- i+=1
+ i+=1
end
return arr.reverse
end
end
class Array
def frequencies
tmp,myhsh=self.uniq,Hash.new
- myhsh.default=0
- self.each{|x| if tmp.include?(x) then myhsh[x]+=1 end}
- return myhsh
+ myhsh.default=0
+ self.each{|x| if tmp.include?(x) then myhsh[x]+=1 end}
+ return myhsh
end
def average
arr,sum=self,0
- arr.each{|x| sum+=x}
+ arr.each{|x| sum+=x}
return (sum.to_f/(arr.length))
end
def drop_every(p)
arr,newarr,i=self,[],p
- arr.each_index{|x|
+ arr.each_index{|x|
if x!=i-1 then newarr.push(arr[x]) else i+=p end}
return newarr
end
def combine_with(a)
tmp,i,p=[],0,0
while self.length>=i or a.length>=p
- tmp.push(self[i]) and i+=1
- tmp.push(a[p]) and p+=1
- end
- tmp=tmp.compact
- return tmp
+ tmp.push(self[i]) and i+=1
+ tmp.push(a[p]) and p+=1
+ end
+ tmp=tmp.compact
+ return tmp
end
end

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

class Integer
def prime?
- i,flag=self-1,false
- flag=true if self>0
- while i>=2
- flag=false if self%i==0
- i-=1 end
- return flag
+ flag_prime=self>0 ? true : false
+ (2..self-1).each do |i|
+ flag_prime=false if self%i==0
+ end
+ return flag_prime
end
def prime_factors
- tmp,arr,i=self,[],2
- while tmp>1
- if tmp%i==0 then tmp=tmp/i and arr.push(i)
- else i+=1 end
+ source_array,prime_factors_array,i=self,[],2
+ while source_array>1
+ if source_array%i!=0 then i+=1 else
+ source_array/=i and prime_factors_array.push(i) end
end
- return arr
+ return prime_factors_array
end
def harmonic
sum,i=0,0
- while i<self
+ (0..self-1).each do |i|
i=(i+1).to_r
sum+=1/i
end
return sum
end
def digits
- arr,i,tmp=[],0,self
- while tmp>0
- arr[i]=tmp%10 and tmp/=10
+ number_digits,i,source_number=[],0,self
+ while source_number>0
+ number_digits[i]=source_number%10 and source_number/=10
i+=1
end
- return arr.reverse
+ return number_digits.reverse
end
end
class Array
def frequencies
- tmp,myhsh=self.uniq,Hash.new
- myhsh.default=0
- self.each{|x| if tmp.include?(x) then myhsh[x]+=1 end}
- return myhsh
+ source_array,node_frequency=self.uniq,{}
+ node_frequency.default=0
+ self.each{|x| if source_array.include?(x) then node_frequency[x]+=1 end}
+ return node_frequency
end
def average
- arr,sum=self,0
- arr.each{|x| sum+=x}
- return (sum.to_f/(arr.length))
+ source_array,sum=self,0
+ source_array.each{|x| sum+=x}
+ return (sum.to_f/(source_array.length))
end
def drop_every(p)
- arr,newarr,i=self,[],p
- arr.each_index{|x|
- if x!=i-1 then newarr.push(arr[x]) else i+=p end}
- return newarr
+ source_array,merged_arrays,i=self,[],p
+ source_array.each_index do |x|
+ if x!=i-1 then
+ merged_arrays.push(source_array[x]) else i+=p end
+ end
+ return merged_arrays
end
- def combine_with(a)
- tmp,i,p=[],0,0
- while self.length>=i or a.length>=p
- tmp.push(self[i]) and i+=1
- tmp.push(a[p]) and p+=1
+ def combine_with(source_parameter_array)
+ source_array,i,p=[],0,0
+ while self.length>=i or source_parameter_array.length>=p
+ source_array.push(self[i]) and i+=1
+ source_array.push(source_parameter_array[p]) and p+=1
end
- tmp=tmp.compact
- return tmp
+ return source_array.compact
end
end