单词搜索助手


12

我一直在做词搜索最近,我认为这将是如此,如果所有的单词阅读更容易从左向右。但是重写所有行都需要很大的精力!因此,我正在邀请代码高尔夫球手提供帮助。

(免责声明:以上报道可能不太准确。)

您的代码将采用一个矩形网格,并在两个方向上输出通过它的所有线。

输出必须包含网格的所有8个旋转(基数和主对角线),从上到下,从左到右“读取”。(这意味着每个“行”都将重复-一次向前,一次向后。)

行分隔可以是空格或换行符。如果选择空格,则网格旋转分隔必须是换行符;否则,网格旋转分割必须是两个换行符。

输入示例(以字符,多行字符串或其他合理格式组成的数组)

ABCDE
FGHIJ
KLMNO
PQRST

输出示例(将第一个选项用于除法)

ABCDE FGHIJ KLMNO PQRST
E DJ CIO BHNT AGMS FLR KQ P
EJOT DINS CHMR BGLQ AFKP
T OS JNR EIMQ DHLP CGK BF A
TSRQP ONMLK JIHGF EDBCA
P QK RLF SMGA TNHB OIC JD E
PKFA QLGB RMHC SNID TOJE
A FB KGC PLHD QMIE RNJ SO T

轮换“读取”的顺序并不重要,只要所有八个主干和主要主干都做一次即可。

这是,因此最短的代码获胜。有标准漏洞。


网格仅包含大写字母,还是可以是整个可打印的ASCII?
Denker


@DigitalTrauma:不,不是真的-这个根本不要求您找到任何单词。
Deusovi'3

Answers:


4

Python 3,181字节

def f(s):
 for k in [1,0]*4:
  b=list(zip(*[([' ']*(len(s)-1-n)*k+list(i)+[' ']*n*k)[::-1] for n,i in enumerate(s)]))
  print([' '.join(i).replace(' ','') for i in b])
  if k==0:s=b

说明

def f(s):
 for k in [0]*4:                  # loop 4 times, we don't need the index so [0]*4 is shorter than range(4)
  l=len(s)-1                      # number of line

  # rotation of 45°
  a=[(['.']*(l-n)+list(i)+['.']*n)[::-1] for n,i in enumerate(s)]
  # tranform matrice :
  #  ABC      ..ABC      CBA..
  #  DEF  --> .DEF.  --> .FED.
  #  GHI      GHI..      ..IHG
  b=list(zip(*a))                 # transpose 
  #  CBA..      C..
  #  .FED.  --> BF.
  #  ..IHG      AEI
  #             .DH
  #             ..G
  print(' '.join(''.join(i).replace('.','') for i in b))

  # rotation of 90°
  a=[(list(i))[::-1] for n,i in enumerate(s)]
  # tranform matrice :
  #  ABC      CBA
  #  DEF  --> FED
  #  GHI      IHG
  b=list(zip(*a))                 # transpose 
  #  CBA       CFI
  #  FED   --> BEH
  #  IHG       ADG
  print(' '.join(''.join(i) for i in b))
  s=b

结果

>>> f(['ABCDE','FGHIJ','KLMNO','PQRST'])
['E', 'DJ', 'CIO', 'BHNT', 'AGMS', 'FLR', 'KQ', 'P']
['EJOT', 'DINS', 'CHMR', 'BGLQ', 'AFKP']
['T', 'OS', 'JNR', 'EIMQ', 'DHLP', 'CGK', 'BF', 'A']
['TSRQP', 'ONMLK', 'JIHGF', 'EDCBA']
['P', 'QK', 'RLF', 'SMGA', 'TNHB', 'OIC', 'JD', 'E']
['PKFA', 'QLGB', 'RMHC', 'SNID', 'TOJE']
['A', 'FB', 'KGC', 'PLHD', 'QMIE', 'RNJ', 'SO', 'T']
['ABCDE', 'FGHIJ', 'KLMNO', 'PQRST']

>>> f(['ABCDEF','GHIJKL','MNOPQR','STUVWX'])
['F', 'EL', 'DKR', 'CJQX', 'BIPW', 'AHOV', 'GNU', 'MT', 'S']
['FLRX', 'EKQW', 'DJPV', 'CIOU', 'BHNT', 'AGMS']
['X', 'RW', 'LQV', 'FKPU', 'EJOT', 'DINS', 'CHM', 'BG', 'A']
['XWVUTS', 'RQPONM', 'LKJIHG', 'FEDCBA']
['S', 'TM', 'UNG', 'VOHA', 'WPIB', 'XQJC', 'RKD', 'LE', 'F']
['SMGA', 'TNHB', 'UOIC', 'VPJD', 'WQKE', 'XRLF']
['A', 'GB', 'MHC', 'SNID', 'TOJE', 'UPKF', 'VQL', 'WR', 'X']
['ABCDEF', 'GHIJKL', 'MNOPQR', 'STUVWX']

输出更清晰(189字节)

j=' '.join
def f(s):
 for k in [1,0]*4:
  b=list(zip(*[([' ']*(len(s)-1-n)*k+list(i)+[' ']*n*k)[::-1] for n,i in enumerate(s)]))
  print(j(j(i).replace(' ','') for i in b))
  if k==0:s=b

>>> f(['ABCDE','FGHIJ','KLMNO','PQRST'])
E DJ CIO BHNT AGMS FLR KQ P
EJOT DINS CHMR BGLQ AFKP
T OS JNR EIMQ DHLP CGK BF A
TSRQP ONMLK JIHGF EDCBA
P QK RLF SMGA TNHB OIC JD E
PKFA QLGB RMHC SNID TOJE
A FB KGC PLHD QMIE RNJ SO T
ABCDE FGHIJ KLMNO PQRST

您如何转换矩阵?(代码块2)
CalculatorFeline

@CatsAreFluffy我加在解释一个步骤
二万

我很确定tranforme不是一个字。(转换?)
CalculatorFeline

@CatsAreFluffy ^^好吧,我认为很明显英语不是我的母语
Erwan

很好的答案!
Timtech '16

1

MATL,40字节

t!tP!tP!GXKZyqXI"IZ}w_w2$:"K@2$Xd!]K!XKx

输入是以Matlab表示法的2D char数组:

['ABCDE'; 'FGHIJ'; 'KLMNO'; 'PQRST']

输出在单独的行上包含每个“单词”。

在线尝试!

t          % input 2D char array. Duplicate. The original copy will produce
           % the words left to right when displayed
!          % transpose. This will produce the words up to down
tP!        % duplicate, flip upside down, transpose. This will produce the
           % words right to left
tP!        % Same. This will produce the words down to up 
GXK        % push input again. Copy to clipboard K
Zy         % get size (length-2 vector; [4 5] in the example)
q          % decrement by 1 (gives  [3 4] in the example)
XI         % copy to clipboard I
"          % loop: do this twice, consuming the decremented length-2 vector
  I        %   push decremented size vector again
  Z}       %   split into its two elements (pushes 3 and 4 in the example)
  w_w      %   swap, negave, swap (results in -3, 4 in the example)
  2$:      %   binary range: indices of diagonals  ([-3 -2 -1 0 1 2 3 4]
           %   in the first iteration in the example, [-4 -3 -2 -1 0 1 2 3]
           %   in the second)
  "        %   for each
    K      %     push input (first iteration) or its tranposed version (second)
    @      %     push index of diagonal
    2$Xd!  %     extract that diagonal as a row vector
  ]        %   end for each
  K!XKx    %   update clipboard K with transposed version for next iteration
           % end loop. Display
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.