Решение на Първа задача от Николай Вълчанов

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

Към профила на Николай Вълчанов

Резултати

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

Код

class Integer
def prime_divisors
result = []
prime = (1..self).select do |number|
count = 0
(1..number).each do |n|
count += 1 if number % n == 0
end
if count == 2
true
else
false
end
end
remainder = self
while remainder != 1
prime.each do |n|
if remainder % n == 0
result << n
remainder = remainder / n
break
end
end
end
result.uniq
end
end
class Range
def fizzbuzz
numbers = self.to_a
result = []
numbers.each do |n|
if n % 3 == 0 && n % 5 != 0
result << :fizz
elsif n % 5 == 0 && n % 3 != 0
result << :buzz
elsif n % 3 == 0 && n % 5 == 0
result << :fizzbuzz
else
result << n
end
end
result
end
end

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

.F..FFFF

Failures:

  1) Integer#prime_divisors works with negative numbers
     Failure/Error: (-10).prime_divisors.should eq [2, 5]
     Timeout::Error:
       execution expired
     # /tmp/d20130307-6960-gqnxy5/solution.rb:22:in `prime_divisors'
     # /tmp/d20130307-6960-gqnxy5/spec.rb:11: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) Hash#group_values maps each value to an array of keys
     Failure/Error: {a: 1}.group_values.should eq 1 => [:a]
     NoMethodError:
       undefined method `group_values' for {:a=>1}:Hash
     # /tmp/d20130307-6960-gqnxy5/spec.rb:41: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) Hash#group_values takes repetitions into account
     Failure/Error: {a: 1, b: 2, c: 1}.group_values.should eq 1 => [:a, :c], 2 => [:b]
     NoMethodError:
       undefined method `group_values' for {:a=>1, :b=>2, :c=>1}:Hash
     # /tmp/d20130307-6960-gqnxy5/spec.rb:45: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
     Failure/Error: [:a, :b, :c].densities.should eq [1, 1, 1]
     NoMethodError:
       undefined method `densities' for [:a, :b, :c]:Array
     # /tmp/d20130307-6960-gqnxy5/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)>'

  5) 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-gqnxy5/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 1.31 seconds
8 examples, 5 failures

Failed examples:

rspec /tmp/d20130307-6960-gqnxy5/spec.rb:10 # Integer#prime_divisors works with negative numbers
rspec /tmp/d20130307-6960-gqnxy5/spec.rb:40 # Hash#group_values maps each value to an array of keys
rspec /tmp/d20130307-6960-gqnxy5/spec.rb:44 # Hash#group_values takes repetitions into account
rspec /tmp/d20130307-6960-gqnxy5/spec.rb:50 # Array#densities maps each element to the number of occurences in the original array
rspec /tmp/d20130307-6960-gqnxy5/spec.rb:56 # Array#densities maps each element to the number of occurences in the original array (again)

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

Николай обнови решението на 15.10.2012 12:47 (преди над 11 години)

+class Integer
+ def prime_divisors
+ result = []
+
+ prime = (1..self).select do |number|
+ count = 0
+
+ (1..number).each do |n|
+ count += 1 if number % n == 0
+ end
+
+ if count == 2
+ true
+ else
+ false
+ end
+ end
+
+ remainder = self
+
+ while remainder != 1
+ prime.each do |n|
+ if remainder % n == 0
+ result << n
+ remainder = remainder / n
+ break
+ end
+ end
+ end
+
+ result.uniq
+ end
+end
+
+class Range
+ def fizzbuzz
+ numbers = self.to_a
+ result = []
+
+ numbers.each do |n|
+ if n % 3 == 0 && n % 5 != 0
+ result << :fizz
+ elsif n % 5 == 0 && n % 3 != 0
+ result << :buzz
+ elsif n % 3 == 0 && n % 5 == 0
+ result << :fizzbuzz
+ else
+ result << n
+ end
+ end
+
+ result
+ end
+end
  • Идентацията и употребата ти на whitespace не е наред и съгласно предупрежденията ни, ти отнемаме една точка за това; за справка виж ръководството за стил на курса
  • Шаблонът list = []; ...each { |x| list << x }; list се нарича map и трябва да ползваш него :) (например в Range#fizzbuzz)
  • self. е излишно на всички места, на които го ползваш; в такива случаи се изпуска по конвенция
  • Редове 6 до 10 може да ги напишеш, ползвайки варианта на Enumerable#count, който приема блок (ще го споменем на презентацията довечера, но може да си провериш документацията му и сам)
  • Редове 12 до 16 са по-известни като count == 2, което си връща boolean така или иначе... :)
  • Цикълът while в prime_divisors е по-добре да се напише пак със select
  • Range#fizzbuzz и Array#densities се получават много добре с map; допълнително, densities се прави на един ред, с този код: map { |item| count item } :)

Ако не си бил на последната сбирка, виж примерното решение, което сме дали в презентацията от тогава и също виж решенията, на които сме дали бонус точки.