is there a mathematical way to go about this coding question?

84 Views Asked by At

i have the following python code(assume x is already defined as an integer):

numList=[] #creates a list that is empty
for i in range(x,-1,-1): #loops through from i=x to i=0, decreasing one each iteration
    if i%5==1: #if i mod 5 is 1...
        numList.pop(1) #remove the second item from the list
    elif i%5==4: # if i mod 5 is 4...
        numList.pop(-1) #remove the last item from the list
    else: #if i mod 5 is not 1 or 4...
        numList.append(i) #add i to the list
print(numList) #prints out the list

i am given that the output is [23, 8, 7, 3, 2, 0](hopefully its clear what this means). i am tasked to find the possible values for x. is there some logical/mathematical way i can go about this without resorting to guess and check? i tried going "backwards" from the end result but it got too convoluted very quickly.

i have also tried setting x to 5a+2, 5a+3, and 5a (we dont consider 5a+1 and 5a+4 because that will terminate the code prematurely) because they give different values mod 5, but i cant seem to get anywhere with that as well.

2

There are 2 best solutions below

0
On

At least roughly, every consecutive five rounds pop two and append three, so net append 1. For six entries you will need 30-ish rounds (and of course certainly you appended 23 at some point.

Also note that the first term will not change any more after a few rounds.


After clarification of undefined behaviour when poping an illegal position:

Write $x=5m+r$ where $m,n\in\Bbb N_0$ asn $r<5$. As we never insert a number $>x$ into the list, we must have $x\ge 23$ and so $m\ge4$.

If $r=0$, the first few rounds produce $$[]\to [5m]\to [] \to [5m-2]\to [5m-2,5m-3]\to [5m-2]\to [5m-2,5m-5] $$ and after this, the length will always be $\ge2$ when a pop is made. It follows that the first term $5m-2$ will stay there until the end. As thsi must be $=23$, we conclude $m=5$ and $x=25$.

If $r=1$, already the first round fails as pop(1) from an empty list fails.

If $r=2$, we fail in the second round: $$ []\to[5m+2]\stackrel{\text{pop}(1)}\longrightarrow ??$$

If $r=3$, $$ []\to [5m+3]\to[5m+3,5m+2]\to[5m+3]\to\ldots$$ which is in fact the sequence belonging to $x+2$ (see the case $r=0$ above) with the first two rounds dropped. It follows that $x=23$ is also a solution if $x=25$ is.

If $r=4$, we fail immeditely with an illegal pop(-1).

IN summary, there are two possibilities

  1. Both $x=25$ and $x=23$ are solutions
  2. There is no solution.

So let's play through $x=23$: $$[]\to[23]\to[23,22]\to[23]\to [23,20]\to[23]\to\\ \to[23,18]\to[23,18,17]\to[23,17]\to [23,17,15]\to[23,17]\to\\ \to[23,17,13]\to[23,17,13,12]\to[23,13,12]\to[23,13,12,10]\to[23,13,12]\to\\ \to[23,13,12,8]\to[23,13,12,8,7]\to[23,12,8,7]\to[23,12,8,7,5]\to\\\to[23,12,8,7]\to[23,12,8,7,3]\to[23,12,8,7,3,2]\to[23,8,7,3,2]\to\\\to[23,8,7,3,2,0]$$ Yay!

1
On

Use a brute force method.

z=numeric(0)
for (x in 0:1000) {
y=numeric(0)
for (i in x:0) {
  if (i%%5==1) {
    y=y[-2]
  } else if (i%%5==4) {
    y=y[-length(y)]   
  } else {
    y=append(y, i)
  }
}
if (identical(c(23, 8, 7, 3, 2, 0), y)) {
  z=append(z, x)
}
}

The answers are

> print(z)
[1] 23 24 25 26