GRUP STEAM
Kickass Programmers quprogs
GRUP STEAM
Kickass Programmers quprogs
0
ÎN JOC
8
ONLINE
Creat la
6 noiembrie 2015
Limbă
Engleză
Toate discuțiile > General Discussion > Detaliile subiectului
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.
< >
Se afișează 1-1 din 1 comentarii
makka 4 apr. 2016 la 1:58 
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"});
Editat ultima dată de makka; 4 apr. 2016 la 1:59
< >
Se afișează 1-1 din 1 comentarii
Per pagină: 1530 50

Toate discuțiile > General Discussion > Detaliile subiectului