以前缀表示法处理命令行参数的最有效方法
我们的作业是编写一个 ruby 脚本,根据表达式计算单词列表的子集。
常规二元运算是
&& And operator
|| Or operator
++ Concatenate operator
! Negation operator
有效的调用就像
./eval.rb wordlist && a c
or
./eval.rb wordlist && || a b c
第一次调用意味着生成一个新的单词列表,其中所有单词至少有一个“a”和“c”。 所以我的问题是如何有效地处理争论?也许递归? 我被困住了...
提前致谢。
our homework is to write a ruby script who calculate a subset of wordlist depending on the expression.
regular binary operations are
&& And operator
|| Or operator
++ Concatenate operator
! Negation operator
A valid call would be like
./eval.rb wordlist && a c
or
./eval.rb wordlist && || a b c
First call means generate a new wordlist which all words have at least one 'a' and 'c'.
So my question is how do I process the arguemnts in a efficent way? Maybe recursiv?
I'm stuck...
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来像带有前缀符号的语法。堆栈确实是您的朋友,最容易使用的堆栈是调用堆栈。例如,给定以下语法:
这是评估它的代码:
虽然这是 Ruby,但它足够接近伪代码。我已经输入了显式返回值,尽管 Ruby 不需要它们,因为对于不了解 Ruby 的人来说可能会更清楚。
Looks like a grammer with prefix notation. A stack is indeed your friend, and the easiest stack to use is the call stack. For example, given this grammar:
This is the code to evaluate it:
Although this is Ruby, it's close enough to pseudo-code. I've put explicit returns in, although Ruby does not require them, because it may be clearer to someone who does not know Ruby.
使用堆栈。最大大小是参数的数量。
Use a stack. The max size would be the number of arguments.