Skip to content

Instantly share code, notes, and snippets.

@efcasado
Created February 1, 2013 23:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save efcasado/4694814 to your computer and use it in GitHub Desktop.
Save efcasado/4694814 to your computer and use it in GitHub Desktop.
Solucion para la kata 'rectangulos de texto'.
-module(kata).
-define(SIDES, 4).
-export([msg2rect/1]).
msg2rect(Message) ->
VChars = length(Message) div ?SIDES,
HChars = VChars + trunc(((length(Message) rem ?SIDES) / 2)),
{Half1, Half2} = lists:split(trunc(length(Message) / 2), Message),
{Top, Rest1} = lists:split(HChars, Half1),
{Bottom, Rest2} = lists:split(HChars, Half2),
io:format("~s~n", [Top]),
msg2rect(lists:append([Rest1, Rest2]), HChars - 2),
io:format("~s~n", [lists:reverse(Bottom)]).
msg2rect([], _) ->
ok;
msg2rect([H|T], WSpaces) ->
{Middle, Last} = lists:split(length(T) - 1, T),
io:format("~s~s~s~n", [Last, lists:flatten(lists:duplicate(WSpaces, " ")), lists:flatten([H])]),
msg2rect(Middle, WSpaces).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment