# Compile error

**URL:** <https://discourse.julialang.org/t/compile-error/44368>\
**Category:** New to Julia\
**Created:** [August 5, 2020, 9:18pm UTC](https://discourse.julialang.org/t/compile-error/44368 "2020-08-05T21:18:43Z")\
**Posts on this page:** 14\
**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, 9:18pm UTC](https://discourse.julialang.org/t/compile-error/44368/1 "2020-08-05T21:18:43Z")

</div>

hi all  
can any one tell me why this code not working ?  
its from

> **[Hash Tables: Ransom Note | HackerRank](https://www.hackerrank.com/challenges/ctci-ransom-note/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=dictionaries-hashmaps)**
>
> Given two sets of dictionaries, tell if one of them is a subset of the other.

```julia
function checkMagazine(magazine, note)
d = Dict{String,Int}()
out = ""

    for i in magazine
        if i in d
            d[i] +=1
        else
            d[i] -=1
        end
    end

    for i in note
        if i not in d
            print("NO")
            break
        end

        if d[i] ==0
            delete!(d[i])
        end

        d[i] -=1
    end
println("YES")
end

```

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [August 5, 2020, 10:10pm UTC](https://discourse.julialang.org/t/compile-error/44368/2 "2020-08-05T22:10:24Z")

</div>

Seems like your problem is the algorithm itself, not Julia.

What exactly you want to do with your first loop? Why do you decrement the quantity available of some word if it is not already in the dict? (and if it is not in the dict, how will you decrement its value? if it isn’t in the dict it has no value) I would expect you to set the value of a word as `1` if it does not exist, and increment if it already exists. Also, I do not think `in` does what you want, are you sure you do not want `haskey`?

Ideally, you should provide your code, some input, the return of your code for that input, and describe what you wanted instead.

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [August 6, 2020, 12:02am UTC](https://discourse.julialang.org/t/compile-error/44368/3 "2020-08-06T00:02:52Z")

</div>

> [@Fuad\_Sami](#):
>
> `d[i] -=1`

Indentation also can be improved. Really not sure about some of the code… either u didn’t understand the problem properly or some serious logic errors in the code… I suggest reading thru ur code and see if you can follow it. Play it out for one of the test cases.

---

<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:45am UTC](https://discourse.julialang.org/t/compile-error/44368/4 "2020-08-06T07:45:38Z")

</div>

yes u are right the code is wrong ,  
what i need is in the first loop to store the magazine word in the hash like that if i have

```julia
magazine = "give me one grand give today night"
note = "give one grand give today"

```

i need it in this way

```julia
Dict("give" => 0,"today" => 0,"night" => 1,"me" => 1,"grand" => 0,"one" => 0)

```

and in the second loop just check if all the word in note are in magazine. if no print NO  
the finally code is working REPL but still not working in Hackerrank…

```julia
function checkMagazine(magazine, note)
d = Dict{String,Int}()
out = ""
magazine = split(magazine, " ")
note = split(note, " ")
println(magazine)
println(note)
for i in magazine
check = get(d,i,0)
    if check != 0
        d[i] +=1
    else
        d[i] = get(d,i,0) +1
    end
end
for i in note
check = get(d,i,0)
    if check != 0
        d[i] -=1
    else
        return "NO"
        break
    end
end

println(d)
return "YES"
end
magazine = "give me one grand give today night"
note = "give one grand give today"
println(checkMagazine(magazine, note))

```

---

<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:46am UTC](https://discourse.julialang.org/t/compile-error/44368/5 "2020-08-06T07:46:53Z")

</div>

that’s a mistake it should be

```julia
d[i] = 1 

```

in python or java it was work

---

<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:50am UTC](https://discourse.julialang.org/t/compile-error/44368/6 "2020-08-06T07:50:54Z")

</div>

any one know why in Hackerrank not working ?  
this the massege error

```julia
Compiler Message

Runtime Error

Error (stderr)

    ERROR: LoadError: UndefVarError: STDIN not defined

```

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [August 6, 2020, 8:31am UTC](https://discourse.julialang.org/t/compile-error/44368/7 "2020-08-06T08:31:50Z")

</div>

stdin instead

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [August 6, 2020, 8:32am UTC](https://discourse.julialang.org/t/compile-error/44368/8 "2020-08-06T08:32:31Z")

</div>

> [@Fuad\_Sami](#):
>
> just check if all the word in note are in magazine.

Wrong understanding

---

<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, 11:13am UTC](https://discourse.julialang.org/t/compile-error/44368/9 "2020-08-06T11:13:51Z")

</div>

why ?

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [August 6, 2020, 12:20pm UTC](https://discourse.julialang.org/t/compile-error/44368/10 "2020-08-06T12:20:29Z")

</div>

> [@xiaodai](#):
>
> Wrong understanding

Can you expand on that xiaodai? I looked at the problem definition and the code from @Fuad_Sami and I would have guessed it solves the problem. The definition says the words need to be in sufficient quantity and are case-sensitive, so his code makes sense.

@Fuad_Sami, I think the problem is that hackerrank expects you to print just “Yes” or “No”, in a way that is case-sensitive, and not to return them. I cleaned a little your code and changed it, see if this works:

```julia
function checkMagazine(magazine, note)
    d = Dict{String,Int}()
    magazine, note = split(magazine), split(note)
    #println(magazine)
    #println(note)
    for word in magazine
        d[word] = get(d, word, 0) + 1
    end
    for word in note
        if get(d, word, 0) > 0
            d[word] -=1
        else
            println("No")
            return
        end
    end

    #println(d)
    println("Yes")
    return
end

#magazine = "give me one grand give today night"
#note = "give one grand give today"
#magazine = "ive got a lovely bunch of coconuts"
#note = "ive got some coconuts"
#magazine = "two times three is not four"
#note = "two times two is four"
#checkMagazine(magazine, note)

```

---

<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, 12:29pm UTC](https://discourse.julialang.org/t/compile-error/44368/11 "2020-08-06T12:29:29Z")

</div>

not working  
the same error  
and i solve another problem and in REPL worked fine and the same error in hacherrank…

i afraid that my exam is in hackerrank wibsite so i should know why it not working their

if you have 2 min to check it in your side from the hackerrank the link is up thier i will appreciate that

thanks a lot

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [August 6, 2020, 1:02pm UTC](https://discourse.julialang.org/t/compile-error/44368/12 "2020-08-06T13:02:13Z")

</div>

This code worked:

```julia
# Complete the checkMagazine function below.
function checkMagazine(magazine, note)
    d = Dict{String,Int}()
    #magazine, note = split(magazine), split(note)
    #println(magazine)
    #println(note)
    for word in magazine
        d[word] = get(d, word, 0) + 1
    end
    for word in note
        if get(d, word, 0) > 0
            d[word] -=1
        else
            print("No")
            return
        end
    end

    #println(d)
    print("Yes")
    return
end

mn = split(readline(stdin))

#m = parse(Int32, mn[1])

#n = parse(Int32, mn[2])

magazine = split(readline(stdin))

note = split(readline(stdin))

checkMagazine(magazine, note)

```

If it is your teacher that suggested hackerrank, I would advise to you suggest to them to find better tools. It is completely stupid and frustrating for the site to give you bad code outside of the function and ask you to just fill in the function. I had to change the code the site already gives to get a pass.

---

<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, 1:16pm UTC](https://discourse.julialang.org/t/compile-error/44368/13 "2020-08-06T13:16:06Z")

</div>

amazing,  
thanks

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [August 6, 2020, 1:39pm UTC](https://discourse.julialang.org/t/compile-error/44368/14 "2020-08-06T13:39:20Z")

</div>

```julia
# Complete the checkMagazine function below.
function checkMagazine(magazine, note)
    mdict = Dict{String, Int}()
    for mag in magazine        
        mdict[mag] = get(mdict, mag, 0) + 1        
    end
    
    for mag in note
        if (mdict[mag] = get(mdict, mag, 0) - 1) == -1
            return "No"
        end
    end

    "Yes"
end

m,n = parse.(Int32, split(readline(stdin)))

magazine = Array{String}(split(readline(stdin)))
note = Array{String}(split(readline(stdin)))

println(checkMagazine(magazine, note))

```
