Starting my numpy reference…


# Options...left over plus any 1 
x[0]+leftOver
# will it increase the score?

# Come back to this
score = Score(x)

def lowest():
    score = Score(x)
    sum=100
    tic=''
    h=score[1]
    for k in h.keys():
        if h[k][0] < sum:
            sum = h[k][0]
            tic=k
    ticN=np.fromstring(tic[1:-1], dtype=int, sep=' ')
    return (tic,sum,ticN)

leftOver=total - np.sum(x,axis=0)


# What you have to work with
leftOver+lowest()[2]

def buildArrayFromScore():
    score = Score(x)
    a=np.array([])
    for k in score[1].keys():
        if a.size==0:
            a=np.fromstring(k[1:-1], dtype=int, sep=' ')
        else:
            a=np.vstack((a,np.fromstring(k[1:-1], dtype=int, sep=' '))  ) 
    return a




def ifFoundReplace(x,b,c):
    idx=np.where(np.all(x==b,axis=1)) 
    if idx[0].size != 0:
        print("Replace",x[(idx[0][0],)]," with",np.array(c))
        x[(idx[0][0],)]=np.array(c) # Increment, not all replace
    return x


# All options - taken from lowest
def options():
    leftOver=total - np.sum(x,axis=0)
    return leftOver+lowest()[2]

def testR(t):
    t=np.array(t)
    if t.shape == (9,):
        t=np.array([t])
    if t.shape != (1,9):
        print("Something wrong with the shape")
        print(t)
        return
    return Score(t)


def isOption(n):
    r = np.where(options() - n < 0)
    if r[0].size == 0: #empty
        if len(n[n>0]) >= 3:
            return True
        else:
            return False
    else:
        return False
 

def buildTable():
    o=options().copy()
    idx=np.where(o>0)[0]
    t=np.zeros_like(o)
    a=np.array([])
    for j in idx:
        for i in range(0,o[j]):
            k=np.zeros_like(o)
            k[j]=1
            if a.size == 0:
                a=np.array(t[j] - k.copy())
            else:
                a=np.vstack((a,t[j] - k.copy()))
            #print(t[j] - k.copy() )
    return a

def allPossibleCombs():
    z=np.array([])
    a=buildTable()
    b=lowest()[2].copy()
    for i in range(0,a.shape[0]):
        b=b-a[i]
        #print(b)
        if z.size == 0:
            z=b.copy()
        else:
            z=np.vstack((z,b.copy()))
    return z
    
#Score([allPossibleCombs()[0]])



# GetMaxValue
def getMaxValue():
    z=Score(allPossibleCombs())[1]
    maxV=0
    maxS=''
    for i in z.keys():
        if z[i][0] > maxV:
            maxV=z[i][0]
            maxS=i
    return np.fromstring(maxS[1:-1], dtype=int, sep=' '),maxV




# Needs to be sorted
def getFirstAbv(maxV=32):
    z=Score(allPossibleCombs())[1]
    maxS=''
    a=np.array([])
    for i in z.keys():
        if z[i][0] > maxV:
            maxV=z[i][0]
            maxS=i
            if a.size == 0:
                a=np.array([np.fromstring(maxS[1:-1], dtype=int, sep=' '),maxV])
            else:
                b=np.array([np.fromstring(maxS[1:-1], dtype=int, sep=' '),maxV])
                a=np.vstack((a,b))
    if a.size != 0:
        #a=a[a[:,1].argsort()][::-1]  # reverse
        a=a[a[:,1].argsort()]
    return a



# This is the walk
# Next try combination
def tryReplace(c):
#    c=[ 1,  0, 0, 0, 0, 0, 0, 0,0]
    r=np.array(c)
    #r=lowest()[2]+ c
    if testR(r)[0] > lowest()[1]:
        print("testR okay")
        if isOption(r):
            print("yes we can replace",r)
            ifFoundReplace(x,lowest()[2],r )
            return [True,r]
    return [False,r]


This sorting stuff is pretty cool. I’ll have to come back to it…


# Needs to be sorted
def getFirstAbv(maxV=32):
    z=Score(allPossibleCombs())[1]
    maxS=''
    a=np.array([])
    for i in z.keys():
        if z[i][0] > maxV:
            maxV=z[i][0]
            maxS=i
            if a.size == 0:
                a=np.array([np.fromstring(maxS[1:-1], dtype=int, sep=' '),maxV])
            else:
                b=np.array([np.fromstring(maxS[1:-1], dtype=int, sep=' '),maxV])
                a=np.vstack((a,b))
    if a.size != 0:
        #a=a[a[:,1].argsort()][::-1]  # reverse
        a=a[a[:,1].argsort()]
    return a