Skip to content

Instantly share code, notes, and snippets.

@pasindud
Created March 10, 2021 08:45
Show Gist options
  • Save pasindud/0af7b610d7ab7ff198c149caa20537a1 to your computer and use it in GitHub Desktop.
Save pasindud/0af7b610d7ab7ff198c149caa20537a1 to your computer and use it in GitHub Desktop.
# import pprint
class Solution(object):
def getProbability(self, balls):
"""
:type balls: List[int]
:rtype: float
"""
trie = {}
newballs = balls + balls[0:-1]
perBall = []
stack = []
for x in range(0, len(balls)):
stack.append([[],len(perBall)])
perBall += [x+1 for _ in range(balls[x])]
# start = 0
# stack = [[[],x] for x in range(0, len(balls))]
print perBall
newSeqList = []
while stack:
theSeq, newPointer = stack.pop()
theSeq.append(perBall[newPointer])
if len(theSeq) == (len(perBall) /2):
newSeqList.append(theSeq)
continue
prev = None
for i in range(newPointer, len(perBall) - 1):
newSeq = theSeq[:]
if prev == None or prev != perBall[i + 1]:
stack.append([newSeq, i+1])
prev = perBall[i + 1]
for x in newSeqList:
print('- ' + str(x))
matches = []
for seq in newSeqList:
leftSideBall = [0 for _ in balls]
rightSideBall = balls[:]
rightSideDistrubution = []
for i in seq:
rightSideBall[i-1] = rightSideBall[i-1] - 1
leftSideBall[i - 1] = leftSideBall[i - 1] + 1
for n in xrange(0, len(rightSideBall)):
for c in range(rightSideBall[n]):
rightSideDistrubution.append(n + 1)
rightSeq = []
# leftSeq = []
for x in xrange(0, len(rightSideBall)):
rightSeq += [balls[x]] * rightSideBall[x]
# print (rightSeq)
# print '----'
# print balls
# print seq
# print rightSideBall
# print rightSideDistrubution
# print rightSideBall
# print leftSideBall
# print([seq, rightSeq])
if len(set(seq)) == len(set(rightSeq)):
# if rightSideBall == leftSideBall:
matches.append([seq, rightSeq])
for x in matches:
print x
s = Solution()
balls = [1,2,1]
balls = [3,2,1]
balls = [1,2,1,2]
s.getProbability(balls)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment