import copy def sequence(outcm, pi): """This function generates outcomes of getting the next possible jerseys sequences for number pi. Actually it can work with any irrational number we would like to. """ temp = [] # A sequence of digits 1 through 9 with leading zero. singles = ['0' + str(i) for i in range(1, 10)] for i in range(len(outcm)): index = outcm[i][1] outcm_copy1 = copy.deepcopy(outcm[i]) if pi[index + 1] not in outcm[i][0]: if pi[index + 1] == '0' and not (set(outcm[i][0]) & set(singles)): outcm_copy1[0].append(pi[index + 1]) temp.append([outcm_copy1[0], index + 1]) elif pi[index + 1] != '0' and '0' + pi[index + 1] not in outcm[i][0]: outcm_copy1[0].append(pi[index + 1]) temp.append([outcm_copy1[0], index + 1]) outcm_copy2 = copy.deepcopy(outcm[i]) if pi[index + 1: index + 3] not in outcm[i][0] and pi[index + 1: index + 3] != '42': if pi[index + 1: index + 3] in singles: if pi[index + 1: index + 3][1] not in outcm[i][0] and '0' not in outcm[i][0]: outcm_copy2[0].append(pi[index + 1: index + 3]) temp.append([outcm_copy2[0], index + 2]) else: outcm_copy2[0].append(pi[index + 1: index + 3]) temp.append([outcm_copy2[0], index + 2]) if not temp: return outcm return sequence(temp, pi) def main(): # copy of pi sequence from Wikipedia pi = '3,1415926535 8979323846 2643383279 5028841971 6939937510 5820974944 5923078164 0628620899 8628034825 ' \ '3421170679 8214808651 3282306647 0938446095 5058223172 5359408128 4811174502 8410270193 8521105559 ' \ '6446229489 5493038196 4428810975 6659334461 2847564823' # get the list of numbers by splitting by spaces pi = ''.join(pi.split()) # getting rid of comma pi = pi[0] + pi[2:] # in most programming languages as in Python indexing starts from zero. # the first possible outcome. Whether '3' or '31'. It's easier perform it through a list outcome = [[['3'], 0], [['31'], 1]] # zero and one are indices that correspond to certain position of the last digit in # outcome. So for '3' it is an element on the position zero. '31' has '1' at the position one final_outcome = sequence(outcome, pi) for i in range(len(final_outcome)): print(' '.join(final_outcome[i][0]), '\t', 'digits used =', len(''.join(final_outcome[i][0])), '\t', 'numbers from jerseys used =', len(final_outcome[i][0])) main()