STEAM-RYHMÄ
Kickass Programmers quprogs
STEAM-RYHMÄ
Kickass Programmers quprogs
0
PELISSÄ
6
PAIKALLA
Perustettu
6. marraskuuta 2015
Kieli
englanti
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.
< >
Näytetään 1-1 / 1 kommentista
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"});
Viimeisin muokkaaja on makka; 4.4.2016 klo 1.59
< >
Näytetään 1-1 / 1 kommentista
Sivua kohden: 1530 50