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

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

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

Резултати

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

Код

class Integer
def prime?
(2..Math.sqrt(abs)).each { |n| return false if abs % n == 0 }
true
end
def prime_factors
upper_limit = abs
factors = []
until upper_limit == 1 do
factors << (2..upper_limit).find { |digit| upper_limit % digit == 0 }
upper_limit /= factors.last
end
factors
end
def harmonic
(1..self).map { |n| Rational(1, n) }.reduce(:+)
end
def digits
to_s.split('').map { |digit| digit.to_i }
end
end
class Array
def frequencies
group_by { |_| _ }.each_with_object({}) { |(k, v), hash| hash[k] = v.count }
end
def average
reduce(:+).to_f / count
end
def drop_every(n)
each_with_index.map { |item, index| item if (index+1) % n != 0 }.compact
end
def combine_with(other)
other_clone = other.clone
flat_map { |item| [item, other_clone.shift].compact }.concat(other_clone)
end
end

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

F....F......F.

Failures:

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

  3) 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-1q7uy1q/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.02012 seconds
14 examples, 3 failures

Failed examples:

rspec /tmp/d20131023-4395-1q7uy1q/spec.rb:2 # Integer#prime? checks if a number is prime
rspec /tmp/d20131023-4395-1q7uy1q/spec.rb:50 # Integer#digits works with negative numbers
rspec /tmp/d20131023-4395-1q7uy1q/spec.rb:103 # Array#combine_with combines two arrays by alternatingly taking elements

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

Димитър обнови решението на 15.10.2013 18:51 (преди около 11 години)

+class Integer
+ def prime?
+ upper_limit = Math.sqrt self
+
+ (2..upper_limit.abs).each { |n| return false if self % n == 0 }
+ true
+ end
+
+ def prime_factors
+ number = abs
+ factors = []
+
+ until number == 1 do
+ factors << (2..number).find { |n| number % n == 0 }
+ number = number / factors.last
+ end
+
+ factors
+ end
+
+ def harmonic
+ reciprocal_numbers = []
+
+ (1..self).each { |n| reciprocal_numbers << Rational(1, n) }
+ reciprocal_numbers.reduce(:+)
+ end
+
+ def digits
+ spliced_digits = []
+ number = abs
+
+ while number % 10 > 0 do
+ spliced_digits << number % 10
+ number = number / 10
+ end
+
+ spliced_digits.reverse
+ end
+end
+
+class Array
+ def frequencies
+ weighted_items = {}
+
+ each do |item|
+ if weighted_items.has_key? item
+ weighted_items[item] = weighted_items[item].next
+ else
+ weighted_items[item] = 1
+ end
+ end
+
+ weighted_items
+ end
+
+ def average
+ reduce(:+).to_f / count
+ end
+
+ def drop_every(n)
+ each_with_index.map { |x, index| x if (index+1) % n != 0 }.compact
+ end
+
+ def combine_with(other)
+ combined_arrays = []
+ zip(other).each { |a, b| combined_arrays << a << b}.flatten.compact
+ end
+end

Димитър обнови решението на 15.10.2013 21:06 (преди около 11 години)

class Integer
def prime?
upper_limit = Math.sqrt self
(2..upper_limit.abs).each { |n| return false if self % n == 0 }
true
end
def prime_factors
number = abs
factors = []
until number == 1 do
factors << (2..number).find { |n| number % n == 0 }
number = number / factors.last
end
factors
end
def harmonic
reciprocal_numbers = []
(1..self).each { |n| reciprocal_numbers << Rational(1, n) }
reciprocal_numbers.reduce(:+)
end
def digits
spliced_digits = []
number = abs
while number % 10 > 0 do
spliced_digits << number % 10
- number = number / 10
+ number /= 10
end
spliced_digits.reverse
end
end
class Array
def frequencies
weighted_items = {}
each do |item|
if weighted_items.has_key? item
weighted_items[item] = weighted_items[item].next
else
weighted_items[item] = 1
end
end
weighted_items
end
def average
reduce(:+).to_f / count
end
def drop_every(n)
each_with_index.map { |x, index| x if (index+1) % n != 0 }.compact
end
def combine_with(other)
- combined_arrays = []
- zip(other).each { |a, b| combined_arrays << a << b}.flatten.compact
+ other_clone = other.clone
+ flat_map { |elem| [elem, other_clone.shift()].compact }.concat(other_clone)
end
end

Димитър обнови решението на 15.10.2013 21:09 (преди около 11 години)

class Integer
def prime?
upper_limit = Math.sqrt self
(2..upper_limit.abs).each { |n| return false if self % n == 0 }
true
end
def prime_factors
number = abs
factors = []
until number == 1 do
factors << (2..number).find { |n| number % n == 0 }
number = number / factors.last
end
factors
end
def harmonic
reciprocal_numbers = []
(1..self).each { |n| reciprocal_numbers << Rational(1, n) }
reciprocal_numbers.reduce(:+)
end
def digits
spliced_digits = []
number = abs
while number % 10 > 0 do
spliced_digits << number % 10
number /= 10
end
spliced_digits.reverse
end
end
class Array
def frequencies
weighted_items = {}
each do |item|
if weighted_items.has_key? item
weighted_items[item] = weighted_items[item].next
else
weighted_items[item] = 1
end
end
weighted_items
end
def average
reduce(:+).to_f / count
end
def drop_every(n)
each_with_index.map { |x, index| x if (index+1) % n != 0 }.compact
end
def combine_with(other)
other_clone = other.clone
- flat_map { |elem| [elem, other_clone.shift()].compact }.concat(other_clone)
+ flat_map { |elem| [elem, other_clone.shift].compact }.concat(other_clone)
end
end

Димитър обнови решението на 15.10.2013 23:59 (преди около 11 години)

class Integer
def prime?
- upper_limit = Math.sqrt self
-
- (2..upper_limit.abs).each { |n| return false if self % n == 0 }
+ (2..Math.sqrt(abs)).each { |n| return false if abs % n == 0 }
true
end
def prime_factors
number = abs
factors = []
until number == 1 do
factors << (2..number).find { |n| number % n == 0 }
- number = number / factors.last
+ number /= factors.last
end
factors
end
def harmonic
reciprocal_numbers = []
(1..self).each { |n| reciprocal_numbers << Rational(1, n) }
reciprocal_numbers.reduce(:+)
end
def digits
spliced_digits = []
number = abs
while number % 10 > 0 do
spliced_digits << number % 10
number /= 10
end
spliced_digits.reverse
end
end
class Array
def frequencies
weighted_items = {}
each do |item|
if weighted_items.has_key? item
weighted_items[item] = weighted_items[item].next
else
weighted_items[item] = 1
end
end
weighted_items
end
def average
reduce(:+).to_f / count
end
def drop_every(n)
each_with_index.map { |x, index| x if (index+1) % n != 0 }.compact
end
def combine_with(other)
other_clone = other.clone
flat_map { |elem| [elem, other_clone.shift].compact }.concat(other_clone)
end
-end
+end

Димитър обнови решението на 16.10.2013 11:24 (преди около 11 години)

class Integer
def prime?
- (2..Math.sqrt(abs)).each { |n| return false if abs % n == 0 }
- true
+ (2..Math.sqrt(abs)).each { |n| return false if abs % n == 0 }
+ true
end
def prime_factors
number = abs
factors = []
until number == 1 do
factors << (2..number).find { |n| number % n == 0 }
number /= factors.last
end
factors
end
def harmonic
reciprocal_numbers = []
(1..self).each { |n| reciprocal_numbers << Rational(1, n) }
reciprocal_numbers.reduce(:+)
end
def digits
spliced_digits = []
number = abs
while number % 10 > 0 do
spliced_digits << number % 10
number /= 10
end
spliced_digits.reverse
end
end
class Array
def frequencies
- weighted_items = {}
-
- each do |item|
- if weighted_items.has_key? item
- weighted_items[item] = weighted_items[item].next
- else
- weighted_items[item] = 1
- end
- end
-
- weighted_items
+ group_by { |_| _ }.each_with_object({}) { |(k, v), hash| hash[k] = v.count }
end
def average
reduce(:+).to_f / count
end
def drop_every(n)
- each_with_index.map { |x, index| x if (index+1) % n != 0 }.compact
+ each_with_index.map { |item, index| item if (index+1) % n != 0 }.compact
end
def combine_with(other)
other_clone = other.clone
- flat_map { |elem| [elem, other_clone.shift].compact }.concat(other_clone)
+ flat_map { |item| [item, other_clone.shift].compact }.concat(other_clone)
end
end

Димитър обнови решението на 16.10.2013 13:40 (преди около 11 години)

class Integer
def prime?
(2..Math.sqrt(abs)).each { |n| return false if abs % n == 0 }
true
end
def prime_factors
number = abs
factors = []
until number == 1 do
factors << (2..number).find { |n| number % n == 0 }
number /= factors.last
end
factors
end
def harmonic
reciprocal_numbers = []
(1..self).each { |n| reciprocal_numbers << Rational(1, n) }
reciprocal_numbers.reduce(:+)
end
def digits
- spliced_digits = []
- number = abs
-
- while number % 10 > 0 do
- spliced_digits << number % 10
- number /= 10
- end
-
- spliced_digits.reverse
+ to_s.split('').map { |digit| digit.to_i }
end
end
class Array
def frequencies
group_by { |_| _ }.each_with_object({}) { |(k, v), hash| hash[k] = v.count }
end
def average
reduce(:+).to_f / count
end
def drop_every(n)
each_with_index.map { |item, index| item if (index+1) % n != 0 }.compact
end
def combine_with(other)
other_clone = other.clone
flat_map { |item| [item, other_clone.shift].compact }.concat(other_clone)
end
end

Димитър обнови решението на 16.10.2013 14:08 (преди около 11 години)

class Integer
def prime?
(2..Math.sqrt(abs)).each { |n| return false if abs % n == 0 }
true
end
def prime_factors
number = abs
factors = []
until number == 1 do
factors << (2..number).find { |n| number % n == 0 }
number /= factors.last
end
factors
end
def harmonic
- reciprocal_numbers = []
-
- (1..self).each { |n| reciprocal_numbers << Rational(1, n) }
- reciprocal_numbers.reduce(:+)
+ (1..self).map { |n| Rational(1, n) }.reduce(:+)
end
def digits
to_s.split('').map { |digit| digit.to_i }
end
end
class Array
def frequencies
group_by { |_| _ }.each_with_object({}) { |(k, v), hash| hash[k] = v.count }
end
def average
reduce(:+).to_f / count
end
def drop_every(n)
each_with_index.map { |item, index| item if (index+1) % n != 0 }.compact
end
def combine_with(other)
other_clone = other.clone
flat_map { |item| [item, other_clone.shift].compact }.concat(other_clone)
end
end

Димитър обнови решението на 16.10.2013 15:11 (преди около 11 години)

class Integer
def prime?
(2..Math.sqrt(abs)).each { |n| return false if abs % n == 0 }
true
end
def prime_factors
- number = abs
+ upper_limit = abs
factors = []
- until number == 1 do
- factors << (2..number).find { |n| number % n == 0 }
- number /= factors.last
+ until upper_limit == 1 do
+ factors << (2..upper_limit).find { |digit| upper_limit % digit == 0 }
+ upper_limit /= factors.last
end
factors
end
def harmonic
(1..self).map { |n| Rational(1, n) }.reduce(:+)
end
def digits
to_s.split('').map { |digit| digit.to_i }
end
end
class Array
def frequencies
group_by { |_| _ }.each_with_object({}) { |(k, v), hash| hash[k] = v.count }
end
def average
reduce(:+).to_f / count
end
def drop_every(n)
each_with_index.map { |item, index| item if (index+1) % n != 0 }.compact
end
def combine_with(other)
other_clone = other.clone
flat_map { |item| [item, other_clone.shift].compact }.concat(other_clone)
end
end