Решение на Първа задача от Николай Стоицев

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

Към профила на Николай Стоицев

Резултати

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

Код

class Array
def to_hash
result = {}
for sub_array in self do
result[sub_array[0]] = sub_array[1]
end
result
end
def subarray_count subarray
occurances = 0
for j in (0..self.size) do
if self[j] == subarray[0]
count = 1
for i in (1..subarray.size) do
if self[j + i] == subarray[i]
count += 1
end
end
if count == subarray.size
occurances += 1
end
end
end
occurances
end
def index_by
result = {}
for element in self do
splited = yield element
result[splited] = element
end
result
end
def occurences_count
count_table = {}
for element in self do
if count_table[element] == nil
count_table[element] = 1
elsif
count_table[element] += 1;
end
end
count_table
end
end

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

..........F.F.F.F

Failures:

  1) Array#subarray_count(subarray) works with arrays with non-numeric keys
     Failure/Error: %w[a b c b c].subarray_count(%w[b c]).should eq 2
       
       expected 2
            got 1
       
       (compared using ==)
     # /tmp/d20111025-2903-5yr3aa/spec.rb:55: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#subarray_count(subarray) work when the argument is larger than the array
     Failure/Error: [1].subarray_count([1, 2]).should eq 0
       
       expected 0
            got 1
       
       (compared using ==)
     # /tmp/d20111025-2903-5yr3aa/spec.rb:63: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#occurences_count returns a hash that defaults to 0 when the key is not present
     Failure/Error: [].occurences_count[:something].should eq 0
       
       expected 0
            got nil
       
       (compared using ==)
     # /tmp/d20111025-2903-5yr3aa/spec.rb:74: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#occurences_count returns a hash, that does not change when indexed with a non-occuring element
     Failure/Error: hash['b'].should eq 0
       
       expected 0
            got nil
       
       (compared using ==)
     # /tmp/d20111025-2903-5yr3aa/spec.rb:84: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.01539 seconds
17 examples, 4 failures

Failed examples:

rspec /tmp/d20111025-2903-5yr3aa/spec.rb:54 # Array#subarray_count(subarray) works with arrays with non-numeric keys
rspec /tmp/d20111025-2903-5yr3aa/spec.rb:62 # Array#subarray_count(subarray) work when the argument is larger than the array
rspec /tmp/d20111025-2903-5yr3aa/spec.rb:73 # Array#occurences_count returns a hash that defaults to 0 when the key is not present
rspec /tmp/d20111025-2903-5yr3aa/spec.rb:81 # Array#occurences_count returns a hash, that does not change when indexed with a non-occuring element

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

Николай обнови решението на 24.10.2011 02:12 (преди над 12 години)

+class Array
+
+ def to_hash
+ result = {}
+ for sub_array in self do
+ result[sub_array[0]] = sub_array[1]
+ end
+ result
+ end
+
+ def subarray_count subarray
+ occurances = 0
+ for j in (0..self.size) do
+ if self[j] == subarray[0]
+ count = 1
+ for i in (1..subarray.size) do
+ if self[j + i] == subarray[i]
+ count += 1
+ end
+ end
+ if count == subarray.size
+ occurances += 1
+ end
+ end
+ end
+ occurances
+ end
+
+ def index_by
+ result = {}
+ for element in self do
+ splited = yield element
+ result[splited] = element
+ end
+ result
+ end
+
+ def occurences_count
+ count_table = {}
+ for element in self do
+ if count_table[element] == nil
+ count_table[element] = 1
+ elsif
+ count_table[element] += 1;
+ end
+ end
+ count_table
+ end
+
+end