Add tags

Add tags to the following comment
Squeak Smalltalk で。

入力をマトリックス化。その全セルについて全方向にチェックして一致したものをメモして出力しています。

Smalltalk のインデックスは1ベースなので、最後の処理は、結果をサンプルと同じ0ベースに合わせるためのつじつま合わせなので通常は不要です。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
| in word cr nCol nRow mat results directions |

in := 'リオウウリウ
ウオリウオリ
オリリオリウ
リリオオウオ'.
word := 'ウオリ'.

cr := Character cr.
directions := {
    #左上 -> (-1@-1). #上 -> (0@-1). #右上 -> (1@-1).
    #左 -> (-1@0).  #右 -> (1@0).
    #左下 -> (1@-1). #下 -> (0@1). #右下 -> (1@1)}.
nCol := (in indexOf: cr) - 1.
nRow := (in occurrencesOf: cr) + 1.
in := in copyWithout: cr. 

mat := Matrix rows: nRow columns: nCol contents: in.
results := OrderedCollection new.
mat indicesDo: [:row :col |
    | pos delta reader |
    directions do: [:dirAssoc |
        pos := col@row.
        delta := dirAssoc value.
        reader := word readStream.
        [(mat at: pos y at: pos x ifInvalid: #NG) = reader peek]
            whileTrue: [pos := pos + delta. reader next].
        reader atEnd ifTrue: [results add: col@row -> dirAssoc key]]].
^(results collect: [:each | each key: each key - 1 asPoint]) asArray

"=> {2@0->#左 . 0@1->#右 . 0@1->#下 . 3@1->#右 . 4@3->#左上} "

Add tags

The input will be splited to tags with space.

Index

Feed

Other

Link

Pathtraq

loading...