Решение на Първа задача от Константин Димитров

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

Към профила на Константин Димитров

Резултати

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

Код

class Integer
def prime?
if self<0 then return false end
is_divisible = false
(2...self).each {|i| is_divisible = (((self % i) == 0) or is_divisible)}
not is_divisible
end
def list_of_primes_up_to_self
list_of_primes = []
(2...self).each {|i| (list_of_primes << i) if i.prime?}
list_of_primes
end
def prime_factors
new_self = self.abs
primes = new_self.list_of_primes_up_to_self
factors = []
primes.each {|i| while new_self%i == 0 do new_self/=i and factors << i end}
factors
end
def harmonic
list_of_rationals = (1..self).map {|i| Rational(1,i)}
accumulator = 0
list_of_rationals.each {|i| accumulator += i}
accumulator
end
def digits
list = []
new_self = self.abs
new_self.to_s.split('').map {|i| list << i.to_i}
list
end
end
class Array
def frequencies
dictionary = {}
self.map {|i| dictionary.has_key?(i) ? dictionary[i]+=1 : dictionary[i]=1}
dictionary
end
def average
sum = 0.0
self.map {|i| sum += i}
(sum/self.size)
end
def drop_every n
new_list = []
self.map {|i| if (self.index(i)+1)%n != 0 then new_list << i end}
new_list
end
def combine_with other
result = []
smaller = [self.size,other.size].min
(0...smaller).map{|i| result << self[i] << other[i]}
result += self.drop(smaller) + other.drop(smaller)
end
end

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

FFF...........

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-ntumf2/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: 61.prime_factors.should  eq [61]
       
       expected: [61]
            got: []
       
       (compared using ==)
     # /tmp/d20131023-4395-ntumf2/spec.rb:22: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: (-61).prime_factors.should  eq [61]
       
       expected: [61]
            got: []
       
       (compared using ==)
     # /tmp/d20131023-4395-ntumf2/spec.rb:28: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.03795 seconds
14 examples, 3 failures

Failed examples:

rspec /tmp/d20131023-4395-ntumf2/spec.rb:2 # Integer#prime? checks if a number is prime
rspec /tmp/d20131023-4395-ntumf2/spec.rb:18 # Integer#prime_factors constructs an array containing the prime factors in ascending order
rspec /tmp/d20131023-4395-ntumf2/spec.rb:26 # Integer#prime_factors works with negative numbers

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

Константин обнови решението на 16.10.2013 02:28 (преди около 11 години)

+class Integer
+ def prime?
+ if self<0 then return false end
+ is_divisible = false
+ (2...self).each { |i| is_divisible = (((self % i) == 0) or is_divisible) }
+ not is_divisible
+ end
+
+ def list_of_primes_up_to_self
+ list_of_primes = []
+ (2...self).each { |i| (list_of_primes << i) if i.prime? }
+ list_of_primes
+ end
+
+ def prime_factors
+ new_self = self.abs
+ primes = new_self.list_of_primes_up_to_self
+ factors = []
+ primes.each {|i| while new_self%i == 0 do new_self/=i and factors << i end}
+ factors.to_s
+ end
+
+ def harmonic
+ list_of_rationals = (1..self).map {|i| Rational(1,i)}
+ accumulator = 0
+ list_of_rationals.each {|i| accumulator += i}
+ accumulator
+ end
+
+ def abs
+ self<0 ? -self : self
+ end
+
+ def digits
+ list = []
+ new_self = self.abs
+ new_self.to_s.split('').map {|i| list << i.to_s }
+ list.map {|i| i.to_i}
+ end
+end
+
+class Array
+ def frequencies
+ dictionary = {}
+ self.map {|i| dictionary.has_key?(i) ? dictionary[i]+=1 : dictionary[i]=1}
+ dictionary
+ end
+
+ def average
+ sum = 0.0
+ self.map {|i| sum += i}
+ (sum/self.size)
+ end
+
+ def drop_every n
+ new_list = []
+ self.map {|i| if (self.index(i)+1)%n != 0 then new_list << i end}
+ new_list.to_s
+ end
+
+ def after_n n
+ self[-(self.size+n)]
+ end
+
+ def combine_with other
+ result = []
+ smaller = [self.size,other.size].min
+ (0...smaller).map{|i| result << self[i] << other[i]}
+ result += self.drop(smaller) + other.drop(smaller)
+ result.to_s
+ end
+end

Константин обнови решението на 16.10.2013 09:59 (преди около 11 години)

class Integer
def prime?
if self<0 then return false end
is_divisible = false
(2...self).each { |i| is_divisible = (((self % i) == 0) or is_divisible) }
not is_divisible
end
def list_of_primes_up_to_self
list_of_primes = []
(2...self).each { |i| (list_of_primes << i) if i.prime? }
list_of_primes
end
def prime_factors
new_self = self.abs
primes = new_self.list_of_primes_up_to_self
factors = []
primes.each {|i| while new_self%i == 0 do new_self/=i and factors << i end}
- factors.to_s
+ factors
end
def harmonic
list_of_rationals = (1..self).map {|i| Rational(1,i)}
accumulator = 0
list_of_rationals.each {|i| accumulator += i}
accumulator
end
def abs
self<0 ? -self : self
end
def digits
list = []
new_self = self.abs
new_self.to_s.split('').map {|i| list << i.to_s }
- list.map {|i| i.to_i}
+ list
end
end
class Array
def frequencies
dictionary = {}
self.map {|i| dictionary.has_key?(i) ? dictionary[i]+=1 : dictionary[i]=1}
dictionary
end
def average
sum = 0.0
self.map {|i| sum += i}
(sum/self.size)
end
def drop_every n
new_list = []
self.map {|i| if (self.index(i)+1)%n != 0 then new_list << i end}
- new_list.to_s
+ new_list
end
def after_n n
self[-(self.size+n)]
end
def combine_with other
result = []
smaller = [self.size,other.size].min
(0...smaller).map{|i| result << self[i] << other[i]}
result += self.drop(smaller) + other.drop(smaller)
- result.to_s
+ result
end
end

Константин обнови решението на 16.10.2013 10:05 (преди около 11 години)

class Integer
def prime?
if self<0 then return false end
is_divisible = false
(2...self).each { |i| is_divisible = (((self % i) == 0) or is_divisible) }
not is_divisible
end
def list_of_primes_up_to_self
list_of_primes = []
(2...self).each { |i| (list_of_primes << i) if i.prime? }
list_of_primes
end
def prime_factors
new_self = self.abs
primes = new_self.list_of_primes_up_to_self
factors = []
primes.each {|i| while new_self%i == 0 do new_self/=i and factors << i end}
factors
end
def harmonic
list_of_rationals = (1..self).map {|i| Rational(1,i)}
accumulator = 0
list_of_rationals.each {|i| accumulator += i}
accumulator
end
def abs
self<0 ? -self : self
end
def digits
list = []
new_self = self.abs
- new_self.to_s.split('').map {|i| list << i.to_s }
+ new_self.to_s.split('').map {|i| list << i.to_i }
list
end
end
class Array
def frequencies
dictionary = {}
self.map {|i| dictionary.has_key?(i) ? dictionary[i]+=1 : dictionary[i]=1}
dictionary
end
def average
sum = 0.0
self.map {|i| sum += i}
(sum/self.size)
end
def drop_every n
new_list = []
self.map {|i| if (self.index(i)+1)%n != 0 then new_list << i end}
new_list
end
def after_n n
self[-(self.size+n)]
end
def combine_with other
result = []
smaller = [self.size,other.size].min
(0...smaller).map{|i| result << self[i] << other[i]}
result += self.drop(smaller) + other.drop(smaller)
result
end
end

Константин обнови решението на 16.10.2013 13:46 (преди около 11 години)

class Integer
def prime?
if self<0 then return false end
is_divisible = false
- (2...self).each { |i| is_divisible = (((self % i) == 0) or is_divisible) }
+ (2...self).each {|i| is_divisible = (((self % i) == 0) or is_divisible)}
not is_divisible
end
def list_of_primes_up_to_self
list_of_primes = []
- (2...self).each { |i| (list_of_primes << i) if i.prime? }
+ (2...self).each {|i| (list_of_primes << i) if i.prime?}
list_of_primes
end
def prime_factors
new_self = self.abs
primes = new_self.list_of_primes_up_to_self
factors = []
primes.each {|i| while new_self%i == 0 do new_self/=i and factors << i end}
factors
end
def harmonic
list_of_rationals = (1..self).map {|i| Rational(1,i)}
accumulator = 0
list_of_rationals.each {|i| accumulator += i}
accumulator
end
- def abs
- self<0 ? -self : self
- end
-
def digits
list = []
new_self = self.abs
- new_self.to_s.split('').map {|i| list << i.to_i }
+ new_self.to_s.split('').map {|i| list << i.to_i}
list
end
end
class Array
def frequencies
dictionary = {}
self.map {|i| dictionary.has_key?(i) ? dictionary[i]+=1 : dictionary[i]=1}
dictionary
end
def average
sum = 0.0
self.map {|i| sum += i}
(sum/self.size)
end
def drop_every n
new_list = []
self.map {|i| if (self.index(i)+1)%n != 0 then new_list << i end}
new_list
end
- def after_n n
- self[-(self.size+n)]
- end
-
def combine_with other
result = []
smaller = [self.size,other.size].min
(0...smaller).map{|i| result << self[i] << other[i]}
result += self.drop(smaller) + other.drop(smaller)
- result
end
end