Przeanalizuj format słownika Bookworm


42

Niedawno oddałem się nostalgii w postaci Bookworm Deluxe:

Jeśli nie widziałeś go wcześniej, to gra słowna, której celem jest łączenie sąsiadujących ze sobą kafelków w celu utworzenia słów. Aby ustalić, czy łańcuch jest prawidłowym słowem, sprawdza go w wewnętrznym słowniku, który jest przechowywany w skompresowanym formacie, który wygląda następująco:

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

Zasady rozpakowywania słownika są proste:

  1. Przeczytaj numer na początku wiersza i skopiuj tyle znaków z początku poprzedniego słowa. (Jeśli nie ma numeru, skopiuj tyle znaków, ile poprzednio.)

  2. Dołącz następujące litery do słowa.

Zatem nasze pierwsze słowo aa, po 2hktórym następuje , oznacza „kopiowanie pierwszych dwóch liter aai dopisz h”, tworząc aah. Następnie 3edstaje się aahed, a ponieważ następny wiersz nie ma liczby, ponownie kopiujemy 3 znaki w celu utworzenia aahing. Ten proces trwa przez resztę słownika. Wynikowe słowa z małej próbki wejściowej to:

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

Twoim wyzwaniem jest wykonanie tego rozpakowywania w jak najmniejszej liczbie bajtów.

Każdy wiersz wprowadzania będzie zawierać zero lub więcej cyfr, 0-9 po których następuje jedna lub więcej małych liter a-z. Możesz wziąć dane wejściowe i dać wynik jako albo listę ciągów, albo jako pojedynczy ciąg ze słowami oddzielonymi dowolnym znakiem innym niż 0-9/ a-z.

Oto kolejny mały przypadek testowy z kilkoma przypadkami brzegowymi nie ujętymi w przykładzie:

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

Możesz także przetestować swój kod na pełnym słowniku: wejście , wyjście .


Czy istnieje możliwość, że w drugiej linii nie będzie numeru? Czy możemy również założyć, że żadna liczba oprócz 0wiodących nie będzie mieć 0?
Erik the Outgolfer,

@EriktheOutgolfer Tak, to możliwe; Dodałem to do przypadku testowego. I tak, możesz założyć, że (a także że liczba nie będzie większa niż długość poprzedniego słowa).
Klamka

11
To ładny format kompresji:]
Poke

1
locateProgram korzysta z tego rodzaju kodowania na ścieżek.
Dan D.

Napisałem ten program na własne potrzeby, około 15 lat temu. Niestety nie wydaje mi się, że mam już źródło ...
hobbs

Odpowiedzi:


13

Vim, 57 bajtów

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

Wypróbuj online!


Czy <H<Gzamiast ostatniej zamiany zadziałałoby?
Kritixi Lithos

@cowsquack Niestety nie. Każde wejście, które nie zaczyna się od cyfry, zwiększa liczbę wiodących spacji, więc nie ma sposobu, aby zagwarantować, że <rozwiązanie będzie niewystarczająco długie.
DJMcMayhem

Myślę, że możesz zrobić :%s/ *zamiast ostatniego podstawienia, aby zapisać dwa bajty.
Dexter CD

10

JavaScript (ES6),  66 62  61 bajtów

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

Wypróbuj online!

Skomentował

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 bajtów

-2 bajty dzięki nwellnhof

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

Wypróbuj online!

Port rozwiązania Arnaulda . Człowieku, ta R||sztuczka była kolejką górską od „Myślę, że to może być możliwe”, „nie, to niemożliwe”, „trochę może możliwe” i wreszcie „aha!”

Wyjaśnienie:

{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||]=~$/Część grubsza przekłada się $l= ~$/||+$ljednak ... ma taką samą ilość bajtów :(. Początkowo zapisywał bajty za pomocą anonimowej zmiennej, więc my$lzniknął, ale to nie działa, ponieważ zakres jest teraz podstawieniem, a nie mapkodem. No cóż. W każdym razie, Rjest odwrotnym metaoperatorem, więc odwraca argumenty ||, więc do $lzmiennej przypisuje się nową liczbę ( ~$/), jeśli istnieje, w przeciwnym razie sama.

Może to być 47 bajtów, jeśli Perl 6 nie zgłosi błędu redundantnego dla kompilatora =~.


5

Rubinowy , 49 45 43 bajtów

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

Wypróbuj online!

Wyjaśnienie

$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 bajtów

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

Wypróbuj online!

Wyjaśnienie:

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

pieprzenie mózgu , 201 bajtów

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

Wypróbuj online!

Wymaga końcowego znaku nowej linii na końcu danych wejściowych. Wersja bez tego wymagania jest o 6 bajtów dłuższa:

pieprzenie mózgu , 207 bajtów

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

Wypróbuj online!

Obie wersje zakładają, że wszystkie liczby są ściśle mniejsze niż 255.

Wyjaśnienie

Taśma jest ułożona w następujący sposób:

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

Komórka „liczba” jest równa 0, jeśli nie wprowadzono żadnych cyfr, oraz n + 1, jeśli wprowadzono liczbę n. Dane wejściowe są pobierane z komórki oznaczonej „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 bajty

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

Wypróbuj online!

Wyjaśnienie

Uderzyłem i użyłem wyrażeń regularnych. To zaoszczędziło co najmniej 17 bajtów. :

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

Jeśli ciąg nie zaczyna się od cyfry, długość tego ciągu będzie wynosić 0. To znaczy że:

n=int(t or n)

będzie, njeśli tjest pusty, i int(t)inaczej.

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

usuwa liczbę, z której znaleziono wyrażenie regularne s(jeśli nie znaleziono żadnej liczby, usuwa 0znaki, pozostawiając snieskalowane) i zastępuje wszystkie oprócz pierwszego nznaku poprzedniego słowa bieżącym fragmentem słowa; i:

yield w

wyświetla bieżące słowo.


4

Haskell, 82 81 bajtów

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

Pobiera i zwraca listę ciągów znaków.

Wypróbuj online!

        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. 

Edycja: -1 bajt dzięki @Nitrodon.


1
W przeciwieństwie do zwykłej mądrości golfa Haskell, możesz faktycznie zaoszczędzić jeden bajt tutaj, nie definiując funkcji pomocnika jako operatora infix.
Nitrodon,

@Nitrodon: dobrze zauważony! Dzięki!
nimi

3

Japt, 19 18 17 bajtów

Początkowo zainspirowany rozwiązaniem JS firmy Arnauld .

;£=¯V=XkB ªV +XoB

Spróbuj

                      :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

Galaretka , 16 bajtów

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

Wypróbuj online!

Jak to działa

⁹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 bajtów

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

Wypróbuj online! Link zawiera trudniejsze przypadki testowe. Wyjaśnienie:

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

Dla wszystkich linii rozpoczynających się od liter, skopiuj liczbę z poprzedniej linii, zapętlając, aż wszystkie linie zaczną się od liczby.

\d+
$*

Konwertuj liczbę na unary.

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

Użyj grup równoważących, aby zastąpić wszystkie 1litery odpowiednią literą z poprzedniego wiersza. (Okazuje się, że jest nieco bardziej golfowy niż zastępowanie wszystkich serii 1s.)




1

Groovy , 74 bajty

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

Wypróbuj online!

Wyjaśnienie:

{                                                                        }  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 -p , 45 41 bajtów

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

Wypróbuj online!

Wyjaśnienie:

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

05AB1E , 20 19 17 bajtów

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

Wypróbuj online lub sprawdź wszystkie przypadki testowe .

Wyjaśnienie:

õ                  # 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 bajtów

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

Wypróbuj online!

Nie golfowany:

(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

Jak zwykle długie identyfikatory Common Lisp sprawiają, że nie jest on szczególnie odpowiedni dla PPCG.



0

C # (interaktywny kompilator Visual C #) , 134 bajty

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

Wypróbuj online!

-9 bajtów dzięki @ASCIIOnly!

Mniej golfa ...

// 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);
  });
}


To całkiem fajne :) Zmieniłem l=n>0?n:lna, l=m>0?n:lponieważ nie wychwytywało sprawy, gdy linia zaczynała się od zera ( 0jkl). Dzięki za wskazówkę!
dana

Korzystając z naszej strony potwierdzasz, że przeczytałeś(-aś) i rozumiesz nasze zasady używania plików cookie i zasady ochrony prywatności.
Licensed under cc by-sa 3.0 with attribution required.