Bookworm辞書形式を解析する


42

私は最近、Bookworm Deluxeの形の懐かしさに夢中になりました。

まだ見たことがない場合は、隣接するタイルを接続して単語を形成することが目的のワードゲームです。文字列が有効な単語かどうかを判断するために、次のような圧縮形式で保存されている内部辞書と照合します。

aa
2h
3ed
ing
s
2l
3iis
s
2rdvark
8s
4wolf
7ves

辞書を展開するためのルールは簡単です:

  1. 行の先頭の数字を読み、前の単語の先頭からその数の文字をコピーします。(数字がない場合は、前回と同じ数の文字をコピーします。)

  2. 単語に次の文字を追加します。

したがって、最初の単語はでaa、その後にが続きます2h。これは、「の最初の2文字をコピーしてaa追加」を意味haahます。次にに3edなりaahed、次の行には数字がないため、3文字を再度コピーしてformにしaahingます。このプロセスは、辞書の残りの部分で継続されます。小さなサンプル入力から得られる単語は次のとおりです。

aa
aah
aahed
aahing
aahs
aal
aaliis
aals
aardvark
aardvarks
aardwolf
aardwolves

あなたの課題は、このアンパックをできるだけ少ないバイトで実行することです。

入力の各行に0-9 は、1つ以上の小文字が続くゼロ以上の数字が含まれますa-z。入力を取得し、出力を文字列のリストとして、または単語を0-9/ 以外の文字で区切った単一の文字列として出力できますa-z

次に、別の小さなテストケースを示します。この例ではカバーされていないエッジケースがいくつかあります。

abc cba 1de fg hi 0jkl mno abcdefghijk 10l
=> abc cba cde cfg chi jkl mno abcdefghijk abcdefghijl

完全な辞書でコードをテストすることもできます: inputoutput


2行目に数字がない可能性はありますか?また、0先頭に0s がある以外の数はないと想定できますか?
エリックアウトゴルファー

@EriktheOutgolferはい、可能です。これをテストケースに追加しました。そして、はい、あなたはそれを仮定することができます(同様に、数字は前の単語の長さより大きくならないでしょう)。
ドアノブ

11
それはかわいい圧縮形式です:]
Poke

1
locateプログラムは、パス名にエンコードのこのタイプを使用しています。
ダンD.

約15年前、実際に使用するためにこのプログラムを作成しました。残念ながら、私はもうソースを持っていないと思う...
ホッブズ

回答:


13

Vim、57バイト

:%s/\a/ &
:%norm +hkyiwjP
:g/\d/norm diw-@"yl+P
:%s/ //g

オンラインでお試しください!


<H<G代わりに、最後の置換作業の?
Kritixi Lithos

@cowsquack残念ながら、いいえ。数字で始まらない入力はすべて先頭のスペースの数を増やすため、<ソリューションが十分なインデントを解除することを保証する方法はありません。
DJMcMayhem

:%s/ *最後の置換の代わりに2バイトを節約できると思います。
デクスターCD

10

JavaScript(ES6)、 66 62  61バイト

a=>a.map(p=s=>a=a.slice([,x,y]=/(\d*)(.*)/.exec(s),p=x||p)+y)

オンラインでお試しください!

コメント済み

a =>                  // a[] = input, re-used to store the previous word
  a.map(p =           // initialize p to a non-numeric value
  s =>                // for each string s in a[]:
    a =               //   update a:
      a.slice(        //     extract the correct prefix from the previous word:
        [, x, y] =    //       load into x and y:
          /(\d*)(.*)/ //         the result of a regular expression which splits the new
          .exec(s),   //         entry into x = leading digits and y = trailing letters
                      //       this array is interpreted as 0 by slice()
        p = x || p    //       update p to x if x is not an empty string; otherwise leave
                      //       it unchanged; use this as the 2nd parameter of slice()
      )               //     end of slice()
      + y             //     append the new suffix
  )                   // end of map()

5

Perl 6の50の 48バイト

nwellnhofのおかげで-2バイト

{my$l;.map:{$!=S[\d*]=substr $!,0,$l [R||]=~$/}}

オンラインでお試しください!

Arnauldのソリューションの移植版。男、そのR||トリックは、「これは可能だと思う」から「な、それは不可能だ」、「多分可能」、そして最後に「あは!」

説明:

{my$l;.map:{$!=S[\d*]=substr $!,0,$l [R||]=~$/}}
{                                              }  # Anonymous code block
 my$l;    # Declare the variable $l, which is used for the previous number
      .map:{                                  }  # Map the input list to
            $!=              # $! is used to save the previous word
               S[\d*]=       # Substitute the number for
                      substr $!,0    # A substring of the previous word
                                 ,              # With the length of 
                                           ~$0     # The num if it exists
                                  $l [R||]=        # Otherwise the previous num

$l [R||]=~$/パートは大体に変換し$l= ~$/||+$l、それはバイトの同じ量を持っている...しかし:(。もともとは、匿名変数を使用してバイトを保存したため、これmy$lはなくなりましたが、スコープはmapコードブロックではなく置換であるため機能しません。しかたがない。とにかく、R逆メタ演算子であるため、の引数を逆にします||。そのため、$l変数には新しい番号(~$/)が割り当てられます。

Perl 6がちょっと冗長なコンパイラエラーをスローしなかった場合、47バイトになる可能性があります=~


5

ルビー49 45 43バイト

$0=$_=$0[/.{0#{p=$_[/\d+/]||p}}/]+$_[/\D+/]

オンラインでお試しください!

説明

$0=                                         #Previous word, assign the value of
   $_=                                      #Current word, assign the value of
      $0[/.{0#{              }}/]           #Starting substring of $0 of length p which is
               p=$_[/\d+/]||p               #defined as a number in the start of $_ if any 
                                 +$_[/\D+/] #Plus any remaining non-digits in $_

5

C、65 57バイト

n;f(){char c[99];while(scanf("%d",&n),gets(c+n))puts(c);}

オンラインでお試しください!

説明:

n;                     /* n is implicitly int, and initialized to zero. */

f() {                  /* the unpacking function. */

    char c[99];        /* we need a buffer to read into, for the longest line in
                          the full dictionary we need 12 + 1 bytes. */

    while(             /* loop while there is input left. */

        scanf("%d",&n) /* Read into n, if the read fails because this line
                          doesn't have a number n's value does not change.
                          scanf's return value is ignored. */

        ,              /* chain expressions with the comma operator. The loop
                          condition is on the right side of the comma. */

        gets(c+n))     /* we read into c starting from cₙ. c₀, c₁.. up to cₙ is
                          the shared prefix of the word we are reading and the
                          previous word. When gets is successful it returns c+n
                          else it will return NULL. When the loop condition is
                          NULL the loop exits. */

        puts(c);}      /* print the unpacked word. */

5

brainfuck、201バイト

,[[[-<+>>>+<<]>-[---<+>]<[[-<]>>]<[-]>>[<<,>>>[-[-<++++++++++>]]++++<[->+<]-[----->-<]<]<]>>>[[>>]+[-<<]>>[[>>]+[<<]>>-]]+[>>]<[-]<[<<]>[->[>>]<+<[<<]>]>[>.>]+[>[-]<,.[->+>+<<]>>----------]<[<<]>-<<<,]

オンラインでお試しください!

入力の最後に末尾の改行が必要です。この要件のないバージョンは6バイト長くなります。

brainfuck、207バイト

,[[[-<+>>>+<<]>-[---<+>]<[[-<]>>]<[-]>>[<<,>>>[-[-<++++++++++>]]++++<[->+<]-[----->-<]<]<]>>>[[>>]+[-<<]>>[[>>]+[<<]>>-]]+[>>]<[-]<[<<]>[->[>>]<+<[<<]>]>[>.>]+[>[-]<,[->+>+<<]>>[----------<.<]>>]<[<<]>-<<<,]

オンラインでお試しください!

どちらのバージョンも、すべての数値が厳密に255未満であると想定しています。

説明

テープは次のように配置されます。

tempinputcopy 85 0 inputcopy number 1 a 1 a 1 r 1 d 0 w 0 o 0 l 0 f 0 ...

「数字」セルは、数字が入力されていない場合は0に等しく、数字nが入力されている場合はn + 1です。入力は、「85」とマークされたセルで行われます。

,[                     take input and start main loop
 [                     start number input loop
  [-<+>>>+<<]          copy input to tempinputcopy and inputcopy
  >-[---<+>]           put the number 85 in the cell where input was taken
  <[[-<]>>]            test whether input is less than 85; ending position depends on result of comparison
                       (note that digits are 48 through 57 while letters are 97 through 122)
  <[-]>                clean up by zeroing out the cell that didn't already become zero
  >[                   if input was a digit:
   <<,>>               get next input character
   >[-[-<++++++++++>]] multiply current value by 10 and add to current input
   ++++                set number cell to 4 (as part of subtracting 47)
   <[->+<]             add input plus 10*number back to number cell
   -[----->-<]         subtract 51
  <]                   move to cell we would be at if input were a letter
 <]                    move to input cell; this is occupied iff input was a digit

                       part 2: update/output word

 >>>                   move to number cell
 [                     if occupied (number was input):
  [>>]+[-<<]>>         remove existing marker 1s and decrement number cell to true value
  [[>>]+[<<]>>-]       create the correct amount of marker 1s
 ]
 +[>>]<[-]             zero out cell containing next letter from previous word
 <[<<]>                return to inputcopy
 [->[>>]<+<[<<]>]      move input copy to next letter cell
 >[>.>]                output word so far
 +[                    do until newline is read:
  >[-]<                zero out letter cell
  ,.                   input and output next letter or newline
  [->+>+<<]            copy to letter cell and following cell
  >>----------         subtract 10 to compare to newline
 ]
 <[<<]>-               zero out number cell (which was 1 to make copy loop shorter)
 <<<,                  return to input cell and take input
]                      repeat until end of input

4

Python 3.6以降、 172 195 156 123 122 121 104バイト

import re
def f(l,n=0,w=""):
 for s in l:t=re.match("\d*",s)[0];n=int(t or n);w=w[:n]+s[len(t):];yield w

オンラインでお試しください!

説明

私は注意し、正規表現を使用しました。これにより、少なくとも17バイトが節約されました。:

t=re.match("\d*",s)[0]

文字列が数字で始まらない場合、この文字列の長さはになります0。この意味は:

n=int(t or n)

なりますn場合tは空、そしてあるint(t)そう。

w=w[:n]+s[len(t):]

正規表現が見つかった数値を削除しs(数値が見つからない場合は、0文字を削除し、s切り捨てられないままにします)n、前の単語の最初の文字を除くすべてを現在の単語フラグメントに置き換えます。そして:

yield w

現在の単語を出力します。


4

Haskell、82 81バイト

tail.map concat.scanl p["",""]
p[n,l]a|[(i,r)]<-reads a=[take i$n++l,r]|1<2=[n,a]

文字列のリストを取得して返します。

オンラインでお試しください!

        scanl p["",""]        -- fold function 'p' into the input list starting with
                              -- a list of two empty strings and collect the
                              -- intermediate results in a list
  p [n,l] a                   -- 1st string of the list 'n' is the part taken form the last word
                              -- 2nd string of the list 'l' is the part from the current line
                              -- 'a' is the code from the next line
     |[(i,r)]<-reads a        -- if 'a' can be parsed as an integer 'i' and a string 'r'
       =[take i$n++l,r]       -- go on with the first 'i' chars from the last line (-> 'n' and 'l' concatenated) and the new ending 'r'
     |1<2                     -- if parsing is not possible
       =[n,a]                 -- go on with the previous beginning of the word 'n' and the new end 'a'
                              -- e.g. [         "aa",     "2h",      "3ed",       "ing"       ] 
                              -- ->   [["",""],["","aa"],["aa","h"],["aah","ed"],["aah","ing"]]
  map concat                  -- concatenate each sublist
tail                          -- drop first element. 'scanl' saves the initial value in the list of intermediate results. 

編集:@Nitrodonのおかげで-1バイト。


1
Haskellの通常のゴルフの知恵とは異なり、ヘルパー関数を中置演算子として定義しないことにより、実際にここで1バイトを節約できます。
ニトロドン

@ニトロドン:よく見かけます!ありがとう!
nimi

3

Japt、19 18 17バイト

最初はArnauldのJSソリューションに触発されました

;£=¯V=XkB ªV +XoB

それを試してみてください

                      :Implicit input of string array U
 £                    :Map each X
   ¯                  :  Slice U to index
      Xk              :    Remove from X
;       B             :     The lowercase alphabet (leaving only the digits or an empty string, which is falsey)
          ªV          :    Logical OR with V (initially 0)
    V=                :    Assign the result to V for the next iteration
             +        :  Append
              Xo      :  Remove everything from X, except
;               B     :   The lowercase alphabet
  =                   :  Reassign the resulting string to U for the next iteration

2

ゼリー、16バイト

⁹fØDVo©®⁸ḣ;ḟØDµ\

オンラインでお試しください!

使い方

⁹fØDVo©®⁸ḣ;ḟØDµ\  Main link. Argument: A (array of strings)

              µ\  Cumulatively reduce A by the link to the left.
⁹                     Yield the right argument.
  ØD                  Yield "0123456789".
 f                    Filter; keep only digits.
    V                 Eval the result. An empty string yields 0.
     o©               Perform logical OR and copy the result to the register.
       ®              Yield the value in the register (initially 0).
        ⁸ḣ            Head; keep that many character of the left argument.
          ;           Concatenate the result and the right argument.
            ØD        Yield "0123456789".
           ḟ          Filterfalse; keep only non-digits.


1

Retina 0.8.2、69バイト

+`((\d+).*¶)(\D)
$1$2$3
\d+
$*
+m`^((.)*(.).*¶(?<-2>.)*)(?(2)$)1
$1$3

オンラインでお試しください!リンクには、より難しいテストケースが含まれています。説明:

+`((\d+).*¶)(\D)
$1$2$3

文字で始まるすべての行について、前の行から数字をコピーし、すべての行が数字で始まるまでループします。

\d+
$*

数値を単項に変換します。

+m`^((.)*(.).*¶(?<-2>.)*)(?(2)$)1
$1$3

バランスグループを使用して、すべて1のsを前の行の対応する文字に置き換えます。(これは1sのすべてのランを置き換えるよりもわずかにゴルファーであることが判明します。)




1

Groovy、74バイト

{w="";d=0;it.replaceAll(/(\d*)(.+)/){d=(it[1]?:d)as int;w=w[0..<d]+it[2]}}

オンラインでお試しください!

説明:

{                                                                        }  Closure, sole argument = it
 w="";d=0;                                                                  Initialize variables
          it.replaceAll(/(\d*)(.+)/){                                   }   Replace every line (since this matches every line) and implicitly return. Loop variable is again it
                                     d=(it[1]?:d)as int;                    If a number is matched, set d to the number as an integer, else keep the value
                                                        w=w[0..<d]+it[2]    Set w to the first d characters of w, plus the matched string


0

Perl 5の -p45の 41バイト

s:\d*:substr($p,0,$l=$&+$l*/^\D/):e;$p=$_

オンラインでお試しください!

説明:

s:\d*:substr($p,0,$l=$&+$l*/^\D/):e;$p=$_ Full program, implicit input
s:   :                           :e;      Replace
  \d*                                       Any number of digits
      substr($p,0,              )           By a prefix of $p (previous result or "")
                  $l=  +                      With a length (assigned to $l) of the sum
                     $&                         of the matched digits
                          *                     and the product
                        $l                        of $l (previous length or 0)
                           /^\D/                  and whether there is no number in the beginning (1 or 0)
                                                (product is $l if no number)
                                    $p=$_ Assign output to $p
                                          Implicit output


0

05AB1E20 19 17 バイト

õUvyþDõÊi£U}Xyá«=

オンラインそれを試してみたり、すべてのテストケースを確認してください

説明:

õ                  # Push an empty string ""
 U                 # Pop and store it in variable `X`
v                  # Loop `y` over the (implicit) input-list
 yþ                #  Push `y`, and leave only the digits (let's call it `n`)
   DõÊi  }         #  If it's NOT equal to an empty string "":
       £           #   Pop and push the first `n` characters of the string
        U          #   Pop and store it in variable `X`
          X        #  Push variable `X`
           yá      #  Push `y`, and leave only the letters
             «     #  Merge them together
              =    #  Print it (without popping)

0

Common Lisp、181バイト

(do(w(p 0))((not(setf g(read-line t()))))(multiple-value-bind(a b)(parse-integer g :junk-allowed t)(setf p(or a p)w(concatenate'string(subseq w 0 p)(subseq g b)))(format t"~a~%"w)))

オンラインでお試しください!

ゴルフをしていない:

(do (w (p 0))   ; w previous word, p previous integer prefix (initialized to 0)
    ((not (setf g (read-line t ()))))   ; read a line into new variable g
                                        ; and if null terminate: 
  (multiple-value-bind (a b)            ; let a, b the current integer prefix
      (parse-integer g :junk-allowed t) ; and the position after the prefix
    (setf p (or a p)                    ; set p to a (if nil (no numeric prefix) to 0)
          w (concatenate 'string        ; set w to the concatenation of prefix
             (subseq w 0 p)             ; characters from the previous word 
             (subseq g b)))             ; and the rest of the current line
    (format t"~a~%"w)))                 ; print the current word

いつものように、Common Lispの長い識別子は、PPCGに特に適していません。



0

C#(Visual C#Interactive Compiler)、134バイト

a=>{int l=0,m,n;var p="";return a.Select(s=>{for(m=n=0;s[m]<58;n=n*10+s[m++]-48);return p=p.Substring(0,l=m>0?n:l)+s.Substring(m);});}

オンラインでお試しください!

@ASCIIOnlyのおかげで-9バイト!

少ないゴルフ...

// a is an input list of strings
a=>{
  // l: last prefix length
  // m: current number of digits
  // n: current prefix length
  int l=0,m,n;
  // previous word
  var p="";
  // run a LINQ select against the input
  // s is the current word
  return a.Select(s=>{
    // nibble digits from start of the
    // current word to build up the
    // current prefix length
    for(m=n=0;
      s[m]<58;
      n=n*10+s[m++]-48);
    // append the prefix from the
    // previous word to the current
    // word and capture values
    // for the next iteration
    return
      p=p.Substring(0,l=m>0?n:l)+
      s.Substring(m);
  });
}


それはかなりクールです:) 行がゼロ()で始まったとき、それはケースを拾っていなかったので、私はに変更l=n>0?n:lしましl=m>0?n:l0jkl。ヒントをありがとう!
ダナ

0

スカラ226の 129 102バイト

ここでの作業(およびGroovyの回答)に対して@ASCIIのみに感謝します。

s=>{var(w,d,r)=("",0,"(\\d*)(.+)".r)
s map(_ match{case r(a,b)=>{if(a>"")d=a.toInt
w=w.take(d)+b}
w})}

オンラインでお試しください!


:| 両方のリンクは同じです
ASCIIのみ

ええ、編集。私はそれをどうやって消すかわからず、急いでいたので、自分がしたことを変更しませんでした。
V.クルトワ



弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.