Python to julia

this is a python code that i try to convert to julia.

Input: [“bella”,“label”,“roller”]
Output: [“e”,“l”,“l”]

class Solution:
    def commonChars(self, A: List[str]) -> List[str]:
        check = list(A[0])
        for word in A:
            newCheck = []
            for c in word:
                if c in check:
                    newCheck.append(c)
                    check.remove(c)
            check = newCheck
        
        return check

the syntax in julia is very difficult that i can’t understand. but i need to learn it cuz there is an exam and i should use julia.
i am good i python so i figure it and i try to convert it .

i try.
this is my julia code.
any help ?

arr = ["bella","label","roller"]
check = arr[1] 
for word in arr
    newcheck = " "
    for c in word
        println("new check :" , newcheck)
        s=split(check,"")
        result = occursin(c, s)
        print(result)
        if isnothing(result)
        continue
        push!(newcheck , c)
        println("char in : ",word ," : " , c)
        print(newcheck)
        end
    end
end

where i am stack ?

if c in check:
                    newCheck.append(c)
                    check.remove(c)
            check = newCheck

is julia have the ability to check if char in word ?
like this if c in check:

and if i can update string like this check = newCheck
remove ? like this check.remove(c)

My god what we have reached, people having to learn Julia in their classes? :smile: You can explore Julia more, you may like it.

It should work like this:

if c in check
    push!(newCheck, c)
    replace(check, c => "")
end
check = newCheck

(yes, you can check if char in string, and use check = newCheck)

1 Like

thanks dear,
they don’t learn this language just the exam in julia…

so after update my finally code still hav an error , it is like that

arr = ["bella","label","roller"]
check = arr[1] 
for word in arr
    newcheck = " "
    for c in word
        if c in check
            push!(newCheck, c)
            replace(check, c => "")
        end
    end
    replace(check, newcheck)
end

i am using repl.it for writing julia and the error is

error during bootstrap:
**LoadError("main.jl", 3, UndefVarError(:newCheck))**
rec_backtrace at /buildworker/worker/package_linux64/build/src/stackwalk.c:94
record_backtrace at /buildworker/worker/package_linux64/build/src/task.c:224 [inlined]
jl_throw at /buildworker/worker/package_linux64/build/src/task.c:461
jl_undefined_var_error at /buildworker/worker/package_linux64/build/src/rtutils.c:130
jl_get_binding_or_error at /buildworker/worker/package_linux64/build/src/module.c:308
top-level scope at ./main.jl:7
jl_toplevel_eval_flex at /buildworker/worker/package_linux64/build/src/toplevel.c:808
jl_parse_eval_all at /buildworker/worker/package_linux64/build/src/ast.c:873
jl_load at /buildworker/worker/package_linux64/build/src/toplevel.c:878
unknown function (ip: 0x4cce05)
unknown function (ip: 0x46114f)

[quote=“Fuad_Sami, post:3, topic:44343”]

arr = ["bella","label","roller"]
check = arr[1]
for word in arr
    global check
    newcheck = ""
    for c in word
        if c in check
            newcheck =  newcheck * c
            replace(check, c => "")
        end
    end
    check = newcheck
end
println("check is $check")

You should get lle
And the code is meant to work in the REPL and not inside a function
If you write it in a function remove the line “global check”
and put everything in lowercase, this way you do not make typing mistakes like
newCheck

1 Like

Instead of your python class, we can do the same with a brief function.

function my_char_checker(check, words)
    for word in words
        check = filter(in(check), word)
    end
    return check
end
julia> my_char_checker(arr[1], arr)
"lle"

the syntax in julia is very difficult that i can’t understand

Actually, it took me quite a while to understand the python solution, while the above seems perfectly clear to me! Maybe it’s a matter of taste. The above can be understood as: "filter (keep) only the characters in word that also appear in(check), then assign those characters to be the new check." One line = one sentence!

4 Likes

If you just want the intersection of letters that are in all the strings, you can do this:

julia> arr = ["bella", "label", "roller"];

julia> reduce(∩, arr)
2-element Vector{Char}:
 'e': ASCII/Unicode U+0065 (category Ll: Letter, lowercase)
 'l': ASCII/Unicode U+006C (category Ll: Letter, lowercase)

julia> String(reduce(∩, arr))
"el"

This doesn’t handle the same letter appearing multiple times, but it’s unclear if that’s handled in a meaningful way in the original code.

7 Likes

Out of curiosity, in which context have you going to have a Julia exam? Is it a Univ course?

1 Like

NO it’s not
this is in course full stack developer that is for free but you have to pass a test in,
so the test is in data structure so they did it in julia cuz if some one dont knoe python and need to do swaping or sort in array, it a bit difficult to do that with s languages and they who know python will do that in 2 min…