Решение на Първа задача от Иван Латунов

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

Към профила на Иван Латунов

Резултати

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

Код

class Integer
# The function 'prime?' can be shorter
# but readability is prior to brevity
# the 'return's fix an unwanted event in reading the lines
# as `if <condition> <do this>`
# Also: comments are in english because `skeptic` doesn't like cyrillic
def prime?
return false if self < 2
return true if self == 2
upper_cap = Math.sqrt(self).to_i + 1
search_range = (2..upper_cap)
search_range.each {|x| return false if (self % x) == 0 and self != 2}
true
end
def prime_factors
list = []
(2..self.abs).each {|x| list << x if x.prime? and (self.abs % x == 0)}
if list.reduce(:*) == self.abs
list.sort
else
list << (self.abs / list.reduce(:*)).prime_factors
list.flatten.sort
end
end
def digits
self.abs.to_s.split("").map {|x| x.to_i}
end
def harmonic
abs_self = self.abs.to_r + 1
harmonic_result = 0.to_r
harmonic_result += (1 / (abs_self -= 1)) while abs_self != 1
harmonic_result
end
end
class Array
def average
self.reduce(:+) / self.length.to_f
end
def frequencies
return_hash = {}
self.uniq.each {|e| return_hash[e] = self.count(e)}
return_hash
end
def drop_every(n)
self.dup.keep_if {|x| (self.index(x) + 1) % n != 0}
end
def combine_with(other)
bigger_length = self.length > other.length ? self.length : other.length
return_array = []
(0...bigger_length).each {|x| return_array << self[x] << other[x]}
return_array.compact
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-1wj8c3z/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.02808 seconds
14 examples, 1 failure

Failed examples:

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

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

Иван обнови решението на 16.10.2013 12:19 (преди над 10 години)

+class Integer
+ # The function 'prime?' can be shorter
+ # but readability is prior to brevity
+ # the 'return's fix a problem with Ruby reading the lines
+ # as `if <condition> <do this>`
+ # Also: comments are in english because `skeptic` doesn't like cyrillic
+ def prime?
+ return false if self < 2
+ return true if self == 2
+ upper_cap = Math.sqrt(self).to_i + 1
+ search_range = (2..upper_cap)
+ search_range.each {|x| return false if (self % x) == 0 and self != 2}
+ true
+ end
+
+ def prime_factors
+ list = []
+ (2..self.abs).each {|x| list << x if x.prime? and (self.abs % x == 0)}
+ if list.reduce(:*) == self.abs
+ list.sort
+ else
+ list << (self.abs / list.reduce(:*)).prime_factors
+ list.flatten.sort
+ end
+ end
+
+
+ def digits
+ self.abs.to_s.split("").map {|x| x.to_i}
+ end
+
+ def harmonic
+ abs_self = self.abs.to_r + 1
+ harmonic_result = 0.to_r
+ harmonic_result += (1 / (abs_self -= 1)) while abs_self != 1
+ harmonic_result
+ end
+end

Иван обнови решението на 16.10.2013 13:48 (преди над 10 години)

class Integer
# The function 'prime?' can be shorter
# but readability is prior to brevity
# the 'return's fix a problem with Ruby reading the lines
# as `if <condition> <do this>`
# Also: comments are in english because `skeptic` doesn't like cyrillic
def prime?
return false if self < 2
return true if self == 2
upper_cap = Math.sqrt(self).to_i + 1
search_range = (2..upper_cap)
search_range.each {|x| return false if (self % x) == 0 and self != 2}
true
end
def prime_factors
list = []
(2..self.abs).each {|x| list << x if x.prime? and (self.abs % x == 0)}
if list.reduce(:*) == self.abs
list.sort
else
list << (self.abs / list.reduce(:*)).prime_factors
list.flatten.sort
end
end
def digits
self.abs.to_s.split("").map {|x| x.to_i}
end
def harmonic
abs_self = self.abs.to_r + 1
harmonic_result = 0.to_r
harmonic_result += (1 / (abs_self -= 1)) while abs_self != 1
harmonic_result
end
+end
+
+
+class Array
+ def drop_every(n)
+ return_list = []
+ self.each_index {|x| return_list << self[x] if x % n != 0}
+ return_list
+ end
+
+ def average
+ self.reduce(:+) / self.length.to_f
+ end
+
+ def drop_every(n)
+ self.dup.keep_if {|x| (self.index(x) - 1) % n != 0}
+ end
end

Иван обнови решението на 16.10.2013 14:05 (преди над 10 години)

class Integer
# The function 'prime?' can be shorter
# but readability is prior to brevity
# the 'return's fix a problem with Ruby reading the lines
# as `if <condition> <do this>`
# Also: comments are in english because `skeptic` doesn't like cyrillic
def prime?
return false if self < 2
return true if self == 2
upper_cap = Math.sqrt(self).to_i + 1
search_range = (2..upper_cap)
search_range.each {|x| return false if (self % x) == 0 and self != 2}
true
end
def prime_factors
list = []
(2..self.abs).each {|x| list << x if x.prime? and (self.abs % x == 0)}
if list.reduce(:*) == self.abs
list.sort
else
list << (self.abs / list.reduce(:*)).prime_factors
list.flatten.sort
end
end
def digits
self.abs.to_s.split("").map {|x| x.to_i}
end
def harmonic
abs_self = self.abs.to_r + 1
harmonic_result = 0.to_r
harmonic_result += (1 / (abs_self -= 1)) while abs_self != 1
harmonic_result
end
end
class Array
- def drop_every(n)
- return_list = []
- self.each_index {|x| return_list << self[x] if x % n != 0}
- return_list
- end
-
def average
self.reduce(:+) / self.length.to_f
end
+ def frequencies
+ return_hash = {}
+ self.uniq.each {|e| return_hash[e] = self.count(e)}
+ return_hash
+ end
+
def drop_every(n)
- self.dup.keep_if {|x| (self.index(x) - 1) % n != 0}
+ self.dup.keep_if {|x| (self.index(x) + 1) % n != 0}
end
end

Иван обнови решението на 16.10.2013 14:39 (преди над 10 години)

class Integer
# The function 'prime?' can be shorter
# but readability is prior to brevity
# the 'return's fix a problem with Ruby reading the lines
# as `if <condition> <do this>`
# Also: comments are in english because `skeptic` doesn't like cyrillic
def prime?
return false if self < 2
return true if self == 2
upper_cap = Math.sqrt(self).to_i + 1
search_range = (2..upper_cap)
search_range.each {|x| return false if (self % x) == 0 and self != 2}
true
end
def prime_factors
list = []
(2..self.abs).each {|x| list << x if x.prime? and (self.abs % x == 0)}
if list.reduce(:*) == self.abs
list.sort
else
list << (self.abs / list.reduce(:*)).prime_factors
list.flatten.sort
end
end
def digits
self.abs.to_s.split("").map {|x| x.to_i}
end
def harmonic
abs_self = self.abs.to_r + 1
harmonic_result = 0.to_r
harmonic_result += (1 / (abs_self -= 1)) while abs_self != 1
harmonic_result
end
end
class Array
def average
self.reduce(:+) / self.length.to_f
end
def frequencies
return_hash = {}
self.uniq.each {|e| return_hash[e] = self.count(e)}
return_hash
end
def drop_every(n)
self.dup.keep_if {|x| (self.index(x) + 1) % n != 0}
end
+
+ def combine_with(other)
+ bigger_length = self.length > other.length ? self.length : other.length
+ return_array = []
+ (0...bigger_length).each {|x| return_array << self[x] << other[x]}
+ return_array.compact
+ end
end

Иван обнови решението на 16.10.2013 14:43 (преди над 10 години)

class Integer
# The function 'prime?' can be shorter
# but readability is prior to brevity
- # the 'return's fix a problem with Ruby reading the lines
+ # the 'return's fix an unwanted event in reading the lines
# as `if <condition> <do this>`
# Also: comments are in english because `skeptic` doesn't like cyrillic
def prime?
return false if self < 2
return true if self == 2
upper_cap = Math.sqrt(self).to_i + 1
search_range = (2..upper_cap)
search_range.each {|x| return false if (self % x) == 0 and self != 2}
true
end
def prime_factors
list = []
(2..self.abs).each {|x| list << x if x.prime? and (self.abs % x == 0)}
if list.reduce(:*) == self.abs
list.sort
else
list << (self.abs / list.reduce(:*)).prime_factors
list.flatten.sort
end
end
def digits
self.abs.to_s.split("").map {|x| x.to_i}
end
def harmonic
abs_self = self.abs.to_r + 1
harmonic_result = 0.to_r
harmonic_result += (1 / (abs_self -= 1)) while abs_self != 1
harmonic_result
end
end
class Array
def average
self.reduce(:+) / self.length.to_f
end
def frequencies
return_hash = {}
self.uniq.each {|e| return_hash[e] = self.count(e)}
return_hash
end
def drop_every(n)
self.dup.keep_if {|x| (self.index(x) + 1) % n != 0}
end
def combine_with(other)
bigger_length = self.length > other.length ? self.length : other.length
return_array = []
(0...bigger_length).each {|x| return_array << self[x] << other[x]}
return_array.compact
end
end