2017-02-10 104 views
0

我目前正在學習python的過程中,所以我試圖重寫我在java中的項目到python。我不確定我出錯的地方。代碼試圖將n個節點分配給k個通道並打印出結果/配置。我接受在評論中被關閉,因爲我真的無法弄清楚我的代碼出了什麼問題,可能是因爲我只知道Java。這可能是因爲我使用終端而不是標準的Python IDE嗎? 此外,這是我在StackOverflow平臺上的第一個問題,所以如果有任何方法可以改變我的方法來提問我想知道和學習的問題。 set_of_channels是指用於保持生成的設置 available_combos保持每個所生成的集合的軌跡基於輸入將java代碼翻譯成python,IDE建議?

鑑於3個節點和2個信道,這是在輸出
的Python

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() 
+2

縮進是絕對錯誤的。我希望你沒有使用這個代碼。此外,你應該給你一個錯誤的賬戶。 – Pbd

+0

將檢討我的語法,謝謝你一堆 – cmoioverflow

回答

2

有許多的問題與此代碼:

  • 作爲@Pbd說:縮進是錯誤的,在Python這是一個大問題。
  • 假設由於複製&粘貼導致縮進錯誤,另一個主要問題是while True,您確實需要退出條件。
  • 剛取出while True使代碼退出,但我不知道這是否是你想要的
  • 你只能做format,但你不是print荷蘭國際集團的字符串或返回它。
  • 即使您要返回格式化的字符串,您也不會使用它。
  • 不要試圖名的變量sum,這是在基本上任何語言,尤其是Python的

還有一些風格問題(使用snake_case而不是駝峯,只需重命名startmain,檢查一個壞主意腳本被正確調用,等等),但我會說首先解決其他問題。

+0

感謝您的所有建議。我跳入Python語法並且看起來光榮地失敗了。 'True True'的意思是讓程序總是重新啓動,但現在我已經刪除了它。這有點像預期的那樣工作,但我不會說出正確的結果。 – cmoioverflow

+0

我認爲你只是有點倉促,可能是因爲你已經習慣了一些IDE,指出了一些錯誤,你沒有在這裏。重要新聞:作爲初學者,如果你不使用IDE,它會更好。你需要在讓別人照顧他們之前瞭解基礎知識:-)儘管如此,隨意用修改後的代碼發佈另一個問題。 – ChatterOne