私は現在Pythonを学んでいるので、私がJavaで持っていたプロジェクトをPythonに書き直そうとしました。どこが間違っているのか分かりません。コードは、n個のノードをk個のチャネルに割り当てて、その結果の構成/セットを印刷しようとしています。私は本当に私のコードで何が間違っているのかを考え出すことができないので、私はコメントの中でシャットダウンを受け入れることを認めます。私は標準のPython IDEではなくターミナルを使用しているため、これが可能かもしれませんか? また、これはStackOverflowプラットフォームに関する私の最初の質問です。私が知りたいと思う質問をするために私のアプローチを変更する方法があれば、学びます。 set_of_channelsが生成さを保持することを意味する available_combosを設定する3つのノード及び2つのチャンネルの指定された入力JavaコードをPythonに翻訳する、IDEの提案?
に基づいて生成された各セットのトラックを保持し、これは出力
あるパイソン
1 sets with occupancies: []
1 sets with occupancies: ['3']
Total number of assignments: 2
ジャワ
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
私の構文を見直します、ありがとう、一束 – cmoioverflow