Skip to content

Instantly share code, notes, and snippets.

@pazworld
Created January 16, 2022 07:49
Show Gist options
  • Save pazworld/8a06404896671f4720b095423e322e83 to your computer and use it in GitHub Desktop.
Save pazworld/8a06404896671f4720b095423e322e83 to your computer and use it in GitHub Desktop.
% this program solve the question from https://twitter.com/nya3_neko2/status/1481503969965142016
-module(gakkou).
-export([permutation/1, start/0, test/0]).
permutation(L) ->
lists:reverse(permutation([], [], L)).
permutation(Results, Current, []) ->
[lists:reverse(Current) | Results];
permutation(Results, Current, Rest) ->
lists:foldl(fun(Elem, Res) ->
permutation(Res, [Elem | Current], lists:delete(Elem, Rest)) end,
Results, Rest).
start() ->
lists:nth(100, lists:usort(permutation("GAKKOU"))).
test() ->
io:format("~w~n", [[[]] =:= permutation([])]),
io:format("~w~n", [["AB", "BA"] =:= permutation("AB")]).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment