gpt4 book ai didi

java - 将 java 代码转换为 python,IDE 建议?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:54:03 24 4
gpt4 key购买 nike

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

给定 3 个节点和 2 个 channel ,这是输出
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()

最佳答案

这段代码有很多问题:

  • 正如@Pbd 所说:缩进是错误的,在 Python 中这很重要。
  • 假设由于复制和粘贴导致缩进错误,另一个主要问题是虽然为真,但您确实需要一个退出条件。
  • 只需删除 while True 即可使代码退出,但我不知道这是否是您想要的
  • 您只执行格式化,但您没有打印字符串或返回它。
  • 即使您返回格式化字符串,您也只是没有使用它。
  • 不要尝试命名变量 sum ,这基本上在任何语言中都是一个坏主意,尤其是 Python

还有一些风格问题(使用 snake_case 而不是 camelCase,只需将 start 重命名为 main,检查脚本是否被正确调用等等),但我会说先解决其他问题。

关于java - 将 java 代码转换为 python,IDE 建议?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42158231/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com