Решение на Първа задача от Георги Шопов

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

Към профила на Георги Шопов

Резултати

  • 6 точки от тестове
  • 0 бонус точки
  • 6 точки общо
  • 13 успешни тест(а)
  • 1 неуспешни тест(а)

Код

class Integer
def divisors
(1..abs).each_with_object([]) { |i, list| list << i if self % i == 0 }
end
def prime_divisors
divisors[1..-1].delete_if { |divisor| not divisor.prime? }
end
def prime?
[1, self] == divisors ? true : false
end
def prime_factors
prime_divisors.each_with_object([]) do |divisor, list|
return [divisor] + (self / divisor).prime_factors if self % divisor == 0
end
end
def harmonic
# Бива ли за 3 символа да не chain-на? Затова ги няма 3те спейса.
(1..self).each_with_object([]){|i, list| list << Rational(1, i)}.reduce(:+)
end
def digits
list_of_digits = []
abs.to_s.each_char { |char| list_of_digits << char.ord - '0'.ord }
list_of_digits
end
end
class Array
def frequencies
each_with_object({}) do |item, hash|
hash.include?(item) ? hash[item] += 1 : hash[item] = 1
end
end
def average
reduce(:+) / length.to_f
end
def drop_every(n)
each_with_index.each_with_object([]) do |item_index_tuple, list|
list << item_index_tuple[0] unless (item_index_tuple[1] + 1) % n == 0
end
end
def combine_with(other)
combined = []
each_index do |index|
combined << self[index]
combined << other[index] if other[index]
end
other.length > length ? combined += other[length..-1] : combined
end
end

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

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

Failures:

  1) 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-1u5193k/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.02204 seconds
14 examples, 1 failure

Failed examples:

rspec /tmp/d20131023-4395-1u5193k/spec.rb:103 # Array#combine_with combines two arrays by alternatingly taking elements

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

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

+class Integer
+ def divisors
+ result = []
+ (1..self.abs).each { |item| result << item if self % item == 0 }
+ result
+ end
+
+ def prime_divisors
+ result = divisors[1..-1]
+ result.delete_if { |item| not item.prime? }
+ end
+
+ def prime?
+ if self == 1
+ false
+ else
+ ([1, self] == divisors)? true : false
+ end
+ end
+
+ def prime_factors
+ return [] if self == 0
+
+ prime_divisors.each do |item|
+ if self % item == 0
+ return [item] + (self / item).prime_factors
+ end
+ end
+ end
+
+ def reciprocal
+ Rational(1, self)
+ end
+
+ def harmonic
+ result = Rational(0)
+ (1..self).each { |i| result += i.reciprocal }
+ result
+ end
+
+ def digits
+ result = []
+ self.abs.to_s.each_char { |char| result << char.ord - '0'.ord }
+ result
+ end
+end
+
+
+class Array
+ def frequencies
+ result = {}
+ self.each { |item| (result.include? item)? result[item] += 1 :
+ result[item] = 1 }
+ result
+ end
+
+ def average
+ self.reduce(:+) / self.length.to_f
+ end
+
+ def drop_every(n)
+ result = []
+ self.each_index do |index|
+ result << self[index] unless (index + 1) % n == 0
+ end
+
+ result
+ end
+
+ def combine_with(other)
+ result = []
+ self.each_index do |index|
+ result << self[index]
+ result << other[index] if other.fetch(index, nil)
+ end
+
+ (other.length > length)? result += other[length..-1] : result
+ end
+end

Георги обнови решението на 14.10.2013 16:04 (преди около 11 години)

class Integer
def divisors
result = []
- (1..self.abs).each { |item| result << item if self % item == 0 }
+ (1..abs).each { |item| result << item if self % item == 0 }
result
end
def prime_divisors
result = divisors[1..-1]
result.delete_if { |item| not item.prime? }
end
def prime?
if self == 1
false
else
- ([1, self] == divisors)? true : false
+ ([1, self] == divisors) ? true : false
end
end
def prime_factors
return [] if self == 0
prime_divisors.each do |item|
- if self % item == 0
- return [item] + (self / item).prime_factors
- end
+ return [item] + (self / item).prime_factors if self % item == 0
end
end
def reciprocal
Rational(1, self)
end
def harmonic
result = Rational(0)
(1..self).each { |i| result += i.reciprocal }
result
end
def digits
result = []
- self.abs.to_s.each_char { |char| result << char.ord - '0'.ord }
+ abs.to_s.each_char { |char| result << char.ord - '0'.ord }
result
end
end
-
class Array
def frequencies
result = {}
- self.each { |item| (result.include? item)? result[item] += 1 :
- result[item] = 1 }
+ each do |item|
+ (result.include? item) ? result[item] += 1 : result[item] = 1
+ end
result
end
def average
- self.reduce(:+) / self.length.to_f
+ reduce(:+) / length.to_f
end
def drop_every(n)
result = []
- self.each_index do |index|
+ each_index do |index|
result << self[index] unless (index + 1) % n == 0
end
result
end
def combine_with(other)
result = []
- self.each_index do |index|
+ each_index do |index|
result << self[index]
result << other[index] if other.fetch(index, nil)
end
- (other.length > length)? result += other[length..-1] : result
+ (other.length > length) ? result += other[length..-1] : result
end
end

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

class Integer
def divisors
result = []
(1..abs).each { |item| result << item if self % item == 0 }
result
end
def prime_divisors
result = divisors[1..-1]
result.delete_if { |item| not item.prime? }
end
def prime?
if self == 1
false
else
- ([1, self] == divisors) ? true : false
+ [1, self] == divisors ? true : false
end
end
def prime_factors
return [] if self == 0
prime_divisors.each do |item|
return [item] + (self / item).prime_factors if self % item == 0
end
end
def reciprocal
Rational(1, self)
end
def harmonic
result = Rational(0)
(1..self).each { |i| result += i.reciprocal }
result
end
def digits
result = []
abs.to_s.each_char { |char| result << char.ord - '0'.ord }
result
end
end
class Array
def frequencies
result = {}
each do |item|
- (result.include? item) ? result[item] += 1 : result[item] = 1
+ result.include?(item) ? result[item] += 1 : result[item] = 1
end
result
end
def average
reduce(:+) / length.to_f
end
def drop_every(n)
result = []
each_index do |index|
result << self[index] unless (index + 1) % n == 0
end
result
end
def combine_with(other)
result = []
each_index do |index|
result << self[index]
result << other[index] if other.fetch(index, nil)
end
- (other.length > length) ? result += other[length..-1] : result
+ other.length > length ? result += other[length..-1] : result
end
end

Георги обнови решението на 14.10.2013 22:57 (преди около 11 години)

class Integer
def divisors
- result = []
- (1..abs).each { |item| result << item if self % item == 0 }
- result
+ all_divisors = []
+ (1..abs).each { |item| all_divisors << item if self % item == 0 }
+ all_divisors
end
def prime_divisors
- result = divisors[1..-1]
- result.delete_if { |item| not item.prime? }
+ all_divisors = divisors[1..-1]
+ all_divisors.delete_if { |item| not item.prime? }
end
def prime?
if self == 1
false
else
[1, self] == divisors ? true : false
end
end
def prime_factors
return [] if self == 0
prime_divisors.each do |item|
return [item] + (self / item).prime_factors if self % item == 0
end
end
def reciprocal
Rational(1, self)
end
def harmonic
- result = Rational(0)
- (1..self).each { |i| result += i.reciprocal }
- result
+ sum_of_reciprocals = Rational(0)
+ (1..self).each { |i| sum_of_reciprocals += i.reciprocal }
+ sum_of_reciprocals
end
def digits
- result = []
- abs.to_s.each_char { |char| result << char.ord - '0'.ord }
- result
+ list_of_digits = []
+ abs.to_s.each_char { |char| list_of_digits << char.ord - '0'.ord }
+ list_of_digits
end
end
class Array
def frequencies
- result = {}
+ item_frequencies = {}
each do |item|
- result.include?(item) ? result[item] += 1 : result[item] = 1
+ if item_frequencies.include? item
+ item_frequencies[item] += 1
+ else
+ item_frequencies[item] = 1
+ end
end
- result
+ item_frequencies
end
def average
reduce(:+) / length.to_f
end
def drop_every(n)
- result = []
+ list_with_every_nth_dropped = []
each_index do |index|
- result << self[index] unless (index + 1) % n == 0
+ list_with_every_nth_dropped << self[index] unless (index + 1) % n == 0
end
- result
+ list_with_every_nth_dropped
end
def combine_with(other)
- result = []
+ combined = []
each_index do |index|
- result << self[index]
- result << other[index] if other.fetch(index, nil)
+ combined << self[index]
+ combined << other[index] if other.fetch(index, nil)
end
- other.length > length ? result += other[length..-1] : result
+ other.length > length ? combined += other[length..-1] : combined
end
end

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

class Integer
def divisors
all_divisors = []
(1..abs).each { |item| all_divisors << item if self % item == 0 }
all_divisors
end
def prime_divisors
all_divisors = divisors[1..-1]
all_divisors.delete_if { |item| not item.prime? }
end
def prime?
- if self == 1
- false
- else
- [1, self] == divisors ? true : false
- end
+ [1, self] == divisors ? true : false
end
def prime_factors
return [] if self == 0
prime_divisors.each do |item|
return [item] + (self / item).prime_factors if self % item == 0
end
end
def reciprocal
Rational(1, self)
end
def harmonic
sum_of_reciprocals = Rational(0)
(1..self).each { |i| sum_of_reciprocals += i.reciprocal }
sum_of_reciprocals
end
def digits
list_of_digits = []
abs.to_s.each_char { |char| list_of_digits << char.ord - '0'.ord }
list_of_digits
end
end
class Array
def frequencies
item_frequencies = {}
each do |item|
if item_frequencies.include? item
item_frequencies[item] += 1
else
item_frequencies[item] = 1
end
end
item_frequencies
end
def average
reduce(:+) / length.to_f
end
def drop_every(n)
list_with_every_nth_dropped = []
each_index do |index|
list_with_every_nth_dropped << self[index] unless (index + 1) % n == 0
end
list_with_every_nth_dropped
end
def combine_with(other)
combined = []
each_index do |index|
combined << self[index]
combined << other[index] if other.fetch(index, nil)
end
other.length > length ? combined += other[length..-1] : combined
end
end

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

class Integer
def divisors
all_divisors = []
(1..abs).each { |item| all_divisors << item if self % item == 0 }
all_divisors
end
def prime_divisors
all_divisors = divisors[1..-1]
all_divisors.delete_if { |item| not item.prime? }
end
def prime?
[1, self] == divisors ? true : false
end
def prime_factors
return [] if self == 0
-
prime_divisors.each do |item|
return [item] + (self / item).prime_factors if self % item == 0
end
end
def reciprocal
Rational(1, self)
end
def harmonic
sum_of_reciprocals = Rational(0)
(1..self).each { |i| sum_of_reciprocals += i.reciprocal }
sum_of_reciprocals
end
def digits
list_of_digits = []
abs.to_s.each_char { |char| list_of_digits << char.ord - '0'.ord }
list_of_digits
end
end
class Array
def frequencies
item_frequencies = {}
each do |item|
if item_frequencies.include? item
item_frequencies[item] += 1
else
item_frequencies[item] = 1
end
end
item_frequencies
end
def average
reduce(:+) / length.to_f
end
def drop_every(n)
list_with_every_nth_dropped = []
each_index do |index|
list_with_every_nth_dropped << self[index] unless (index + 1) % n == 0
end
list_with_every_nth_dropped
end
def combine_with(other)
combined = []
each_index do |index|
combined << self[index]
- combined << other[index] if other.fetch(index, nil)
+ combined << other[index] if other[index]
end
other.length > length ? combined += other[length..-1] : combined
end
end

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

class Integer
def divisors
- all_divisors = []
- (1..abs).each { |item| all_divisors << item if self % item == 0 }
- all_divisors
+ (1..abs).each_with_object([]) { |i, list| list << i if self % i == 0 }
end
def prime_divisors
- all_divisors = divisors[1..-1]
- all_divisors.delete_if { |item| not item.prime? }
+ divisors[1..-1].delete_if { |divisor| not divisor.prime? }
end
def prime?
[1, self] == divisors ? true : false
end
def prime_factors
- return [] if self == 0
- prime_divisors.each do |item|
- return [item] + (self / item).prime_factors if self % item == 0
+ prime_divisors.each_with_object([]) do |divisor, list|
+ return [divisor] + (self / divisor).prime_factors if self % divisor == 0
end
end
- def reciprocal
- Rational(1, self)
- end
-
def harmonic
- sum_of_reciprocals = Rational(0)
- (1..self).each { |i| sum_of_reciprocals += i.reciprocal }
- sum_of_reciprocals
+ # Бива ли за 3 символа да не chain-на? Затова ги няма 3те спейса.
+ (1..self).each_with_object([]){|i, list| list << Rational(1, i)}.reduce(:+)
end
def digits
list_of_digits = []
abs.to_s.each_char { |char| list_of_digits << char.ord - '0'.ord }
list_of_digits
end
end
class Array
def frequencies
- item_frequencies = {}
- each do |item|
- if item_frequencies.include? item
- item_frequencies[item] += 1
- else
- item_frequencies[item] = 1
- end
+ each_with_object({}) do |item, hash|
+ hash.include?(item) ? hash[item] += 1 : hash[item] = 1
end
- item_frequencies
end
def average
reduce(:+) / length.to_f
end
def drop_every(n)
- list_with_every_nth_dropped = []
- each_index do |index|
- list_with_every_nth_dropped << self[index] unless (index + 1) % n == 0
+ each_with_index.each_with_object([]) do |item_index_tuple, list|
+ list << item_index_tuple[0] unless (item_index_tuple[1] + 1) % n == 0
end
-
- list_with_every_nth_dropped
end
def combine_with(other)
combined = []
each_index do |index|
combined << self[index]
combined << other[index] if other[index]
end
other.length > length ? combined += other[length..-1] : combined
end
end