Last active
March 27, 2019 12:45
-
-
Save Ahmah2009/6ba3499d6d6d5cf36fd15b16c5faa9b5 to your computer and use it in GitHub Desktop.
Hacker Rank problems solve by Erlang @Ahmah2009
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% https://www.hackerrank.com/challenges/common-divisors/submissions/code/13608658 | |
% Enter your code here. Read input from STDIN. Print output to STDOUT | |
% Your class should be named solution | |
-module(solution). | |
-export([main/0]). | |
-compile(export_all). | |
main() -> | |
{ok,[A]}=io:fread("", "~d"), | |
scanner(A). | |
scanner(0)-> | |
ok; | |
scanner(N)-> | |
{ok,[A,B]}=io:fread("", "~d ~d"), | |
solve(A,B), | |
scanner(N-1). | |
rotate(List)-> | |
rotate(List,length(List)). | |
rotate(List,1)-> | |
[H|List1]=List, | |
NewList=List1++[H], | |
io:format("~s~n",[NewList]); | |
rotate(List,N)-> | |
[H|List1]=List, | |
NewList=List1++[H], | |
io:format("~s ",[NewList]), | |
rotate(NewList,N-1). | |
solve(A1,B1)-> | |
A=solution:divisors(A1), | |
B=solution:divisors(B1), | |
AS=sets:from_list(A), | |
BS=sets:from_list(B), | |
ABS=sets:intersection(AS,BS), | |
C=sets:size(ABS), | |
io:format("~p~n",[C]). | |
divisors(N) -> | |
L=divisors(N, trunc(math:sqrt(N)), []), | |
divisors_2(N,L). | |
divisors(_N, 0, D) -> D; | |
divisors(N, X, []) when N rem X == 0 -> divisors(N, X - 1, [X]); | |
divisors(N, X, []) when N rem X /= 0 -> divisors(N, X - 1, []); | |
divisors(N, X, D) when N rem X == 0 -> divisors(N, X - 1, [X|D]); | |
divisors(N,X,D) when N rem X /= 0 -> divisors(N,X-1,D). | |
divisors_2(N,Divisors)-> | |
Fun=fun(X,Acc)-> | |
Acc++[N div X] | |
end, | |
Divisors ++lists:foldl(Fun,[],Divisors). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% https://www.hackerrank.com/challenges/eval-ex/problem | |
% Enter your code here. Read input from STDIN. Print output to STDOUT | |
% Your class should be named solution | |
-module(solution). | |
-export([main/0]). | |
-compile(export_all). | |
main() -> | |
{ok,[A]}=io:fread("", "~d"), | |
scanner(A). | |
scanner(0)->ok; | |
scanner(Tc)-> | |
{ok,[A]}=io:fread("", "~f"), | |
R=solve(A), | |
io:format("~.4f~n",[R]), | |
scanner(Tc-1). | |
solve(X)-> | |
1 + | |
for_x(X,1) + | |
for_x(X,2) + | |
for_x(X,3) + | |
for_x(X,4) + | |
for_x(X,5) + | |
for_x(X,6) + | |
for_x(X,7) + | |
for_x(X,8) + | |
for_x(X,9) . | |
for_x(X,I)-> | |
math:pow(X,I)/fact(I). | |
fact(0)->1; | |
fact(N)->N*fact(N-1). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%%https://www.hackerrank.com/challenges/fibonacci-fp/submissions/code/13629482 | |
% Enter your code here. Read input from STDIN. Print output to STDOUT | |
% Your class should be named solution | |
-module(solution). | |
-export([main/0]). | |
-compile(export_all). | |
main() -> | |
{ok,[A]}=io:fread("", "~d"), | |
solve(A). | |
solve(0)-> | |
ok; | |
solve(N)-> | |
{ok,[A]}=io:fread("", "~d"), | |
io:format("~p~n",[fib(A)]), | |
solve(N-1). | |
fib(N)-> | |
fib_arith(N) rem 100000007. | |
fib_arith(0)->0; | |
fib_arith(N) when N > 0 -> fib_arith(N, 0, 1). | |
fib_arith(0, F1, _F2) -> F1; | |
fib_arith(N, F1, F2) -> fib_arith(N - 1, F2, F1 + F2). | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% https://www.hackerrank.com/challenges/fp-reverse-a-list/problem | |
% Enter your code here. Read input from STDIN. Print output to STDOUT | |
% Your class should be named solution | |
-module(solution). | |
-export([main/0]). | |
main() -> | |
solve(). | |
solve()-> | |
Arr=get_array([]), | |
print(Arr). | |
get_array(Acc)-> | |
Acc++ | |
case io:fread("", "~d") of | |
{ok,[A]}-> | |
get_array(Acc)++[A]; | |
_-> | |
[] | |
end. | |
print(Arr)-> | |
Fun=fun | |
(X) -> | |
io:format("~p~n",[X]) | |
end, | |
lists:foreach(Fun,Arr). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% https://www.hackerrank.com/challenges/jumping-bunnies/problem | |
% Enter your code here. Read input from STDIN. Print output to STDOUT | |
% Your class should be named solution | |
-module(solution). | |
-export([main/0]). | |
-export([solve/2]). | |
main() -> | |
{ok, [Tc1]} = io:fread("", "~d"), | |
Pat= string:copies("~d ",Tc1), | |
{ok, [A|List]} = io:fread("", Pat), | |
io:format("~p~n",[solve(List,A)]). | |
mins(X,Y) when X<Y-> | |
{X,Y}; | |
mins(X,Y)-> | |
{X,Y}. | |
gcd(X,0)-> | |
X; | |
gcd(X,Y)-> | |
gcd(Y,(X rem Y)). | |
lcm({X,Y})-> | |
X*Y div gcd(X,Y). | |
solve([],V)-> | |
V; | |
solve([B|Li],V)-> | |
solve(Li,lcm(mins(B,V))). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% https://www.hackerrank.com/challenges/kmp-fp | |
% Enter your code here. Read input from STDIN. Print output to STDOUT | |
% Your class should be named solution | |
-module(solution). | |
-export([main/0]). | |
-compile(export_all). | |
main() -> | |
{ok,[Tc]}=io:fread("", "~d"), | |
solve(Tc). | |
solve(Tc) when Tc =:=0-> | |
ok; | |
solve(Tc)-> | |
{ok,[Source,Find]}=io:fread("", "~s ~s"), | |
io:format("~s~n",[test(Source, Find)]), | |
solve(Tc-1). | |
test(Source, Find) -> | |
case find(Source, Find)of | |
{not_found} -> 'NO'; | |
_-> 'YES' | |
end. | |
find(MWord,SWord) when is_list(MWord),is_list(SWord) -> | |
search(MWord, SWord,1). | |
search(MWord,SWord,M) when M=<length(MWord)-> | |
case lists:nth(M, MWord) == lists:nth(1, SWord) of | |
true -> | |
case doSearch(MWord, SWord, M+1, 2) of | |
{not_found} ->search(MWord, SWord, M+1); | |
Found -> {found} | |
end; | |
false -> search(MWord, SWord,M+1) | |
end; | |
search(MWord,SWord,M) -> | |
{not_found}. | |
doSearch(MWord,SWord,M,I) when I==length(SWord)+1 -> | |
{found,M-I+1,I-1}; | |
doSearch(MWord,SWord,M,I) when M>length(MWord) -> | |
{not_found}; | |
doSearch(MWord,SWord,M,I) -> | |
case lists:nth(M, MWord)==lists:nth(I, SWord) of | |
false -> {not_found}; | |
true -> doSearch(MWord, SWord, M+1, I+1) | |
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% https://www.hackerrank.com/challenges/lambda-march-compute-the-area-of-a-polygon/problem | |
% Enter your code here. Read input from STDIN. Print output to STDOUT | |
% Your class should be named solution | |
-module(solution). | |
-export([main/0]). | |
main() -> | |
{ok, [Tcs]} = io:fread("", "~d"), | |
solve(Tcs). | |
solve(0)-> | |
ok; | |
solve(Tc)-> | |
Data=load(Tc,[]), | |
F= fun(I,Acc) -> | |
V=case I+1=:=length(Data)+1 of | |
false-> | |
dis(lists:nth(I,Data),lists:nth(I+1,Data)); | |
true-> | |
dis(lists:nth(I,Data),lists:nth(1,Data)) | |
end, | |
case I rem 2 of | |
1-> | |
Acc+V; | |
0-> | |
Acc+V | |
end | |
end, | |
R= lists:foldl(F,0,lists:seq(1,length(Data))), | |
io:format("~p",[abs(R)/2]). | |
load(0,Agg)-> | |
Agg; | |
load(T,Agg)-> | |
{ok, [A,B]} = io:fread("", "~d ~d"), | |
load(T-1,Agg++[{A,B}]). | |
dis({X1,Y1},{X2,Y2})-> | |
X1*Y2-X2*Y1. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%%https://www.hackerrank.com/challenges/missing-numbers-fp/problem | |
-module(solution). | |
-export([main/0]). | |
-compile(export_all). | |
main() -> | |
{ok,[A]}=io:fread("", "~d"), | |
Ar=scan_array([],A), | |
{ok,[B]}=io:fread("", "~d"), | |
Br=scan_array([],B), | |
Res=scan(Ar,Br), | |
print(Res). | |
scan_array([],N)-> | |
Line=line_scan(N), | |
{ok,A}=io:fread("", Line), | |
A. | |
line_scan(1)-> | |
"~d"; | |
line_scan(N)-> | |
"~d "++line_scan(N-1). | |
scan(A,B)-> | |
Fun=fun(X,Acc)-> | |
{A1,Acc1}=Acc, | |
case lists:member(X,A1)of | |
true-> | |
{ | |
lists:delete(X,A1),Acc1 | |
}; | |
false-> | |
{ | |
A1,Acc1++[X] | |
} | |
end | |
end, | |
{_,L}=lists:foldl(Fun,{A,[]},B), | |
L. | |
print([])-> | |
io:format("~n"); | |
print(L) when length(L)==1 -> | |
[H|List]=lists:usort(L), | |
io:format("~p",[H]), | |
print(List); | |
print(L)-> | |
[H|List]=lists:usort(L), | |
io:format("~p ",[H]), | |
print(List). | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% https://www.hackerrank.com/challenges/pascals-triangle/problem | |
% Enter your code here. Read input from STDIN. Print output to STDOUT | |
% Your class should be named solution | |
-module(solution). | |
-export([main/0]). | |
main() -> | |
{ok,[A]}=io:fread("", "~d"), | |
R=lists:reverse(pascal(A)), | |
Fun=fun(X) -> | |
print(X) | |
end, | |
lists:foreach(Fun,R). | |
pascal(1)-> [[1]]; | |
pascal(N) -> | |
L = pascal(N-1), | |
[H|_] = L, | |
[lists:zipwith(fun(X,Y)->X+Y end,[0]++H,H++[0])|L]. | |
print(List)-> | |
[H|List1]=List, | |
Fun=fun(X)-> | |
io:format(" ~p",[X]) | |
end, | |
io:format("~p",[H]), | |
lists:foreach(Fun,List1), | |
io:format("~n"). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%%https://www.hackerrank.com/challenges/pentagonal-numbers/problem | |
% Enter your code here. Read input from STDIN. Print output to STDOUT | |
% Your class should be named solution | |
-module(solution). | |
-export([main/0]). | |
-compile(export_all). | |
main() -> | |
{ok,[A]}=io:fread("", "~d"), | |
solve(A). | |
solve(N)-> | |
{ok,A}=io:fread("",line_scan(N)), | |
do_solve(A). | |
do_solve([])-> | |
ok; | |
do_solve(Arr)-> | |
[H|List]=Arr, | |
io:format("~p~n",[ans(H)]), | |
do_solve(List). | |
scan_array([],N)-> | |
Line=line_scan(N), | |
{ok,A}=io:fread("", Line), | |
A. | |
line_scan(1)-> | |
"~d"; | |
line_scan(N)-> | |
"~d "++line_scan(N-1). | |
fib(N)-> | |
fib_arith(N) rem 100000007. | |
fib_arith(0)->0; | |
fib_arith(N) when N > 0 -> fib_arith(N, 0, 1). | |
fib_arith(0, F1, _F2) -> F1; | |
fib_arith(N, F1, F2) -> fib_arith(N - 1, F2, F1 + F2). | |
ans(N)-> | |
N*((3*N)-1)div 2. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%%https://www.hackerrank.com/challenges/remove-duplicates/problem | |
% Enter your code here. Read input from STDIN. Print output to STDOUT | |
% Your class should be named solution | |
-module(solution). | |
-export([main/0]). | |
-compile(export_all). | |
main() -> | |
{ok,[A]}=io:fread("", "~s"), | |
B=scanner(A), | |
io:format("~s~n",[B]). | |
scanner(A)-> | |
Fun=fun(X,Acc)-> | |
case lists:member(X,Acc)of | |
true-> | |
Acc; | |
false-> | |
Acc++[X] | |
end | |
end, | |
lists:foldl(Fun,[],A). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% https://www.hackerrank.com/challenges/rotate-string/problem | |
% Enter your code here. Read input from STDIN. Print output to STDOUT | |
% Your class should be named solution | |
-module(solution). | |
-export([main/0]). | |
-compile(export_all). | |
main() -> | |
{ok,[A]}=io:fread("", "~d"), | |
scanner(A). | |
scanner(0)-> | |
ok; | |
scanner(N)-> | |
{ok,[A]}=io:fread("", "~s"), | |
rotate(A), | |
scanner(N-1). | |
rotate(List)-> | |
rotate(List,length(List)). | |
rotate(List,1)-> | |
[H|List1]=List, | |
NewList=List1++[H], | |
io:format("~s~n",[NewList]); | |
rotate(List,N)-> | |
[H|List1]=List, | |
NewList=List1++[H], | |
io:format("~s ",[NewList]), | |
rotate(NewList,N-1). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% https://www.hackerrank.com/challenges/string-mingling | |
-module (solution). | |
-export ([main/0]). | |
main()-> | |
{ok,[A]}=io:fread("", "~s"), | |
{ok,[B]}=io:fread("", "~s"), | |
R=combine(A,B), | |
io:format("~s~n",[R]). | |
combine(X,Y) when X =:= []; Y =:= [] -> []; | |
combine([H1|A1],[H2|B1])-> | |
[H1,H2|combine(A1,B1)]. | |
% solve(A,B)-> | |
% lists:foldl(Fun,"",[A,B]) | |
% zip3(X, Y) when X =:= []; Y =:= [] -> []; | |
% zip3([HX | TX], [HY | TY]) -> [ HX, HY| zip3(TX, TY)]. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% https://www.hackerrank.com/challenges/string-o-permute/problem | |
% Enter your code here. Read input from STDIN. Print output to STDOUT | |
% Your class should be named solution | |
-module(solution). | |
-export([main/0]). | |
main() -> | |
{ok, [Tcs]} = io:fread("", "~d"), | |
solve(Tcs). | |
load(0,Agg)-> | |
Agg; | |
load(T,Agg)-> | |
{ok, [A]} = io:fread("", "~s"), | |
load(T-1,Agg++[A]). | |
solve(0)-> | |
ok; | |
solve(Tc)-> | |
Data=load(Tc,[]), | |
F= fun(I) -> | |
E= ff(I,""), | |
E1= lists:flatten(E), | |
io:format("~s~n",[E1]) | |
end, | |
lists:foreach(F,Data). | |
ff([],Result)-> | |
Result; | |
ff([A,B],Result)-> | |
[Result,B,A]; | |
ff([A,B|Word],Result)-> | |
ff(Word,[Result,B,A]). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% https://www.hackerrank.com/challenges/string-reductions/problem | |
% Enter your code here. Read input from STDIN. Print output to STDOUT | |
% Your class should be named solution | |
-module(solution). | |
-export([main/0]). | |
-compile(export_all). | |
main() -> | |
{ok,[A]}=io:fread("", "~s"), | |
B=scanner(A), | |
io:format("~s~n",[B]). | |
scanner(A)-> | |
Fun=fun(X,Acc)-> | |
case lists:member(X,Acc)of | |
true-> | |
Acc; | |
false-> | |
Acc++[X] | |
end | |
end, | |
lists:foldl(Fun,[],A). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment