Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
168 views
in Technique[技术] by (71.8m points)

python - 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

question from:https://stackoverflow.com/questions/65661443/how-to-save-the-value-of-i-in-a-for-loop-without-letting-the-iteration-affecting

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...