arr1 = [6,12,8,10,20,16]
arr2 = [5,4,3,2,1,5]
arrn=[]
for i in range(len(arr1)):
for j in range((arr2[i])):
arrn.append(arr1[i])
print(arrn)
output [6, 6, 6, 6, 6, 12, 12, 12, 12, 8, 8, 8, 10, 10, 20, 16, 16, 16, 16, 16]
Above code works fine, but when put in function it doesn't work Please advise where iam going wrong Here iam try to distribute arr1
as per frequency in arr2 respective elements, it prints fine, but
function is return improper result
x = [6,12,8,10,20,16]
y = [5,4,3,2,1,5]
n = len(x)
def defreq(a,b):
arrn = []
for i in range(n):
print(i,[a[i]],b[i])
arrn += [a[i]] * b[i]
return arrn
print(defreq(x,y))
[6, 6, 6, 6, 6]
question from:
https://stackoverflow.com/questions/65938039/iam-looping-through-one-array-and-printing-frequency-as-in-the-second-arrary-fo