将java代码翻译成python,IDE建议?

时间:2017-02-10 11:25:11

标签: java algorithm python-2.7

我目前正在学习python,所以我尝试将java中的项目重写为python。我不确定自己哪里出错了。代码尝试将n个节点分配给k个通道并打印生成的配置/集。我接受在评论中被关闭,因为我真的无法理解我的代码有什么问题,可能因为我只知道Java。这可能是因为我使用的是终端而不是标准的Python IDE吗? 另外,这是我在StackOverflow平台上的第一个问题,所以如果有任何方法可以改变我提出问题的方法,我想知道和学习。 set_of_channels用于保存生成的集合 available_combos根据输入

保留每个生成集的轨迹

给定3个节点和2个通道,这是输出
Python

1 sets with occupancies: []  
1 sets with occupancies: ['3'] 
Total number of assignments: 2  

Java

3 Nodes, 2 Channels: 
1 set(s) with occupancies: [0, 3]  
3 set(s) with occupancies: [1, 2]  
3 set(s) with occupancies: [2, 1]  
1 set(s) with occupancies: [3, 0]  
Total number of assignments: 8

不确定是否放置新代码,因为我的帖子可能太长,不知道这不是堆栈溢出礼仪

def start():
availableCombos = [[]]
numberOfNodes = raw_input("How many nodes?")
numberOfChannels = raw_input("How many channels?")
index = 0
setOfChannels = [numberOfChannels]
while True:
    generate(numberOfNodes, setOfChannels,
             index, numberOfNodes, availableCombos)
    totalAssignments(availableCombos, numberOfNodes)


# The base case is the last channel
# Set the channel to have the value of nodes
# currentChannel should be set to 0
# as this is the base that lists in java work with
# it is incremented with every call so as to fill up next channel
# remaining nodes helps to keep track of nodes
# to put into channels based on nodes in previous channels
# As arrays are filled they are copied into the list of available combinations
def generate(nodes, combo, currentChannel, remainingNodes, availableCombos):
sum = 0
if(currentChannel < len(combo) - 1):
    if(currentChannel != 0):
        remainingNodes -= combo[currentChannel - 1]
        for i in range(0, remainingNodes):
            combo[currentChannel] = i
            sum + 1
        generate(nodes - i, combo, currentChannel + 1, remainingNodes)
        print(sum)
# base case
if(currentChannel == len(combo) - 1):
    combo[currentChannel] = nodes
    channelSet = range(len(combo))
    for i in range(0, len(combo)):
        channelSet[i] = combo[i]
    availableCombos.append(channelSet)

# computes the total number of combos
# and displays sets of channels that were generated
def totalAssignments(combos, nodes):
totalCombos = 0
for combo in combos:
    totalCombos += countCombos(combo, nodes, 0)
    '{}{}{}'.format(countCombos(combo, nodes, 0),
                    " sets with occupancies: ", combo)
'{}{}'.format("Total number of assignments: ", totalCombos)

def countCombos(combo, nodes, currentChannel):
if(currentChannel < len(combo)):
    binomials = binomial(nodes, combo[currentChannel])
    recursed = countCombos(combo, int(nodes) -
                           int(combo[currentChannel]), currentChannel + 1)
    result = binomials * recursed
    return result
return 1

def binomial(n, k):
if(k == n or k == 0):
    return 1
result = binomial(n - 1, k - 1) + binomial(n - 1, k)
return result
def main():
start()


main()

1 个答案:

答案 0 :(得分:2)

此代码存在许多问题:

  • 正如@Pbd所说:缩进是错误的,在Python中这是一个大问题。
  • 假设由于副本而导致缩进错误粘贴,另一个主要问题是while True,你真的需要退出条件。
  • 只需删除while True即可退出代码,但我不知道这是否属于您想要的内容
  • 您只执行format,但您没有print字符串或将其返回。
  • 即使您要返回格式化字符串,也只是不使用它。
  • 不要试图命名变量sum,这在任何语言中都是个坏主意,特别是Python

还有许多风格问题(使用snake_case而不是camelCase,只需将start重命名为main,检查脚本是否正确调用等等,但是我和#39; d先说出解决其他问题。