0

If I have a code like:

def foo(a):
    for i in range(len(a)):
        do something
        return a

def main():
    while len(a) > 1:
        n = int(a[0])
        print(foo((a[:n+1])))
        a = a[n+1:]

Q1: How do I compute the run time to see which part of my code is not efficient?


Q2: Is there a way to iterate through a list continuously but only use a section of it? for example lets say a = [1, 2, 3, 4, 7, 8, 8]. I want to run it from [1, 2, 3, 4] then print a value then run the rest of the list ([7, 8, 9]). Is the only way to do this through slicing?

martineau
  • 119,623
  • 25
  • 170
  • 301
lunalilac
  • 19
  • 4
  • [How can you profile a Python script?](https://stackoverflow.com/questions/582336/how-can-you-profile-a-python-script) Also see – martineau Oct 01 '21 at 01:01

1 Answers1

0

You can try something like this for part 2 of you question:

def do_something(my_stops, my_range):
    lst=[]
    #count=0
    for x in my_range:
        if my_range.index(x) not in  my_stops:
            x+=1
            lst.append(x)
        else:
            x+=1
            lst.append(x)
            print(lst)
    return lst

do_something([1,3,8], [1,2,3,4,5,6,7,8,9,10])

Output:

[2, 3]
[2, 3, 4, 5]
[2, 3, 4, 5, 6, 7, 8, 9, 10]
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
j__carlson
  • 1,346
  • 3
  • 12
  • 20