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

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

Към профила на Мая Недялкова

Резултати

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

Код

class Array
def hash
self.flatten(1)
c=Hash[self]
return c
end
def index_by
c=Hash.new()
for i in 0..(self.length-1) do
c[yield(self[i])] = self[i]
end
return c
end
def subarray_count(subarray)
count=0
for i in 0..(self.length-1) do
if self[i]==subarray[0]
for j in 1..(subarray.length-1) do
if self[i+j]!=subarray[j]
break
elsif j==(subarray.length-1)
count+=1
end
end
end
end
return count
end
def occurences_count
h=Hash.new(0)
for i in 0..(self.length-1) do
h[self[i]]=self.count{|n| n==self[i]}
end
return h
end
end

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

FFFFF............

Failures:

  1) Array#to_hash converts an array to a hash
     Failure/Error: [[:one, 1], [:two, 2]].to_hash.should eq(one: 1, two: 2)
     NoMethodError:
       undefined method `to_hash' for [[:one, 1], [:two, 2]]:Array
     # /tmp/d20111025-2903-1kzev0b/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) Array#to_hash uses the last pair when a key is present multiple times
     Failure/Error: [[1, 2], [3, 4], [1, 3]].to_hash.should eq(1 => 3, 3 => 4)
     NoMethodError:
       undefined method `to_hash' for [[1, 2], [3, 4], [1, 3]]:Array
     # /tmp/d20111025-2903-1kzev0b/spec.rb:7: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) Array#to_hash works when the keys and values are arrays
     Failure/Error: [[1, [2, 3]], [[4, 5], 6]].to_hash.should eq(1 => [2, 3], [4, 5] => 6)
     NoMethodError:
       undefined method `to_hash' for [[1, [2, 3]], [[4, 5], 6]]:Array
     # /tmp/d20111025-2903-1kzev0b/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)>'

  4) Array#to_hash works on empty arrays
     Failure/Error: [].to_hash.should eq({})
     NoMethodError:
       undefined method `to_hash' for []:Array
     # /tmp/d20111025-2903-1kzev0b/spec.rb:15: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) Array#to_hash works with boolean keys
     Failure/Error: [nil, 'nil'],
     NoMethodError:
       undefined method `to_hash' for [[nil, "nil"], [true, "true"], [false, "false"]]:Array
     # /tmp/d20111025-2903-1kzev0b/spec.rb:20: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.01306 seconds
17 examples, 5 failures

Failed examples:

rspec /tmp/d20111025-2903-1kzev0b/spec.rb:2 # Array#to_hash converts an array to a hash
rspec /tmp/d20111025-2903-1kzev0b/spec.rb:6 # Array#to_hash uses the last pair when a key is present multiple times
rspec /tmp/d20111025-2903-1kzev0b/spec.rb:10 # Array#to_hash works when the keys and values are arrays
rspec /tmp/d20111025-2903-1kzev0b/spec.rb:14 # Array#to_hash works on empty arrays
rspec /tmp/d20111025-2903-1kzev0b/spec.rb:18 # Array#to_hash works with boolean keys

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

Мая обнови решението на 23.10.2011 22:51 (преди около 13 години)

+class Array
+ def hash
+ self.flatten(1)
+ c=Hash[self]
+ return c
+ end
+
+ def index_by
+ c=Hash.new()
+ for i in 0..(self.length-1) do
+ c[yield(self[i])] = self[i]
+ end
+ return c
+ end
+
+ def subarray_count(subarray)
+ count=0
+ for i in 0..(self.length-1) do
+ if self[i]==subarray[0]
+ for j in 1..(subarray.length-1) do
+ if self[i+j]!=subarray[j]
+ break
+ elsif j==(subarray.length-1)
+ count+=1
+ end
+ end
+ end
+ end
+ return count
+ end
+
+ def occurences_count
+ h=Hash.new(0)
+ for i in 0..(self.length-1) do
+ h[self[i]]=self.count{|n| n==self[i]}
+ end
+ return h
+ end
+end