Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia - last() does not work inside the function

Tags:

julia

I am writing the below code, and in the code the last() function does not work. I get the below error.

ERROR: UndefVarError: last not defined

But when I use last() outside of the function with same logic it works.

I am trying to write the below function -

function mergeOverlappingIntervals(intervals)
    sort!(intervals, by = x -> intervals[1])
    new_interval = intervals[1]
    for i in range(2, length(intervals))
        if last(new_intervals)[2] >= intervals[i][1]
            last(new_intervals) = [minimum!(last(new_intervals)[1], intervals[i][1]), maximum!(last(new_intervals)[2], intervals[2])]
        else
            push!(new_interval, intervals[i])
        end
    end
end

Can you please help?

like image 801
Harneet.Lamba Avatar asked Oct 27 '25 22:10

Harneet.Lamba


2 Answers

If you are trying to merge the intervals, note that your sort does not work since `by = x -> intervals[1] is a constant: you wanted to say by = x -> x[1]. and the default sort on vectors does that already.

You could instead do:

using Random

intervals = shuffle([[1, 2], [3, 5], [4, 6]])

function mergeintervals(intervals)
    @assert length(intervals) > 1
    sort!(intervals)
    a = [copy(intervals[begin])]
    for v in @view intervals[begin+1:end]
        if a[end][end] >= v[begin]
            if a[end][end] < v[end]
                a[end][end] = v[end]
            end
        else
            push!(a, copy(v))
        end
    end
    return a
end

@show mergeintervals(intervals) # [[1, 2], [3, 6]]

The reason last(x) does not work is that (unlike x[end]) it does not return an lvalue, that is, it does not produce a value or syntactic expression that you can assign to. So Julia thinks you are trying to redefine the function last(x) when you attempt to assign to it (as DNF pointed out). (An lvalue is something that can be used as the left hand side of an assignment, which in Julia does not mean it is a direct memory address: see below).

like image 116
Bill Avatar answered Oct 29 '25 13:10

Bill


A straightforward implementation, after fixing two errors. last() cannot be assigned to as others said, and minimum or maximum are used with arrays, you want max here to compare scalars. Finally you should return the new merged stack of intervals.

function mergeIntervals(intervals)
    sort!(intervals)
    stack = [intervals[1]]
    for i in 2:length(intervals)
        if stack[end][2] < intervals[i][1]
            push!(stack, intervals[i])
        else
            stack[end][2] = max(stack[end][2], intervals[i][2])
        end
    end
    return stack
end

Test an example:

intervals = [[6, 8], [1, 9], [2, 4], [4, 7]]
mergeIntervals(intervals) 
[[1, 9]]
like image 41
AboAmmar Avatar answered Oct 29 '25 15:10

AboAmmar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!