-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTop Secret
More file actions
49 lines (43 loc) · 1.36 KB
/
Top Secret
File metadata and controls
49 lines (43 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
defmodule TopSecret do
def to_ast(string) do
# Please implement the to_ast/1 function
{_,result}=Code.string_to_quoted(string)
result
end
def get_function(function_name) do
tuple= List.first(function_name)
params = elem(tuple, 2)
result= tuple
|> elem(0)
|> to_string
case result do
"when" -> guards(params) |> num_letter(params)
_ -> if params == [] or params == nil, do: "", else: String.slice(result, 0, length(params))
end
end
def num_letter(value, params) do
len = if String.last(value) != "?", do: 1 , else: length(params)
String.slice(value,0, len)
end
def guards(params) do
List.first(params)
|> elem(0)
|> to_string
end
def decode_secret_message_part({op,_,function_name}= ast_node, accumulator)when op in [:def, :defp] do
# Please implement the decode_secret_message_part/2 function
{ast_node, [get_function(function_name) | accumulator]}
end
def decode_secret_message_part(ast_node, accumulator) do
# Please implement the decode_secret_message_part/2 function
{ast_node, accumulator}
end
def decode_secret_message(string) do
# Please implement the decode_secret_message/1 function
ast = to_ast(string)
{new_ast, result} = Macro.prewalk(ast, [], fn ast_node,accumulator ->
decode_secret_message_part(ast_node,accumulator)
end)
result|> Enum.reverse |> Enum.join()
end
end