Решение на Първа задача от Йоана Тодорова

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

Към профила на Йоана Тодорова

Резултати

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

Код

def prime_num?(num)
i, count = 2, 0
while i<=Math.sqrt(num) do
count+=1 if num%i==0
i+=1
end
count==0
end
class Integer
def prime_divisors
divisors, i = [], 2
while i<=self/2.0
divisors.push(i) if prime_num?(i) and self%i==0
i+=1
end
divisors.push(self) if prime_num?(self)
divisors
end
end
class Range
def fizzbuzz
array = []
self.each do |e|
case
when e%3==0
array.push(:fizz)
when e%5==0
array.push(:buzz)
when (e%5==0 and e%3==0)
array.push(:fizzbuzz)
else
array.push(e)
end
end
array
end
end
class Hash
def group_values
hash_rev = Hash.new
flag=false
for key, value in self
if hash_rev.empty?
hash_rev[value] = [key]
else
hash_rev.each do |key_rev,value_rev|
if value==key_rev
value_rev.push(key)
flag=true
end
end
hash_rev[value] = [key] if flag==false
end
end
hash_rev
end
end
class Array
def densities
array_copy = []
self.each {|e| array_copy.push(e)}
i=0
while (i<array_copy.count)
array_copy[i]=self.count(array_copy[i])
i+=1
end
array_copy
end
end

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

.FFF....

Failures:

  1) Integer#prime_divisors works with negative numbers
     Failure/Error: (-10).prime_divisors.should eq [2, 5]
     Math::DomainError:
       Numerical argument is out of domain - "sqrt"
     # /tmp/d20130307-6960-qejq1o/solution.rb:3:in `sqrt'
     # /tmp/d20130307-6960-qejq1o/solution.rb:3:in `prime_num?'
     # /tmp/d20130307-6960-qejq1o/solution.rb:17:in `prime_divisors'
     # /tmp/d20130307-6960-qejq1o/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) 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, 16, 17, :fizz, 19, :buzz, :fizz, 22, 23, :fizz, :buzz, 26, :fizz, 28, 29, :fizz, 31, 32, :fizz, 34, :buzz, :fizz, 37, 38, :fizz, :buzz, 41, :fizz, 43, 44, :fizz, 46, 47, :fizz, 49, :buzz, :fizz, 52, 53, :fizz, :buzz, 56, :fizz, 58, 59, :fizz, 61, 62, :fizz, 64, :buzz, :fizz, 67, 68, :fizz, :buzz, 71, :fizz, 73, 74, :fizz, 76, 77, :fizz, 79, :buzz, :fizz, 82, 83, :fizz, :buzz, 86, :fizz, 88, 89, :fizz, 91, 92, :fizz, 94, :buzz, :fizz, 97, 98, :fizz, :buzz]
       
       (compared using ==)
     # /tmp/d20130307-6960-qejq1o/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)>'

  3) 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, 16]
       
       (compared using ==)
     # /tmp/d20130307-6960-qejq1o/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.00947 seconds
8 examples, 3 failures

Failed examples:

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

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

Йоана обнови решението на 14.10.2012 22:45 (преди около 12 години)

+def prime_num?(num)
+ i, count = 2, 0
+ while i<=Math.sqrt(num) do
+ count+=1 if num%i==0
+ i+=1
+ end
+ count==0
+end
+
+class Integer
+ def prime_divisors
+ divisors, i = [], 2
+ while i<=self/2.0
+ divisors.push(i) if prime_num?(i) and self%i==0
+ i+=1
+ end
+ divisors.push(self) if prime_num?(self)
+ divisors
+ end
+end
+
+class Range
+ def fizzbuzz
+ array = []
+ self.each do |e|
+ case
+ when e%3==0
+ array.push(:fizz)
+ when e%5==0
+ array.push(:buzz)
+ when (e%5==0 and e%3==0)
+ array.push(:fizzbuzz)
+ else
+ array.push(e)
+ end
+
+ end
+ array
+ end
+end
+
+class Hash
+ def group_values
+ hash_rev = Hash.new
+ flag=false
+ for key, value in self
+ if hash_rev.empty?
+ hash_rev[value] = [key]
+ else
+ hash_rev.each do |key_rev,value_rev|
+ if value==key_rev
+ value_rev.push(key)
+ flag=true
+ end
+ end
+ hash_rev[value] = [key] if flag==false
+ end
+ end
+ hash_rev
+ end
+end
+
+class Array
+ def densities
+ array_copy = []
+ self.each {|e| array_copy.push(e)}
+ i=0
+ while (i<array_copy.count)
+ array_copy[i]=self.count(array_copy[i])
+ i+=1
+
+ end
+ array_copy
+ end
+end

Коментари:

  • Грубо не спазваш конвенциите. Ruby код не се идентира така, интервали се поставят на няколко места, на които не си поставила, ползваш for въпреки изричното "не го правете" на лекции.
  • Имаш monkey patch върху Object с този prime_num?. Това е неприемливо.
  • Това не е Ruby код, а Java/C код в кожата на Ruby. Има един тон възможности да ползваш функционални идиоми, които си изпуснала.
  • Добре е да ползваш while цикъл само и единствено, когато не знаеш колкто итерации е нужно да бъдат направени. В твоята програма няма нужда от while цикли.
  • Не се пише Hash.new, а {}.
  • Въобще, има ред Ruby идиоми, които сме показали, но не прилагаш. map, select и all? например.