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

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

Към профила на Магдалена Патронска

Резултати

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

Код

# Integer#prime_divisors
class Integer
def prime_divisors(num)
def prime?(num)
return false if (num < 2)
2.upto(Math.sqrt(num)) {|i| return false if (num % i == 0)}
return true
end
return (2..num.abs).select {|i| i if (num % i == 0 && prime?(i))}
end
end

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

FFFFFFFF

Failures:

  1) Integer#prime_divisors can partition a simple number
     Failure/Error: 10.prime_divisors.should eq [2, 5]
     ArgumentError:
       wrong number of arguments (0 for 1)
     # /tmp/d20130307-6960-1p86qk0/solution.rb:3:in `prime_divisors'
     # /tmp/d20130307-6960-1p86qk0/spec.rb:3: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) Integer#prime_divisors works with negative numbers
     Failure/Error: (-10).prime_divisors.should eq [2, 5]
     ArgumentError:
       wrong number of arguments (0 for 1)
     # /tmp/d20130307-6960-1p86qk0/solution.rb:3:in `prime_divisors'
     # /tmp/d20130307-6960-1p86qk0/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)>'

  3) Range#fizzbuzz it works with the first 100 numbers
     Failure/Error: (1..100).fizzbuzz.should eq [
     NoMethodError:
       undefined method `fizzbuzz' for 1..100:Range
     # /tmp/d20130307-6960-1p86qk0/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)>'

  4) Range#fizzbuzz works with tricky ranges
     Failure/Error: (10...17).fizzbuzz.should eq [:buzz, 11, :fizz, 13, 14, :fizzbuzz, 16]
     NoMethodError:
       undefined method `fizzbuzz' for 10...17:Range
     # /tmp/d20130307-6960-1p86qk0/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)>'

  5) 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-1p86qk0/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)>'

  6) 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-1p86qk0/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)>'

  7) 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-1p86qk0/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)>'

  8) 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-1p86qk0/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.00594 seconds
8 examples, 8 failures

Failed examples:

rspec /tmp/d20130307-6960-1p86qk0/spec.rb:2 # Integer#prime_divisors can partition a simple number
rspec /tmp/d20130307-6960-1p86qk0/spec.rb:10 # Integer#prime_divisors works with negative numbers
rspec /tmp/d20130307-6960-1p86qk0/spec.rb:17 # Range#fizzbuzz it works with the first 100 numbers
rspec /tmp/d20130307-6960-1p86qk0/spec.rb:32 # Range#fizzbuzz works with tricky ranges
rspec /tmp/d20130307-6960-1p86qk0/spec.rb:40 # Hash#group_values maps each value to an array of keys
rspec /tmp/d20130307-6960-1p86qk0/spec.rb:44 # Hash#group_values takes repetitions into account
rspec /tmp/d20130307-6960-1p86qk0/spec.rb:50 # Array#densities maps each element to the number of occurences in the original array
rspec /tmp/d20130307-6960-1p86qk0/spec.rb:56 # Array#densities maps each element to the number of occurences in the original array (again)

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

Магдалена обнови решението на 15.10.2012 07:46 (преди около 12 години)

+# Integer#prime_divisors
+class Integer
+ def prime_divisors(num)
+ def prime?(num)
+ return false if (num < 2)
+ 2.upto(Math.sqrt(num)) {|i| return false if (num % i == 0)}
+ return true
+ end
+ return (2..num.abs).select {|i| i if (num % i == 0 && prime?(i))}
+ end
+end
  • Идентацията и употребата ти на whitespace не е наред и съгласно предупрежденията ни, ти отнемаме една точка за това; за справка виж ръководството за стил на курса
  • Не дефинирай методи в методи, това не е Lisp, JavaScript или друг език, в който тези неща биха имали друг ефект; тук стават странни неща и това не се прави (затова и не сме го показвали); prime? можеш да го изведеш като редови метод в Integer в случая
  • Скобите около условието на if-а на ред 5 (а и на следващите редове) са ненужни
  • return пред последния израз в метод е излишен и се изпуска; това важи за ред 7 и 9

Ако Ruby-мисленето ти е странно и запънеш някъде на следващото домашно, ни пиши на fmi@ruby.bg за някой друг hint.