Ad

def spawn_to_range(arr,val, ran):
if val in arr:
# figure out position of val in arr
pos = arr.index(val)
# new elements to add. Val repeated ran times
new_arr = [val] * ran
# create the final array
final_arr = arr[:pos] + new_arr + arr[pos+1:]
return final_arr
# val was not in the array
else:
return arr

Code
Diff
  • #spawn_to_range = lambda l, x, n: l[:(i:=l.index(x))] + [x]*n + l[i+1:] if x in l else l
    #spawn_to_range=lambda l,x,n:l[:(i:=l.index(x))]+[x]*n+l[i+1:]if x in l else l
    
    def spawn_to_range(arr,val, ran):
        if val in arr:
            # figure out position of val in arr
            pos = arr.index(val)
            # new elements to add. Val repeated ran times
            new_arr = [val] * ran
            # create the final array
            final_arr = arr[:pos] + new_arr + arr[pos+1:]
            return final_arr
        # val was not in the array
        else:
            return arr
        
    • #spawn_to_range = lambda l, x, n: l[:(i:=l.index(x))] + [x]*n + l[i+1:] if x in l else l
    • spawn_to_range=lambda l,x,n:l[:(i:=l.index(x))]+[x]*n+l[i+1:]if x in l else l
    • #spawn_to_range=lambda l,x,n:l[:(i:=l.index(x))]+[x]*n+l[i+1:]if x in l else l
    • def spawn_to_range(arr,val, ran):
    • if val in arr:
    • # figure out position of val in arr
    • pos = arr.index(val)
    • # new elements to add. Val repeated ran times
    • new_arr = [val] * ran
    • # create the final array
    • final_arr = arr[:pos] + new_arr + arr[pos+1:]
    • return final_arr
    • # val was not in the array
    • else:
    • return arr