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

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

Към профила на Адриан Кателиев

Резултати

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

Код

class Integer
def prime_divisors
num=self.abs
if ( (num!=1) and (num!=0) )
results=[]
(2..num).each do |n|
if (num%n==0)
num=(num/n)
results.push(n)
end
end
print results
else
puts 'This Method is not defined for 0 1 and -1'
end
end
end
#-1.prime_divisors
class Range
def fizzbuzz
results=[];
if !(self.exclude_end?)
self.each do |n|
if ( (n%3==0) and (n%5==0))
results.push(:fizzbuzz);
elsif(n%3==0)
results.push(:fizz)
elsif(n%5==0)
results.push(:buzz);
else
results.push(n);
end
end
print results
else
puts 'This method is defined only for double dots (..)';
end
end
end
# (1...27).fizzbuzz
class Hash
def group_values
result = Hash.new("")
self.each_pair do | key, value |
if (result.has_key?(value))
result[value].push(key)
else
result[value]=[key]
end
end
print result
end
end
#{a: 1, b: 2, c: 1}.group_values
class Array
def densities
results=Array.new
counter = Hash.new()
self.each do |n|
if (counter.has_key?(n))
counter[n] += 1
else
counter[n]=1
end
end
self.each do |n|
results.push(counter[n])
end
print results
end
end
#[:a, :b, :c].densities
#[:a, :a, :a].densities
#[:a, :b, :a].densities

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

[2, 5]F[2, 5]F[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]FThis method is defined only for double dots (..)
F{1=>[:a]}F{1=>[:a, :c], 2=>[:b]}F[1, 1, 1]F[]F

Failures:

  1) Integer#prime_divisors can partition a simple number
     Failure/Error: 10.prime_divisors.should eq [2, 5]
       
       expected: [2, 5]
            got: nil
       
       (compared using ==)
     # /tmp/d20130307-6960-i5x3pf/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]
       
       expected: [2, 5]
            got: nil
       
       (compared using ==)
     # /tmp/d20130307-6960-i5x3pf/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-i5x3pf/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-i5x3pf/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]
       
       expected: {1=>[:a]}
            got: nil
       
       (compared using ==)
     # /tmp/d20130307-6960-i5x3pf/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]
       
       expected: {1=>[:a, :c], 2=>[:b]}
            got: nil
       
       (compared using ==)
     # /tmp/d20130307-6960-i5x3pf/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]
       
       expected: [1, 1, 1]
            got: nil
       
       (compared using ==)
     # /tmp/d20130307-6960-i5x3pf/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 []
       
       expected: []
            got: nil
       
       (compared using ==)
     # /tmp/d20130307-6960-i5x3pf/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.00946 seconds
8 examples, 8 failures

Failed examples:

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

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

Адриан обнови решението на 15.10.2012 01:18 (преди около 12 години)

+class Integer
+ def prime_divisors
+ num=self.abs
+ if ( (num!=1) and (num!=0) )
+ results=[]
+ (2..num).each do |n|
+ if (num%n==0)
+ num=(num/n)
+ results.push(n)
+ end
+ end
+ print results
+ else
+ puts 'This Method is not defined for 0 1 and -1'
+ end
+ end
+end
+#-1.prime_divisors
+
+class Range
+ def fizzbuzz
+ results=[];
+ if !(self.exclude_end?)
+ self.each do |n|
+ if ( (n%3==0) and (n%5==0))
+ results.push(:fizzbuzz);
+ elsif(n%3==0)
+ results.push(:fizz)
+ elsif(n%5==0)
+ results.push(:buzz);
+ else
+ results.push(n);
+ end
+ end
+ print results
+ else
+ puts 'This method is defined only for double dots (..)';
+ end
+ end
+end
+# (1...27).fizzbuzz
+
+class Hash
+ def group_values
+ result = Hash.new("")
+ self.each_pair do | key, value |
+ if (result.has_key?(value))
+ result[value].push(key)
+ else
+ result[value]=[key]
+ end
+ end
+ print result
+ end
+end
+#{a: 1, b: 2, c: 1}.group_values
+
+class Array
+ def densities
+ results=Array.new
+ counter = Hash.new()
+ self.each do |n|
+ if (counter.has_key?(n))
+ counter[n] += 1
+ else
+ counter[n]=1
+ end
+ end
+ self.each do |n|
+ results.push(counter[n])
+ end
+ print results
+ end
+end
+#[:a, :b, :c].densities
+#[:a, :a, :a].densities
+#[:a, :b, :a].densities
  • Идентацията и употребата ти на whitespace не е наред и съгласно предупрежденията ни, ти отнемаме една точка за това; за справка виж ръководството за стил на курса
  • Предпочитай да ползваш Array#<< пред Array#push за добавяне на елементи в списък; т.е. some_list << foo
  • fizzbuzz и densities се получават много добре с map; допълнително, densities се прави на един ред, с този код: map { |item| count item } :)
  • Това на 45-ти ред нямам никаква идея защо го правиш: result = Hash.new("") — за какво ти е хеш с дифолтна стойност на липсващи ключове празен низ?
  • Тук скобите около условието са абсолютно излишни и се изпускат: if (result.has_key?(value))
  • Array.new, Hash.new и т.н. никога не се ползват в този си вид (без аргументи); винаги се предпочита литералния синтаксис в тези случаи, т.е. [], {} и прочее
  • self. е излишно на всички места, на които го ползваш; в такива случаи се изпуска по конвенция
  • Избягвай да ползваш метода puts по този начин, по който си го ползвал (за грешки); за тази цел има изключения, за които ще говорим по-натам; в случая, условието не изискваше да покривате тези случаи и работата, която си свършил, е излишна (да не говорим, че не се прави така). Принципно, добре е да се стремиш да не извършваш излишна работа, тъй като повече код = повече проблеми във всяко едно отношение :)
  • Обърни внимание на решенията на твои колеги, на които сме дали бонус точки, както и на примерното решение, което ние показахме на тази задача и ако имаш въпроси, ни пиши