STEAM GROUP
Kickass Programmers quprogs
STEAM GROUP
Kickass Programmers quprogs
1
IN-GAME
7
ONLINE
Founded
November 6, 2015
Language
English
makka Apr 4, 2016 @ 1:53am
Code Golf -- Read a crossword
Take a 2D string (of any format, you may assume it's padded with spaces to form a rectangle) and output each word (>1 character) found both horizontally and vertically. The code golf link is here[codegolf.stackexchange.com]

Example:
word
e e
step
t d
--------
word step west reed

Feel free to discuss and post your answer both here and on code golf.
< >
Showing 1-1 of 1 comments
makka Apr 4, 2016 @ 1:58am 
C++14, 209 bytes

using S=vector<string>;[](S m){S t=m;int C=size(m[0]),j=0;for(;j<C*C;++j)t[j%C][j/C]=m[j/C][j%C];for(j=0;j<C+C;++j){stringstream Z(j<C?m[j]:t[j-C]);string s;while(getline(Z,s,' '))cout<<(size(s)>1?s+' ':"");}}

Transpose matrix, split string. Easy. Too bad no native function for transposing in C++14.

Ungolfed:

using S=vector<string>; [](S m){ S t=m; int C=size(m[0]),j=0; for(;j<C*C;++j)t[j%C][j/C]=m[j/C][j%C]; // Transpose for(j=0;j<C+C;++j){ // since rectangle matrix, we can iterate like so stringstream Z(j<C?m[j]:t[j-C]); // Get string from m or t string s; while(getline(Z,s,' ')) cout<<(size(s)>1?s+' ':""); } }


How to use it (note you must enforce padding as the question states so that the matrix is a rectangle):

using S = vector<string>;[](S m) { ... }({"pies", " not", " no", "wasp"});
Last edited by makka; Apr 4, 2016 @ 1:59am
< >
Showing 1-1 of 1 comments
Per page: 1530 50