Comment detail

与えられた文字列でピラミッド (Nested Flatten)
段階的に目的の文字列へ近づけ、最後に出力します。

1> c(string_pyramid).
{ok,string_pyramid}
2> string_pyramid:string_pyramid("hoge").
   e
  g e
 o g e
h o g e
ok
3> string_pyramid:string_pyramid("abracadabra").
          a
         r a
        b r a
       a b r a
      d a b r a
     a d a b r a
    c a d a b r a
   a c a d a b r a
  r a c a d a b r a
 b r a c a d a b r a
a b r a c a d a b r a
ok
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
-module(string_pyramid).
-import(lists, [flatmap/2, foreach/2, nthtail/2, seq/2]).
-import(string, [centre/2, len/1, strip/3]).
-export([string_pyramid/1]).

string_pyramid(Str) ->
    L1 = [nthtail(len(Str) - X, Str) || X <- seq(1, len(Str))],
    L2 = [flatmap(fun(Y) -> [Y, " "] end, X) || X <- L1],
    L3 = [centre(strip(X, both, " "), len(Str) * 2 - 1) || X <- L2],
    foreach(fun(X) -> io:format("~s~n", [X]) end, L3).

Index

Feed

Other

Link

Pathtraq

loading...