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

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

Към профила на Александър Антов

Резултати

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

Код

class Integer
def prime?
self < 2 and return false
iter, temp = 2, self
while iter < temp do
self % iter == 0 and return false
iter, temp = [iter+1, self/iter]
end
return true
end
end
class Integer
def prime_factors
temp, array = self.abs, Array.new
(1..temp).each do |iter|
while temp % iter == 0 and iter.prime?
temp = temp / iter
array.push iter
end
end
return array
end
end
class Integer
def harmonic
temp, sum = self, 0
while temp != 0
sum, temp = [sum + Rational(1, temp), temp - 1]
end
return sum
end
end
class Integer
def digits
temp, array = self.abs, Array.new
while temp / 10 >= 1
array.push temp%10
temp = temp/10
end
array.push temp%10
return array.reverse
end
end
class Array
def frequencies
temp, hash = self.clone, Hash.new
temp.each do |iter|
if !hash.has_key?(iter)
hash[iter] = temp.count(iter)
end
end
return hash
end
end
class Array
def average
temp, sum = self.clone, 0
temp.each do |iter|
sum = sum + iter
end
return sum.to_f/temp.count.to_f
end
end
class Array
def drop_every(index_to_skip)
counter, array = 1, Array.new
self.each do |iter|
if counter%index_to_skip != 0
array.push iter
end
counter = counter + 1
end
return array
end
end
class Array
def combine_with(second_array)
array, longest_length = Array.new, [self.length, second_array.length].max
(0..longest_length).each do |index|
if index < self.length then array.push self[index] end
if index < second_array.length then array.push second_array[index] end
end
return array
end
end

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

..............

Finished in 0.01756 seconds
14 examples, 0 failures

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

Александър обнови решението на 16.10.2013 03:09 (преди около 11 години)

+class Integer
+ def prime?
+ self < 2 and return false
+ iter, temp = 2, self
+ while iter < temp do
+ self % iter == 0 and return false
+ iter, temp = [iter+1, self/iter]
+ end
+ return true
+ end
+end
+
+class Integer
+ def prime_factors
+ temp, array = self.abs, Array.new
+ (1..temp).each do |iter|
+ while temp % iter == 0 and iter.prime?
+ temp = temp / iter
+ array.push iter
+ end
+ end
+ return array
+ end
+end
+
+class Integer
+ def harmonic
+ temp, sum = self, 0
+ while temp != 0
+ sum, temp = [sum + Rational(1, temp), temp - 1]
+ end
+ return sum
+ end
+end
+
+class Integer
+ def digits
+ temp, array = self.abs, Array.new
+ while temp / 10 >= 1
+ array.push temp%10
+ temp = temp/10
+ end
+ array.push temp%10
+ return array.reverse
+ end
+end
+
+class Array
+ def frequencies
+ temp, hash = self.clone, Hash.new
+ temp.each do |iter|
+ if !hash.has_key?(iter)
+ hash[iter] = temp.count(iter)
+ end
+ end
+ return hash
+ end
+end
+
+class Array
+ def average
+ temp, sum = self.clone, 0
+ temp.each do |iter|
+ sum = sum + iter
+ end
+ return sum.to_f/temp.count.to_f
+ end
+end
+
+class Array
+ def drop_every(index_to_skip)
+ counter, array = 1, Array.new
+ self.each do |iter|
+ if counter%index_to_skip != 0
+ array.push iter
+ end
+ counter = counter + 1
+ end
+ return array
+ end
+end
+
+class Array
+ def combine_with(second_array)
+ array, longest_length = Array.new, [self.length, second_array.length].max
+ (0..longest_length).each do |index|
+ if index < self.length then array.push self[index] end
+ if index < second_array.length then array.push second_array[index] end
+ end
+ return array
+ end
+end

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

class Integer
- def prime?
- self < 2 and return false
- iter, temp = 2, self
- while iter < temp do
- self % iter == 0 and return false
- iter, temp = [iter+1, self/iter]
- end
- return true
- end
+ def prime?
+ self < 2 and return false
+ iter, temp = 2, self
+ while iter < temp do
+ self % iter == 0 and return false
+ iter, temp = [iter+1, self/iter]
+ end
+ return true
+ end
end
class Integer
- def prime_factors
- temp, array = self.abs, Array.new
- (1..temp).each do |iter|
- while temp % iter == 0 and iter.prime?
- temp = temp / iter
- array.push iter
- end
- end
- return array
- end
+ def prime_factors
+ temp, array = self.abs, Array.new
+ (1..temp).each do |iter|
+ while temp % iter == 0 and iter.prime?
+ temp = temp / iter
+ array.push iter
+ end
+ end
+ return array
+ end
end
class Integer
- def harmonic
- temp, sum = self, 0
- while temp != 0
- sum, temp = [sum + Rational(1, temp), temp - 1]
- end
- return sum
- end
+ def harmonic
+ temp, sum = self, 0
+ while temp != 0
+ sum, temp = [sum + Rational(1, temp), temp - 1]
+ end
+ return sum
+ end
end
class Integer
- def digits
- temp, array = self.abs, Array.new
- while temp / 10 >= 1
- array.push temp%10
- temp = temp/10
- end
- array.push temp%10
- return array.reverse
- end
+ def digits
+ temp, array = self.abs, Array.new
+ while temp / 10 >= 1
+ array.push temp%10
+ temp = temp/10
+ end
+ array.push temp%10
+ return array.reverse
+ end
end
class Array
- def frequencies
- temp, hash = self.clone, Hash.new
- temp.each do |iter|
- if !hash.has_key?(iter)
- hash[iter] = temp.count(iter)
- end
- end
- return hash
- end
+ def frequencies
+ temp, hash = self.clone, Hash.new
+ temp.each do |iter|
+ if !hash.has_key?(iter)
+ hash[iter] = temp.count(iter)
+ end
+ end
+ return hash
+ end
end
class Array
- def average
- temp, sum = self.clone, 0
- temp.each do |iter|
- sum = sum + iter
- end
- return sum.to_f/temp.count.to_f
- end
+ def average
+ temp, sum = self.clone, 0
+ temp.each do |iter|
+ sum = sum + iter
+ end
+ return sum.to_f/temp.count.to_f
+ end
end
class Array
- def drop_every(index_to_skip)
- counter, array = 1, Array.new
- self.each do |iter|
- if counter%index_to_skip != 0
- array.push iter
- end
- counter = counter + 1
- end
- return array
- end
+ def drop_every(index_to_skip)
+ counter, array = 1, Array.new
+ self.each do |iter|
+ if counter%index_to_skip != 0
+ array.push iter
+ end
+ counter = counter + 1
+ end
+ return array
+ end
end
class Array
- def combine_with(second_array)
- array, longest_length = Array.new, [self.length, second_array.length].max
- (0..longest_length).each do |index|
- if index < self.length then array.push self[index] end
- if index < second_array.length then array.push second_array[index] end
- end
- return array
- end
+ def combine_with(second_array)
+ array, longest_length = Array.new, [self.length, second_array.length].max
+ (0..longest_length).each do |index|
+ if index < self.length then array.push self[index] end
+ if index < second_array.length then array.push second_array[index] end
+ end
+ return array
+ end
end