将Python代码翻译为Oz

时间:2012-08-24 04:07:16

标签: python oz mozart

有人可以帮助我将这段Python代码翻译成Oz语言吗?

def rep_subset_conditional(S, tset, index, t, count):
    for i in xrange(index, len(S)):
        tset += [S[i]]
        tsum = sum(tset)
        if tsum == t:
            print tset
            count += 1
            tset.remove(S[i])
            return count
        elif tsum > t:
            tset.remove(S[i])
            return count
        else:
            count=rep_subset_conditional(S, tset, i, t, count)
            tset.remove(S[i])
    return count

此代码简单地计算并打印给定集合的所有子集(具有重复的元素),其总和等于t。 以下是此代码的试运行:

>>> rep_subset_conditional([1, 5, 10, 25, 50], [], 0, 10, 0)
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
    [1, 1, 1, 1, 1, 5]
    [5, 5]
    [10]
    4

t这里是10 S是子集[1, 5, 10, 25, 50] 我想把这个程序翻译成Oz。但我无法正确地做到这一点。请帮忙! 这就是我所尝试的一切:

declare Rep T1 T2 Sum
fun {Rep L S MainList AuxList C}
   case L of nil then C
   [] H|T then
      {Browse H|AuxList}
      if {Sum H|AuxList} == S then
      T1={Rep MainList S MainList H|AuxList C+1}
      {Rep T S MainList AuxList T1}
      else
      if {Sum H|AuxList} < S then
         T2 = {Rep MainList S MainList AuxList C}
         {Rep T S MainList AuxList T2}
      end
      end      
   end
end
fun {Sum L}
  case L of nil then 0
  [] H|T then
     H + {Sum T}
  end
end

0 个答案:

没有答案