バイナリシーケンス


23

d> 1桁の2進数Aを入力として、Bのn番目の桁を見つけるための以下の規則に従って、d桁の2進数Bを出力します。

  • Aの最初の数字と2番目の数字が等しい場合、Bの最初の数字はゼロです。それ以外の場合は1つです。

  • 1 <n <dの場合、Aの(n-1)番目、n番目、および(n + 1)番目の桁が等しい場合、Bのn番目の桁はゼロになります。それ以外の場合は1つです。

  • Aの(d-1)番目とd番目の桁が等しい場合、Bのd番目の桁はゼロです。それ以外の場合は1つです。

ルール

文字列/リストの入出力形式は問題ありません。入出力の別の許可された方法は、整数とそれに続く先行ゼロの数(または先行ゼロの数に続く)です。

コードをできるだけ短くしてください。

テストケース

00 -> 00
01 -> 11
11 -> 00
010111100111 -> 111100111100
1000 -> 1100
11111111 -> 00000000
01010101 -> 11111111
1100 -> 0110

さらに10分間待つ必要があります。そうすれば、帽子が手に入ります。ナイスチャレンジ!
コイナーリンガーアーイング

@cairdcoinheringaahing去年のことを覚えてる...まあ。:-(
0WJYxW9FMN

2
推奨されるテストケース:(1100 -> 0110出力の最初の2桁は、他のすべてのテストケースで常に同じです。最後の2桁も同じです)
Arnauld

このチャレンジまたは25の回答に対して下票が投じられなかったことは嬉しいことです。みなさん、よくできました!
0WJYxW9FMN

回答:


7

Haskell、59 58 54バイト

f s=[1-0^(a-b+a-c)^2|a:b:c:_<-scanr(:)[last s]$s!!0:s]

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

f s=                        -- input is a list of 0 and 1
          s!!0:s            -- prepend the first and append the last number of s to s
      scanr(:)[last s]      --   make a list of all inits of this list
     a:b:c:_<-              -- and keep those with at least 3 elements, called a, b and c
    1-0^(a-b+a-c)^2         -- some math to get 0 if they are equal or 1 otherwise

編集:@ØrjanJohansenは4バイトを節約しました。ありがとう!


文字列出力への切り替えを気にしない場合は"0110"!!(a+b+c)、バイトを保存します。
ライコニ

@Laikoni:ありがとう。でも、数学にバイトが見つかりました。
nimi

2
[last s]scanr初期値に移動できます。
Ørjanヨハンセン

ワオ。inits(インポートあり); 腹筋; if-then-else; マップ(テイク3); zipWith; takeWhile(not.null); chunksOf(そのインポートを含む)...すべてゴルフで去りました!どこか、どこかでゴルフの名声のホールはありますか?
ウィルネス

7

ゼリー、9バイト

.ịṚjṡ3E€¬

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

数字のリストとしてのI / O。

説明:

.ịṚjṡ3E€¬
.ịṚ       Get first and last element
   j      Join the pair with the input list, thus making a list [first, first, second, ..., last, last]
    ṡ3    Take sublists of length 3
      E€  Check if each has all its elements equal
        ¬ Logical NOT each

私の試みとほぼ同じ:P
リーキー修道女

@LeakyNun簡単なチャレンジで同一のコードを取得することは非常に一般的です; p
エリック・ザ・アウトゴルファー

2
説明を追加してもらえますか?
コイナーリンガーをケア

@cairdcoinheringaahing コードを理解している可能性が高いですが、Erikが追加するまで(これがあれば)、これをすべての参照として追加してい.ịます:-インデックス0.5の要素を取得します。以来CEIL(0.5)≠(0.5)床インデックスで要素を返す0および1。Jellyは1つのインデックスが付けられているため、実際には0が最後の要素を取得します。ペアを逆にします(それらはとして返されるためlast, first)。次にj、入力でペアを結合し、ṡ3長さ3の重複するスライスに分割します。E€すべての要素が等しいかどうかを(リストごとに)チェックし、それぞれを¬論理的に否定します。
Xcoder氏17

6

05AB1E、6バイト

¥0.ø¥Ā

I / Oはビット配列の形式です。

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

使い方

¥       Compute the forward differences of the input, yielding -1, 0, or 1 for each
        pair. Note that there cannot be two consecutive 1's or -1's.
 0.ø    Surround the resulting array with 0‘s.
    ¥   Take the forward differences again. [0, 0] (three consecutive equal 
        elements in the input) gets mapped to 0, all other pairs get mapped to a 
        non-zero value.
     Ā  Map non-zero values to 1.





2

15 11バイト

Ẋȯ¬EėSJ§e←→

入力をリストとして受け取り、オンライン試してください!または、I / Oに文字列を使用するこれを試してください

説明

Ẋ(¬Eė)SJ§e←→ -- implicit input, for example [1,0,0,0]
      SJ     -- join self with the following
        §e   --   listify the
                  first and
                  last element: [1,0]
             -- [1,1,0,0,0,0]
Ẋ(   )       -- with each triple (eg. 1 0 0) do the following:
    ė        --   listify: [1,1,0]
   E         --   are all equal: 0
  ¬          --   logical not: 1
             -- [1,1,0,0]

2

ゼリー、8バイト

I0;;0In0

I / Oはビット配列の形式です。

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

使い方

I0;;0In0  Main link. Argument: A (bit array of length d)

I         Increments; compute the forward differences of all consecutive elements
          of A, yielding -1, 0, or 1 for each pair. Note that there cannot be
          two consecutive 1's or -1's.
 0;       Prepend a 0 to the differences.
   ;0     Append a 0 to the differences.
     I    Take the increments again. [0, 0] (three consecutive equal elements in A)
          gets mapped to 0, all other pairs get mapped to a non-zero value.
      n0  Perform not-equal comparison with 0, mapping non-zero values to 1.

:私は多分あなたは、このからインスピレーションを描くことができ、面白い選択肢に到着したI0,0jI¬¬
氏Xcoder

2

JavaScript(ES6)、45バイト

入力を文字の配列として受け取ります。整数の配列を返します。

a=>a.map((v,i)=>(i&&v^p)|((p=v)^(a[i+1]||v)))

テストケース

コメント済み

a =>                  // given the input array a
  a.map((v, i) =>     // for each digit v at position i in a:
    (                 //   1st expression:
      i &&            //     if this is not the 1st digit:
           v ^ p      //       compute v XOR p (where p is the previous digit)
    ) | (             //   end of 1st expression; bitwise OR with the 2nd expression:
      (p = v) ^       //     update p and compute v XOR:
      (a[i + 1] ||    //       the next digit if it is defined
                   v) //       v otherwise (which has no effect, because v XOR v = 0)
    )                 //   end of 2nd expression
  )                   // end of map()


1

ゼリー、16バイト

ḣ2W;ṡ3$;ṫ-$W$E€¬

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

私はこれをゴルフに行こうとしていましたが、エリックはすでにより短い解決策を持っています。私はまだゴルフをしていますが、彼を打ち負かすか、ユニークなアイデアを見つけられない限り、アップデートしません。

説明

ḣ2W;ṡ3$;ṫ-$W$E€¬  Main Link
ḣ2                First 2 elements
  W               Wrapped into a list (depth 2)
   ;              Append
    ṡ3$           All overlapping blocks of 3 elements
       ;          Append
        ṫ-$W$     Last two elements wrapped into a list
             E€   Are they all equal? For each
               ¬  Vectorizing Logical NOT

少ないお金を使うと、任意のエリックさんに、より類似していない
coinheringaahing caird



1

Japt14 13 12バイト

一部はDennisのJellyソリューションから移植されました。入力と出力は数字の配列です。

ä- pT äaT mg

ETHproductionsのおかげで1バイト節約できました。

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


説明

arrayの暗黙的な入力Uä-配列のデルタを取得します。pT配列の末尾に0をプッシュします。äaT絶対デルタを取得する前に、最初に別の0を配列の先頭に追加します。mg配列の要素にマップし、各要素の符号を負の数の場合は-1、0の場合は0、正の数の場合は1として返します。


うーん、05AB1Eの回答のように、配列の先頭と末尾にアイテムを配置するメソッドを作成する良い方法があるのだろうか。私は...それはそれは1バイト短くなるだろうと思う
ETHproductions

@ETHproductionsではA.ä()、2番目の引数を先頭に追加するために、3番目の引数を追加して追加できます。したがって、この場合には、pT äaTなる可能性がäaTT2バイトの節約のために。
シャギー


1

J、32バイト

B=:2&(+./\)@({.,],{:)@(2&(~:/\))

使い方:

B=:                              | Define the verb B
                       2&(~:/\)  | Put not-equals (~:) between adjacent elements of the array, making a new one
            ({.,],{:)            | Duplicate the first and last elements
   2&(+./\)                      | Put or (+.) between adjacent elements of the array

いくつかの@と括弧を省略しました。

段階的な例:

    2&(~:/\) 0 1 0 1 1 1 1 0 0 1 1 1
1 1 1 0 0 0 1 0 1 0 0

    ({.,],{:) 1 1 1 0 0 0 1 0 1 0 0
1 1 1 1 0 0 0 1 0 1 0 0 0

    2&(+./\) 1 1 1 1 0 0 0 1 0 1 0 0 0
1 1 1 1 0 0 1 1 1 1 0 0

    B 0 1 0 1 1 1 1 0 0 1 1 1
1 1 1 1 0 0 1 1 1 1 0 0

0

網膜、35バイト

(.)((?<=(?!\1)..)|(?=(?!\1).))?
$#2

オンラインでお試しください!リンクにはテストケースが含まれます。説明:正規表現は、各入力数字を順番に照合することから始まります。キャプチャグループは、検討中の数字の前または後に別の数字を照合しようとします。?接尾辞は、キャプチャが0または1回を一致させることができます。$#2これを出力数字に変換します。


0

Pyth、15バイト

mtl{d.:++hQQeQ3

ここで試してみてください!

代わりに:

  • mtl{d.:s+hQeBQ3
  • .aM._M.+++Z.+QZ

これは最初の要素を先頭に追加し、最後の要素を追加してから、長さ3のすべての重複する部分文字列を取得し、最後に各サブリスト内の個別の要素の数を取得してデクリメントします。この混乱は真夜中にモバイルで行われたので、簡単なゴルフがあったとしても私は驚かないでしょう。


0

ガイア、9バイト

ọ0+0¤+ọ‼¦

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

説明

ọ0+0¤+ọ!¦〜1つの引数、2進数のリストを受け入れるプログラム。

ọ〜デルタ。
 0+〜0を追加します。
   0〜ゼロをスタックにプッシュします。
    ¤〜スタックの上の2つの引数を交換します。
     +〜連結(最後の3バイトは基本的に0を先頭に追加)。
      ọ〜デルタ。
        ¦〜そして各要素Nについて:
       !〜N≠0の場合は1、それ以外の場合は0。

ガイア、9バイト

ọ0¤;]_ọ‼¦

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


0

C309バイト

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(int argc,char** argv){int d=strlen(argv[1]);char b[d + 1];char a[d + 1];strcpy(a, argv[1]);b[d]='\0';b[0]=a[0]==a[1]?'0':'1';for(int i=1;i<d-1;i++){b[i]=a[i]==a[i+1]&&a[i]==a[i - 1]?'0':'1';}b[d-1]=a[d-1]==a[d-2]?'0':'1';printf("%s\n",b);}

ゴルフにぴったりの言語ではありませんが、それでも答えの価値はあります。ここで試してみてください

説明

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char** argv) {
    /* Find the number of digits in number (taken in as a command line argument) */
    int d = strlen(argv[1]);

    /* d + 1 to account for d digits plus the null character */
    char b[d + 1];
    char a[d + 1];

    /* Saves having to type argv[1] every time we access it. */
    strcpy(a, argv[1]);

    /* Set the null character, so printf knows where our string ends. */
    b[d] = '\0';

    /* First condition */
    /* For those not familiar with ternary operators, this means b[0] is equal to '0' if a[0] equals a[1] and '1' if they aren't equal. */
    b[0] = a[0] == a[1] ? '0' : '1';

    /* Second condition */
    for(int i = 1; i < d - 1; i++) {
        b[i] = a[i] == a[i+1] && a[i] == a[i - 1] ? '0' : '1';
    }

    /* Third condition */
    b[d - 1] = a[d - 1] == a[d - 2] ? '0' : '1';

    /* Print the answer */
    printf("%s\n", b);
}

PPCGへようこそ:)
シャギー

0

APL + WIN、29バイト

(↑b),(×3|3+/v),¯1↑b←×2|2+/v←⎕

数字のベクトルとして画面入力を要求し、数字のベクトルを出力します。

説明

b←×2|2+/v signum of 2 mod sum of successive pairs of elements

×3|3+/v signum of 3 mod sum of successive triples of elements

(↑b),...., ¯1↑b concatenate first and last elements of b for end conditions

0

SNOBOL4(CSNOBOL4)、273バイト

	I =INPUT
	D =SIZE(I)
N	P =P + 1
	EQ(P,1)	:S(S)
	EQ(P,D)	:S(E)
	I POS(P - 2) LEN(2) . L
	I POS(P - 1) LEN(2) . R
T	Y =IDENT(L,R) Y 0	:S(C)
	Y =Y 1
C	EQ(P,D) :S(O)F(N)
S	I LEN(1) . L
	I POS(1) LEN(1) . R :(T)
E	I RPOS(2) LEN(1) . L
	I RPOS(1) LEN(1) . R :(T)
O	OUTPUT =Y
END

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

	I =INPUT			;* read input
	D =SIZE(I)			;* get the string length
N	P =P + 1			;* iNcrement step; all variables initialize to 0/null string
	EQ(P,1)	:S(S)			;* if P == 1 goto S (for Start of string)
	EQ(P,D)	:S(E)			;* if P == D goto E (for End of string)
	I POS(P - 2) LEN(2) . L		;* otherwise get the first two characters starting at n-1
	I POS(P - 1) LEN(2) . R		;* and the first two starting at n
T	Y =IDENT(L,R) Y 0	:S(C)	;* Test if L and R are equal; if so, append 0 to Y and goto C
	Y =Y 1				;* otherwise, append 1
C	EQ(P,D) :S(O)F(N)		;* test if P==D, if so, goto O (for output), otherwise, goto N
S	I LEN(1) . L			;* if at start of string, L = first character
	I POS(1) LEN(1) . R :(T)	;* R = second character; goto T
E	I RPOS(2) LEN(1) . L		;* if at end of string, L = second to last character
	I RPOS(1) LEN(1) . R :(T)	;* R = last character; goto T
O	OUTPUT =Y			;* output
END


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