from itertools import product, count, islice import fileinput from math import sqrt import itertools def extract(roll): x=0 for i in numLists: if(roll[i]>i): x=1 if(x==0): check(roll) def check(roll): x=0 prev=0 for i in roll: if(abs(prev-i)>1): x=1 prev=i if(x==0): checklist.append(roll) def is_prime(n): return n > 1 and all(n % i for i in islice(count(2), int(sqrt(n)-1))) def permutation(d): for combo in itertools.product(*[d[k] for k in sorted(d.keys())]): ay= ''.join(combo) roll= [] for i in range(num): roll.append(int(ay[i])) extract(roll) def nonPrime(list): j=0 nonPrime=0 list2 = [] for i in list: if(is_prime(lists[j][i])==True): nonPrime+=1 j=j+1 j=0 if(nonPrime==0): for i in list: list2.append(lists[j][i]) j+=1 thisList.append(list2) def maxSum(): max=0 maxList = [] for i in thisList: sum=0 for j in i: sum+=j if(sum>max): max=sum maxList=i print("List:",end="") print(maxList) print("Maximum sum of list:", max) def dic(list): values = [] for i in list: liste=[] for j in range(i+1): liste.append(str(j)) values.append(liste) dicts = {} i = 1 for x in values: dicts[i] = x i+=1 permutation(dicts) lists = [] numLists = [] num = 0 checklist = [] thisList = [] for f in fileinput.input(): list = [] commands = f.split() if(commands[0]=="exit"): dic(numLists) for i in checklist: nonPrime(i) maxSum() quit() else: numLists.append(num) num=num+1 for i in commands: t=int(i) list.append(t) lists.append(list)