here2share

# ultranary_repeater.py

Jan 5th, 2026
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.33 KB | None | 0 0
  1. # ultranary_repeater.py
  2.  
  3. def next_ultranary(q,rep=3):
  4.  L=len(q);B=2;D=[0]*L
  5.  def ok(a):
  6.   c={}
  7.   for x in a:
  8.    c[x]=c.get(x,0)+1
  9.    if c[x]>rep:return False
  10.   return True
  11.  def build(cnt,mins,rem):
  12.   o=[]
  13.   for i in range(rem):
  14.    lo=mins[i];p=False
  15.    for x in range(lo,B):
  16.     if cnt.get(x,0)<rep:
  17.      o.append(x);cnt[x]=cnt.get(x,0)+1;p=True;break
  18.    if not p:return
  19.   return o
  20.  def bump():
  21.   nonlocal B,D
  22.   B+=1;D=[0]*(L-2)+[1,B-1] if L>=2 else [B-1]
  23.  def inc():
  24.   nonlocal B,D
  25.   if all(x==B-1 for x in D):bump();return
  26.   for i in range(L-1,-1,-1):
  27.    c={};okp=1
  28.    for x in D[:i]:
  29.     c[x]=c.get(x,0)+1
  30.     if c[x]>rep:okp=0;break
  31.    if not okp:continue
  32.    for nv in range(D[i]+1,B):
  33.     c2=dict(c);c2[nv]=c2.get(nv,0)+1
  34.     if c2[nv]>rep:continue
  35.     rem=L-(i+1);mins=D[i+1:]
  36.     s=build(dict(c2),mins,rem)
  37.     if s is not None:
  38.      D=D[:i]+[nv]+s;return
  39.   for j in range(L-2,-1,-1):
  40.    if D[j]<B-1:
  41.     D[j]=B-1
  42.     for k in range(j+1,L):D[k]=0
  43.     return
  44.   bump()
  45.  D=[0]*L
  46.  while 1:
  47.   if tuple(D)==q:
  48.    inc()
  49.    while not ok(D):inc()
  50.    return tuple(D)
  51.   inc()
  52.   while not ok(D):inc()
  53.  
  54. def test(q):
  55.     p = next_ultranary(q)
  56.     print(q, "->", p)
  57.     return p
  58.  
  59. q = (0, 0, 0, 1, 1)
  60. for _ in range(100):
  61.     q = test(q)
  62.  
  63. print("\nDemo:")
  64. q = (1, 1, 1, 0, 0)
  65. test(q)
  66. q = (2, 2, 2, 0, 1)
  67. test(q)
  68.  
  69.  
Advertisement
Add Comment
Please, Sign In to add comment