Setup¶

In [1]:
from math import floor as MF
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
In [8]:
#Inputs and outputs are the same.
#unslappedSeq = A list of unslapped sequences each being a list of length seqlen, and integers from 1 to n.
#slappedSeq = A dictionary of integers from 1 to n, where the values are the number of slapped sequences that occur of that length.
#n = Number of values of cards in deck, from 1 to n.
#seqlen = Current sequence length of the iteration.


def produceAllSeq(unslappedSeq, slappedSeq, n, seqlen) :
    
    #Sequences will add a new rightmost digit.
    seqlen += 1
    #Create an empty list for new unslapped sequences.  
    newUnslappedSeq = []
    
    #For each sequence that has so far been unslapped...
    for seq in unslappedSeq:
        
        #Append any of the numbers being used to create a new sequence.
        for i in range(1, n+1) :
            nSeq = seq + [i]
            #The new sequence is unslapped.
            slapped = False
            
            #But...
            while not slapped:
                #Looking at the top j cards.   
                for j in range(1, seqlen) :
                    #Looking at the top k cards under the top j cards.
                    for k in range(1, seqlen-j+1) :
                        #See if their sums are the same.
                        if np.array(nSeq[(seqlen-j):seqlen]).sum() == np.array(nSeq[(seqlen-j-k):(seqlen-j)]).sum() :
                            #Slap 'em! Breaks out of loop.
                            slapped = True
                            
                #This new sequence is unslapped.  Break out of loop.
                break
            
            #The new sequence is either slapped or not.
            if slapped:
                slappedSeq.append(nSeq)
            #    l = len(nSeq)
            #    if l in slappedSeq.keys() :
            #        slappedSeq[l] += 1
            #    else :
            #        slappedSeq[l] = 1
            else:
                newUnslappedSeq.append(nSeq)

    #As long as there are unslapped sequences, keep going.
    if len(newUnslappedSeq) > 0 :
        return(produceAllSeq(newUnslappedSeq, slappedSeq, n, seqlen))
    
    #Return.
    else:      
        return(newUnslappedSeq, slappedSeq, n, seqlen)
In [3]:
stats = {}
for i in range(1, 8):
    
    data = produceAllSeq([[]], {}, i, 0)
    
    stats[i] = data[1]        
    print("A deck of cards with values from 1 to " + str(i) + " produce a maximum unslapped sequence length of " + str(data[3]-1) + ".")
A deck of cards with values from 1 to 1 produce a maximum unslapped sequence length of 1.
A deck of cards with values from 1 to 2 produce a maximum unslapped sequence length of 3.
A deck of cards with values from 1 to 3 produce a maximum unslapped sequence length of 7.
A deck of cards with values from 1 to 4 produce a maximum unslapped sequence length of 9.
A deck of cards with values from 1 to 5 produce a maximum unslapped sequence length of 15.
A deck of cards with values from 1 to 6 produce a maximum unslapped sequence length of 21.
A deck of cards with values from 1 to 7 produce a maximum unslapped sequence length of 31.
In [4]:
stats
Out[4]:
{1: {2: 1},
 2: {2: 2, 3: 2, 4: 4},
 3: {2: 3, 3: 10, 4: 20, 5: 8, 6: 8, 7: 8, 8: 12},
 4: {2: 4, 3: 20, 4: 64, 5: 121, 6: 204, 7: 260, 8: 234, 9: 23, 10: 4},
 5: {2: 5,
  3: 36,
  4: 156,
  5: 471,
  6: 1167,
  7: 2148,
  8: 3002,
  9: 2929,
  10: 2573,
  11: 1990,
  12: 1746,
  13: 1446,
  14: 1364,
  15: 1169,
  16: 555},
 6: {2: 6,
  3: 54,
  4: 310,
  5: 1342,
  6: 4708,
  7: 12895,
  8: 28752,
  9: 54525,
  10: 93410,
  11: 143939,
  12: 195384,
  13: 226855,
  14: 223362,
  15: 172482,
  16: 91070,
  17: 26133,
  18: 7680,
  19: 2661,
  20: 1050,
  21: 283,
  22: 30},
 7: {2: 7,
  3: 78,
  4: 556,
  5: 3047,
  6: 13601,
  7: 49325,
  8: 151935,
  9: 411215,
  10: 979439,
  11: 2025789,
  12: 3650947,
  13: 5699102,
  14: 7606116,
  15: 8572973,
  16: 8192625,
  17: 6875468,
  18: 5336302,
  19: 3761643,
  20: 2353005,
  21: 1298837,
  22: 655673,
  23: 316419,
  24: 145537,
  25: 58154,
  26: 21686,
  27: 8837,
  28: 4353,
  29: 2121,
  30: 1233,
  31: 608,
  32: 154}}
In [15]:
produceAllSeq([[]], [], 3, 0)[1][-12:]
Out[15]:
[[1, 3, 2, 3, 1, 3, 2, 1],
 [1, 3, 2, 3, 1, 3, 2, 2],
 [1, 3, 2, 3, 1, 3, 2, 3],
 [2, 3, 1, 3, 2, 3, 1, 1],
 [2, 3, 1, 3, 2, 3, 1, 2],
 [2, 3, 1, 3, 2, 3, 1, 3],
 [3, 1, 3, 2, 3, 1, 3, 1],
 [3, 1, 3, 2, 3, 1, 3, 2],
 [3, 1, 3, 2, 3, 1, 3, 3],
 [3, 2, 3, 1, 3, 2, 3, 1],
 [3, 2, 3, 1, 3, 2, 3, 2],
 [3, 2, 3, 1, 3, 2, 3, 3]]
In [17]:
produceAllSeq([[]], [], 4, 0)[1][-4:]
Out[17]:
[[1, 4, 3, 4, 2, 4, 3, 4, 1, 1],
 [1, 4, 3, 4, 2, 4, 3, 4, 1, 2],
 [1, 4, 3, 4, 2, 4, 3, 4, 1, 3],
 [1, 4, 3, 4, 2, 4, 3, 4, 1, 4]]

Rohan Lewis¶

2024.02.19¶