• + 0 comments

    I don't try to compress code. So you cat read it and make observations.

    def whatsNext(arr):
        if len(arr)==0:
            ret = []
        elif len(arr)==1:
            arr[-1] -= 1
            ret = [1]+[1]+[arr[-1]]
        elif len(arr)==2:
            arr[-1] += 1
            arr[-2] -= 1
            ret = [1]+[arr[-1]]+[arr[-2]]
        elif len(arr)%2!=0:
            arr[-1] -= 1
            arr[-2] -= 1
            ret = arr[:-1]+[1]+[1]+[arr[-1]]
        else:
            arr[-1] += 1
            arr[-2] -= 1
            arr[-3] -= 1
            ret = arr[:-2]+[1]+[arr[-1]]+[arr[-2]]
        if len(ret)>2:
            for i in range(1,len(ret)-1):
                if ret[i]==0 and ret[i+1]==1:
                    ret[i-1] += 1
                    ret[i+1] = None
        ret = list(filter(bool,ret))
        print(len(ret))
        print(*ret)