mchd
mchd

Reputation: 3163

How to save the value of i in a for loop without letting the iteration affecting it

In this chunk of the code:

def lin_search(A,N,X):
  first_occurance = 0
  counter = 0

  for i in range(0, N):
    if counter != X:
      if A[i] == 0:
        first_occurance = i
        counter += 1
        print('True at ' + str(first_occurance) + ' ' + str(counter))
      else:
        continue
    elif counter == X:
      print('Memory has enough space for ' + str(X) + ' amount starting at index ' + str(first_occurance))
      break;

The moment first occurrence gets assigned to i, I want it to stay at that exact value even if the for loop continues. Let's say that A[2] == 0 and first_occurance became 2. I want first_occurance to remain 2 no matter how long the for loop is running.

If I run

memory = [1,0,0,0,1,1,0,0,1,1,1,0,0,0,0,0]

lin_search(memory, len(memory), 4)

first_occurance should be 11 because that is the index where there are 4 consecutive 0s

Upvotes: 0

Views: 68

Answers (2)

Epsi95
Epsi95

Reputation: 9047

Initialize first_occurance to None. It will only get assigned value once when the condition

if(first_occurance == None):

will be satisfied. Once first_occurance will be assigned to any value, the above condition doesn't hold true. By this first_occurance will be initialized only once.

Here

str(first_occurance if first_occurance else 0)

The extra if-else condition is provided so that str is not applied to None. Then it will give 'None'. if first_occurance is None then just give 0.

def lin_search(A,N,X):
  first_occurance = None
  counter = 0

  for i in range(0, N):
    if counter != X:
      if A[i] == 0:
        if(first_occurance == None):
           first_occurance = i
        counter += 1
        print('True at ' + str(first_occurance if first_occurance else 0) + ' ' + str(counter))
      else:
        continue
    elif counter == X:
      print('Memory has enough space for ' + str(X) + ' amount starting at index ' + str(first_occurance if first_occurance else 0))
      break;

Not in scope of this question though Solution for your problem

def lin_search(memory):
    stack = []
    for index, i in enumerate(memory):
        if(i == 0):
            stack.append(i)
            if(len(stack) == 4):
                return index - 3
        else:
            stack.clear()
    return -1 #not found

modification to your solution

def lin_search(A,N,X):
    first_occurance = None
    counter = 0

    for i in range(0, N):
        if counter != X:
            if A[i] == 0:
                if(first_occurance == None):
                    first_occurance = i
                counter += 1
                print('True at ' + str(first_occurance) + ' ' + str(counter))
            else:
                first_occurance = None
                counter = 0
        if counter == X:
            print('Memory has enough space for ' + str(X) + ' amount starting at index ' + str(first_occurance))
            break

Upvotes: 2

Chinmay Kanchi
Chinmay Kanchi

Reputation: 65893

If you set first_occurance to None to begin with, you can check if the variable has ever been set before. Note that I explicitly do not check if first_occurance is 0, because 0 can be a valid value - i.e., first_occurance could be at the very first index. In this case, it doesn't matter, but it might if you were doing more work inside the if block. If you are sure you don't want to do anything other than assignment of first_occurance inside the if condition, you could make only a single change to your code: if A[i] == 0 and first_occurance == 0:

def lin_search(A,N,X):
  first_occurance = None # set it to None to begin with
  counter = 0

  for i in range(0, N):
    if counter != X:
      if A[i] == 0 and first_occurance is None: # ensure that first_occurance is only set once
        first_occurance = i
        counter += 1
        print('True at ' + str(first_occurance) + ' ' + str(counter))
      else:
        continue
    elif counter == X:
      print('Memory has enough space for ' + str(X) + ' amount starting at index ' + str(first_occurance or 0))
      break;

    # if you want to use first_occurance down the line and it needs to be set to 0, do it here
    if not first_occurance:
        first_occurance = 0

Upvotes: 0

Related Questions