14
\$\begingroup\$

Inspired by this question about packing into this format.

Occasionally I see a completed crossword and being as I am, I can't be bothered to find out what the solutions to the clues actually were.

Input:

  • A 2D string (any format, newline separated, 2d list etc.)
  • Blank squares will be represented with a (space character)
  • Every other square will be in the lower case alphabet.
  • You may assume that the input will be padded with spaces to form a rectangle

Output:

  • Each word found
    • You must search for words along and down
    • Words will be at least two letters long
    • If there are duplicate words, you have to output them for each time they appear
  • You don't have to do any validation
  • Words may be outputted in any order
  • No strict formatting rules

Test cases:

word
e e 
step
t d 

word, step, west, reed
---
pies
 not
  no
wasp

pies, not, no, wasp, in, eons, stop
---
igloo
    n
word

igloo, word, on
\$\endgroup\$
0

11 Answers 11

10
\$\begingroup\$

Pyth - 11 10 8 7 bytes

saved one bytes thanks to @issacg.

t#cjsCB

Try it online here.

t#               Filter by if len > 1
 c               Chop by whitespace by default
  j              Join by newlines
   sCB           Input, implicit and its transpose in one list
\$\endgroup\$
4
  • \$\begingroup\$ @Maltysen Wonderful. \$\endgroup\$
    – Leaky Nun
    Apr 3 '16 at 17:20
  • 1
    \$\begingroup\$ Pyth wins. As always. \$\endgroup\$
    – Leaky Nun
    Apr 3 '16 at 17:41
  • 1
    \$\begingroup\$ You can save one byte by removing the d, which makes j join on newlines, which are still chopped by c ... ) \$\endgroup\$
    – isaacg
    Apr 4 '16 at 0:19
  • \$\begingroup\$ @isaacg that's really cool, thanks \$\endgroup\$
    – Maltysen
    Apr 4 '16 at 0:27
2
\$\begingroup\$

CJam, 14 bytes

{_z+S*S%{,(},}

An unnamed block that expects a list of (padded) strings on top of the stack and leaves a list of words instead.

Test it here.

Explanation

_z    e# Duplicate and transpose the grid.
+     e# Append the transpose to the original grid.
S*    e# Join all lines by spaces to ensure that we don't get words 
      e# spanning multiple lines.
S%    e# Split around spaces, discarding empty segments.
{,(}, e# Filter: keep only those strings with length 2 or greater.
\$\endgroup\$
2
\$\begingroup\$

05AB1E, 9 bytes

ø«€#J˜ʒ¦Ā

Input as a character matrix.

Try it online or verify all test cases.

Explanation:

ø          # Zip/transpose the (implicit) input-matrix; swapping rows/columns
 «         # Merge this list of lists to the (implicit) input list of lists
  €        # For each inner list:
   #       #  Split them on spaces
    J      # Join each innermost list together to a single string
     ˜     # Flatten the entire list
      ʒ    # Filter the strings by:
       ¦   #  Remove the first character of the string (no-op if it's already empty)
        Ā  #  And check that it's non-empty
           # (after which the result is output implicitly)
\$\endgroup\$
1
  • \$\begingroup\$ Typo in title ? \$\endgroup\$
    – Dion
    Oct 10 '20 at 11:06
1
\$\begingroup\$

JavaScript (ES6), 83 bytes

s=>(s+` `+[...(t=s.split`
`)[0]].map((_,i)=>t.map(t=>t[i]).join``)).match(/\w\w+/g)
\$\endgroup\$
1
\$\begingroup\$

T-SQL, 322 bytes

I can't prove that this works, but feel free to test it in Microsoft SQL server Management Studio sql server 2017+

DECLARE @ varchar(max)=
('MICROWAVE
U       S
M OCEAN C
M  OWL  A
I SWELL P
E       E
SCHEDULES');

WITH C as(SELECT 1x,@ a UNION ALL SELECT 
iif(ascii(a)=13,0,x+1),stuff(a,1,1,'')FROM C
WHERE''<a)SELECT w FROM(SELECT(SELECT left(a,1)FROM
c WHERE x=t.x for xml path(''),type).value('.','char(99)')a
FROM c t GROUP BY x UNION ALL SELECT @)r
CROSS APPLY(SELECT*FROM string_split(translate(a,'
','  '),' '))g(w)WHERE len(w)>1

This script will return

MUMMIES
COW
EWE
ALL
ESCAPES
MICROWAVE
OCEAN
OWL
SWELL
SCHEDULES
\$\endgroup\$
1
\$\begingroup\$

J, 30 bytes

[:(#~1<#&>)@,' '<;._1"1@,.],|:

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Pyth, 18 bytes

Lm:d"\S\S+"1byQyCQ

Try it online!

Sample input:

["pies"," not","  no","wasp"," t  "]

Sample output:

[['pies'], ['not'], ['no'], ['wasp'], []]
[[], ['in', 'at'], ['eons'], ['stop']]

How it works:

Lm:d"\S\S+"1byQyCQ                                 The "1" here is mode
                    assign('Q',eval_input())       "1" which means show
                    @memoized                      all matches
L                   def y(b):                               v
 m:d"\S\S+"1b           return map(lambda d:regex(d,"\S\S+",1),b)
             yQ     imp_print(y(Q))
               yCQ  imp_print(y(transpose(Q)))
\$\endgroup\$
0
\$\begingroup\$

Haskell, 58 bytes

import Data.List
f x=[w|w@(_:_:_)<-words=<<x++transpose x]

Usage example: f ["pies"," not"," no","wasp"] -> ["pies", "not", "no", "wasp", "in", "eons", "stop"].

How it works: split each of the lines of the input and the transposition of it at spaces it into a single list words. Keep those that match (_:_:_), i.e. have at least two letters.

\$\endgroup\$
0
\$\begingroup\$

C++14, 209207201 bytes

Ridiculously high amount of bytes... but oh well. Transpose matrix, split string. Easy. Too bad no native function for transposing

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

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):

using S = vector<string>;[](S m) { ... }({"pies", " not", "  no", "wasp"});
\$\endgroup\$
0
\$\begingroup\$

Pyke, 9 bytes

,+Fdc#t(s

Try it here!

Or 7 bytes prettified

,+Fdc#t

Try it here!

\$\endgroup\$
0
\$\begingroup\$

Japt, 8 bytes

Takes input as an array of lines.

cUy)c¸fÅ

Try it (includes all test cases)

If we could include a leading and/or trailing newline in the input then this could be 7 bytes, taking input as a multi-line string.

pÕ·c¸fÅ

Try it

cUy)c¸fÅ     :Implicit input of array U
c            :Concatenate
 Uy          :  U transposed
   )         :End concat
    c        :Flat map
     ¸       :  Split on spaces
      f      :Filter elements that return true after
       Å     :  Slicing off the first character, resulting in the (falsey) empty string when length is <2
pÕ·c¸fÅ       :Implicit input of multi-line string
p             :Append
 Õ            :  The transposition
  ·           :Split on newlines
   c¸fÅ       :As above
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.