|
|
|
 |
Reply From: |
supper_raptor |
You can make a function for this.
func subtractArr(a : Array, b : Array) -> Array:
var final_arr = Array()
a.sort()
b.sort()
var b_index = 0
var b_size = b.size()
for i in a:
if b_index >= b_size or i != b[b_index]:
final_arr.append(i)
if i >= b[b_index]:
b_index += 1
return final_arr
Usage example
var x = [1,2,3,4,5,6,7,8,9,0]
var y = [1,3,5,7,9]
var r = subtractArr(x,y) # (will return [2,4,6,8,0])
and how to print r
i tried:
func subtractArr(a : Array, b : Array) -> Array:
var final_arr = Array()
a.sort()
b.sort()
var b_index = 0
var b_size = b.size()
for i in a:
if b_index >= b_size or i != b[b_index]:
final_arr.append(i)
if i >= b[b_index]:
b_index += 1
return final_arr
var x = [1,2,3,4,5,6,7,8,9,0]
var y = [1,3,5,7,9]
var r = subtractArr(x,y)
func prin():
print (r)
praveenyadav1602 | 2020-04-09 07:17
Why not just
print(r)
Instead of declaring a whole function for this? However, if you do so, you also have to call your declared function to execute it:
func prin():
print(r)
prin() # Call the function to execute it
Blackthorn | 2020-04-09 07:34
You can make a function to print and final code will be -
func printArr(arr : Array):
for i in arr:
print(i)
func subtractArr(a : Array, b : Array) -> Array:
var final_arr = Array()
a.sort()
b.sort()
var b_index = 0
var b_size = b.size()
for i in a:
if b_index >= b_size or i != b[b_index]:
final_arr.append(i)
if i >= b[b_index]:
b_index += 1
return final_arr
var x = [1,2,3,4,5,6,7,8,9,0]
var y = [1,3,5,7,9]
var r = subtractArr(x,y)
printArr(r)
supper_raptor | 2020-04-09 07:36
@blackthorn
var y = [1,3,5,7,9]
var r = subtractArr(x,y)
print (r)
not working returened error unexpected token: Build-in Func:
func prin():
print(r)
prin()
above also not working error:unexpected token: Identifier:prin
@supper_raptor whole code snipt copy paste but not working error:unexpected token: Identifier:prin
checked indent level, but not working
praveenyadav1602 | 2020-04-09 08:01
Bro printArr
and subtractArr
are functions
printArr
prints array and substractArray
substracts array/list
You need to call substractArray
then printArr
func printArr(arr : Array):
for i in arr:
print(i)
func subtractArr(a : Array, b : Array) -> Array:
var final_arr = Array()
a.sort()
b.sort()
var b_index = 0
var b_size = b.size()
for i in a:
if b_index >= b_size or i != b[b_index]:
final_arr.append(i)
if i >= b[b_index]:
b_index += 1
return final_arr
Both are functions you need to call them to use.
for example if you need to use them in ready() ,then the code is
func _ready():
var x = [1,2,3,4,5,6,7,8,9,0]
var y = [1,3,5,7,9]
var r = subtractArr(x,y) # (will return [2,4,6,8,0])
printArr(r)
supper_raptor | 2020-04-09 10:40