Решение на Първа задача от Борислава Аладжова

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

Към профила на Борислава Аладжова

Резултати

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

Код

#зад1
def is_prime(number)
factor = 1
while not number == 1
factor+=1
while number%factor == 0 do
number = number/factor
end
end
end
class Integer
def prime_divisiors
factors = []
if is_prime(self)
factors.push(self)
end
divisior = 2
while divisior <= self/2.0
if is_prime(divisior) and self%divisior==0
factors.push(divisior)
end
divisior+=1
end
print factor
end
end
#зад2
class Range
def fizzbuzz
temp = []
self.each do |element|
if element%3==0
temp.push(:fizz)
end
if element%5==0
temp.push(:buzz)
end
if element%3==0 and element%5==0
temp.push(:fizzbuzz)
end
if not element%3==0 and not element%5==0
temp.push(element)
end
end
print temp
end
end
(1..15).fizzbuzz

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

[1, 2, :fizz, 4, :buzz, :fizz, 7, 8, :fizz, :buzz, 11, :fizz, 13, 14, :fizz, :buzz, :fizzbuzz]FF[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]F[:buzz, 11, :fizz, 13, 14, :fizz, :buzz, :fizzbuzz, 16]FFFFF

Failures:

  1) Integer#prime_divisors can partition a simple number
     Failure/Error: 10.prime_divisors.should eq [2, 5]
     NoMethodError:
       undefined method `prime_divisors' for 10:Fixnum
     # /tmp/d20130307-6960-po9936/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]
     NoMethodError:
       undefined method `prime_divisors' for -10:Fixnum
     # /tmp/d20130307-6960-po9936/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 [
       
       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: nil
       
       (compared using ==)
     # /tmp/d20130307-6960-po9936/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]
       
       expected: [:buzz, 11, :fizz, 13, 14, :fizzbuzz, 16]
            got: nil
       
       (compared using ==)
     # /tmp/d20130307-6960-po9936/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-po9936/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-po9936/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-po9936/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-po9936/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.00849 seconds
8 examples, 8 failures

Failed examples:

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

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

Борислава обнови решението на 15.10.2012 09:38 (преди около 12 години)

+#зад1
+def is_prime(number)
+ factor = 1
+ while not number == 1
+ factor+=1
+ while number%factor == 0 do
+ number = number/factor
+ end
+ end
+end
+
+class Integer
+ def prime_divisiors
+ factors = []
+ if is_prime(self)
+ factors.push(self)
+ end
+ divisior = 2
+ while divisior <= self/2.0
+ if is_prime(divisior) and self%divisior==0
+ factors.push(divisior)
+ end
+ divisior+=1
+ end
+ print factor
+ end
+end
+
+
+
+
+#зад2
+class Range
+ def fizzbuzz
+ temp = []
+ self.each do |element|
+ if element%3==0
+ temp.push(:fizz)
+ end
+ if element%5==0
+ temp.push(:buzz)
+ end
+ if element%3==0 and element%5==0
+ temp.push(:fizzbuzz)
+ end
+ if not element%3==0 and not element%5==0
+ temp.push(element)
+ end
+ end
+ print temp
+ end
+end
+
+(1..15).fizzbuzz
  • Методът is_prime (щеше да е по-добре да се казва prime?, между другото) не е добре да се намира в "глобалното" пространство; за това ще взимаме точки от следващи задачи; алтернативата е да го сложиш като метод в Integer, или да си направиш един собствен клас, който да държи този код
  • За някои променливи си избрала кофти имена; temp е възможно най-лошото име на променлива (вероятно вече знаеш това, ако си била на последната лекция)
  • self. е излишно на всички места, на които го ползваш; в такива случаи се изпуска по конвенция
  • Предпочитай да ползваш Array#<< пред Array#push за добавяне на елементи в списък; т.е. some_list << foo
  • Не спазваш съвсем конвенциите за поставяне на whitespace, по-специално около "оператори" от типа на %, == и други; за справка виж ръководството за стил на курса, защото занапред ще взимаме точки за това
  • fizzbuzz и densities се получават много добре с map; допълнително, densities се прави на един ред, с този код: map { |item| count item } :)
  • 54-ти ред не е трябвало да го оставяш в това, което си пратила

Разгледай примерното решение, което сме дали в последната презентация и виж решенията на твои колеги, на които сме дали бонус точки. Ако имаш някакви въпроси, пиши ни на fmi@ruby.bg (или питай във форумите).