Ich bin gerade dabei, Python zu lernen, also habe ich versucht, ein Projekt, das ich in Java hatte, in Python umzuschreiben. Ich bin mir nicht sicher, wo ich falsch gelaufen bin. Der Code versucht, n Knoten k Kanälen zuzuordnen und die resultierenden Konfigurationen/Sätze zu drucken. Ich akzeptiere, in den Kommentaren abgeschaltet zu sein, denn ich kann wirklich nicht herausfinden, was mit meinem Code nicht stimmt, wahrscheinlich, weil ich Java nur kenne. Ist das möglich, weil ich Terminal anstelle einer Standard-Python-IDE verwende? Auch dies ist meine erste Frage auf der StackOverflow-Plattform. Wenn es irgendeinen Weg gibt, meinen Ansatz zu ändern, um die Frage zu stellen, würde ich gerne wissen und lernen. set_of_channels soll einen erzeugten Satz available_combos hält Spuren eines jeden erzeugten Satz halten, basierend auf dem EingangsÜbersetzen von Java-Code zu Python, IDE-Vorschläge?
3 angegebenen Knoten und 2 Kanäle, ist dies die Ausgabe
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
Unsicher, ob neuen Code zu setzen, wie meine Post zu lang sein kann, weiß nicht, ob das nicht Stapelüberlauf Etikette
istdef 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()
Einrückung ist absolut falsch. Ich hoffe, Sie benutzen diesen Code nicht. Außerdem sollten Sie einen Fehlerbericht angeben, den Sie erhalten. – Pbd
Wird meine Syntax überprüfen, danke ein Haufen – cmoioverflow