Решение на Четвърта задача от Милен Мавров

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

Към профила на Милен Мавров

Резултати

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

Код

module Asm
def self.asm(&block)
instructions = ArithmeticInstructions.new
instructions.instance_eval &block
p instructions.registers.values
end
class ArithmeticInstructions
attr_reader :ax, :bx, :cx, :dx
attr_reader :registers
def initialize
@operations_queue = []
@registers = {
ax: 0,
bx: 0,
cx: 0,
dx: 0,
}
@ax, @bx = :ax, :bx
@cx, @dx = :cx, :dx
end
def mov(register, value)
if value.is_a? Fixnum
@registers[register.to_sym] = value
else
@registers[register.to_sym] = @registers[value.to_sym]
end
@operations_queue << ['mov', [register.to_sym, value]]
end
def inc(register, value)
if value.is_a? Fixnum
@registers[register.to_sym] += value
else
@registers[register.to_sym] += @registers[value.to_sym]
end
@operations_queue << ['inc', [register.to_sym, value]]
end
def dec(register, value)
if value.is_a? Fixnum
@registers[register.to_sym] -= value
else
@registers[register.to_sym] -= @registers[value.to_sym]
end
@operations_queue << ['dec', [register.to_sym, value]]
end
def cmp(register, value)
if value.is_a? Fixnum
@last_cmp_result = @registers[register.to_sym] <=> value
else
@last_cmp_result = @registers[register.to_sym] <=> @registers[value.to_sym]
end
@operations_queue << ['cmp', [register.to_sym, value]]
end
def label(value)
@operations_queue << ['label', value]
end
def method_missing(method_name)
method_name
end
end
end

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

[0, 0, 0, 0]
.[3, 4, 3, 0]
.FFFFFF

Failures:

  1) Asm.asm implements INC
     Failure/Error: inc ax
     ArgumentError:
       wrong number of arguments (1 for 2)
     # /tmp/d20140115-8451-f87ktw/solution.rb:33:in `inc'
     # /tmp/d20140115-8451-f87ktw/spec.rb:16:in `block (3 levels) in <top (required)>'
     # /tmp/d20140115-8451-f87ktw/solution.rb:4:in `instance_eval'
     # /tmp/d20140115-8451-f87ktw/solution.rb:4:in `asm'
     # /tmp/d20140115-8451-f87ktw/spec.rb:15: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 DEC
     Failure/Error: dec ax
     ArgumentError:
       wrong number of arguments (1 for 2)
     # /tmp/d20140115-8451-f87ktw/solution.rb:42:in `dec'
     # /tmp/d20140115-8451-f87ktw/spec.rb:25:in `block (3 levels) in <top (required)>'
     # /tmp/d20140115-8451-f87ktw/solution.rb:4:in `instance_eval'
     # /tmp/d20140115-8451-f87ktw/solution.rb:4:in `asm'
     # /tmp/d20140115-8451-f87ktw/spec.rb:23: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 CMP
     Failure/Error: je 5
     ArgumentError:
       wrong number of arguments (2 for 1)
     # /tmp/d20140115-8451-f87ktw/solution.rb:64:in `method_missing'
     # /tmp/d20140115-8451-f87ktw/spec.rb:38:in `block (3 levels) in <top (required)>'
     # /tmp/d20140115-8451-f87ktw/solution.rb:4:in `instance_eval'
     # /tmp/d20140115-8451-f87ktw/solution.rb:4:in `asm'
     # /tmp/d20140115-8451-f87ktw/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)>'

  4) Asm.asm implements JMP
     Failure/Error: jmp l1
     ArgumentError:
       wrong number of arguments (2 for 1)
     # /tmp/d20140115-8451-f87ktw/solution.rb:64:in `method_missing'
     # /tmp/d20140115-8451-f87ktw/spec.rb:70:in `block (3 levels) in <top (required)>'
     # /tmp/d20140115-8451-f87ktw/solution.rb:4:in `instance_eval'
     # /tmp/d20140115-8451-f87ktw/solution.rb:4:in `asm'
     # /tmp/d20140115-8451-f87ktw/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)>'

  5) Asm.asm implements LABEL
     Failure/Error: je l1
     ArgumentError:
       wrong number of arguments (2 for 1)
     # /tmp/d20140115-8451-f87ktw/solution.rb:64:in `method_missing'
     # /tmp/d20140115-8451-f87ktw/spec.rb:81:in `block (3 levels) in <top (required)>'
     # /tmp/d20140115-8451-f87ktw/solution.rb:4:in `instance_eval'
     # /tmp/d20140115-8451-f87ktw/solution.rb:4:in `asm'
     # /tmp/d20140115-8451-f87ktw/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)>'

  6) Asm.asm can be used to find GCD of two numbers
     Failure/Error: je finish
     ArgumentError:
       wrong number of arguments (2 for 1)
     # /tmp/d20140115-8451-f87ktw/solution.rb:64:in `method_missing'
     # /tmp/d20140115-8451-f87ktw/spec.rb:94:in `block (3 levels) in <top (required)>'
     # /tmp/d20140115-8451-f87ktw/solution.rb:4:in `instance_eval'
     # /tmp/d20140115-8451-f87ktw/solution.rb:4:in `asm'
     # /tmp/d20140115-8451-f87ktw/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.00791 seconds
8 examples, 6 failures

Failed examples:

rspec /tmp/d20140115-8451-f87ktw/spec.rb:14 # Asm.asm implements INC
rspec /tmp/d20140115-8451-f87ktw/spec.rb:22 # Asm.asm implements DEC
rspec /tmp/d20140115-8451-f87ktw/spec.rb:33 # Asm.asm implements CMP
rspec /tmp/d20140115-8451-f87ktw/spec.rb:67 # Asm.asm implements JMP
rspec /tmp/d20140115-8451-f87ktw/spec.rb:77 # Asm.asm implements LABEL
rspec /tmp/d20140115-8451-f87ktw/spec.rb:88 # Asm.asm can be used to find GCD of two numbers

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

Милен обнови решението на 15.01.2014 14:35 (преди почти 11 години)

+module Asm
+ def self.asm(&block)
+ instructions = ArithmeticInstructions.new
+ instructions.instance_eval &block
+ p instructions.registers.values
+ end
+
+ class ArithmeticInstructions
+ attr_reader :ax, :bx, :cx, :dx
+ attr_reader :registers
+
+ def initialize
+ @operations_queue = []
+ @registers = {
+ ax: 0,
+ bx: 0,
+ cx: 0,
+ dx: 0,
+ }
+ @ax, @bx = :ax, :bx
+ @cx, @dx = :cx, :dx
+ end
+
+ def mov(register, value)
+ if value.is_a? Fixnum
+ @registers[register.to_sym] = value
+ else
+ @registers[register.to_sym] = @registers[value.to_sym]
+ end
+ @operations_queue << ['mov', [register.to_sym, value]]
+ end
+
+ def inc(register, value)
+ if value.is_a? Fixnum
+ @registers[register.to_sym] += value
+ else
+ @registers[register.to_sym] += @registers[value.to_sym]
+ end
+ @operations_queue << ['inc', [register.to_sym, value]]
+ end
+
+ def dec(register, value)
+ if value.is_a? Fixnum
+ @registers[register.to_sym] -= value
+ else
+ @registers[register.to_sym] -= @registers[value.to_sym]
+ end
+ @operations_queue << ['dec', [register.to_sym, value]]
+ end
+
+ def cmp(register, value)
+ if value.is_a? Fixnum
+ @last_cmp_result = @registers[register.to_sym] <=> value
+ else
+ @last_cmp_result = @registers[register.to_sym] <=> @registers[value.to_sym]
+ end
+ @operations_queue << ['cmp', [register.to_sym, value]]
+ end
+
+ def label(value)
+ @operations_queue << ['label', value]
+ end
+
+ def method_missing(method_name)
+ method_name
+ end
+ end
+end

Оправдано :) Не искам да прозвучи като заяждане, по-скоро като въпрос относно форматирането. Пиша на Sublime. Там всичко изглежда наред. Като копирам кода във формата на сайта трябва отново да подравнявам много от нещата. Къде е проблема?

@Милен, за Sublime ти препоръчвам тези неща:

  1. Сложи си опцията "draw_white_space": "all" в настройките на редактора.
  2. Инсталирай си плъгин, който ти показва trailing whitespace. Ако имаш Package Control, потърси плъгин, който се казва "TrailingSpaces" или нещо сродно. Ако нямаш Package Control, задължително си инсталирай.
  3. Увери се, че syntax specific настройките за Ruby (има го в менюто), имат следните две опции вътре:

     "tab_size": 2,
     "translate_tabs_to_spaces": true,