# Python to julia

**URL:** <https://discourse.julialang.org/t/python-to-julia/44343>\
**Category:** New to Julia\
**Created:** [August 5, 2020, 3:22pm UTC](https://discourse.julialang.org/t/python-to-julia/44343 "2020-08-05T15:22:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fuad\_Sami](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fuad_sami/32/16900_2.png) [@Fuad\_Sami](https://discourse.julialang.org/u/Fuad_Sami)\
**Post date:** [August 5, 2020, 3:22pm UTC](https://discourse.julialang.org/t/python-to-julia/44343/1 "2020-08-05T15:22:12Z")

</div>

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

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

```julia
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 ?

```julia
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 ?

```julia
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)`

---

<div class="post-metadata">

**Author:** ![anon37204545](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon37204545](https://discourse.julialang.org/u/anon37204545)\
**Post date:** [August 5, 2020, 3:25pm UTC](https://discourse.julialang.org/t/python-to-julia/44343/2 "2020-08-05T15:25:45Z")

</div>

> [@Fuad\_Sami](#):
>
> 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.

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

It should work like this:

```julia
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`)

---

<div class="post-metadata">

**Author:** ![Fuad\_Sami](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fuad_sami/32/16900_2.png) [@Fuad\_Sami](https://discourse.julialang.org/u/Fuad_Sami)\
**Post date:** [August 5, 2020, 3:34pm UTC](https://discourse.julialang.org/t/python-to-julia/44343/3 "2020-08-05T15:34:43Z")

</div>

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

```julia
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

```julia
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)

```

---

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [August 5, 2020, 3:51pm UTC](https://discourse.julialang.org/t/python-to-julia/44343/4 "2020-08-05T15:51:22Z")

</div>

[quote=“Fuad\_Sami, post:3, topic:44343”]

```julia
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

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [August 5, 2020, 4:18pm UTC](https://discourse.julialang.org/t/python-to-julia/44343/5 "2020-08-05T16:18:03Z")

</div>

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

```julia
function my_char_checker(check, words)
    for word in words
        check = filter(in(check), word)
    end
    return check
end

```

```julia
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!

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [August 5, 2020, 4:29pm UTC](https://discourse.julialang.org/t/python-to-julia/44343/6 "2020-08-05T16:29:34Z")

</div>

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

```julia
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.

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [August 5, 2020, 10:05pm UTC](https://discourse.julialang.org/t/python-to-julia/44343/7 "2020-08-05T22:05:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Fuad\_Sami](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fuad_sami/32/16900_2.png) [@Fuad\_Sami](https://discourse.julialang.org/u/Fuad_Sami)\
**Post date:** [August 6, 2020, 7:55am UTC](https://discourse.julialang.org/t/python-to-julia/44343/8 "2020-08-06T07:55:34Z")

</div>

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…
