makeAnagram, there is an error that i cannot find in my code

i need to write a function for two strings, and , that may or may not be of the same length, determine the minimum number of character deletions required to make and anagrams. Any characters can be deleted from either of the strings.
For example, if and , we can delete from string and from string so that both remaining strings are and which are anagrams.
for example = a = cde , b = abc ; we delete from a → de and from b → ab so we return 4.


function makeAnagram(a::String,b::String) 

  count = 0
  map = Dict{String, Int}()
  for value in a
     if !haskey(map, value)
       map[value]=1
      continue
     end
  end
   for value in b
     if !haskey(map, value)
       count+=1
      continue
     end
     if haskey(map, value)
       map[value]-=1
      continue
     end
  end
     while haskey(map, value)
           count+=1
     end
return count 
end

a = ["a" ,  "b",  "c"]
b = ["q"]
println("Total  " , makeAnagram(a, b))


Okay but what is the question? :slight_smile:

2 Likes

there is an error that i cannot find where is that

So what is the error message?

i use repl.it for compiling.
this is the error

julia version 1.1.0
error during bootstrap:
LoadError(“main.jl”, 30, MethodError(makeAnagram, ([“a”, “b”, “c”], [“q”]), 0x00000000000063e2))
rec_backtrace at /buildworker/worker/package_linux64/build/src/stackwalk.c:94
record_backtrace at /buildworker/worker/package_linux64/build/src/task.c:217 [inlined]
jl_throw at /buildworker/worker/package_linux64/build/src/task.c:417
jl_method_error_bare at /buildworker/worker/package_linux64/build/src/gf.c:1649
jl_method_error at /buildworker/worker/package_linux64/build/src/gf.c:1667
jl_apply_generic at /buildworker/worker/package_linux64/build/src/gf.c:2195
do_call at /buildworker/worker/package_linux64/build/src/interpreter.c:323
eval_value at /buildworker/worker/package_linux64/build/src/interpreter.c:411
eval_stmt_value at /buildworker/worker/package_linux64/build/src/interpreter.c:362 [inlined]
eval_body at /buildworker/worker/package_linux64/build/src/interpreter.c:773
jl_interpret_toplevel_thunk_callback at /buildworker/worker/package_linux64/build/src/interpreter.c:885
Interpreter frame (ip: 0)
Core.CodeInfo(code=Array{Any, (3,)}[
Expr(:call, :makeAnagram, :a, :b),
Expr(:call, :println, "Total ", SSAValue(1)),
Expr(:return, SSAValue(2))], codelocs=Array{Int32, (3,)}[1, 1, 1], method_for_inference_limit_heuristics=nothing, ssavaluetypes=3, linetable=Array{Any, (1,)}[Core.LineInfoNode(mod=Main, method=Symbol(“top-level scope”), file=:none, line=0, inlined_at=0)], ssaflags=Array{UInt8, (0,)}, slotflags=Array{UInt8, (0,)}, slotnames=Array{Any, (0,)}, inferred=false, inlineable=false, propagate_inbounds=false, pure=false)jl_interpret_toplevel_thunk at /buildworker/worker/package_linux64/build/src/interpreter.c:894
jl_toplevel_eval_flex at /buildworker/worker/package_linux64/build/src/toplevel.c:764
jl_parse_eval_all at /buildworker/worker/package_linux64/build/src/ast.c:883
jl_load at /buildworker/worker/package_linux64/build/src/toplevel.c:826
exec_program at /gocode/src/github.com/replit/prybar/languages/julia/pry.h:48
runtime.asmcgocall at /usr/lib/go-1.10/src/runtime/asm_amd64.s:688

Well, if I paste your code into the repl I get a different error:

julia> println("Total  " , makeAnagram(a, b))
ERROR: MethodError: Cannot `convert` an object of type Char to an object of type String
Closest candidates are:
  convert(::Type{T<:AbstractString}, ::T<:AbstractString) where T<:AbstractString at strings/basic.jl:207
  convert(::Type{T<:AbstractString}, ::AbstractString) where T<:AbstractString at strings/basic.jl:208
  convert(::Type{T}, ::T) where T at essentials.jl:154
Stacktrace:
 [1] setindex!(::Dict{String,Int64}, ::Int64, ::Char) at ./dict.jl:373
 [2] makeAnagram(::String, ::String) at ./REPL[1]:7
 [3] top-level scope at none:0

julia> 

On which operating system do you work?
Does your Repl work at all?
Try:

julia> 1+1
2

julia> sin(pi)
1.2246467991473532e-16

julia> versioninfo()
Julia Version 1.1.0
Commit 80516ca202 (2019-01-21 21:24 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i7-4800MQ CPU @ 2.70GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.1 (ORCJIT, haswell)
Environment:
  JULIA_NUM_THREADS = 1

yes my repl is work

What is your output of “versioninfo()”?

1.1.0

Well, this is not the output of the function versioninfo().

Sorry, but I have the impression that you want someone else do your homework for you and that you not even sit at a computer that has Julia installed. :frowning:

Dear, I just learn this language and what I asked is to look on the code that I write and check if the logic is fine. What I mean is

  1. if the definition of string in the func is write.
  2. The if like that definition an array of string.
    a = [“a” ,“b”]

That what I need
Thank

Well, I am sorry, but I cannot help you if you do not have access to a computer where Julia is installed. If you only have access to a web-browser you could try Julia box:
https://www.juliabox.com/

can any one help me with the syntex ? i am confused i dont know where is my error

Your function makeAnagram has two strings, a and b, as input paramters.
You try to call it with two arrays of (single letter) strings. As there is not function defined which expects two arrays of strings you are lost.
Try

function makeAnagram(a::Array,b::Array)

Later on in your function body this part

while haskey(map, value)
       count+=1
end

will throw a Undefined error, because value is not defined.

1 Like

thanks a lot , its work