Home > other >  Splitting Array and adding splitted part to end
Splitting Array and adding splitted part to end

Time:06-06

I want to split the array at a point s and add it to the end of the array. But my output is my input list.

def split_and_add(arr, s):
    n = len(arr)
    if n >= s or s <=0:
        return arr
    else:
        end = []
        for i in range(0,s):
            end = end   [arr[i]]
        start = []
        for i in range(s,n):
            start = start   [arr[i]]
        return start   end


print(split_and_add([1,2,3,4,5,6],3))



The output is still [1,2,3,4,5,6]. Could somebody help?

CodePudding user response:

Only issue in your code is in line

if n >= s or s <=0:

Here, you are checking if length of array n is greater than the break point s and if yes, you are returning the originl array. But what you need to do is check if s is greater than or equal to length of array and if yes, return original array. So, all you need to do is replace n >= s with s >= n. So your condition would be

if s >= n or s <=0:
    return arr

Now, this should work.

  • Related