Created
December 18, 2013 02:33
-
-
Save pazworld/8016431 to your computer and use it in GitHub Desktop.
「XY-Sort」をErlangで(横へな7参考) ref: http://qiita.com/pazworld/items/8c2316f2a7feb0671040
This file contains hidden or 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
| -module(xysort). | |
| -compile(export_all). | |
| table() -> [ | |
| [4, 1, 4, 2, 1, 3], [7, 3, 2, 0, 5, 0], [2, 3, 6, 0, 6, 7], | |
| [6, 4, 5, 7, 5, 1], [3, 1, 6, 6, 2, 4], [6, 0, 5, 5, 5, 1]]. | |
| solve(Data) -> [X + $0 || X <- hd(lists:foldl(fun(C, Tbl) -> | |
| sort(C, Tbl) end, table(), Data))]. | |
| sort(C, Tbl) when ($u =< C) and (C =< $z) -> sortidx(C - $u + 1, Tbl); | |
| sort(C, Tbl) -> transpose(sortidx(C - $A + 1, transpose(Tbl, [])), []). | |
| sortidx(Idx, Tbl) -> lists:foldl(fun(X, Acc) -> Acc ++ | |
| lists:filter(fun(L) -> lists:nth(Idx, L) =:= X end, Tbl) end, | |
| [], lists:seq(0, 7)). | |
| transpose([[] | _], Acc) -> Acc; | |
| transpose(Tbl, Acc) -> | |
| transpose([tl(L) || L <- Tbl], Acc ++ [[hd(L) || L <- Tbl]]). | |
| test(Data, Expected) -> test(Data, solve(Data), Expected). | |
| test(Data, Result, Expected) -> io:fwrite("~s: ~s -> ~s~n", | |
| [case Result =:= Expected of true -> ok; false -> ng end, | |
| Data, Result]). | |
| tests() -> | |
| test("AvEx", "305027"), %0 | |
| test("A", "112344"), %1 | |
| test("C", "241413"), %2 | |
| test("F", "134214"), %3 | |
| test("u", "236067"), %4 | |
| test("w", "732050"), %5 | |
| test("y", "414213"), %6 | |
| test("yx", "732050"), %7 | |
| test("ux", "236067"), %8 | |
| test("EF", "131424"), %9 | |
| test("DF", "134124"), %10 | |
| test("Au", "055165"), %11 | |
| test("uA", "023667"), %12 | |
| test("By", "234114"), %13 | |
| test("yB", "114342"), %14 | |
| test("yBy", "357020"), %15 | |
| test("yByB", "350072"), %16 | |
| test("AuBvCw", "131244"), %17 | |
| test("FAuFBvFCw", "300527"), %18 | |
| test("AuBv", "112344"), %19 | |
| test("CwDx", "515056"), %20 | |
| test("FzyE", "324114"), %21 | |
| test("uAwDyB", "114324"), %22 | |
| test("zExCvF", "073520"), %23 | |
| test("uFxEv", "002357"), %24 | |
| test("DyCwB", "076362"). %25 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment