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

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

Към профила на Никола Ненков

Резултати

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

Код

class Integer
def prime?
if self > 1
not (2..pred).reduce(false) { |was_div, i| was_div or divisable_by?(i) }
else
false
end
end
def prime_factors
pfs_repeating = []
pfs = (2..abs.pred).select { |i| divisable_by?(i) and i.prime? }
pfs.each { |pf| pfs_repeating += Array.new(times_to_repeat(pf), pf) }
pfs_repeating
end
def harmonic
(1..self).reduce { |so_far, i| so_far + i.to_r**-1 } #TODO test with 2.1
end
def digits
abs.to_s.split('').map(&:to_i)
end
def divisable_by?(what)
(self % what).zero?
end
private
def times_to_repeat(prime_factor)
number = self
count = 0
while number.divisable_by?(prime_factor)
number /= prime_factor
count += 1
end
count
end
end
class Array
def frequencies
result = {}
each { |element| result[element] = result[element].to_i.next }
result
end
def average
reduce(:+) / size.to_f
end
def drop_every(n)
reject { |i| index(i).next.divisable_by?(n) }
end
def combine_with(other)
combined = []
the_greater_size = (size > other.size) ? size : other.size
(0..the_greater_size.pred).each { |i| combined += what_to_append(other, i) }
combined
end
private
def what_to_append(other, index)
if size > index and other.size > index
[self[index], other[index]]
else
size <= index ? [other[index]] : [self[index]]
end
end
end

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

.FF...........

Failures:

  1) 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-157mkei/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)>'

  2) Integer#prime_factors works with negative numbers
     Failure/Error: (-61).prime_factors.should  eq [61]
       
       expected: [61]
            got: []
       
       (compared using ==)
     # /tmp/d20131023-4395-157mkei/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.01952 seconds
14 examples, 2 failures

Failed examples:

rspec /tmp/d20131023-4395-157mkei/spec.rb:18 # Integer#prime_factors constructs an array containing the prime factors in ascending order
rspec /tmp/d20131023-4395-157mkei/spec.rb:26 # Integer#prime_factors works with negative numbers

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

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

+class Integer
+
+ def prime?
+ if self > 1
+ not (2..self - 1).inject(false) { |so_far, i| so_far or (self % i).zero? }
+ else
+ false
+ end
+ end
+
+ def prime_factors
+ result = (2..self - 1).select { |i| (self % i).zero? and i.prime? }
+ #TODO times and negative
+ end
+
+ def harmonic
+ (1..self).inject { |so_far, i| so_far + i.to_r**-1 } #TODO test with 2.1
+ end
+
+ def digits
+ if self > 0
+ self.to_s.split('').map { |d| d.to_i }
+ else
+ (-self).digits
+ end
+ end
+
+end
+
+class Array
+
+ def frequencies
+ result = {}
+ self.each { |el| result[el] = result[el].to_i.next }
+ result
+ end
+
+ def average
+ self.reduce(:+) / self.size.to_f
+ end
+
+ #TODO drop_every(n) and combine_with(other)
+
+end

Никола обнови решението на 14.10.2013 18:59 (преди около 11 години)

class Integer
def prime?
if self > 1
not (2..self - 1).inject(false) { |so_far, i| so_far or (self % i).zero? }
else
false
end
end
def prime_factors
- result = (2..self - 1).select { |i| (self % i).zero? and i.prime? }
- #TODO times and negative
+ if self > 0
+ result = (2..self - 1).select { |i| (self % i).zero? and i.prime? }
+ #result.each { |pf| result[result.index(0)..result.index(0)] = #TODO
+ else
+ (-self).prime_factors
+ end
end
def harmonic
(1..self).inject { |so_far, i| so_far + i.to_r**-1 } #TODO test with 2.1
end
def digits
if self > 0
self.to_s.split('').map { |d| d.to_i }
else
(-self).digits
end
+ end
+
+ #private
+ def times_to_repeat(prime_factor)
+ num = self
+ count = 0
+ while (num % prime_factor).zero?
+ num /= prime_factor
+ count += 1
+ end
+ count
end
end
class Array
def frequencies
result = {}
self.each { |el| result[el] = result[el].to_i.next }
result
end
def average
self.reduce(:+) / self.size.to_f
end
#TODO drop_every(n) and combine_with(other)
end

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

class Integer
def prime?
if self > 1
not (2..self - 1).inject(false) { |so_far, i| so_far or (self % i).zero? }
else
false
end
end
def prime_factors
+ pfs_repeating = []
if self > 0
- result = (2..self - 1).select { |i| (self % i).zero? and i.prime? }
- #result.each { |pf| result[result.index(0)..result.index(0)] = #TODO
+ pfs = (2..self - 1).select { |i| (self % i).zero? and i.prime? }
+ pfs.each { |pf| pfs_repeating += Array.new(times_to_repeat(pf), pf) }
+ pfs_repeating
else
(-self).prime_factors
end
end
def harmonic
(1..self).inject { |so_far, i| so_far + i.to_r**-1 } #TODO test with 2.1
end
def digits
if self > 0
self.to_s.split('').map { |d| d.to_i }
else
(-self).digits
end
end
- #private
+ private
def times_to_repeat(prime_factor)
num = self
count = 0
while (num % prime_factor).zero?
num /= prime_factor
count += 1
end
count
end
end
class Array
def frequencies
result = {}
self.each { |el| result[el] = result[el].to_i.next }
result
end
def average
self.reduce(:+) / self.size.to_f
end
#TODO drop_every(n) and combine_with(other)
end

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

class Integer
def prime?
if self > 1
- not (2..self - 1).inject(false) { |so_far, i| so_far or (self % i).zero? }
+ not (2..pred).reduce(false) { |was_div, i| was_div or divisable_by?(i) }
else
false
end
end
def prime_factors
pfs_repeating = []
if self > 0
- pfs = (2..self - 1).select { |i| (self % i).zero? and i.prime? }
+ pfs = (2..pred).select { |i| divisable_by?(i) and i.prime? }
pfs.each { |pf| pfs_repeating += Array.new(times_to_repeat(pf), pf) }
pfs_repeating
else
(-self).prime_factors
end
end
def harmonic
- (1..self).inject { |so_far, i| so_far + i.to_r**-1 } #TODO test with 2.1
+ (1..self).reduce { |so_far, i| so_far + i.to_r**-1 } #TODO test with 2.1
end
def digits
- if self > 0
- self.to_s.split('').map { |d| d.to_i }
- else
- (-self).digits
- end
+ abs.to_s.split('').map(&:to_i)
end
+ def divisable_by?(what)
+ (self % what).zero?
+ end
+
private
def times_to_repeat(prime_factor)
- num = self
+ number = self
count = 0
- while (num % prime_factor).zero?
- num /= prime_factor
+ while number.divisable_by?(prime_factor)
+ number /= prime_factor
count += 1
end
count
end
end
class Array
def frequencies
result = {}
- self.each { |el| result[el] = result[el].to_i.next }
+ each { |element| result[element] = result[element].to_i.next }
result
end
def average
- self.reduce(:+) / self.size.to_f
+ reduce(:+) / size.to_f
end
- #TODO drop_every(n) and combine_with(other)
+ def drop_every(n)
+ reject { |i| index(i).next.divisable_by?(n) }
+ end
+
+ def combine_with(other)
+ combined = []
+ the_greater_size = (size > other.size) ? size : other.size
+ (0..the_greater_size.pred).each { |i| combined += what_to_append(other, i) }
+ combined
+ end
+
+ private
+ def what_to_append(other, index)
+ if size > index and other.size > index
+ [self[index], other[index]]
+ else
+ size <= index ? [other[index]] : [self[index]]
+ end
+ end
end

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

class Integer
def prime?
if self > 1
not (2..pred).reduce(false) { |was_div, i| was_div or divisable_by?(i) }
else
false
end
end
def prime_factors
pfs_repeating = []
- if self > 0
- pfs = (2..pred).select { |i| divisable_by?(i) and i.prime? }
- pfs.each { |pf| pfs_repeating += Array.new(times_to_repeat(pf), pf) }
- pfs_repeating
- else
- (-self).prime_factors
- end
+ pfs = (2..abs.pred).select { |i| divisable_by?(i) and i.prime? }
+ pfs.each { |pf| pfs_repeating += Array.new(times_to_repeat(pf), pf) }
+ pfs_repeating
end
def harmonic
(1..self).reduce { |so_far, i| so_far + i.to_r**-1 } #TODO test with 2.1
end
def digits
abs.to_s.split('').map(&:to_i)
end
def divisable_by?(what)
(self % what).zero?
end
private
def times_to_repeat(prime_factor)
number = self
count = 0
while number.divisable_by?(prime_factor)
number /= prime_factor
count += 1
end
count
end
end
class Array
def frequencies
result = {}
each { |element| result[element] = result[element].to_i.next }
result
end
def average
reduce(:+) / size.to_f
end
def drop_every(n)
reject { |i| index(i).next.divisable_by?(n) }
end
def combine_with(other)
combined = []
the_greater_size = (size > other.size) ? size : other.size
(0..the_greater_size.pred).each { |i| combined += what_to_append(other, i) }
combined
end
private
def what_to_append(other, index)
if size > index and other.size > index
[self[index], other[index]]
else
size <= index ? [other[index]] : [self[index]]
end
end
end