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

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

Към профила на Лъчезар Цанов

Резултати

  • 3 точки от тестове
  • 1 отнета точка
  • 2 точки общо
  • 4 успешни тест(а)
  • 4 неуспешни тест(а)

Код

class Integer
def prime_divisors()
(1..self.abs).select { |a| self % a == 0 unless /^1?$|^(11+?)\1+$/.match '1' * a }
end
end
class Range
def fizzbuzz()
mapToValues = { 3 => :fizz, 4 => :buzz , 15 => :fizzbuzz }
self.map { |a| mapToValues[a.gcd 15] || a }
end
end
class Hash
def group_values
result = Hash.new { |hash, key| hash[key] = [] }
self.each { |key, value| result[value] << key }
result
end
end

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

..FF..FF

Failures:

  1) Range#fizzbuzz it works with the first 100 numbers
     Failure/Error: (1..100).fizzbuzz.should eq [
       
       expected: [1, 2, :fizz, 4, :buzz, :fizz, 7, 8, :fizz, :buzz, 11, :fizz, 13, 14, :fizzbuzz, 16, 17, :fizz, 19, :buzz, :fizz, 22, 23, :fizz, :buzz, 26, :fizz, 28, 29, :fizzbuzz, 31, 32, :fizz, 34, :buzz, :fizz, 37, 38, :fizz, :buzz, 41, :fizz, 43, 44, :fizzbuzz, 46, 47, :fizz, 49, :buzz, :fizz, 52, 53, :fizz, :buzz, 56, :fizz, 58, 59, :fizzbuzz, 61, 62, :fizz, 64, :buzz, :fizz, 67, 68, :fizz, :buzz, 71, :fizz, 73, 74, :fizzbuzz, 76, 77, :fizz, 79, :buzz, :fizz, 82, 83, :fizz, :buzz, 86, :fizz, 88, 89, :fizzbuzz, 91, 92, :fizz, 94, :buzz, :fizz, 97, 98, :fizz, :buzz]
            got: [1, 2, :fizz, 4, 5, :fizz, 7, 8, :fizz, 10, 11, :fizz, 13, 14, :fizzbuzz, 16, 17, :fizz, 19, 20, :fizz, 22, 23, :fizz, 25, 26, :fizz, 28, 29, :fizzbuzz, 31, 32, :fizz, 34, 35, :fizz, 37, 38, :fizz, 40, 41, :fizz, 43, 44, :fizzbuzz, 46, 47, :fizz, 49, 50, :fizz, 52, 53, :fizz, 55, 56, :fizz, 58, 59, :fizzbuzz, 61, 62, :fizz, 64, 65, :fizz, 67, 68, :fizz, 70, 71, :fizz, 73, 74, :fizzbuzz, 76, 77, :fizz, 79, 80, :fizz, 82, 83, :fizz, 85, 86, :fizz, 88, 89, :fizzbuzz, 91, 92, :fizz, 94, 95, :fizz, 97, 98, :fizz, 100]
       
       (compared using ==)
     # /tmp/d20130307-6960-1kvhecp/spec.rb:18:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  2) Range#fizzbuzz works with tricky ranges
     Failure/Error: (10...17).fizzbuzz.should eq [:buzz, 11, :fizz, 13, 14, :fizzbuzz, 16]
       
       expected: [:buzz, 11, :fizz, 13, 14, :fizzbuzz, 16]
            got: [10, 11, :fizz, 13, 14, :fizzbuzz, 16]
       
       (compared using ==)
     # /tmp/d20130307-6960-1kvhecp/spec.rb:33:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  3) Array#densities maps each element to the number of occurences in the original array
     Failure/Error: [:a, :b, :c].densities.should eq [1, 1, 1]
     NoMethodError:
       undefined method `densities' for [:a, :b, :c]:Array
     # /tmp/d20130307-6960-1kvhecp/spec.rb:51:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  4) Array#densities maps each element to the number of occurences in the original array (again)
     Failure/Error: [].densities.should eq []
     NoMethodError:
       undefined method `densities' for []:Array
     # /tmp/d20130307-6960-1kvhecp/spec.rb:57:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.00884 seconds
8 examples, 4 failures

Failed examples:

rspec /tmp/d20130307-6960-1kvhecp/spec.rb:17 # Range#fizzbuzz it works with the first 100 numbers
rspec /tmp/d20130307-6960-1kvhecp/spec.rb:32 # Range#fizzbuzz works with tricky ranges
rspec /tmp/d20130307-6960-1kvhecp/spec.rb:50 # Array#densities maps each element to the number of occurences in the original array
rspec /tmp/d20130307-6960-1kvhecp/spec.rb:56 # Array#densities maps each element to the number of occurences in the original array (again)

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

Лъчезар обнови решението на 15.10.2012 14:24 (преди около 12 години)

+class Integer
+ def prime_divisors()
+ (1..self.abs).select { |a| self % a == 0 unless /^1?$|^(11+?)\1+$/.match '1' * a }
+ end
+end
+
+class Range
+ def fizzbuzz()
+ mapToValues = { 3 => :fizz, 4 => :buzz , 15 => :fizzbuzz }
+ self.map { |a| mapToValues[a.gcd 15] || a }
+ end
+end
+
+class Hash
+ def group_values
+ result = Hash.new { |hash, key| hash[key] = [] }
+ self.each { |key, value| result[value] << key }
+ result
+ end
+end

Браво Лъчка един от най стилните кодове. Личи си кои са старите кучета :D

идеята за fizzbuzz e адски добра и проста, само да беше написал вместо 4 петица :)

и искам да ми изведеш доказателство за това с регулярния израз :))

Не, не е то най-стилните кодове. Даже никак :)

  • Не спазваш конвенции - идентира се с два интервала.
  • Не се проверява дали едно число е просто по този начин. Да, кода е забавен. Но не е добър.
  • Това с НОД-то е хитро, но не е невероятно четимо. Май си допуснал и грешка.
  • mapToValues е неприемливо име в Ruby
  • hash[a] || b се записва по-ясно като hash.fetch a, b
  • self. е излишен.

Всички това отива на -1 точка.