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

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

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

Резултати

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

Код

class Integer
def prime?
return false if self < 2
square_root = Math.sqrt(self)
(2..square_root).each { |n| if self.modulo(n).zero? then return false end }
true
end
def harmonic
harmonic_sum = 0
(1..self).each { |n| harmonic_sum += Rational(1, n) }
harmonic_sum
end
def digits
number_to_array = self.abs.to_s.split('')
number_to_array.map do |dig|
dig.to_i
end
end
end
class Array
def drop_every(n)
result_drop = []
tll = ->(i,n) {|i, n| (i+1).modulo(n).zero? } #too long line
(0...self.size).each { |i| result_drop << self[i] unless tll(i,n) }
result_drop
end
def combine_with(other)
combination = []
length_max = if self.size > other.size then self.size else other.size end
(0...length_max).each { |i| combination << self[i] << other[i] }
combination.compact
end
def average
sum = 0.0
self.each {|n| sum += n }
return sum / self.size
end
def frequencies
inject(Hash.new(0)) { |h,e| h[e] += 1 and h }.inject({}) {
|r, e| r[e.first] = e.last and r }
end
end

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

/data/rails/evans-2013/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.5/lib/rspec/core/configuration.rb:867:in `require': /tmp/d20131023-4395-u3ylyj/solution.rb:26: syntax error, unexpected '|' (SyntaxError)
	tll = ->(i,n) {|i, n| (i+1).modulo(n).zero? } #too long line
	                ^
/tmp/d20131023-4395-u3ylyj/solution.rb:26: syntax error, unexpected '|', expecting '='
	tll = ->(i,n) {|i, n| (i+1).modulo(n).zero? } #too long line
	                     ^
	from /data/rails/evans-2013/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.5/lib/rspec/core/configuration.rb:867:in `block in setup_load_path_and_require'
	from /data/rails/evans-2013/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.5/lib/rspec/core/configuration.rb:867:in `each'
	from /data/rails/evans-2013/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.5/lib/rspec/core/configuration.rb:867:in `setup_load_path_and_require'
	from /data/rails/evans-2013/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.5/lib/rspec/core/configuration_options.rb:25:in `configure'
	from /data/rails/evans-2013/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.5/lib/rspec/core/command_line.rb:21:in `run'
	from /data/rails/evans-2013/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.5/lib/rspec/core/runner.rb:80:in `run'
	from /data/rails/evans-2013/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.5/lib/rspec/core/runner.rb:17:in `block in autorun'

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

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

+class Integer
+ def prime?
+ return false if self < 2
+ square_root = Math.sqrt(self)
+ (2..square_root).each { |n| if self.modulo(n).zero? then return false end }
+ true
+ end
+
+ def harmonic
+ harmonic_sum = 0
+ (1..self).each { |n| harmonic_sum += Rational(1, n) }
+ harmonic_sum
+ end
+
+ def digits
+ number_to_array = self.abs.to_s.split('')
+ number_to_array.map do |dig|
+ dig.to_i
+ end
+ end
+end
+
+class Array
+ def drop_every(n)
+ result_drop = []
+ tll = ->(i,n) {|i, n| (i+1).modulo(n).zero? } #too long line
+ (0...self.size).each { |i| result_drop << self[i] unless tll(i,n) }
+ result_drop
+ end
+
+ def combine_with(other)
+ combination = []
+ length_max = if self.size > other.size then self.size else other.size end
+ (0...length_max).each { |i| combination << self[i] << other[i] }
+ combination.compact
+ end
+
+ def average
+ sum = 0.0
+ self.each {|n| sum += n }
+ return sum / self.size
+ end
+
+ def frequencies
+ inject(Hash.new(0)) { |h,e| h[e] += 1 and h }.inject({}) {
+ |r, e| r[e.first] = e.last and r }
+ end
+end