title: How to reference a counter outside of a for loop?
body: <p>I came across a question in Hackerank. An e-commerce website contains one to many items in each page. To mimic the logic of the website, a programmer has a list of items and each item is in the form of a string array: [name, relevance, price] where each value of relevance and price are integers as strings. After sorting the items by (name:0, relevance :1, price:2), the programmer is trying to find out a list of items displayed in a chosen page. Given a list of items, the sort column, the sort order (0 : ascending, 1: descending), the number of items to be displayed in each page and a page number. Determine the list of item names in the specified page while respecting the item's order (Page number starts at 0). Example items = [[item1, 10, 15), [item2, 3, 4), (item3, 17, 8]] sortParameter = 1 sortOrder = 0 itemsPerPage = 2 pageNumber = 1 there are n = 3 items. • Sort them by (relevance : 1) in ascending order (items = [[item2, 3, 4], [item1, 10, 15). [item3, 17, 8]]). Display up to 2 items in each page. • The page O contains 2 item names [item2, item1] and page 1 contains only 1 item name, result = item3. Function Description Complete the function fetchitemsToDisplay in the editor below. fetchltemsToDisplay has the following parameter(s): string items[n][3]: a 2D array of arrays of strings in the form (name, relevance, price] int sortParameter: the column of the items to sort on int sortOrder:0 = ascending and 1 = descending int itemsPerPage: the number of items per page int pageNumber: the page number to display item names Returns: string pageltems[m]: array of item names on the requested page in the order they are displayed
  Returns: string pageltems[m]: array of item names on the requested page in the order they are displayed</p>
  <p>Now, I am done with the sorting part and also most of the test cases pass but as you can see in the output I get, the pagenumbers seem to repeat. I have tried many approaches but I can't seem to figure out how to manage the count? I think I need a fresh perspective on this one.</p>
  <pre><code>def fetchitemstoDisplay(stringitems,sortparameter,sortorder,itemsperpage,pagenumber):
    p=sortparameter
    if sortorder==0:
      so=False
    else:
      so=True
    try:
      a=(sorted(stringitems, key = lambda x: int(x[p]),reverse=so))
    except ValueError:
      a=(sorted(stringitems, key = lambda x: x[0],reverse=False))
    tn=[]
    pn=0
    m=0
    for i in range(len(a)):
      if m&lt;=itemsperpage-1:
        tn.append([pn,a[i][0]])
        m+=1
      elif m&gt;itemsperpage-1 and itemsperpage==1:
        m=0
        pn+=1
        tn.append([pn,a[i][0]])
        pn+=1
      elif m&gt;itemsperpage-1:
        m=0
        pn+=1
        tn.append([pn,a[i][0]])
    print(tn )
    for i in tn:
      if(i[0])==pagenumber:
        print(i[1])
      else:
        pass
  
  </code></pre>
  <pre><code>fetchitemstoDisplay([['p1','1','2'],['p2','2','1'],['p3','2','1'],['p5','2','1'],['p6','2','1'],['p6','2','1']],0,1,2,0)
  </code></pre>
  <pre><code>[[0, 'p1'], [0, 'p2'], [1, 'p3'], [1, 'p5'], [1, 'p6'], [2, 'p6']]
  </code></pre>