Browse Source

获取阶乘数组

duh 7 năm trước cách đây
mục cha
commit
537589655c
2 tập tin đã thay đổi với 71 bổ sung10 xóa
  1. 47 6
      phxb/utils/getarray.py
  2. 24 4
      phxb/utils/matchWall.py

+ 47 - 6
phxb/utils/getarray.py

@@ -8,6 +8,25 @@ def jieChen(n):
         myArray[i] = (i+1)*myArray[i-1];
         i = i+1
     return myArray;
+#获取阶乘排列数组
+def jiechenarray(n):
+    array = [];
+    if n < 1:
+        return array
+    if n == 1:
+        onearray = [];
+        onearray.append(n)
+        array.append(onearray)
+        return array
+    subArray = jiechenarray(n-1);
+    subArrayLength = len(subArray)
+    for item in range(subArrayLength):
+        for index in range(len(subArray)+1):
+            newArrayItem = subArray[item].copy()
+            newArrayItem.insert(index,n)
+            array.append(newArrayItem)
+    return array
+
 #获取指定数的阶乘
 def jieChenNumber(n):
     if(n <= 1):
@@ -30,8 +49,8 @@ def changeInt2Str(i,m):
     for item in myarray :
         result=str(myarray[item])+result
     return result
-#获取组合数列表
-def getAllArray(m,n):
+#获取组合数位置列表
+def getAllPosition(m, n):
     allArray = {}
     max = math.pow(m,n)
     index = 0;
@@ -46,7 +65,7 @@ def getAllArray(m,n):
 #获取组合数列表的排列总数
 def getAllArrayNumber(m,n):
     total = 0
-    allArray = getAllArray(m,n)
+    allArray = getAllPosition(m, n)
     for item in allArray:
         total = getitemIndexArrayNumber(allArray[item],m,n)+total
         print("total=",total)
@@ -64,6 +83,25 @@ def getitemIndexArray(itemIndex,m,n):
         a[item]=indexNum
     print("a=",a)
     return a
+#获取单个组合的各边重复项--包含各项位置
+# 如{0: {0: 0, 1: {}}, 1: {0: 0, 1: {}}, 2: {0: 2, 1: {0: 0, 1: 4}}, 3: {0: 3, 1: {0: 1, 1: 2, 2: 3}}, 4: {0: 0, 1: {}}}
+def getitemIndexAndPositionArray(itemIndex, m, n):
+    a = {};
+    for item in range(m):
+        index = 0;
+        indexNum = 0;
+        positions = {};
+        while(index < n):
+            if(itemIndex[index] == str(item)):
+                positions[len(positions)] = index
+                indexNum = indexNum + 1
+            index = index+1
+        aItem = {}
+        aItem[0] = indexNum
+        aItem[1]=positions
+        a[item]=aItem
+    print("a=",a)
+    return a
 #获取单个组合的各边重复项的排列数
 def getitemIndexArrayNumber(itemIndex,m,n):
     itemIndexArrayNumber = 0;
@@ -77,8 +115,11 @@ def getitemIndexArrayNumber(itemIndex,m,n):
     return itemIndexArrayNumber
 # result = changeInt2Str(14,3);
 # print(result)
-# getAllArray(4,3)
-getitemIndexArray("01243",5,5)
+# getAllPosition(3, 3)
+# getAllArrayNumber(4,3)
+# getitemIndexArray("01243",5,5)
+# getitemIndexAndPositionArray("23332", 5, 5)
 # number = getitemIndexArrayNumber("51243",5)
 # print(number)
-# getAllArrayNumber(4,2)
+# getAllArrayNumber(4,2)
+# print(jiechenarray(3))

+ 24 - 4
phxb/utils/matchWall.py

@@ -15,21 +15,41 @@ def canMatch(wallLength, moduleLengths):
 def matchWalls(roomLengths, moduleLengths):
     m = len(roomLengths)
     n = len(moduleLengths)
-    a=getarray.getAllArray(m,n)
+    a = getarray.getAllPosition(m, n)
     canMatchTop = canMatch(roomLengths[2], moduleLengths)
     if(canMatchTop):
         print("begin to calc top")
-        matchWall(roomLengths,moduleLengths,"222")
+        matchTopWall(roomLengths[2],moduleLengths)
     else:
         for item in a:
             matchWall(roomLengths,moduleLengths,a[item])
 
+
+def matchSingleWall(wallLength, wallModules,wallIndex):
+    print("wallLength",wallLength)
+    print("wallModules",wallModules)
+    print("wallIndex",wallIndex)
+    pass
+
+
 def matchWall(roomLengths, moduleLengths, positions):
     m = len(roomLengths)
     n = len(moduleLengths)
-    a = getarray.getitemIndexArray(positions,m,n)
+    a = getarray.getitemIndexAndPositionArray(positions,m,n)
+    for item in a:
+        if(a[item][0] == 1):
+            position = a[item][1][0]
+            wallModules = {};
+            wallModules[0] = moduleLengths[position]
+            matchSingleWall(roomLengths[position],wallModules,position)
+        if(a[item][0] > 1):
+            wallModules = {};
+            for modulePosition in a[item][1]:
+                moduleIndex = a[item][1][modulePosition]
+                wallModules[modulePosition] = moduleLengths[moduleIndex]
+            matchSingleWall(roomLengths[item],wallModules,a[item][1][0])
     pass
 #一字摆法
 def matchTopWall(roomLengths,moduleLengths):
-    matchWalls(roomLengths, moduleLengths, "222")
+    matchSingleWall(roomLengths, moduleLengths, "012")
 matchWalls([1,2,3,4],[1,1,1])