-2

Here is my code, Could anyone please help me why the 'long_band' resulted in empty list?

Expected output: [[9,10],[0,1,2,3,4,5,6,7],[18],[12]]

Code begins here:

arr = [1, 9, 3, 0, 18, 5, 2, 4, 10, 7, 12, 6]
len1 = len(arr)
long_band = list()
chain = list()

def band(i):
    chain.append(i)
    j = i+1
    if j in arr:
        band(j)
    else:
        #print(chain)
        #print(long_band)
        long_band.append([chain])
        #print(long_band)
        del chain[:]


def isHead(n):
    x = n-1
    if x in arr:
        None
    else:
        band(n)


for i in range(len1):
    isHead(arr[i])


print(long_band)
  • I would guess, either band() is never called, or the else part in band() isn't run. – Olaf Dietsche Jan 20 '22 at 07:29
  • Can you explain what this code is supposed to do? There's no obvious logic to the expected output, and the code itself obviously doesn't work and thus also doesn't serve as explanation. – deceze Jan 20 '22 at 07:32
  • no.. band() is getting called for elements 9,0,18,12. I have deugged this. – lahari chandu Jan 20 '22 at 07:34
  • i'm deleing the list called 'chain' after appending it to the list 'long_band', not sure why long_band is also getting empty. – lahari chandu Jan 20 '22 at 07:36
  • The values from `chain` are not copied, but only a reference to chain is appended. When you clear chain, there will only remain an empty list (`[]`) – Olaf Dietsche Jan 20 '22 at 07:38
  • Explain what the logic is to transform `arr` into "expected output". There's probably a very different, much easier approach to it. Yes, maybe we can drill down and try to understand why your current code doesn't do what you expect it to, but you can do that yourself on http://pythontutor.com or in a debugger. If you want a solution for the general task you're working on, tell us what that is. – deceze Jan 20 '22 at 07:38
  • Thr problem statement is to find a longest band from the given array/list. Longest band serves the meaning of chain of consecutive numbers. For example my list 'arr' has elements [1,9,3,0,18,5,2,4,10,7,12,6]. I'm picking each element and checking if the element is the head of the band or not, if not the head of the band then ignore and move to other element in the list. If head of the band, then call the band() function to get the list of elements in the array making the chain. – lahari chandu Jan 20 '22 at 07:46
  • A likely easier approach would be: sort the array, traverse and split it wherever numbers aren't consecutive. – deceze Jan 20 '22 at 07:48

2 Answers2

0

The immediate problem is this:

long_band.append([chain])
del chain[:]

You're appending the chain object to the list, and then immediately clear it. This also clears the object in the list, because there's only one object. Appending it to a list doesn't create a copy of it.


The bigger problem is that your algorithm is way overcomplicated. Here's a much simpler approach:

>>> arr = [1, 9, 3, 0, 18, 5, 2, 4, 10, 7, 12, 6]
>>> arr.sort()
>>> res = [arr[0:1]]
>>> for i, j in zip(arr, arr[1:]):
...   if i != j - 1:
...     res.append([])
...   res[-1].append(j)
... 
>>> res
[[0, 1, 2, 3, 4, 5, 6, 7], [9, 10], [12], [18]]
deceze
  • 510,633
  • 85
  • 743
  • 889
  • thank you very much for the new code too.. but i want to try this problem without sorting the array. Could you tell me how can i copy the 'chain' object values to 'long_band' and preserve them? – lahari chandu Jan 20 '22 at 09:06
  • See https://stackoverflow.com/a/2612815/476. – deceze Jan 20 '22 at 09:18
  • thank you so much, the assignment should go this way long_band.append(chain[:]) and it actually worked :) :) – lahari chandu Jan 20 '22 at 09:21
0
arr = [1, 9, 3, 0, 18, 5, 2, 4, 10, 7, 12, 6]
len1 = len(arr)
long_band = list()
chain = list()

def band(i):
    chain.append(i)
    j = i+1
    if j in arr:
        band(j)
    else:
        long_band.append(chain[:])
        del chain[:]


def isHead(n):
    x = n-1
    if x in arr:
        None
    else:
        band(n)


def FindMaxLen(long_band):
    maxLength = max(len(x) for x in long_band)
    print(maxLength)


for i in range(len1):
    isHead(arr[i])


print(long_band)
FindMaxLen(long_band)