Решение на Четвърта задача от Лилия Любенова

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

Към профила на Лилия Любенова

Резултати

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

Код

module Asm
class Instruction
attr_accessor :memory
def initialize
@memory = {ax: 0, bx: 0, cx: 0, dx: 0}
end
def method_missing(name, *args)
case name
when :ax then :ax
when :bx then :bx
when :cx then :cx
when :dx then :dx
end
end
def mov (destination_register, source)
evaluate(:mov, destination_register, source)
end
def inc (destination_register, value = 1)
evaluate(:inc, destination_register, value)
end
def dec (destination_register, value = 1)
evaluate(:dec, destination_register, value)
end
def cmp (register, value)
evaluate(:cmp, register, value)
end
def evaluate(instruction_name, register, value)
value = @memory[value] unless value.is_a? Integer
case instruction_name
when :mov then @memory[register] = value
when :inc then @memory[register] += value
when :dec then @memory[register] -= value
when :cmp then @memory[register] <=> value
end
@memory.values
end
end
def self.asm(&block)
Instruction.new.instance_eval &block
end
end

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

F...FFFF

Failures:

  1) Asm.asm works with empty programs
     Failure/Error: Asm.asm {}.should eq [0, 0, 0, 0]
       
       expected: [0, 0, 0, 0]
            got: nil
       
       (compared using ==)
     # /tmp/d20140115-8451-1i9zitu/spec.rb:3:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  2) Asm.asm implements CMP
     Failure/Error: Asm.asm do
       
       expected: [2, 2, 0, 3]
            got: [2, 2, 3, 3]
       
       (compared using ==)
     # /tmp/d20140115-8451-1i9zitu/spec.rb:34:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  3) Asm.asm implements JMP
     Failure/Error: Asm.asm do
       
       expected: [0, 0, 1, 1]
            got: [1, 0, 1, 1]
       
       (compared using ==)
     # /tmp/d20140115-8451-1i9zitu/spec.rb:68:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  4) Asm.asm implements LABEL
     Failure/Error: Asm.asm do
       
       expected: [1, 1, 0, 0]
            got: [1, 1, 1, 0]
       
       (compared using ==)
     # /tmp/d20140115-8451-1i9zitu/spec.rb:78:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  5) Asm.asm can be used to find GCD of two numbers
     Failure/Error: Asm.asm do
       
       expected: [8, 8, 0, 0]
            got: nil
       
       (compared using ==)
     # /tmp/d20140115-8451-1i9zitu/spec.rb:89:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.0078 seconds
8 examples, 5 failures

Failed examples:

rspec /tmp/d20140115-8451-1i9zitu/spec.rb:2 # Asm.asm works with empty programs
rspec /tmp/d20140115-8451-1i9zitu/spec.rb:33 # Asm.asm implements CMP
rspec /tmp/d20140115-8451-1i9zitu/spec.rb:67 # Asm.asm implements JMP
rspec /tmp/d20140115-8451-1i9zitu/spec.rb:77 # Asm.asm implements LABEL
rspec /tmp/d20140115-8451-1i9zitu/spec.rb:88 # Asm.asm can be used to find GCD of two numbers

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

Лилия обнови решението на 15.01.2014 17:26 (преди почти 11 години)

+module Asm
+ class Instruction
+ attr_accessor :memory
+
+ def initialize
+ @memory = {ax: 0, bx: 0, cx: 0, dx: 0}
+ end
+
+ def method_missing(name, *args)
+ case name
+ when :ax then :ax
+ when :bx then :bx
+ when :cx then :cx
+ when :dx then :dx
+ end
+ end
+
+ def mov (destination_register, source)
+ evaluate(:mov, destination_register, source)
+ end
+
+ def inc (destination_register, value = 1)
+ evaluate(:inc, destination_register, value)
+ end
+
+ def dec (destination_register, value = 1)
+ evaluate(:dec, destination_register, value)
+ end
+
+ def cmp (register, value)
+ evaluate(:cmp, register, value)
+ end
+
+ def evaluate(instruction_name, register, value)
+ value = @memory[value] unless value.is_a? Integer
+ case instruction_name
+ when :mov then @memory[register] = value
+ when :inc then @memory[register] += value
+ when :dec then @memory[register] -= value
+ when :cmp then @memory[register] <=> value
+ end
+ @memory.values
+ end
+ end
+
+ def self.asm(&block)
+ p Instruction.new.instance_eval &block
+ end
+end

Лилия обнови решението на 15.01.2014 17:29 (преди почти 11 години)

module Asm
class Instruction
attr_accessor :memory
def initialize
@memory = {ax: 0, bx: 0, cx: 0, dx: 0}
end
def method_missing(name, *args)
case name
when :ax then :ax
when :bx then :bx
when :cx then :cx
when :dx then :dx
end
end
def mov (destination_register, source)
evaluate(:mov, destination_register, source)
end
def inc (destination_register, value = 1)
evaluate(:inc, destination_register, value)
end
def dec (destination_register, value = 1)
evaluate(:dec, destination_register, value)
end
def cmp (register, value)
evaluate(:cmp, register, value)
end
def evaluate(instruction_name, register, value)
value = @memory[value] unless value.is_a? Integer
case instruction_name
when :mov then @memory[register] = value
when :inc then @memory[register] += value
when :dec then @memory[register] -= value
when :cmp then @memory[register] <=> value
end
@memory.values
end
end
def self.asm(&block)
- p Instruction.new.instance_eval &block
+ Instruction.new.instance_eval &block
end
end

Не трябва да оставяш интервал преди отварящата скоба при дефиниция на метод: mov (destination_register, source). Освен това го правиш неконсистентно – някъде си оставяла, другаде – не. Неконсистентността е още по-лошо нещо.

От друга страна, останалият код, макар и непълен, е много добре подреден, затова ще ти се разминат наказателните точки :)