Информация об изменениях

Сообщение Re: Задачки с Amazon SDE Interview от 04.12.2020 6:42

Изменено 05.12.2020 16:13 novitk

Re: Задачки с Amazon SDE Interview
Здравствуйте, Chriso, Вы писали:

C>1. Дан список строк S[1..N], нужно вернуть список R всех возможных списков таких, что:

C>R[i][j] ∈ S
C>string.Join("", R[i]) ∈ S
C>R[i].Count >= 2

C>Пример:

C>вход [a, b, ab, cd, abcd, fgh, f, g, h]
C>выход [[a, b], [a, b, cd], [ab, cd], [f, g, h]]

Извиняюсь за эзотерическую julia, но я сейчас на ней пытаюсь мыслить:
julia> s = ["a", "b", "ab", "cd", "abcd", "fgh", "f", "g", "h"]
9-element Array{String,1}:
 "a"
 "b"
 "ab"
 "cd""f"
 "g"
 "h"
julia> pairs = map(outer->outer=>filter(inner->inner != outer && occursin(inner, outer), s), s) |> x->filter(e->!isempty(last(e)),x)
3-element Array{Pair{String,Array{String,1}},1}:
   "ab" => ["a", "b"]
 "abcd" => ["a", "b", "ab", "cd"]
  "fgh" => ["f", "g", "h"]

julia> function sbreak(outer::String, inner::Vector{String})::Vector{Vector{String}}
    #println(outer, inner)
    if outer in inner
        [[outer]]
    else
        return vcat([
                let new_outer = outer[length(e)+1:end]; new_inner = filter(e2->e != e2, inner); new_sbreak = sbreak(new_outer, new_inner)                    
                    map(x->[e;x], new_sbreak)
                end 
                for e in inner if startswith(outer, e)
        ]...)
    end
end

julia> sbreak("abcd",["a", "b", "ab", "cd"])
2-element Array{Array{String,1},1}:
 ["a", "b", "cd"]
 ["ab", "cd"]

julia> vcat(map(p->sbreak(first(p), last(p)), pairs)...)
4-element Array{Array{String,1},1}:
 ["a", "b"]
 ["a", "b", "cd"]
 ["ab", "cd"]
 ["f", "g", "h"]
Re: Задачки с Amazon SDE Interview
Здравствуйте, Chriso, Вы писали:

C>1. Дан список строк S[1..N], нужно вернуть список R всех возможных списков таких, что:

C>R[i][j] ∈ S
C>string.Join("", R[i]) ∈ S
C>R[i].Count >= 2

C>Пример:

C>вход [a, b, ab, cd, abcd, fgh, f, g, h]
C>выход [[a, b], [a, b, cd], [ab, cd], [f, g, h]]

Извиняюсь за эзотерическую julia, но я сейчас на ней пытаюсь мыслить:
julia> s = ["a", "b", "ab", "cd", "abcd", "fgh", "f", "g", "h"]
9-element Array{String,1}:
 "a"
 "b"
 "ab"
 "cd""f"
 "g"
 "h"
julia> pairs = map(outer->outer=>filter(inner->inner != outer && occursin(inner, outer), s), s) |> x->filter(e->!isempty(last(e)),x)
3-element Array{Pair{String,Array{String,1}},1}:
   "ab" => ["a", "b"]
 "abcd" => ["a", "b", "ab", "cd"]
  "fgh" => ["f", "g", "h"]

julia> function sbreak(outer::String, inner::Vector{String})::Vector{Vector{String}}
    function sbreak2(outer::String, inner::Vector{String})::Vector{Vector{String}}
        #println(outer, inner)
        if isempty(outer)
            [[""]]
        else
            vcat([
                    let new_outer = outer[length(e)+1:end]; new_inner = filter(e2->e != e2, inner); new_sbreak = sbreak2(new_outer, new_inner)
                        map(x->[e;x], new_sbreak)
                    end 
                    for e in inner if startswith(outer, e)
            ]...)            
        end
    end
    [e[1:end-1] for e in sbreak2(outer, inner)]
end

julia> sbreak("abcd",["a", "b", "ab", "cd"])
2-element Array{Array{String,1},1}:
 ["a", "b", "cd"]
 ["ab", "cd"]

julia> vcat(map(p->sbreak(first(p), last(p)), pairs)...)
4-element Array{Array{String,1},1}:
 ["a", "b"]
 ["a", "b", "cd"]
 ["ab", "cd"]
 ["f", "g", "h"]