Mercurial > lbo > hg > juliaplay
changeset 40:5e3662b65004 default tip
Land/water find peak problem
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Mon, 03 Apr 2023 22:04:42 +0200 |
parents | 8a21e7a32029 |
children | |
files | julia/JSONStructs/src/metaparser.jl julia/JSONStructs/test/runtests.jl julia/arrays.jl |
diffstat | 3 files changed, 51 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/julia/JSONStructs/src/metaparser.jl Sat Mar 25 21:39:20 2023 +0100 +++ b/julia/JSONStructs/src/metaparser.jl Mon Apr 03 22:04:42 2023 +0200 @@ -59,7 +59,6 @@ :take_list! end function map_type_to_parse_method(::Type{T})::Symbol where {T} - error("unexpected type") if isstructtype(T) :take_struct! else
--- a/julia/JSONStructs/test/runtests.jl Sat Mar 25 21:39:20 2023 +0100 +++ b/julia/JSONStructs/test/runtests.jl Mon Apr 03 22:04:42 2023 +0200 @@ -12,8 +12,23 @@ json = "{\"a\": 33, \"b\": 55.55, \"c\": [\"xyz\", \"abc\"]}" have = parse_struct(TestStruct1, json) want = TestStruct1(33, 55.55, ["xyz", "abc"]) + @show have, want @assert string(have) == string(want) "$have == $want" end +@json_parseable struct OuterStruct + a::String + b::TestStruct1 +end +function test_parse_2() + json = "{\"a\": \"Outer Struct\", \"b\": {\"a\": 33, \"b\": 55.55, \"c\": [\"xyz\", \"abc\"]}}" + have = parse_struct(OuterStruct, json) + want = OuterStruct("Outer Struct", TestStruct1(33, 55.55, ["xyz", "abd"])) + @show have, want + @assert string(have) == string(want) "$have == $want" +end + +println("Starting JSONStructs test") test_parse_1() +test_parse_2()
--- a/julia/arrays.jl Sat Mar 25 21:39:20 2023 +0100 +++ b/julia/arrays.jl Mon Apr 03 22:04:42 2023 +0200 @@ -489,3 +489,39 @@ end subarrays end + +# https://interviewing.io/questions/find-peak-element +function find_peak_element(m::Matrix{Int}) + waters = Tuple.(findall((==)(1), m)) + + up(ix) = (ix) .+ (-1, 0) + down(ix) = (ix) .+ (1, 0) + left(ix) = (ix) .+ (0, -1) + right(ix) = (ix) .+ (0, 1) + inbounds(ix) = begin + a, b = (ix) + a > 0 && b > 0 && a <= size(m, 1) && b <= size(m, 2) + end + + mark(ix, n) = begin + if !inbounds(ix) + return + end + if !(ix in waters) + if m[ix...] > 0 && m[ix...] <= n + return + end + m[ix...] = n + end + mark(up(ix), n+1) + mark(down(ix), n+1) + mark(left(ix), n+1) + mark(right(ix), n+1) + end + + for w in waters + m[w...] = 0 + mark(w, 0) + end + m +end