Created
November 15, 2013 13:26
-
-
Save tekkoc/7484292 to your computer and use it in GitHub Desktop.
転置文字列
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
import std.stdio; | |
import std.algorithm; | |
import std.range; | |
void main() | |
{ | |
auto str_list = [ | |
"abcdefg", | |
"dlang", | |
"qwerty", | |
"hello world!", | |
]; | |
writeln(str_list); | |
writeln(str_list.transpose); | |
} | |
string[] transpose(string[] str_list) | |
{ | |
auto width = str_list.reduce!((s1, s2) => (s1.length < s2.length) ? s2 : s1).length; | |
auto height = str_list.length; | |
string[] trans_str_list; | |
trans_str_list.length = width; | |
foreach (i; iota(height)) { | |
foreach (j; iota(width)) { | |
trans_str_list[j] ~= (j < str_list[i].length) ? str_list[i][j] : ' '; | |
} | |
} | |
return trans_str_list; | |
} | |
unittest | |
{ | |
auto sut = [ | |
"12", | |
"34", | |
]; | |
auto expected = [ | |
"13", | |
"24", | |
]; | |
assert(sut.transpose == expected); | |
} | |
unittest | |
{ | |
auto sut = [ | |
"abc", | |
]; | |
auto expected = [ | |
"a", | |
"b", | |
"c", | |
]; | |
assert(sut.transpose == expected); | |
} | |
unittest | |
{ | |
auto sut = [ | |
"abcd", | |
"efg", | |
"hi", | |
]; | |
auto expected = [ | |
"aeh", | |
"bfi", | |
"cg ", | |
"d ", | |
]; | |
assert(sut.transpose == expected); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment