getarray.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import math
  2. #获取阶乘列表
  3. def jieChen(n):
  4. myArray = {};
  5. myArray[0] = 1;
  6. i = 1;
  7. while i<n :
  8. myArray[i] = (i+1)*myArray[i-1];
  9. i = i+1
  10. return myArray;
  11. #获取阶乘排列数组
  12. def jiechenarray(n):
  13. array = [];
  14. if n < 1:
  15. return array
  16. if n == 1:
  17. onearray = [];
  18. onearray.append(n)
  19. array.append(onearray)
  20. return array
  21. subArray = jiechenarray(n-1);
  22. subArrayLength = len(subArray)
  23. for item in range(subArrayLength):
  24. for index in range(len(subArray)+1):
  25. newArrayItem = subArray[item].copy()
  26. newArrayItem.insert(index,n)
  27. array.append(newArrayItem)
  28. return array
  29. #获取指定数的阶乘
  30. def jieChenNumber(n):
  31. if(n <= 1):
  32. return 1;
  33. return jieChen(n)[n-1];
  34. #辗转相除法进行进制转化
  35. def changeInt2Str(i,m):
  36. if(not isinstance(m,int)):
  37. print("m is not int",m)
  38. return
  39. if(m*i == 0):
  40. return "0";
  41. result = "";i
  42. myarray = {};
  43. while(i>=m):
  44. myarray[myarray.__len__()] = i%m
  45. i=int(i/m)
  46. if(i!=0):
  47. myarray[myarray.__len__()] = i
  48. for item in myarray :
  49. result=str(myarray[item])+result
  50. return result
  51. #获取组合数位置列表
  52. def getAllPosition(m, n):
  53. allArray = {}
  54. max = math.pow(m,n)
  55. index = 0;
  56. while (index < max):
  57. itemIndex = changeInt2Str(index,m)
  58. while(itemIndex.__len__()<n):
  59. itemIndex = "0"+itemIndex
  60. allArray[index]=itemIndex
  61. index = index+1
  62. print(allArray)
  63. return allArray
  64. #获取组合数列表的排列总数
  65. def getAllArrayNumber(m,n):
  66. total = 0
  67. allArray = getAllPosition(m, n)
  68. for item in allArray:
  69. total = getitemIndexArrayNumber(allArray[item],m,n)+total
  70. print("total=",total)
  71. return total
  72. #获取单个组合的各边重复项
  73. def getitemIndexArray(itemIndex,m,n):
  74. a = {};
  75. for item in range(m):
  76. index = 0;
  77. indexNum = 0;
  78. while(index < n):
  79. if(itemIndex[index] == str(item)):
  80. indexNum = indexNum + 1
  81. index = index+1
  82. a[item]=indexNum
  83. print("a=",a)
  84. return a
  85. #获取单个组合的各边重复项--包含各项位置
  86. # 如{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: {}}}
  87. def getitemIndexAndPositionArray(itemIndex, m, n):
  88. a = {};
  89. for item in range(m):
  90. index = 0;
  91. indexNum = 0;
  92. positions = {};
  93. while(index < n):
  94. if(itemIndex[index] == str(item)):
  95. positions[len(positions)] = index
  96. indexNum = indexNum + 1
  97. index = index+1
  98. aItem = {}
  99. aItem[0] = indexNum
  100. aItem[1]=positions
  101. a[item]=aItem
  102. print("a=",a)
  103. return a
  104. #获取单个组合的各边重复项的排列数
  105. def getitemIndexArrayNumber(itemIndex,m,n):
  106. itemIndexArrayNumber = 0;
  107. a = getitemIndexArray(itemIndex,m,n)
  108. for item in a:
  109. intItem = int(a[item])
  110. if(intItem > 1):
  111. itemIndexArrayNumber = jieChenNumber(intItem)+itemIndexArrayNumber
  112. if(itemIndexArrayNumber == 0):
  113. itemIndexArrayNumber = 1
  114. return itemIndexArrayNumber
  115. # result = changeInt2Str(14,3);
  116. # print(result)
  117. # getAllPosition(3, 3)
  118. # getAllArrayNumber(4,3)
  119. # getitemIndexArray("01243",5,5)
  120. # getitemIndexAndPositionArray("23332", 5, 5)
  121. # number = getitemIndexArrayNumber("51243",5)
  122. # print(number)
  123. # getAllArrayNumber(4,2)
  124. # print(jiechenarray(3))