整数を指定して、そのレーベンシュタインコードを計算します


10

免責事項:レーベンシュタインのコーディングはレーベンシュタイン編集距離メトリックとはまったく無関係です。

<レーベンシュタインコードをここで計算する必要がある理由についての詳細な説明を挿入します。>

コード

レーベンシュタイン符号化は、バイナリコードを非負の整数に割り当てるシステムであり、この課題に関係のない確率で奇妙な特性を保持します。このコードをLn)と表記します。ウィキペディアはこれを5段階のプロセスとして説明しています。

  1. 歩数変数Cを1に初期化します。
  2. 1コードの先頭に導くことなく、数値のバイナリ表現を記述します。
  3. ましょMはステップ2に書き込まれたビットの数です。
  4. Mが0でない場合は、Cをインクリメントし、ステップ2からMを新しい数として繰り返します。
  5. C 1ビットとa 0をコードの先頭に書き込みます。

ただし、コードを再帰的に記述することもできます。

  1. 数値が0の場合、そのコードは0です。
  2. 1コードの先頭に導くことなく、数値のバイナリ表現を記述します。
  3. ましょMはステップ2に書き込まれたビットの数です。
  4. LM)をコードの先頭に書き込みます。
  5. 1コードの先頭に少し書き込みます。

例を好む人のために、ここにL(87654321)の再帰的プロセスがあり、連結を示しています:

チャレンジ

数値nを指定して、ビットストリングLn)を適切な形式で出力するプログラムまたは関数を記述します(これには、上記のビットを持つ数値を返すことも含まれます)。標準の抜け穴は、いつものように禁止されています。

入力: 5

出力: 1110001

入力: 30

出力: 111100001110

入力: 87654321

出力: 111110000101001001110010111111110110001

入力: 0

出力: 0

回答:


2

ゼリー13 11 バイト

Ḣ;LÑ$;
BÇṀ¡

オンラインでお試しください!またはすべてのテストケースを確認します

使い方

送信は、相互に再帰的なリンクのペアで構成されます。

BÇṀ¡    Main link. Argument: n

B       Convert n to binary.
   ¡    Execute...
 Ç        the helper link...
  Ṁ       m times, where m is the maximum of n's binary digits.

Ḣ;LÑ$;  Helper link. Argument: A (array of binary digits)

Ḣ       Head; remove and return the first element of A.
    $   Combine the two links to the left into a monadic chain.
  L       Yield the length (l) of A without its first element.
   Ñ      Call the main link with argument l.
 ;      Concatenate the results to both sides.
     ;  Append the tail of A.

8

Haskell、70バイト

b 0=[]
b n=b(div n 2)++[mod n 2]
f 0=[0]
f n|1:t<-b n=1:f(length t)++t

関数を定義しますf : Int -> [Int]。たとえば、f 5 == [1,1,1,0,0,0,1]



4

Mathematica、61バイト

f@0={0};f@n_:=Join[{1},f@Length@#,#]&@Rest@IntegerDigits[n,2]

1
±関数の代わりに単項演算子を定義することで、数バイトを節約できると確信していますf
マーティンエンダー

3

JavaScript(ES6)、54 52バイト

f=n=>(s=n.toString(2)).replace(1,_=>1+f(s.length-1))
<input type=number oninput=o.textContent=f(+this.value)><pre id=o>

編集:@Arnauldのおかげで2バイトが節約されました。


52バイトのreplace(1,...代わりに安全に使用できると思いますreplace(/1/,...
Arnauld

2

Pyth、12バイト

L&bX1.Bbyslb

デモンストレーション

y最後に、結果の関数を入力で実行します)

説明:

L&bX1.Bbyslb
L               def y(b):
 &b             If b is 0, return 0. This is returned as an int, but will be cast
                to a string later.
          lb    Take the log of b
         s      Floor
        y       Call y recursively
   X1           Insert at position 1 into
     .Bb        Convert b to binary.

1

SQF、110

再帰関数:

f={params[["i",0],["l",[]]];if(i<1)exitWith{[0]};while{i>1}do{l=[i%2]+l;i=floor(i/2)};[1]+([count l]call f)+l}

次のように呼び出す: [NUMBER] call f

ArmAエンジンのバグのため、これは実際には87654321またはその他の大きな数値では機能しません。それはおそらくすぐに修正されるでしょうが、仕様に従って動作するはずです。

このチケットはこちら


0

PHP、116114バイト

<?$f=function($i)use(&$f){$b=decbin($i);return!$b?0:preg_replace('/^1/',1 .$f(~~log10($b)),$b);};echo$f($argv[1]);

最初の引数として数値を指定します。

更新:

  • 交換することによりバイトに保存strlen($b)-1して~~log10($b)、異なる連結することで(他のみんなは、対数を使用していた理由をようやく理解される)と別のものを。


0

Java 8(フルプログラム)、257 249バイト

interface M{static void main(String[]a)throws Exception{int i=0,j;while((j=System.in.read())>10)i=i*10+j-48;System.out.print(L(i));}static String L(int i){if(i==0)return "0";String s=Integer.toString(i,2);return "1"+L(s.length()-1)+s.substring(1);}}

説明付きの読み取り可能なバージョン(ほとんどが再帰です):

interface M {
    static void main(String[]a) throws Exception { // Using Exception is unadvised in real coding, but this is Code Gold
        int i = 0, j; // i stores the input; j is a temporary variable
        while ((j = System.in.read()) > 10) // Read the input to j and stop if it is a newline. Technically this stops for tabulators as well, but we shouldn't encounter any of those...
            i = i * 10 + j - 48; // Looping this step eventually reads the whole number in from System.in without using a reader (those take up a lot of bytes)
        System.out.print(L(i)); // Make a method call
    }

    static String L(int i) { // This gets the actual Levenshtein Code
        if (i == 0)
            return "0"; // The program gets a StackOverflowException without this part
        String s = Integer.toString(i, 2); // Shorter than toBinaryString
        return "1" + L(s.length() - 1) + s.substring(1); // Write in the first character (which is always a one), followed by the next L-code, followed by the rest of the binary string
    }
}

編集18バイト保存:バイナリ文字列の最初の文字は常に 1です。したがって、を使用s.charAt(0)するよりも、より良いオプションは単に"1"です。

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