Quantcast
Channel: First steps - JuliaLang
Viewing all articles
Browse latest Browse all 2795

List into list

$
0
0

hi
thanks this site help me to improve my knowledge in julia, so

function pair_sum(nums, target)
#Given nums = [2, 7, 11, 15], target = 9,

d = Dict()
output = Int[]
count =0
for i in nums
    component = target - i
    res = []
    if component in keys(d)
        push!(res,component)
        push!(res,i)
        d[component] -=1
        append!(output,Tuple(res))
        count +=1
    else
        d[i] = 1
    end
end
println(count)
return output
end
nums = [2, 7, 11, 15 , 4 ,5]
target = 9
println("Total pairs   " , pair_sum(nums,target))

this code is work fine, but the output is

[2,7,4,5]

what i need is

[[2,7] , [4,5]]

list into list

9 posts - 5 participants

Read full topic


Viewing all articles
Browse latest Browse all 2795

Trending Articles