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