Skip to content

Instantly share code, notes, and snippets.

@etataurov
Last active August 29, 2015 13:56
Show Gist options
  • Save etataurov/9181965 to your computer and use it in GitHub Desktop.
Save etataurov/9181965 to your computer and use it in GitHub Desktop.
Facebook task solution http://habrahabr.ru/post/210796/
-module(facebook).
-export([square/1, print_square/1]).
square(N)->square(1, N).
square(Start, 1)->[[Start]];
square(Start, 2)->[[Start, Start+1], [Start+3, Start+2]];
square(Start, N)->
TopRight = Start+N-1,
[lists:seq(Start, TopRight)]++
extend(square(TopRight+3*N-3, N-2), TopRight+3*N-4, TopRight+1, [])++
[lists:reverse(lists:seq(TopRight+N-1, TopRight+2*N-2))].
extend([H|T], Left, Right, Acc)->
extend(T, Left-1, Right+1, [[Left]++H++[Right]|Acc]);
extend([], _, _, Acc)->lists:reverse(Acc).
print_square(N)->
lists:foreach(fun(Row)->io:format("~w~n", [Row]) end, square(N)).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment