Решение на Първа задача от Иван Георгиев

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

Към профила на Иван Георгиев

Резултати

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

Код

class Integer
def prime_divisors
def is_prime(n)
2.upto(n-1) do |x|
if n % x ==0 then return false
end
end
return true
end
help_var = self
if help_var<0 then help_var = help_var*(-1)
end
list=[]
2.upto(help_var) do |i|
if help_var % i == 0 and is_prime(i) then list<<i
end
end
list
end
end
class Range
def fizzbuzz
help_range = self
list = []
help_range.each do |i|
if i % 3 == 0 then list<< :fizz
end
if i % 5 == 0 then list<< :buzz
end
if i % 3 == 0 and i % 5 == 0 then list<< :fizzbuzz
end
if not(i % 3 == 0) and not(i % 5 == 0) then list<< i
end
end
list
end
end
class Hash
def group_values
help_hash = self
new_hash = Hash.new
list_values = []
help_hash.each_value do |x|
if not(list_values.include?(x)) then list_values<<x
end
end
list_values.each do |i|
list=[]
help_hash.each_pair do |k, v|
if v == i then list<<k
end
end
new_hash[i] = list
end
new_hash
end
end
class Array
def densities
help_arr = self
list = []
help_arr.each do |x|
list<< help_arr.count(x)
end
list
end
end

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

..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, :buzz, :fizz, 7, 8, :fizz, :buzz, 11, :fizz, 13, 14, :fizz, :buzz, :fizzbuzz, 16, 17, :fizz, 19, :buzz, :fizz, 22, 23, :fizz, :buzz, 26, :fizz, 28, 29, :fizz, :buzz, :fizzbuzz, 31, 32, :fizz, 34, :buzz, :fizz, 37, 38, :fizz, :buzz, 41, :fizz, 43, 44, :fizz, :buzz, :fizzbuzz, 46, 47, :fizz, 49, :buzz, :fizz, 52, 53, :fizz, :buzz, 56, :fizz, 58, 59, :fizz, :buzz, :fizzbuzz, 61, 62, :fizz, 64, :buzz, :fizz, 67, 68, :fizz, :buzz, 71, :fizz, 73, 74, :fizz, :buzz, :fizzbuzz, 76, 77, :fizz, 79, :buzz, :fizz, 82, 83, :fizz, :buzz, 86, :fizz, 88, 89, :fizz, :buzz, :fizzbuzz, 91, 92, :fizz, 94, :buzz, :fizz, 97, 98, :fizz, :buzz]
       
       (compared using ==)
     # /tmp/d20130307-6960-zbkoto/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: [:buzz, 11, :fizz, 13, 14, :fizz, :buzz, :fizzbuzz, 16]
       
       (compared using ==)
     # /tmp/d20130307-6960-zbkoto/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)>'

Finished in 0.00795 seconds
8 examples, 2 failures

Failed examples:

rspec /tmp/d20130307-6960-zbkoto/spec.rb:17 # Range#fizzbuzz it works with the first 100 numbers
rspec /tmp/d20130307-6960-zbkoto/spec.rb:32 # Range#fizzbuzz works with tricky ranges

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

Иван обнови решението на 14.10.2012 14:52 (преди около 12 години)

+class Integer
+ def prime_divisors
+
+ def is_prime(n)
+ 2.upto(n-1) do |x|
+ if n % x ==0 then return false
+ end
+ end
+ return true
+ end
+
+ help_var = self
+
+ if help_var<0 then help_var = help_var*(-1)
+ end
+
+ list=[]
+
+ 2.upto(help_var) do |i|
+ if help_var % i == 0 and is_prime(i) then list<<i
+ end
+ end
+
+ list
+
+ end
+end
+
+class Range
+ def fizzbuzz
+
+ help_range = self
+ list = []
+
+ help_range.each do |i|
+ if i % 3 == 0 then list<< :fizz
+ end
+ if i % 5 == 0 then list<< :buzz
+ end
+ if i % 3 == 0 and i % 5 == 0 then list<< :fizzbuzz
+ end
+ if not(i % 3 == 0) and not(i % 5 == 0) then list<< i
+ end
+ end
+
+ list
+
+ end
+end
+
+class Hash
+ def group_values
+
+ help_hash = self
+ new_hash = Hash.new
+ list_values = []
+
+ help_hash.each_value do |x|
+ if not(list_values.include?(x)) then list_values<<x
+ end
+ end
+
+ list_values.each do |i|
+ list=[]
+ help_hash.each_pair do |k, v|
+ if v == i then list<<k
+ end
+ end
+ new_hash[i] = list
+ end
+
+ new_hash
+
+ end
+end
+
+class Array
+ def densities
+
+ help_arr = self
+ list = []
+
+ help_arr.each do |x|
+ list<< help_arr.count(x)
+ end
+
+ list
+
+ end
+end
  • help_var е едно от най-лошите имена на променливи, които съм виждал в тази задача.
  • is_prime не е име на метод в Ruby. Пише се prime?.
  • help_var*(-1) се записва като -help_var. С изключение на името, разбира се.
  • Подравняването ти е ужасно. Моля погледни style guide-а.
  • В Ruby методи на могат да се влагат.

Всичко това ще ти струва 1 точка.