123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- import math
- from utils import conf
- def jieChen(n):
- myArray = {};
- myArray[0] = 1;
- i = 1;
- while i<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):
- return 1;
- return jieChen(n)[n-1];
- def changeInt2Str(i,m):
- if(not isinstance(m,int)):
- conf.myprint("m is not int",m)
- return
- if(m*i == 0):
- return "0";
- result = "";i
- myarray = {};
- while(i>=m):
- myarray[myarray.__len__()] = i%m
- i=int(i/m)
- if(i!=0):
- myarray[myarray.__len__()] = i
- for item in myarray :
- result=str(myarray[item])+result
- return result
- def getAllPosition(m, n):
- allArray = {}
- max = math.pow(m,n)
- index = 0;
- while (index < max):
- itemIndex = changeInt2Str(index,m)
- while(itemIndex.__len__()<n):
- itemIndex = "0"+itemIndex
- allArray[index]=itemIndex
- index = index+1
- conf.myprint(allArray)
- return allArray
- def getAllArrayNumber(m,n):
- total = 0
- allArray = getAllPosition(m, n)
- for item in allArray:
- total = getitemIndexArrayNumber(allArray[item],m,n)+total
- conf.myprint("total=",total)
- return total
- def getitemIndexArray(itemIndex,m,n):
- a = {};
- for item in range(m):
- index = 0;
- indexNum = 0;
- while(index < n):
- if(itemIndex[index] == str(item)):
- indexNum = indexNum + 1
- index = index+1
- a[item]=indexNum
- conf.myprint("a=",a)
- return a
- def getitemIndexArrayAndIndex(itemIndex,m,n):
- a = {};
- for item in range(m):
- index = 0;
- indexList = [];
- while(index < n):
- if(itemIndex[index] == str(item)):
- indexList.append(index)
- index = index+1
- a[item]=indexList
- conf.myprint("a=",a)
- return a
- def getPositionArray(positions):
- newPositons = []
- jiechen_array = jiechenarray(len(positions))
- for item in jiechen_array:
- m_position = []
- for item_item in item:
- m_position.append(positions[item_item-1])
- newPositons.append(m_position)
- return newPositons
- def getitemIndexArrayAndPosition(itemIndex,m,n):
- a = getitemIndexArray(itemIndex,m,n)
- for item in a:
- if(a[item] > 1):
- newItem = {}
- newItem[0] = a[item]
- newItem[1] = jiechenarray(a[item])
- a[item] = newItem
- conf.myprint("getitemIndexArrayAndPosition=",a)
- return a
- def getModuleIndex(itemIndex,m,n):
- indexArrayAndIndex = getitemIndexArrayAndIndex(itemIndex,m,n)
- positions=[]
- for item in indexArrayAndIndex:
- item1 = indexArrayAndIndex[item]
- if(len(item1)>0):
- position_array = getPositionArray(item1)
- if(len(positions) == 0):
- positions= position_array
- else:
- temp_positions=[]
- for temp_item in positions:
- for this_temp_item in position_array:
- new_temp_item = temp_item.copy()
- for item1_item in item1:
- item1_item_index=item1.index(item1_item)
- new_temp_item.insert(this_temp_item[item1_item_index],item1_item)
- temp_positions.append(new_temp_item)
- positions=temp_positions
- return positions
- 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
- conf.myprint("a=",a)
- return a
- def getitemIndexArrayNumber(itemIndex,m,n):
- itemIndexArrayNumber = 1;
- a = getitemIndexArray(itemIndex,m,n)
- for item in a:
- intItem = int(a[item])
- if(intItem > 1):
- itemIndexArrayNumber = jieChenNumber(intItem)*itemIndexArrayNumber
- if(itemIndexArrayNumber == 0):
- itemIndexArrayNumber = 1
- return itemIndexArrayNumber
|