import math as m import random as r bombs = 5 board = [30,30,5] iboard = [0,0,5] targets = len(board) dist = [[bombs]] lst = [bombs] while len(lst) < bombs: idx = list(lst+[1]).index(1) - 1 # index of last non-1 value oval = lst[idx] # last non-1 value tval = oval - 1 # target max value of collapse total = sum(lst[idx:]) # sum of collapse (of lst, from idx to end, inclusive) # print("LIST: " + str(lst)) # print(" oVal: " + str(oval)) # print(" tVal: " + str(tval)) # print(" Totl: " + str (total)) lst = lst[:idx] # remains of collapse (lst, up to idx, non-inclusive) cotton = list(int(tval) for i in range(0,int(total/tval))) tail = total % tval if tail: lst += cotton + [tail] else: lst += cotton dist += [lst] # print() for i in dist: i.sort() # print(i) # print("TOTAL DISTRIBUTIONS: " + str(len(dist))) invalid = [] for i in dist: if len(i) > targets: invalid += [i] # (str(i) + "c INVALID: " + str(i)) else: for j in range(len(i),targets): i += [0] i.sort() # print(str(i) + "c VALID: " + str(i)) for i in invalid: dist.remove(i) # print() # for i in dist: # print(i) # print("VALID DISTRIBUTIONS: " + str(len(dist))) perms = [] for i in dist: pt = list(i) # print("++ " + str(perms) + " + " + str(pt)) # print(" = " + str(perms)) while next: # input(dist) # print(str(perms) + " + " + str(pt)) perms.append(list(pt)) idx = len(pt) - 1 while pt[idx] >= (pt + [pt[-1]])[idx+1]: # print(idx) idx -= 1 if idx < - len(pt): break # print(idx) if idx < 0: break idx0 = idx idx1 = len(pt) - 1 while pt[idx0] >= pt[idx1]: idx1 -= 1 tmp = pt[idx0] pt[idx0] = pt[idx1] pt[idx1] = tmp # print(str(idx0) + " <-> " + str(idx1)) # print(str(pt[idx0]) + " <-> " + str(pt[idx1])) rem = pt[:idx0+1] rev = pt[idx0+1:] rev.reverse() # print(str(rem) + " + " + str(rev)) pt = rem + rev # print(" p" + str(perms) + " + " + str(pt)) # print(" + " + str(pt)) # print() # for i in perms: # print(i) # print("POSSIBLE OUTCOMES FOR " + str(bombs) + " BOMBS OVER " + str(targets) + " TARGETS: " + str(len(perms))) invalid = [] for i in perms: if sum(i[j] > board[j] for j in range(0,targets)): invalid += [i] iper = 0 for i in perms: chk = 1 for j in range(0,targets): chk *= i[j] >= iboard[j] iper += chk ostr = "CUR: " for i in board: ostr += "0"[:i < 10] + str(i) + " " tstr = "TGT: " for i in iboard: tstr += "0"[:i < 10] + str(i) + " " print("BMBS: " + str(bombs)) print(ostr) print(tstr) perc = 100*float(iper)/len(perms) print("PROBABILITY: " + str(round(perc,3)) + "% ( " + str(iper) + " / " + str(len(perms)) + " )")
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question