【问题标题】:RPN Calculator Ruby matching in arrays数组中的 RPN 计算器 Ruby 匹配
【发布时间】:2014-08-17 22:04:16
【问题描述】:

我正在制作一个反向波兰符号计算器。我写了一个游戏计划并试图填写它。我正处于我想要将数组中的整数移动到我的新数组中的部分(使用 shift 将其从数组中永久删除)。

这应该很容易,但我就是想不通。我试着用像

这样的东西来识别整数
Integer.match(x)
x.class == Integer

但这不起作用。我也尝试过构建一些东西来拉取所有不在操作数组中的东西,比如这个失败的东西:

x.include?(operations)

我是新手,不擅长编码。如果你想责备我的代码是多么草率或者我发布的错误,那么已经有人这样做了。我真的很努力。谢谢,

class RPNCalculator
    def initialize(problem)
        @problem = problem
    end

    def resolve
        array = @problem.split(" ")
        @array = array
        operations = ["+", "-", "*"]
        @operations = operations
        queue = []
        @queue = queue

        array.map! { |x| /[0-9]/.match(x) ? x.to_i : x}

        array.each do |x|
           #THIS IS WHERE I'M BUILDING & FAILING 

            array.shift(x)
            queue << x
        end     
end
# iterate through the array
# if array elements don't match a operations element, push them into the queue
# once you have a match on the function, launch a method
# make 3 new RPN methods, one each for + - *
# method launched should affect last 2 items in the queue.
# result of that method gets pushed to the end of the queue. 
# return the remaining value in the queue as the answer

# placeholder for method +
# placeholder for method -
# placeholder for method *

end

test = "1 3 +"
mytest  = RPNCalculator.new(test)
mytest.resolve

【问题讨论】:

    标签: ruby arrays


    【解决方案1】:

    CJ,您要仔细查看错误消息。

    Integer.match(x)
      #=> NameError: undefined local variable or method `x' for main:Object
    

    Ruby 最初假设matchInteger 类的类方法。在将matchmatch 的参数x 发送到Integer 之前,它会评估x 并发现它既不是方法也不是局部变量;因此例外。也就是说,你还没有定义x

    如果你定义了x(作为方法或局部变量),Ruby 会给你一个不同的错误信息:

    Integer.match(4)
      #NoMethodError: undefined method `match' for Integer:Class
    

    那是因为您没有为类Integer 定义类方法。如果你写了

    class Integer
      def self.match(x)
        2 * x
      end
    end
    

    Ruby 会像蛤蜊一样快乐:

    Integer.match(4) #=> 8
    

    让我们继续讨论您的方法,Rubyists 将使用两个空格缩进编写该方法。

    def resolve
      array = @problem.split(" ")
      @array = array
      operations = ["+", "-", "*"]
      @operations = operations
      queue = []
      @queue = queue
    
      array.map! { |x| /[0-9]/.match(x) ? x.to_i : x}
    
      array.each do |x|
         #THIS IS WHERE I'M BUILDING & FAILING 
        array.shift(x)
        queue << x
      end     
    end
    

    第一行马上就有问题了:

    array = @problem.split(" ")
      #NoMethodError: undefined method `split' for nil:NilClass
    

    该错误消息是什么意思?首先,让我们看看 IRB 能告诉我们关于方法split 的接收者:

    @problem       #=> nil
    @problem.class #=> NilClass
    

    现在再次查看错误消息。它表示您尚未为类NilClass 的实例nil 定义方法splitsplit 只为String 类定义(即String#split),所以split 的接收者必须是字符串。

    您的代码存在更多问题,但如果没有之前的基础,这些问题很难处理。不过,我希望我的 cmets 已经解决了您的部分问题。

    如果您主要将这个问题作为练习来解决,我认为您应该退后一步,问问自己学习 Ruby 最有效和最高效的方法是什么。我认为您应该专注于为教授 Ruby 原则而精心设计的问题,从简单的问题开始,然后再进行更具挑战性的问题。互联网上有很多 Ruby 教学网站。寻找那些以及人们对他们的评价。大多数都是免费的,但不提供导师支持。如果你觉得你会从指导中受益,我强烈推荐 Ruby Learning 的Core Ruby course。我看到一个新的开始这个月晚些时候。它不是免费的,但也不贵。课程学习笔记here

    【讨论】:

      【解决方案2】:

      你想要x.is_a? Integer。 Ruby 从不直接创建 Integer 的实例,而是使用子类 Fixnum 和 Bignum,这就是您的 == 测试失败的原因。 is_a? 一直检查继承链的类型。

      【讨论】:

        【解决方案3】:

        如果要泛化为浮点数,请尝试添加以下方法:

        def is_decimal_number?(x)
          /\A[+-]?[[:digit:]]+(\.[[:digit:]]+)?\Z/.match(x)
        end
        
        if is_decimal_number? x
           # push x.to_f onto your stack
        else
           # Check if it's a valid operator, perform the specified operation or do error handling
        end
        

        请注意,这个函数并不复杂,它允许一个可选的前缀加或减,但它不能处理科学记数法、十六进制值或任何其他花哨的东西。

        如果您只想坚持使用整数,请将小数点和右边的数字去掉:

        def is_decimal_number?(x)
          /\A[+-]?[[:digit:]]+\Z/.match(x)
        end
        

        【讨论】:

        • CJ 的代码存在比编写正则表达式来查看字符串是否是特定类型数字的表示形式更多的基本问题。另外,你怎么能指望一个新手破译正则表达式——尤其是没有解释——这会让一些有经验的 Ruby 爱好者感到困惑?
        • @CarySwoveland 也许是这样,但 CJ 专门询问如何将数字与运算符分开。我以函数形式给出了响应,并展示了如何将它与作为 RPN 计算器基本核心的 if/else 一起使用。无论 CJ 是否可以创建该方法,它都是可用的并解决了分类问题。
        猜你喜欢
        • 2014-10-10
        • 1970-01-01
        • 2014-05-04
        • 1970-01-01
        • 2017-08-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多