Compile error

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

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

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.

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.

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

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

i need it in this way

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…

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

that’s a mistake it should be

d[i] = 1 

in python or java it was work

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

Compiler Message

Runtime Error

Error (stderr)

    ERROR: LoadError: UndefVarError: STDIN not defined

stdin instead

Wrong understanding

why ?

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:

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)

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

This code worked:

# 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.

2 Likes

amazing,
thanks

# 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))
1 Like