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

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

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

Резултати

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

Код

class Integer
def prime?
return false if pred < 1
2.upto(pred).all? { |integer| remainder(integer).nonzero? }
end
def prime_factors
2.upto(abs).each do |number|
if abs.remainder(number).zero? and number.prime?
return [number] + (abs / number).prime_factors
end
end
[]
end
def harmonic
downto(1).map { |number| Rational(1, number) }.reduce(:+)
end
def digits
return [abs] if abs < 10
(abs / 10).digits + [abs % 10]
end
end
class Array
def frequencies
Hash[each.map { |element| [element, count(element)] }]
end
def average
reduce(:+) / size.to_f
end
def drop_every(n)
each_index.reject { |index| index.next.remainder(n).zero? }.map do |index|
at(index)
end
end
def combine_with(other)
shorter_size = size < other.size ? size : other.size
combined_array = [self[0...shorter_size], other[0...shorter_size]]
combined_array = combined_array.transpose
combined_array += self[shorter_size..-1]
combined_array += other[shorter_size..-1]
combined_array.flatten
end
end

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

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

Finished in 0.01852 seconds
14 examples, 0 failures

История (14 версии и 4 коментара)

Методи обнови решението на 10.10.2013 11:39 (преди над 10 години)

+class Integer
+ def prime?
+ 2.upto(abs - 1).all? { |integer| abs % integer != 0 }
+ end
+
+ def prime_factors
+ 2.upto(abs).each do |x|
+ if x.prime? and abs % x == 0 and abs / x > 1
+ return [x, (abs/x).prime_factors].flatten
+ end
+ if abs / x == 1 and abs % x == 0
+ return [x]
+ end
+ end
+ end
+
+ def harmonic
+ denominator = 1.upto(abs).inject { |a,b| a * b }
+ numerator = 1.upto(abs).map { |x| denominator / x }. inject { |a, b| a + b }
+ Rational(numerator, denominator)
+ end
+
+ def digits
+ digit = abs % 10
+ if abs / 10 > 0
+ return [(abs / 10).digits, digit].flatten
+ end
+ [digit]
+ end
+end
+
+class Array
+ def frequencies
+ output = Hash.new
+ each do |x|
+ output[x] = count(x)
+ end
+ output
+ end
+
+ def average
+ inject { |a, b| a + b } / count.to_f
+ end
+
+ def drop_every(n)
+ output = []
+ each_index do |x|
+ if (x + 1) % n != 0 then output << at(x) end
+ end
+ output
+ end
+
+ def combine_with(other)
+ output = []
+ each_index do |x|
+ output << at(x)
+ output << other[x]
+ end
+ output.compact
+ end
+end
  • Мисля, че може да попреработиш #prime_factos малко. Има повтаряща се логика и изглежда сложничко.
  • Стилово уточнение там: (abs/x) – тук трябва да има интервали около /
  • return [x, (abs/x).prime_factors].flatten може да се запише по-просто като return [x] + (abs / x).prime_factors; подобно нещо важи и за return-а в #digits – събиране на списъци е по-проста за възприемане операция от Array#flatten
  • Стил - интервал след запетаята тук: |a,b| (на всичкото отгоре, един ред по-надолу си сложил запетая, т.е. си неконсистентен, което е дори по-зле)
  • Няма нужда да ползваш abs в #harmonic, по условие имаме: "Приемете, че за n <= 0, методът е недефиниран."
  • На ред 19 има много лошо поставен интервал тук: . inject
  • Ред 25: abs / 10 > 0 се записва по-простичко като abs >= 10 :) Не виждам причина да делиш, за да направиш тази проверка
  • Ред 34, още не сме го казали, но Hash.new без никакви аргументи не се ползва; използва се литералният синтаксис: {}
  • Ползвай size или length ако искаш просто бройката елементи в списък, недей да ползваш count за целта, понеже той прави повечко неща (както си видял вече)
  • Ред 49 си плаче за postfix-ен if, т.е.: output << at(x) if (x + 1) % n != 0
  • Пак на същото място, виж дали няма някакви методи на целите числа, които да ти връщат предишно/следващо число
  • Потърси документацията на each_with_index и виж дали може да ти е полезен някак
  • Като цяло, предпочитаме комбинации (вериги, chain-вания) от map + select/reject и т.н., отколкото това пълнене на резултат в output
  • output е недостатъчно добро име на променлива; твърде общо е; ползваш го навсякъде и това не е добър вариант; затова виж предния съвет, който ще те спаси от нуждата да имаш такава локална променлива, или поне прави усилието да я кръстиш по-ясно; например, в combine_with, може да бъде combined_elements и т.н.
  • Операторът << (или още, методът Array#<<) може да се chain-ва, защото връща масива (виж му документацията); иде реч за редове 56 и 57

Спирам дотук, че станаха доста неща. Като цяло, си на прав път и се справяш прилично добре за първо решение на първа задача :) А и си първият предал решение, така че ще ти дам бонус точка след оценяването (ако не забравя :P)

Методи обнови решението на 11.10.2013 10:10 (преди над 10 години)

class Integer
def prime?
2.upto(abs - 1).all? { |integer| abs % integer != 0 }
end
def prime_factors
2.upto(abs).each do |x|
if x.prime? and abs % x == 0 and abs / x > 1
- return [x, (abs/x).prime_factors].flatten
+ return [x] + (abs / x).prime_factors
end
if abs / x == 1 and abs % x == 0
return [x]
end
end
end
def harmonic
- denominator = 1.upto(abs).inject { |a,b| a * b }
- numerator = 1.upto(abs).map { |x| denominator / x }. inject { |a, b| a + b }
+ denominator = downto(1).inject { |a, b| a * b }
+ numerator = downto(1).map { |x| denominator / x }.inject { |a, b| a + b }
+ return nil unless numerator and denominator
Rational(numerator, denominator)
end
def digits
digit = abs % 10
- if abs / 10 > 0
- return [(abs / 10).digits, digit].flatten
+ if abs >= 10
+ return (abs / 10).digits + [digit]
end
[digit]
end
end
class Array
def frequencies
- output = Hash.new
+ output = {}
each do |x|
- output[x] = count(x)
+ output[x] = select { |y| y == x}.size
end
output
end
def average
inject { |a, b| a + b } / count.to_f
end
def drop_every(n)
output = []
- each_index do |x|
- if (x + 1) % n != 0 then output << at(x) end
- end
+ each_with_index { |item, index| output << item if index.next % n != 0 }
output
end
def combine_with(other)
output = []
each_index do |x|
- output << at(x)
- output << other[x]
+ output << at(x) << other[x]
end
output.compact
end
end

Методи обнови решението на 11.10.2013 10:15 (преди над 10 години)

class Integer
def prime?
- 2.upto(abs - 1).all? { |integer| abs % integer != 0 }
+ 2.upto(abs.pred).all? { |integer| abs % integer != 0 }
end
def prime_factors
2.upto(abs).each do |x|
if x.prime? and abs % x == 0 and abs / x > 1
return [x] + (abs / x).prime_factors
end
if abs / x == 1 and abs % x == 0
return [x]
end
end
end
def harmonic
denominator = downto(1).inject { |a, b| a * b }
numerator = downto(1).map { |x| denominator / x }.inject { |a, b| a + b }
return nil unless numerator and denominator
Rational(numerator, denominator)
end
def digits
- digit = abs % 10
+ last_digit = abs % 10
if abs >= 10
- return (abs / 10).digits + [digit]
+ return (abs / 10).digits + [last_digit]
end
- [digit]
+ [last_digit]
end
end
class Array
def frequencies
- output = {}
+ element_frequencies = {}
each do |x|
- output[x] = select { |y| y == x}.size
+ element_frequencies[x] = select { |y| y == x}.size
end
- output
+ element_frequencies
end
def average
inject { |a, b| a + b } / count.to_f
end
def drop_every(n)
output = []
each_with_index { |item, index| output << item if index.next % n != 0 }
output
end
def combine_with(other)
- output = []
+ combined_array = []
each_index do |x|
- output << at(x) << other[x]
+ combined_array << at(x) << other[x]
end
- output.compact
+ combined_array
end
end

Методи обнови решението на 11.10.2013 10:20 (преди над 10 години)

class Integer
def prime?
2.upto(abs.pred).all? { |integer| abs % integer != 0 }
end
def prime_factors
2.upto(abs).each do |x|
if x.prime? and abs % x == 0 and abs / x > 1
return [x] + (abs / x).prime_factors
end
if abs / x == 1 and abs % x == 0
return [x]
end
end
end
def harmonic
denominator = downto(1).inject { |a, b| a * b }
numerator = downto(1).map { |x| denominator / x }.inject { |a, b| a + b }
return nil unless numerator and denominator
Rational(numerator, denominator)
end
def digits
last_digit = abs % 10
if abs >= 10
return (abs / 10).digits + [last_digit]
end
[last_digit]
end
end
class Array
def frequencies
element_frequencies = {}
each do |x|
element_frequencies[x] = select { |y| y == x}.size
end
element_frequencies
end
def average
+ return nil unless size > 0
inject { |a, b| a + b } / count.to_f
end
def drop_every(n)
output = []
each_with_index { |item, index| output << item if index.next % n != 0 }
output
end
def combine_with(other)
combined_array = []
each_index do |x|
combined_array << at(x) << other[x]
end
combined_array
end
end

Методи обнови решението на 11.10.2013 10:30 (преди над 10 години)

class Integer
def prime?
2.upto(abs.pred).all? { |integer| abs % integer != 0 }
end
def prime_factors
2.upto(abs).each do |x|
if x.prime? and abs % x == 0 and abs / x > 1
return [x] + (abs / x).prime_factors
end
if abs / x == 1 and abs % x == 0
return [x]
end
end
end
def harmonic
denominator = downto(1).inject { |a, b| a * b }
numerator = downto(1).map { |x| denominator / x }.inject { |a, b| a + b }
return nil unless numerator and denominator
Rational(numerator, denominator)
end
def digits
last_digit = abs % 10
if abs >= 10
return (abs / 10).digits + [last_digit]
end
[last_digit]
end
end
class Array
def frequencies
element_frequencies = {}
each do |x|
element_frequencies[x] = select { |y| y == x}.size
end
element_frequencies
end
def average
return nil unless size > 0
inject { |a, b| a + b } / count.to_f
end
def drop_every(n)
- output = []
- each_with_index { |item, index| output << item if index.next % n != 0 }
- output
+ filtered_array = []
+ each_with_index do |item, index|
+ filtered_array << item if index.next % n != 0
+ end
+ filtered_array
end
def combine_with(other)
combined_array = []
each_index do |x|
combined_array << at(x) << other[x]
end
combined_array
end
end

Методи обнови решението на 11.10.2013 23:37 (преди над 10 години)

class Integer
def prime?
2.upto(abs.pred).all? { |integer| abs % integer != 0 }
end
+ def single_prime_factors
+ 2.upto(abs).select { |x| x.prime? and abs % x == 0 }
+ end
+
+ def single_factors_rest
+ abs / abs.single_prime_factors.inject { |a, b| a * b }
+ end
+
def prime_factors
- 2.upto(abs).each do |x|
- if x.prime? and abs % x == 0 and abs / x > 1
- return [x] + (abs / x).prime_factors
- end
- if abs / x == 1 and abs % x == 0
- return [x]
- end
- end
+ single_prime_factors + single_factors_rest.single_prime_factors
end
def harmonic
- denominator = downto(1).inject { |a, b| a * b }
- numerator = downto(1).map { |x| denominator / x }.inject { |a, b| a + b }
- return nil unless numerator and denominator
- Rational(numerator, denominator)
+ denom = downto(1).inject { |a, b| a * b }
+ numerator = downto(1).map { |x| denom / x }.inject { |a, b| a + b }
+ Rational(numerator, denom)
end
def digits
- last_digit = abs % 10
- if abs >= 10
- return (abs / 10).digits + [last_digit]
- end
- [last_digit]
+ return abs >= 10 ? (abs / 10).digits + [abs % 10] : [abs % 10]
end
end
class Array
def frequencies
- element_frequencies = {}
- each do |x|
- element_frequencies[x] = select { |y| y == x}.size
- end
- element_frequencies
+ Hash[each.map { |x| [x, count(x)] }]
end
def average
- return nil unless size > 0
inject { |a, b| a + b } / count.to_f
end
def drop_every(n)
- filtered_array = []
- each_with_index do |item, index|
- filtered_array << item if index.next % n != 0
- end
- filtered_array
+ each_index.select { |index| index.next % n != 0 }.map { |x| at(x) }
end
def combine_with(other)
- combined_array = []
- each_index do |x|
- combined_array << at(x) << other[x]
- end
- combined_array
+ shorter = size < other.size ? size : other.size
+ combined_array = [self[0..shorter.pred], other[0..shorter.pred]].transpose
+ combined_array += [self[shorter..-1]]+[other[shorter..-1]]
+ combined_array.flatten
end
end

Методи обнови решението на 11.10.2013 23:40 (преди над 10 години)

class Integer
def prime?
2.upto(abs.pred).all? { |integer| abs % integer != 0 }
end
def single_prime_factors
2.upto(abs).select { |x| x.prime? and abs % x == 0 }
end
def single_factors_rest
abs / abs.single_prime_factors.inject { |a, b| a * b }
end
def prime_factors
single_prime_factors + single_factors_rest.single_prime_factors
end
def harmonic
denom = downto(1).inject { |a, b| a * b }
numerator = downto(1).map { |x| denom / x }.inject { |a, b| a + b }
Rational(numerator, denom)
end
def digits
- return abs >= 10 ? (abs / 10).digits + [abs % 10] : [abs % 10]
+ abs >= 10 ? (abs / 10).digits + [abs % 10] : [abs % 10]
end
end
class Array
def frequencies
Hash[each.map { |x| [x, count(x)] }]
end
def average
inject { |a, b| a + b } / count.to_f
end
def drop_every(n)
each_index.select { |index| index.next % n != 0 }.map { |x| at(x) }
end
def combine_with(other)
shorter = size < other.size ? size : other.size
combined_array = [self[0..shorter.pred], other[0..shorter.pred]].transpose
combined_array += [self[shorter..-1]]+[other[shorter..-1]]
combined_array.flatten
end
end

Методи обнови решението на 12.10.2013 00:12 (преди над 10 години)

class Integer
def prime?
- 2.upto(abs.pred).all? { |integer| abs % integer != 0 }
+ 2.upto(pred).all? { |integer| self % integer != 0} and 2.upto(pred).size > 0
end
def single_prime_factors
2.upto(abs).select { |x| x.prime? and abs % x == 0 }
end
def single_factors_rest
abs / abs.single_prime_factors.inject { |a, b| a * b }
end
def prime_factors
single_prime_factors + single_factors_rest.single_prime_factors
end
def harmonic
denom = downto(1).inject { |a, b| a * b }
numerator = downto(1).map { |x| denom / x }.inject { |a, b| a + b }
Rational(numerator, denom)
end
def digits
abs >= 10 ? (abs / 10).digits + [abs % 10] : [abs % 10]
end
end
class Array
def frequencies
Hash[each.map { |x| [x, count(x)] }]
end
def average
inject { |a, b| a + b } / count.to_f
end
def drop_every(n)
each_index.select { |index| index.next % n != 0 }.map { |x| at(x) }
end
def combine_with(other)
shorter = size < other.size ? size : other.size
combined_array = [self[0..shorter.pred], other[0..shorter.pred]].transpose
combined_array += [self[shorter..-1]]+[other[shorter..-1]]
combined_array.flatten
end
end

Методи обнови решението на 12.10.2013 00:32 (преди над 10 години)

class Integer
def prime?
- 2.upto(pred).all? { |integer| self % integer != 0} and 2.upto(pred).size > 0
+ 2.upto(pred).all? { |integer| self % integer != 0} and 1.upto(pred).size > 0
end
def single_prime_factors
2.upto(abs).select { |x| x.prime? and abs % x == 0 }
end
def single_factors_rest
abs / abs.single_prime_factors.inject { |a, b| a * b }
end
def prime_factors
- single_prime_factors + single_factors_rest.single_prime_factors
+ (single_prime_factors + single_factors_rest.single_prime_factors).sort
end
def harmonic
denom = downto(1).inject { |a, b| a * b }
numerator = downto(1).map { |x| denom / x }.inject { |a, b| a + b }
Rational(numerator, denom)
end
def digits
abs >= 10 ? (abs / 10).digits + [abs % 10] : [abs % 10]
end
end
class Array
def frequencies
Hash[each.map { |x| [x, count(x)] }]
end
def average
inject { |a, b| a + b } / count.to_f
end
def drop_every(n)
each_index.select { |index| index.next % n != 0 }.map { |x| at(x) }
end
def combine_with(other)
shorter = size < other.size ? size : other.size
combined_array = [self[0..shorter.pred], other[0..shorter.pred]].transpose
combined_array += [self[shorter..-1]]+[other[shorter..-1]]
combined_array.flatten
end
end

Методи обнови решението на 12.10.2013 19:25 (преди над 10 години)

class Integer
def prime?
2.upto(pred).all? { |integer| self % integer != 0} and 1.upto(pred).size > 0
end
def single_prime_factors
2.upto(abs).select { |x| x.prime? and abs % x == 0 }
end
def single_factors_rest
abs / abs.single_prime_factors.inject { |a, b| a * b }
end
def prime_factors
(single_prime_factors + single_factors_rest.single_prime_factors).sort
end
def harmonic
denom = downto(1).inject { |a, b| a * b }
numerator = downto(1).map { |x| denom / x }.inject { |a, b| a + b }
Rational(numerator, denom)
end
def digits
abs >= 10 ? (abs / 10).digits + [abs % 10] : [abs % 10]
end
end
class Array
def frequencies
Hash[each.map { |x| [x, count(x)] }]
end
def average
- inject { |a, b| a + b } / count.to_f
+ inject { |a, b| a + b } / size.to_f
end
def drop_every(n)
each_index.select { |index| index.next % n != 0 }.map { |x| at(x) }
end
def combine_with(other)
- shorter = size < other.size ? size : other.size
- combined_array = [self[0..shorter.pred], other[0..shorter.pred]].transpose
- combined_array += [self[shorter..-1]]+[other[shorter..-1]]
+ shorter_size = size < other.size ? size : other.size
+ combined_array = [self[0...shorter_size], other[0...shorter_size]].transpose
+ combined_array += self[shorter_size..-1] + other[shorter_size..-1]
combined_array.flatten
end
end

Още малко бележки:

  • Предпочитаме вариантите с думи на проверката за делимост, т.е. remainder(number) и методите zero? и nonzero?
  • Същото важи когато се проверява дали списък е празен или не -- empty?, вместо size = 0, например
  • На ред 3 има нужда от интервал преди затварящата скоба }
  • x не е добро име; number е по-добро; същото важи и за a и b
  • Методите inject и reduce са синоними и предпочитаме reduce, по-близък е до идеята на това, което искаме да постигнем
  • По-прост начин за намиране произведението на елементите в списък е [...].reduce(:*); същото важи и за намиране сумата на елементите в списък
  • Съкращаване на пробменливи с няколко символа не е добре; по-добре е да се напише denominator, отколкото denom; предпочитайте яснота пред краткост (clarity over brevity)
  • Тернарният оператор на ред 25 е твърде сложен, за да е в този вид; отново, clarity over brevity – на два реда, ако трябва, лошо няма
  • Нещо не ми харесват имената single_prime_factors и single_factors_rest; не ми говорят ясно какво ги различава и не виждам ясна нужда да съществуват; лошо няма да се извежда логика в помощни методи, но в случая, с това именоване, не мисля, че се е получило добре
  • В drop_every, по-добре да ползваш reject, отколкото select, струва ми се по-близко до идеята
  • Добра практика за по-четим код е когато имаме няколко реда с равенства един под друг, да ги подравняваш по символа =; при теб това може да стане на ред 19-20 и ред 43-45
  • Празните редове не се броят от skeptic; на някои места помагат за четимостта на кода

Методи обнови решението на 13.10.2013 19:31 (преди над 10 години)

class Integer
def prime?
- 2.upto(pred).all? { |integer| self % integer != 0} and 1.upto(pred).size > 0
+ return false if pred < 0
+ 2.upto(pred).all? { |integer| remainder(integer).nonzero? }
end
- def single_prime_factors
- 2.upto(abs).select { |x| x.prime? and abs % x == 0 }
+ def unique_prime_factors
+ 2.upto(abs).select do |integer|
+ integer.prime? and abs.remainder(integer).zero?
+ end
end
- def single_factors_rest
- abs / abs.single_prime_factors.inject { |a, b| a * b }
+ def unique_prime_factors_rest
+ abs / abs.unique_prime_factors.reduce(:*)
end
def prime_factors
- (single_prime_factors + single_factors_rest.single_prime_factors).sort
+ (unique_prime_factors + unique_prime_factors_rest.unique_prime_factors).sort
end
def harmonic
- denom = downto(1).inject { |a, b| a * b }
- numerator = downto(1).map { |x| denom / x }.inject { |a, b| a + b }
- Rational(numerator, denom)
+ downto(1).map { |number| Rational(1, number) }.reduce(:+)
end
def digits
- abs >= 10 ? (abs / 10).digits + [abs % 10] : [abs % 10]
+ return [abs] if abs < 10
+ (abs / 10).digits + [abs % 10]
end
end
class Array
def frequencies
- Hash[each.map { |x| [x, count(x)] }]
+ Hash[each.map { |element| [element, count(element)] }]
end
def average
- inject { |a, b| a + b } / size.to_f
+ reduce(:+) / size.to_f
end
def drop_every(n)
- each_index.select { |index| index.next % n != 0 }.map { |x| at(x) }
+ each_index.reject { |index| index.next.remainder(n).zero? }.map do |index|
+ at(index)
+ end
end
def combine_with(other)
- shorter_size = size < other.size ? size : other.size
- combined_array = [self[0...shorter_size], other[0...shorter_size]].transpose
- combined_array += self[shorter_size..-1] + other[shorter_size..-1]
+ shorter_size = size < other.size ? size : other.size
+ combined_array = [self[0...shorter_size], other[0...shorter_size]]
+ combined_array = combined_array.transpose
+ combined_array += self[shorter_size..-1]
+ combined_array += other[shorter_size..-1]
combined_array.flatten
end
end

Методи обнови решението на 15.10.2013 10:43 (преди над 10 години)

class Integer
def prime?
return false if pred < 0
2.upto(pred).all? { |integer| remainder(integer).nonzero? }
end
- def unique_prime_factors
- 2.upto(abs).select do |integer|
- integer.prime? and abs.remainder(integer).zero?
- end
- end
-
- def unique_prime_factors_rest
- abs / abs.unique_prime_factors.reduce(:*)
- end
-
def prime_factors
- (unique_prime_factors + unique_prime_factors_rest.unique_prime_factors).sort
+ 2.upto(abs).each do |number|
+ if abs.remainder(number).zero? and number.prime?
+ return [number] + (abs / number).prime_factors
+ end
+ end
+ []
end
def harmonic
downto(1).map { |number| Rational(1, number) }.reduce(:+)
end
def digits
return [abs] if abs < 10
(abs / 10).digits + [abs % 10]
end
end
class Array
def frequencies
Hash[each.map { |element| [element, count(element)] }]
end
def average
reduce(:+) / size.to_f
end
def drop_every(n)
each_index.reject { |index| index.next.remainder(n).zero? }.map do |index|
at(index)
end
end
def combine_with(other)
shorter_size = size < other.size ? size : other.size
combined_array = [self[0...shorter_size], other[0...shorter_size]]
combined_array = combined_array.transpose
combined_array += self[shorter_size..-1]
combined_array += other[shorter_size..-1]
combined_array.flatten
end
end

Методи обнови решението на 15.10.2013 12:28 (преди над 10 години)

class Integer
def prime?
- return false if pred < 0
+ return false if pred < 2
2.upto(pred).all? { |integer| remainder(integer).nonzero? }
end
def prime_factors
2.upto(abs).each do |number|
if abs.remainder(number).zero? and number.prime?
return [number] + (abs / number).prime_factors
end
end
[]
end
def harmonic
downto(1).map { |number| Rational(1, number) }.reduce(:+)
end
def digits
return [abs] if abs < 10
(abs / 10).digits + [abs % 10]
end
end
class Array
def frequencies
Hash[each.map { |element| [element, count(element)] }]
end
def average
reduce(:+) / size.to_f
end
def drop_every(n)
each_index.reject { |index| index.next.remainder(n).zero? }.map do |index|
at(index)
end
end
def combine_with(other)
shorter_size = size < other.size ? size : other.size
combined_array = [self[0...shorter_size], other[0...shorter_size]]
combined_array = combined_array.transpose
combined_array += self[shorter_size..-1]
combined_array += other[shorter_size..-1]
combined_array.flatten
end
end

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

class Integer
def prime?
- return false if pred < 2
+ return false if pred < 1
2.upto(pred).all? { |integer| remainder(integer).nonzero? }
end
def prime_factors
2.upto(abs).each do |number|
if abs.remainder(number).zero? and number.prime?
return [number] + (abs / number).prime_factors
end
end
[]
end
def harmonic
downto(1).map { |number| Rational(1, number) }.reduce(:+)
end
def digits
return [abs] if abs < 10
(abs / 10).digits + [abs % 10]
end
end
class Array
def frequencies
Hash[each.map { |element| [element, count(element)] }]
end
def average
reduce(:+) / size.to_f
end
def drop_every(n)
each_index.reject { |index| index.next.remainder(n).zero? }.map do |index|
at(index)
end
end
def combine_with(other)
shorter_size = size < other.size ? size : other.size
combined_array = [self[0...shorter_size], other[0...shorter_size]]
combined_array = combined_array.transpose
combined_array += self[shorter_size..-1]
combined_array += other[shorter_size..-1]
combined_array.flatten
end
end