小编Idl*_*ard的帖子

如果两个Tree具有相同的值节点,则使用set来计数

在"def same_child_values"中.我试图比较两个树,看看另一棵树是否具有与主tree.children相同的值节点.在这种情况下,我使用一组来比较它们.但是在我的代码中,当树深度大于1时,我的代码无法正确地将节点添加到集合中.谁能帮我?

class Tree:
      '''Tree ADT; nodes may have any number of children'''

  def __init__(self: 'Tree',
               item: object =None, children: list =None):
    '''Create a node with item and any number of children'''

    self.item = item
    if not children:
      self.children = []
    else:
      self.children = children[:]

  def __repr__(self: 'Tree') -> str:
    '''Return representation of Tree as a string'''

    if self.children:
      return 'Tree({0}, {1})'.format(repr(self.item), repr(self.children))
    else:
      return 'Tree({})'.format(repr(self.item))

  def is_leaf(self: 'Tree') -> bool:
    '''Return True iff this Tree node is a leaf …
Run Code Online (Sandbox Code Playgroud)

python python-3.x

5
推荐指数
1
解决办法
140
查看次数

在C中,我们如何强制varargs函数中的最后一个参数为终止null?

在C++中,我们可能会写这样的东西:

#include <cassert.h>
#include <cstdio.h>
#include <cstdarg.h>

void func(..., short end = 0) {

   // prevent caller from overriding default value with something other than null 
   assert(end == 0);

   va_list args;  

   short x;
   x = va_arg(args, short); 

   while (x != 0) {
       printf("%d", va_arg(args, short));
   }

   va_end(list);

   return;
}
Run Code Online (Sandbox Code Playgroud)

但是,C不支持默认函数参数.有没有我可以强制func在其参数列表的末尾有一个尾随空字符?有没有办法在没有程序员明确地将终止空值传递给函数的情况下执行此操作?

我们不希望看到如下调用:

int   x = 5;
float y = 6.4;
func(x, y, 0);
Run Code Online (Sandbox Code Playgroud)

我们只是想要func(x, y);.

是否可以编写一个可以转换文本的宏

func(x, y);

进入电话:

func(x, y, 0);

有没有办法在没有宏的情况下做到这一点?

c function variadic-functions default-arguments

1
推荐指数
1
解决办法
403
查看次数