Terriers, Wheeler et dos


15

Contexte

La transformation Burrows – Wheeler (BWT) est une permutation réversible des caractères d'une chaîne qui se traduit par de grandes séries de caractères similaires pour certains types de chaînes telles que le texte brut. Il est utilisé, par exemple, dans l' algorithme de compression bzip2 .

Le BWT est défini comme suit:

Étant donné une chaîne d'entrée telle que codegolf, calculez toutes les rotations possibles et triez-les dans l'ordre lexicographique:

codegolf
degolfco
egolfcod
fcodegol
golfcode
lfcodego
odegolfc
olfcodeg

Le BWT de la chaîne codegolfest la chaîne constituée du dernier caractère de chaque chaîne dans cet ordre, c'est-à-dire la dernière colonne du bloc ci-dessus. Pour codegolf, cela donne fodleocg.

En soi, cette transformation n'est pas réversible, car les chaînes codegolfet golfcoderésultent dans la même chaîne. Cependant, si nous savons que la chaîne se termine par un f, il n'y a qu'une seule pré-image possible.

Tâche

Implémentez un programme ou une fonction involutive qui lit une chaîne unique à partir de STDIN ou comme argument de ligne de commande ou de fonction et imprime ou renvoie le BWT ou son inverse de la chaîne d'entrée.

Si la chaîne d'entrée ne contient aucun espace, votre soumission doit ajouter un seul espace à l'entrée et calculer le BWT.

Si la chaîne d'entrée contient déjà un espace, elle doit calculer la préimage du BWT qui a un espace de fin et supprimer cet espace.

Exemples

INPUT:  ProgrammingPuzzles&CodeGolf
OUTPUT: fs&e grodllnomzomaiCrGgPePzu
INPUT:  fs&e grodllnomzomaiCrGgPePzu
OUTPUT: ProgrammingPuzzles&CodeGolf
INPUT:  bt4{2UK<({ZyJ>LqQQDL6!d,@:~L"#Da\6%EYp%y_{ed2GNmF"1<PkB3tFbyk@u0#^UZ<52-@bw@n%m5xge2w0HeoM#4zaT:OrI1I<|f#jy`V9tGZA5su*b7X:Xn%L|9MX@\2W_NwQ^)2Yc*1b7W<^iY2i2Kr[mB;,c>^}Z]>kT6_c(4}hIJAR~x^HW?l1+^5\VW'\)`h{6:TZ)^#lJyH|J2Jzn=V6cyp&eXo4]el1W`AQpHCCYpc;5Tu@$[P?)_a?-RV82[):[@94{*#!;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aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
OUTPUT: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Règles supplémentaires

  • Vous ne pouvez pas utiliser d'opérateurs intégrés qui calculent le BWT (ou son inverse) d'une chaîne.

  • Vous ne pouvez pas utiliser d'opérateurs intégrés qui inversent les fonctions.

  • Votre code peut imprimer une nouvelle ligne de fin si vous choisissez STDOUT pour la sortie, même s'il ne prend pas en charge les nouvelles lignes de fin dans l'entrée.

  • Votre code doit fonctionner pour toute entrée de 500 caractères ASCII imprimables ou moins (0x20 à 0x7E), y compris au plus un espace.

  • Pour l'une des entrées possibles décrites ci-dessus, votre code doit se terminer en moins de dix minutes sur ma machine (Intel Core i7-3770, 16 Go de RAM). Le dernier cas de test devrait être le plus lent, alors assurez-vous de synchroniser votre code avec celui-ci.

    Pour la plupart des langues et des approches, il devrait être facile de respecter le délai. Cette règle vise uniquement à empêcher l'implémentation de l'une ou l'autre des transformations en tant qu'inverse de force brute de l'autre.

  • Les règles de golf à code standard s'appliquent. La soumission la plus courte en octets l'emporte.


"Vous ne pouvez pas utiliser d'opérateurs intégrés qui inversent les fonctions." Je serais très surpris si une telle chose existait!
Hugh Allen

4
@HughAllen J l'a fait inv.
Dennis

Cela pourrait fonctionner pour des fonctions triviales ou intégrées, mais comment pourrait-il fonctionner pour des fonctions générales définies par l'utilisateur? Cet «inv» est-il documenté quelque part?
Hugh Allen

@ HughAllen: Je ne connais pas vraiment J, mais voici un exemple de invtravail sur une fonction définie par l'utilisateur. Je ne sais pas si invcela fonctionnerait pour le BWT, mais mieux vaut prévenir que guérir.
Dennis

En relation, voir codegolf.stackexchange.com/questions/4771/… pour un bzip2 complet.
Keith Randall

Réponses:


9

Pyth, 29 octets

?tthu+VzSGzz}dzseMS.>L+z\ hlz

Manifestation. Harnais de test.

Cela se divise simplement en un segment de codage et de décodage, le code décidant lequel utiliser avec un ternaire.

?tthu+VzSGzz}dzseMS.>L+z\ hlz
                                 Implicit:
                                 z = input()
                                 d = ' '

?           }dz                  If there is a space in the input,
    u     zz                     Update G to the result of the following,
                                 with G starting as z and repeating len(z) times:
     +V                          The vectorized sum of
       zSG                       z and sorted(G)
   h                             Take the first such result, which will consist of
                                 the first character of z followed by the
                                 first cyclic permuation of the pre-BWT string,
                                 which must start with ' '.
 tt                              Remove the first two characters and return.
                 L               Otherwise, left-map (map with the variable as the 
                                 left parameter and a constant as the right)
               .>                cyclic right shift
                  +z\            of z + ' '
                      hlz        over range(len(z)+1)
              S                  Sort the shifted strings,
            eM                   take their last charactes,
           s                     combine into one string and return.

6

CJam, 41 36 35 octets

q:XS&LX,{X\.+$}*0=1>XS+_,,\fm<$zW>?

Testez-le ici.

Explication

q:X   e# Read STDIN and store it in X.
S&    e# Take the set intersection with " ". We'll use this as a truthy/falsy value to
      e# select the correct output later.

# Compute the iBWT:
LX,   e# Push an empty array, compute the length of X.
{     e# Run the following block that many times:
  X\  e# Push X and pull the other array on top.
  .+  e# Add the characters of X to the corresponding line of the other array,
      e# i.e. prepend X as a new column.
  $   e# Sort the rows.
}*
0=    e# Since we just sorted the rows, the first permutation of the output will be
      e# one starting with a space, followed by the string we actually want. So just
      e# pick the first permutation.
1>    e# Remove the leading space.

# Compute the BWT:
XS+   e# Push X and append a space.
_,    e# Get that string's length N.
,\    e# Turn it into a range [0 .. N-1], swap it with the string.
fm<   e# Map each value in the range to the string shifted left by that many characters.
$     e# Sort the permutations.
zW>   e# Transpose the grid and discard all lines but the last.

?     e# Choose between the iBWT and the BWT based on whether the input had a space.

2

Perl 5, 179

Un autre pas si bon de moi. Il y a probablement des gains à gagner dans celui-ci, mais il ne va pas rivaliser avec les langages de golf spécialement conçus.

$_=<>;@y=/./g;if(/ /){@x=sort@y;for(1..$#y){@x=sort map$y[$_].$x[$_],0..$#x}
say$x[0]=~/^ (.*)/}else{push@y," ";say map/(.)$/,sort map{$i=$_;join"",
map$y[($_+$i)%@y],0..$#y}0..$#y}

Non golfé:

# Read input
$_ = <>;
# Get all the chars of the input
my @chars = /./g;

if (/ /) {
  # If there's a space, run the IBWT:
  # Make the first column of the table
  my @working = sort @chars;

  # For each remaining character
  for (1 .. $#chars) {
    # Add the input as a new column to the left of @working,
    # then sort @working again
    @working = sort map {
      $chars[$_] . $working[$_]
    } 0 .. $#working;
  }
  # Print the first element of @working (the one beginning with space), sans space
  say $working[0] =~ /^ (.*)/;
} else {
  # BWT
  # Add a space to the end of the string
  push @chars, " ";
  # Get all the rotations of the string and sort them
  @rows = sort map {
    my $offset = $_;
    join "", map {
      $chars[($_ + $offset) % @chars]
    } 0 .. $#chars
  } 0 .. $#chars;

  # Print all the last characters
  say map /(.)$/, @rows;
}

Quelques améliorations mineures: 1. Si vous utilisez le -ncommutateur (généralement compté comme 1 octet), vous n'en avez pas besoin $_=<>;. 2. for(1..$#y){...}peut devenir ... for 1..$#y. 3. $"est initialisé à " "et un octet plus court.
Dennis

@Dennis bon appel. J'avais eu plusieurs déclarations forà un moment donné, donc le formulaire postfix n'était pas une victoire, mais quand je l'ai réduit à un, je ne l'ai pas remarqué :)
hobbs
En utilisant notre site, vous reconnaissez avoir lu et compris notre politique liée aux cookies et notre politique de confidentialité.
Licensed under cc by-sa 3.0 with attribution required.