Решение на Четвърта задача от Георги Пурнаров

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

Към профила на Георги Пурнаров

Резултати

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

Код

class Asm_functions_module
def method_missing (name,*args,&block)
name.to_s
end
def mov destination_register, source
second = if source.is_a? Proc
source.call
else
source
end
destination_register.call "equal",second
end
def inc destination_register, value
second = if value.is_a? Proc
value.call
else
value
end
destination_register.call :+.to_proc,second
end
def dec destination_register, value
second = if value.is_a? Proc
value.call
else
value
end
destination_register.call :-.to_proc,second
end
def cmp register, value
second = if value.is_a? Proc
value.call
else
value
end
register.call <=> second
end
end
class Asm_functions
attr_accessor :functions
def initialize
@ax=0
@bx=0
@cx=0
@dx=0
@functions=[]
end
def method_missing(name, *args, &block)
@functions<<[name,args]
end
def ax
Proc.new do |function, value|
if not function
@ax
elsif function.is_a? Proc
@ax = function.call @ax,value
else
@ax=value
end
end
end
def bx
Proc.new do |function, value|
if not function
@bx
elsif function.is_a? Proc
@bx = function.call @bx,value
else
@bx=value
end
end
end
def cx
Proc.new do |function, value|
if not function
@cx
elsif function.is_a? Proc
@cx = function.call @cx,value
else
@cx=value
end
end
end
def dx
Proc.new do |function, value|
if not function
@dx
elsif function.is_a? Proc
@dx = function.call @dx,value
else
@dx=value
end
end
end
def result
functions = @functions[0..-1]
Asm_functions_module.new.instance_eval do
functions.each do |block_line|
send(block_line[0],block_line[1][0],block_line[1][1])
end
end
[@ax,@bx,@cx,@dx]
end
end
class Asm
def self.asm &block
asm=Asm_functions.new
asm.instance_eval &block
asm.result
end
end

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

..FFFFFF

Failures:

  1) Asm.asm implements INC
     Failure/Error: Asm.asm do
     TypeError:
       nil can't be coerced into Fixnum
     # /tmp/d20140115-8451-nyu3w3/solution.rb:62:in `+'
     # /tmp/d20140115-8451-nyu3w3/solution.rb:62:in `call'
     # /tmp/d20140115-8451-nyu3w3/solution.rb:62:in `block in ax'
     # /tmp/d20140115-8451-nyu3w3/solution.rb:21:in `call'
     # /tmp/d20140115-8451-nyu3w3/solution.rb:21:in `inc'
     # /tmp/d20140115-8451-nyu3w3/solution.rb:109:in `block (2 levels) in result'
     # /tmp/d20140115-8451-nyu3w3/solution.rb:108:in `each'
     # /tmp/d20140115-8451-nyu3w3/solution.rb:108:in `block in result'
     # /tmp/d20140115-8451-nyu3w3/solution.rb:107:in `instance_eval'
     # /tmp/d20140115-8451-nyu3w3/solution.rb:107:in `result'
     # /tmp/d20140115-8451-nyu3w3/solution.rb:121:in `asm'
     # /tmp/d20140115-8451-nyu3w3/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: Asm.asm do
     TypeError:
       nil can't be coerced into Fixnum
     # /tmp/d20140115-8451-nyu3w3/solution.rb:62:in `-'
     # /tmp/d20140115-8451-nyu3w3/solution.rb:62:in `call'
     # /tmp/d20140115-8451-nyu3w3/solution.rb:62:in `block in ax'
     # /tmp/d20140115-8451-nyu3w3/solution.rb:30:in `call'
     # /tmp/d20140115-8451-nyu3w3/solution.rb:30:in `dec'
     # /tmp/d20140115-8451-nyu3w3/solution.rb:109:in `block (2 levels) in result'
     # /tmp/d20140115-8451-nyu3w3/solution.rb:108:in `each'
     # /tmp/d20140115-8451-nyu3w3/solution.rb:108:in `block in result'
     # /tmp/d20140115-8451-nyu3w3/solution.rb:107:in `instance_eval'
     # /tmp/d20140115-8451-nyu3w3/solution.rb:107:in `result'
     # /tmp/d20140115-8451-nyu3w3/solution.rb:121:in `asm'
     # /tmp/d20140115-8451-nyu3w3/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: Asm.asm do
       
       expected: [2, 2, 0, 3]
            got: [2, 2, 3, 3]
       
       (compared using ==)
     # /tmp/d20140115-8451-nyu3w3/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: Asm.asm do
       
       expected: [0, 0, 1, 1]
            got: [1, 0, 1, 1]
       
       (compared using ==)
     # /tmp/d20140115-8451-nyu3w3/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: Asm.asm do
     TypeError:
       nil can't be coerced into Fixnum
     # /tmp/d20140115-8451-nyu3w3/solution.rb:86:in `+'
     # /tmp/d20140115-8451-nyu3w3/solution.rb:86:in `call'
     # /tmp/d20140115-8451-nyu3w3/solution.rb:86:in `block in cx'
     # /tmp/d20140115-8451-nyu3w3/solution.rb:21:in `call'
     # /tmp/d20140115-8451-nyu3w3/solution.rb:21:in `inc'
     # /tmp/d20140115-8451-nyu3w3/solution.rb:109:in `block (2 levels) in result'
     # /tmp/d20140115-8451-nyu3w3/solution.rb:108:in `each'
     # /tmp/d20140115-8451-nyu3w3/solution.rb:108:in `block in result'
     # /tmp/d20140115-8451-nyu3w3/solution.rb:107:in `instance_eval'
     # /tmp/d20140115-8451-nyu3w3/solution.rb:107:in `result'
     # /tmp/d20140115-8451-nyu3w3/solution.rb:121:in `asm'
     # /tmp/d20140115-8451-nyu3w3/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: Asm.asm do
       
       expected: [8, 8, 0, 0]
            got: [8, 24, 0, 0]
       
       (compared using ==)
     # /tmp/d20140115-8451-nyu3w3/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.00789 seconds
8 examples, 6 failures

Failed examples:

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

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

Георги обнови решението на 15.01.2014 13:44 (преди почти 11 години)

+class Asm_functions_module
+ def method_missing (name,*args,&block)
+ name.to_s
+ end
+
+ def mov destination_register, source
+ second = if source.is_a? Proc
+ source.call
+ else
+ source
+ end
+ destination_register.call "equal",second
+ end
+
+ def inc destination_register, value
+ second = if value.is_a? Proc
+ value.call
+ else
+ value
+ end
+ destination_register.call :+.to_proc,second
+ end
+
+ def dec destination_register, value
+ second = if value.is_a? Proc
+ value.call
+ else
+ value
+ end
+ destination_register.call :-.to_proc,second
+ end
+
+ def cmp register, value
+ second = if value.is_a? Proc
+ value.call
+ else
+ value
+ end
+ register.call <=> second
+ end
+end
+
+class Asm_functions
+ attr_accessor :functions
+ def initialize
+ @ax=0
+ @bx=0
+ @cx=0
+ @dx=0
+ @functions=[]
+ end
+
+ def method_missing(name, *args, &block)
+ @functions<<[name,args]
+ end
+
+ def ax
+ Proc.new do |function, value|
+ if not function
+ @ax
+ elsif function.is_a? Proc
+ @ax = function.call @ax,value
+ else
+ @ax=value
+ end
+ end
+ end
+
+ def bx
+ Proc.new do |function, value|
+ if not function
+ @bx
+ elsif function.is_a? Proc
+ @bx = function.call @bx,value
+ else
+ @bx=value
+ end
+ end
+ end
+
+ def cx
+ Proc.new do |function, value|
+ if not function
+ @cx
+ elsif function.is_a? Proc
+ @cx = function.call @cx,value
+ else
+ @cx=value
+ end
+ end
+ end
+
+ def dx
+ Proc.new do |function, value|
+ if not function
+ @dx
+ elsif function.is_a? Proc
+ @dx = function.call @dx,value
+ else
+ @dx=value
+ end
+ end
+ end
+
+ def result
+ functions = @functions[0..-1]
+ p functions
+ Asm_functions_module.new.instance_eval do
+ functions.each do |block_line|
+ send(block_line[0],block_line[1][0],block_line[1][1])
+ end
+ end
+ [@ax,@bx,@cx,@dx]
+ end
+
+end
+
+class Asm
+ def self.asm &block
+ asm=Asm_functions.new
+ asm.instance_eval &block
+ p asm.result
+ end
+end

Георги обнови решението на 15.01.2014 13:53 (преди почти 11 години)

class Asm_functions_module
def method_missing (name,*args,&block)
name.to_s
end
def mov destination_register, source
second = if source.is_a? Proc
source.call
else
source
end
destination_register.call "equal",second
end
def inc destination_register, value
second = if value.is_a? Proc
value.call
else
value
end
destination_register.call :+.to_proc,second
end
def dec destination_register, value
second = if value.is_a? Proc
value.call
else
value
end
destination_register.call :-.to_proc,second
end
def cmp register, value
second = if value.is_a? Proc
value.call
else
value
end
register.call <=> second
end
end
class Asm_functions
attr_accessor :functions
def initialize
@ax=0
@bx=0
@cx=0
@dx=0
@functions=[]
end
def method_missing(name, *args, &block)
@functions<<[name,args]
end
def ax
Proc.new do |function, value|
if not function
@ax
elsif function.is_a? Proc
@ax = function.call @ax,value
else
@ax=value
end
end
end
def bx
Proc.new do |function, value|
if not function
@bx
elsif function.is_a? Proc
@bx = function.call @bx,value
else
@bx=value
end
end
end
def cx
Proc.new do |function, value|
if not function
@cx
elsif function.is_a? Proc
@cx = function.call @cx,value
else
@cx=value
end
end
end
def dx
Proc.new do |function, value|
if not function
@dx
elsif function.is_a? Proc
@dx = function.call @dx,value
else
@dx=value
end
end
end
def result
functions = @functions[0..-1]
- p functions
Asm_functions_module.new.instance_eval do
functions.each do |block_line|
send(block_line[0],block_line[1][0],block_line[1][1])
end
end
[@ax,@bx,@cx,@dx]
end
end
class Asm
def self.asm &block
asm=Asm_functions.new
asm.instance_eval &block
- p asm.result
+ asm.result
end
end

Георги, не спазваш конвенциите за стил.

Не слагаш скоби при дефиниране на метод, приемащ параметри, не слагаш интервали около и след оператори, не си консистентен. Това ще ти струва наказателни точки.

Освен това, имаш повече дублирана логика, отколкото е добра идея. На места дизайнът ти има нужда от преработка.

След малко ще може да видиш нашето и решенията на колегите ти.