阅读填字游戏


11

受有关将其打包成这种格式的问题的启发。

有时,我会看到完整的填字游戏,而且就像我一样,我不费力气找出线索的真正解决方案是什么。

输入:

  • 2D字符串(任何格式,换行符分隔,2d列表等)
  • 空白方块将以(空格字符)表示
  • 每隔一个正方形将使用小写字母。
  • 您可能假设输入将用空格填充以形成一个矩形

输出:

  • 找到每个字
    • 您必须上下搜索单词
    • 单词长度至少为两个字母
    • 如果有重复的单词,则每次出现时都必须输出它们
  • 您不必进行任何验证
  • 单词可以以任何顺序输出
  • 没有严格的格式化规则

测试用例:

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

Answers:


8

腐霉病-11 10 8 7个字节

@issacg节省了一个字节。

t#cjsCB

在这里在线尝试

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

@Maltysen太好了。
Leaky Nun

1
佩斯赢了。一如既往。
Leaky Nun

1
您可以删除d,以节省一个字节,这会j在换行符上进行c ... )
联接

@isaacg真的很酷,谢谢
Maltysen

2

CJam,14个字节

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

一个未命名的块,期望在堆栈顶部包含(填充的)字符串列表,并保留单词列表。

在这里测试。

说明

_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.

1

JavaScript(ES6),83个字节

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

0

Pyth,18个字节

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

在线尝试!

输入样例:

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

样本输出:

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

这个怎么运作:

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

0

Haskell,58个字节

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

用法示例:f ["pies"," not"," no","wasp"]-> ["pies", "not", "no", "wasp", "in", "eons", "stop"]

它是如何工作的:将输入的每一行及其换位分隔为单个列表字。保持匹配(_:_:_),即至少有两个字母。


0

C ++ 14、209 207 201字节

字节数高得可笑...但是哦。转置矩阵,分割字符串。简单。太糟糕了,没有用于转置的本机功能

[](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+' ':"");}}

取消高尔夫:

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+' ':"");
  }
}

如何使用它(请注意,如问题所述,您必须强制执行填充操作):

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

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.