インデックス付きフラクタルを描く


14

前書き

この課題では、2×2マトリックスに次のようにインデックスが付けられます。

0 1
2 3

フラクタルのようなパターンのファミリーを定義しますF(L)。ここでLは、nこれらのインデックスの長さリストであり、F(L)size を持ちます。2n-1 × 2n-1

  • の場合L == []F(L)は1×1パターン#です。
  • の場合L != []F(L)次のように構築されます。ましょう最初の要素が削除されたPから取得されたパターンになりますL。ピリオドで満たされたサイズの4つのグリッドを取得し、インデックス付きのグリッドをパターンで置き換えます。次に、グリッドの間にハッシュの1つの層を使用してグリッドを接着します。以下に4つのケースの図を示します。2n-1-1 × 2n-1-1.L[0]P#

    L[0]==0  L[0]==1  L[0]==2  L[0]==3
       #...  ...#     ...#...  ...#...
    [P]#...  ...#[P]  ...#...  ...#...
       #...  ...#     ...#...  ...#...
    #######  #######  #######  #######
    ...#...  ...#...     #...  ...#   
    ...#...  ...#...  [P]#...  ...#[P]
    ...#...  ...#...     #...  ...#   
    

入力を考慮してくださいL = [2,0]。1×1グリッドから始め、右から#横断Lします。一番右の要素は0なので、1×1グリッドのコピーを4つ取得し.、最初のコピーをに置き換え、#それらをハッシュで接着します。これにより、3×3グリッドが作成されます

##.
###
.#.

次の要素は2であるため、.sの3×3グリッドの4つのコピーを取得し、3番目のグリッドを上記のグリッドに置き換えます。4つのグリッドは

...  ...  ##.  ...
...  ...  ###  ...
...  ...  .#.  ...

それらを#sと一緒に接着すると、7×7グリッドになります

...#...
...#...
...#...
#######
##.#...
####...
.#.#...

これが最終出力です。

入力

入力はLインデックスのリストです0, 1, 2, 3。整数のリストまたは数字のストリングとして取得できます。空の場合があり、重複が含まれている場合があることに注意してください。の長さLは最大5です。

出力

出力は、F(L)改行区切り文字列としてのパターンです。

ルールとスコアリング

完全なプログラムまたは関数を作成できます。最小のバイトカウントが優先され、標準の抜け穴は許可されません。

テストケース

[]
#

[0]
##.
###
.#.

[3]
.#.
###
.##

[2,0]
...#...
...#...
...#...
#######
##.#...
####...
.#.#...

[1,1]
...#.##
...####
...#.#.
#######
...#...
...#...
...#...

[1,2,0]
.......#...#...
.......#...#...
.......#...#...
.......########
.......###.#...
.......#####...
.......#.#.#...
###############
.......#.......
.......#.......
.......#.......
.......#.......
.......#.......
.......#.......
.......#.......

[3,3,1]
.......#.......
.......#.......
.......#.......
.......#.......
.......#.......
.......#.......
.......#.......
###############
.......#...#...
.......#...#...
.......#...#...
.......########
.......#...#.##
.......#...####
.......#...#.#.

[0,1,2,3]
.......#...#...#...............
.......#...#...#...............
.......#...#...#...............
.......#########...............
.......#.#.#...#...............
.......#####...#...............
.......#.###...#...............
################...............
.......#.......#...............
.......#.......#...............
.......#.......#...............
.......#.......#...............
.......#.......#...............
.......#.......#...............
.......#.......#...............
###############################
...............#...............
...............#...............
...............#...............
...............#...............
...............#...............
...............#...............
...............#...............
...............#...............
...............#...............
...............#...............
...............#...............
...............#...............
...............#...............
...............#...............
...............#...............

[0,0,1,2,3]
.......#...#...#...............#...............................
.......#...#...#...............#...............................
.......#...#...#...............#...............................
.......#########...............#...............................
.......#.#.#...#...............#...............................
.......#####...#...............#...............................
.......#.###...#...............#...............................
################...............#...............................
.......#.......#...............#...............................
.......#.......#...............#...............................
.......#.......#...............#...............................
.......#.......#...............#...............................
.......#.......#...............#...............................
.......#.......#...............#...............................
.......#.......#...............#...............................
################################...............................
...............#...............#...............................
...............#...............#...............................
...............#...............#...............................
...............#...............#...............................
...............#...............#...............................
...............#...............#...............................
...............#...............#...............................
...............#...............#...............................
...............#...............#...............................
...............#...............#...............................
...............#...............#...............................
...............#...............#...............................
...............#...............#...............................
...............#...............#...............................
...............#...............#...............................
###############################################################
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................
...............................#...............................

あなたの例では、なぜ1x1グリッドから始めるの#ですか?L !=[]その例では、1つ以上の要素があるためです。これは、F(L)であることを意味して常に#最初に?
R.ガプス

2
@ R.Kapさて、この例はあまり明確ではありません。定義は再帰的であるためL = [2,0]、頭を切り落としてパターンを見てF([0])から、頭を切り落とし[0]てパターンF([])(1x1グリッド)を見てください#。次に、その上で切り刻まれたインデックスを使用し0て3x3パターンを構築し、その上で切り刻まれたインデックスを使用し2て7x7パターンを構築します。あなたの質問に答えるには:はい、それは再帰の基本的なケースなので、常に1x1グリッドから始めます。
-Zgarb

回答:


6

CJam、59 47 43 41 40バイト

1バイトを節約してくれたSp3000に感謝します。

Sal~W%{_Bff|a4*I@t2/{zSf*z}:F%F}fI3ff+N*

ここでテストしてください。

説明

少し時代遅れ。後で修正します。

4Dリストのすべての次元の並べ替えでめまいがした...

このコードは、再帰的な定義ではなく、例のセクションの反復アルゴリズムを使用して、文字通り仕様を実装します。主要なゴルフのトリックの1つは#、計算中ではなくスペースを使用#し、最後にのみ置換することです。コードを1か所に配置し、S代わりに、'#または"#"複数で使用できるようにします。

Sa       e# Push [" "], i.e. a 1x1 grid containing only a space as the
         e# initial fractal.
l~       e# Read and evaluate input.
W%       e# Reverse the list.
{        e# For each list element, assigning the element to variable I...
  _      e#   Duplicate the grid.
  Eff|   e#   Map (OR 14) over each character in the grid, turning spaces into
         e#   periods and leaving periods unchanged.
  a4*    e#   Create an array with four copies of this cleared grid.
  I@t    e#   Replace the Ith element in this list with the previous grid.
  2/     e#   Split this array into a 2x2 grid of subgrids...
         e#   Now it's getting a bit weird... we've got 4 dimensions now, which are:
         e#    - Rows of the 2x2 meta-grid.
         e#    - Cells in each row of the 2x2 meta-grid (i.e. subgrids).
         e#    - Rows of each subgrid.
         e#    - Characters in each row of each subgrid.
  :z     e#   Transpose each outer row, i.e. swap dimensions 2 and 3.
         e#   We've now got in each row of the meta-grid, a list of pairs of
         e#   corresponding rows of the subgrids.
  Sff*   e#   Join those pairs of rows with a single space each. We're now down
         e#   to three dimensions:
         e#    - Rows of the 2x2 meta-grid.
         e#    - Rows of each 1x2 block of the meta-grid.
         e#    - Characters in each row of those blocks.
  :z     e#   Transpose the blocks, i.e. turn the 1x2 blocks into a list of
         e#   columns of their characters.
  z      e#   Transpose the outer grid, i.e. turn it into a list of pairs of
         e#   corresponding columns in the two 1x2 blocks.
  Sf*    e#   Join each pair of columns with a single space. We've now got the
         e#   new grid we're looking for, but it's a list of columns, i.e. transposed.
  z      e#   Fix that by transposing the entire grid once more.
}I
N*       e# Join the rows of the grid with linefeeds.
S'#er    e# Replace all spaces with #.

3

MATL42 41バイト

'.#'4:He!XIiP"Iq@=wX*1X@WZ(l5MY(]3Lt3$)Q)

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

説明

これは、Kronecker製品を使用して反復的に機能し、各反復で配列を拡張します。配列は0andの1代わりに.and #で構築され、最後に適切な文字に置き換えられます。

入力サイズと同じ数の反復があります。入力は右から左に処理されます。反復インデックスはから始まり1ます。

inputを使用したチャレンジの例を使用する[2,0]と、配列は次のように初期化されます。

1 2
3 4

これは、1行1列拡張された初期1#)に対応しますが、その目的は後で明らかになります。これらの列の値は上書きされるため、重要ではありません。それらも同様に可能性があります。

1 1
1 1

各反復で、既存の配列は1、入力の現在のエントリと0他のエントリで示される位置に含まれる2×2のゼロ1配列でクロネッカー乗算されます。反復i = 1 の例では0、右端の入力エントリはであるため、ゼロから1の配列は

1 0
0 0

そして、これら2つの配列のクロネッカー積は

 1 1 0 0
 1 1 0 0
 0 0 0 0
 0 0 0 0

次に、インデックス付きの行と列に1 2^iが入力されます。

 1 1 0 0
 1 1 1 1
 0 1 0 0
 0 1 0 0

最初の3つの行と列は、最初の反復の結果を構成します。前と同じように、余分な行と列があり、次の反復で配列を拡張するのに役立ちます。

反復i = 2で、現在の入力値に2は上記の配列が含まれるため、クロネッカー乗算

0 0
1 0

与える

 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0
 1 1 0 0 0 0 0 0
 1 1 1 1 0 0 0 0
 0 1 0 0 0 0 0 0
 0 1 0 0 0 0 0 0

充填2^iものと番目の行と列を与えます

 0 0 0 1 0 0 0 0
 0 0 0 1 0 0 0 0
 0 0 0 1 0 0 0 0
 1 1 1 1 1 1 1 1
 1 1 0 1 0 0 0 0
 1 1 1 1 0 0 0 0
 0 1 0 1 0 0 0 0
 0 1 0 1 0 0 0 0

これが最後の反復なので、余分な行と列が削除されます。

 0 0 0 1 0 0 0
 0 0 0 1 0 0 0
 0 0 0 1 0 0 0
 1 1 1 1 1 1 1
 1 1 0 1 0 0 0
 1 1 1 1 0 0 0
 0 1 0 1 0 0 0

そして、文字の置換が行われ、最終結果が生成されます。

...#...
...#...
...#...
#######
##.#...
####...
.#.#...

コードの詳細な説明は次のとおりです。

'.#'      % Push this string. Will be indexed into
4:He!     % Push 2×2 array [1 2; 3 4]
XI        % Copy it into clipboard I
iP        % Input array and reverse it
"         % For each entry of the reversed input
  I       %   Push [1 2; 3 4] from clipboard I
  q       %   Subtract 1 to yield [0 1; 2 3]
  @=      %   Compare with current entry of the input. Gives 2×2 array
          %   with an entry equal to `1` and the rest `0`
  wX*     %   Swap. Kronecker product
  1       %   Push 1
  X@      %   Push iteration index, i
  W       %   Compute 2^i
  Z(      %   Write 1 into column 2^i
  l       %   Push 1
  5M      %   Push 2^i again
  Y(      %   Write 1 into row 2^i
]         % End for each
3Lt       % Push [1, -1j] (corresponding to index 1:end-1) twice
3$)       % Apply index. Removes last row and column
Q         % Add 1. Gives an array of values 1 and 2
)         % Index into initial string

2

Haskell、123 122バイト

unlines.foldr(#)["#"]
n#p=zipWith(++)(r++h:t)$('#':)<$>u++h:s where b='.'<$p<$p;h='#'<$p;(r:s:t:u:_)=drop n$cycle[p,b,b,b]

使用例:

*Main> putStr $ (unlines.foldr(#)["#"]) [2,3,1]
.......#.......
.......#.......
.......#.......
.......#.......
.......#.......
.......#.......
.......#.......
###############
...#...#.......
...#...#.......
...#...#.......
########.......
...#.###.......
...#####.......
...#.#.#.......

使い方:

                ["#"]      -- starting with "#" 
        foldr(#)           -- fold the function # from the right into the input
unlines                    -- and join the result with newlines

n#p=                       -- helper function #
                           -- n: next index, p: fractal so far
    zipWith(++)            -- join the left and right part elementwise
       (r++h:t)            -- left part
       ('#':) <$> u++h:s   -- right part (prepend '#' to each line for vertical
                           -- separator

                           -- helper
b='.'<$p<$p                -- b is a blank square of the same size as p
h='#'<$p                   -- h is a line of '#' of the same length as p
(r:s:t:u:_)=               -- drop the first n elements of the infinite
    drop n$cycle[p,b,b,b]  --   list [p,b,b,b,p,b,b,b,p,b,b,b,...] and
                           --   assign the next 4 element to r,s,t,u.
                           --   As r,s,t,u are always inserted at the
                           --   same position in the fractal, we get the
                           --   variants by assigning different values.

1

JavaScript(ES6)、171 152バイト

([d,...a],h=`#`,r=`replace`)=>d<4?(s=f(a)[r](/.+/g,s=>(t=s[r](/./g,`.`),d&1?t+h+s:s+h+t)),t=s[r](/.+/g,w=t+h+t),w=`
${w[r](/./g,h)}
`,d&2?t+w+s:s+w+t):h

再帰呼び出しの結果を取得し、各行を必要に応じて逆の順序でハッシュと同じ長さのドットの文字列に置き換えてから、その部分的な結果から、改行と中央列を除くドットの文字列を作成しますハッシュのハッシュ、および周囲の改行を含むハッシュの文字列も、これらの3つの文字列を適切な順序で結合します。


1

ルビー、143 134バイト

無名関数。

最初の行の再配置によって保存された1バイト。式からテーブルへのzの増分方法を変更することで6バイト節約されます。varableを削除することで2バイト節約されますw

->a{r=-1+u=2<<a.size
s=(?.*r+$/)*r
a<<0
z=r*u/2-1
a.each{|i|r/=2
(-r..r).each{|j|s[z+j]=s[z+j*u]=?#}
z+=-r/2*[u+1,u-1,1-u,-u-1][i]}
s}

テストプログラムでゴルフをしていない

f=->a{
  r=w=(u=2<<a.size)-1        #w=length of line excluding newline, u=length of line including newline.
  s=(?.*w+$/)*w              #initialize string s with w rows of w dots terminated by newlines.
  z=w*u/2-1                  #z is the centre of the fractal
  a<<0                       #add a dummy value to the end of a
  a.each{|i|                 #for each element in a
    r/=2                     #r is the radius of the current iteration: ....15,7,3,1
    (-r..r).each{|j|         #for j=-r to r
      s[z+j]=s[z+j*u]=?#     #overwrite . with #, forming horizontal and vertical lines
    }
    z+=-r/2*(u+1)+           #move z to centre of upper left quarter (where it should be if i=0)
      i%2*(q=r+1)+           #move across if i=1,3
      i/2%2*q*u              #and down if i=2,3  
  }
s}                           #return string

puts $/,f[[]]

puts $/,f[[0]]

puts $/,f[[3]]

puts $/,f[[2,0]]

puts $/,f[[1,1]]

puts $/,f[[1,2,0]]

puts $/,f[[3,3,1]]

puts $/,f[[0,1,2,3]]

puts $/,f[[0,0,1,2,3]]

0

ルビー、150バイト

無名関数。再帰呼び出しを使用して、文字列のリスト(1行に1文字列)を作成し、最後にそれらをすべて結合します。

->i{f=->l{s=2**l.size-1;g=[[?.*s]*s]*4;m=->x,y{x.zip(y).map{|a,b|a+?#+b}}
s<1?[?#]:(g[l.shift]=f[l];m[*g[0,2]]+[?#*(2*s+1)]+m[*g[2,2]])}
f[i].join"
"}

0

Python 3.5、1151バイト:

コードゴルフではありませんが、まあまあです。私ができる時間をかけてそれをもっと剪定しようとします。

def x(s):
 y=[''];l=['#'];k=[' ']
 for z in s[::-1]:y.append(z)
 y=y[::-1]
 for h in range(len(y)):
  if y[-1]!='':u=(int(y.pop())&3)
  else:u=y.pop()
  if len(l)<2:k.append(u);p=((2**(len(k)-1))-1);l.append((('.'*p+'#'+'.'*p+'\n')*p)+'#'*((p*2)+1)+'\n'+(('.'*p+'#'+'.'*p+'\n')*p))
  else:
   if len(l)>2:del l[0]
   p=((2**(len(k)-1))-1);a=[[_+i for i in range(p)]for _ in range(len(l[1]))if _%((p*2)+2)==0 and _!=(((p*2)+2)*(p))];b=[[_+i for i in range(p)]for _ in range(len(l[1]))if _%(int(((p*2)+2)/2))==0 and _!=(int(((p*2)+2)/2)*((p)*2))and _ not in[g for i in a for g in i]];W=[g for i in a[:len(a)-(int(len(a)/2)):1]for g in i];B=[g for i in b[:len(b)-(int(len(b)/2)):1]for g in i];C=[g for i in a[len(a)-(int(len(a)/2)):len(a):1]for g in i];T=[g for i in b[len(b)-(int(len(b)/2)):len(b):1]for g in i];f=list(l[1])
   for i in list(''.join(l[0].split())):
    if u==0:f[W[0]]=i;del W[0]
    elif u==1:f[B[0]]=i;del B[0]
    elif u==2:f[C[0]]=i;del C[0]
    elif u==3:f[T[0]]=i;del T[0]
   del l[0];k.append(u);p=((2**(len(k)-1))-1);l.append(''.join(f));l.append((('.'*p+'#'+'.'*p+'\n')*p)+'#'*((p*2)+1)+'\n'+(('.'*p+'#'+'.'*p+'\n')*p))
 print(l[-2])

これを行うためのかなり単純な方法ですが、それでも、現在は完全に機能し、ご覧のとおり、外部モジュール/ライブラリを使用しません。さらに、それが取ることができる方法提供されたリストでは5つの以上のアイテムをs(ハードウェアがそれを扱うことができれば、です)任意の精度を失うことなく。それはすべての要件を満たしており、私が得たものに満足することはできませんでした。:)

また、ビット単位の演算子のおかげで、範囲内の0=>3任意の値を任意の値として受け入れるだけでなく、任意の数値、ピリオドを受け入れることができます&!それらについての詳細はこちらをご覧ください。今、例えば、[4,4,1,2,3]入力リストのようになり、同じように[0,0,1,2,3]

注:入力リストとして提供する必要あります

説明のないゴルフ:

def x(s):
 # Create 3 lists:
 # `y` is for the values of `s` (the list provided) and an empty element for the 
 # first pattern
 # `l` is reserved for the pattersn created through each item in list `y`
 # `k` is created for the value of `p` which is the main value through which the 
 # pattern is created.
 y=[''];l=['#'];k=[' ']
 # Reverse s, and then add each element from `s` to `y` 
 # (in addition to the empty element) 
 for z in s[::-1]:
     y.append(z)
 # `y` should now equal the list created, but reversed
 # If not reversed, then, if, for instance, the input is `0,1,2` and list `y` 
 # therefore contains `'',2,1,0`, the empty element will be called at the end, 
 # which is NOT what we want.
 y=y[::-1]
 # The main loop; will be iterated through the length of `y` number of times
 for h in range(len(y)):
  # Here is where each element from the end of `y` is recieved as `u` for 
  # use in the pattern in each iteration.
  # As you can also see, a bitwise operator (`&`) is used here so that 
  # ALL numbers can be accepted. Not just those in the range `0-4`.     
  # However, that will happen only if the value of y[-1] (the last elment in y) is 
  # NOT ''.
  if y[-1]!='':
      u=(int(y.pop())&3)
  else:
      u=y.pop()
  # If the length of list `l` is less than 2 
  # (which means it only contains `#`), then do the following:
  if len(l)<2:
      # Append `u` to `k`
      k.append(u)
      # Use the length of `k` as `n` in the operation `(2^(n-1)-1)` to get the 
      # length of the dot filled part of the new pattern.
      p=((2**(len(k)-1))-1)
      # Add that pattern to the list (currently empty, 
      # i.e. containing no other pattern in any other quadrant)
      l.append((('.'*p+'#'+'.'*p+'\n')*p)+'#'*((p*2)+1)+'\n'+(('.'*p+'#'+'.'*p+'\n')*p))
  # Now, if the length of l is >=2, do the following:
  else:
   # If the length of l is >2, then delete the first element in list `l` 
   # (this will happen only once, when the `#` is still the first element)
   if len(l)>2:
       del l[0]
   # Again, use the length of `k` as `n` in the operation `(2^(n-1)-1)`
   # to get the length of the dot filled part of the pattern.
   p=((2**(len(k)-1))-1)
   # Create a list with all the index values of all the dot elements on the left hand 
   # side of the grid l[-1], and the index value + i where i is every integer in 
   # the range `0-p` (this way, it will create lists within a list, each 
   # which contain `p` number of integers, which are all indexes of all the dots on 
   # the very left side of the grid) 
   a=[[_+i for i in range(p)]for _ in range(len(l[1]))if _%((p
      *2)+2)==0 and _!=(((p*2)+2)*(p))]
   # Create another list with all the index values of the dots using the same 
   # strategy as above, but this time, those in the right half of the grid. 
   b=[[_+i for i in range(p)]for _ in range(len(l[1]))if _%(int(((p*2)+2)/2))==0 
      and _!=(int(((p*2)+2)/2)*((p)*2))and _ not in[g for i in a for g in i]]
   # Create 4 lists, each containing index values specific to each of the 
   # 4 quadrants of the grid.
   # W is the list, based on A, containing all the indexes for the 1st quadrant of 
   # the grid in l[-1] containing dots (index 0 in the grid)
   W=[g for i in a[:len(a)-(int(len(a)/2)):1]for g in i]
   # B is the list, this time based on b, containing all indexes for the 2nd 
   # dot-filled quadrant of the grid l[-1] (index 1 in the grid)
   B=[g for i in b[:len(b)-(int(len(b)/2)):1]for g in i]
   # C is the list, also, like W, based on a, containg all the index values for 
   # the 3rd dot-filled quadrant of the grid in l[-1] (index 2 in the grid)
   C=[g for i in a[len(a)-(int(len(a)/2)):len(a):1]for g in i]
   # T is the final list, which, also like B, is based on b, and contains all the 
   # index values for the final (4th) dot-filled quadrant of the grid in l[-1] 
   T=[g for i in b[len(b)-(int(len(b)/2)):len(b):1]for g in i];f=list(l[1])
   # Finally, in this `for` loop, utilize all the above lists to create the new 
   # pattern, using the last two elements in list `l`, where each character of grid 
   # l[-2] (the second to last element) is added to the correct index of grid l[-1] 
   # based on the value of `u`
   for i in list(''.join(l[0].split())):
    if u==0:
        f[W[0]]=i
        del W[0]
    elif u==1:
        f[B[0]]=i
        del B[0]
    elif u==2:
        f[C[0]]=i
        del C[0]
    elif u==3:
        f[T[0]]=i
        del T[0]
   # Delete the very first element of `l`, as it is now not needed anymore
   del l[0]
   # Append `u` to list`k` at the end of the loop this time
   k.append(u)
   # Update the value of `p` with the new value of length(k)
   p=((2**(len(k)-1))-1)
   # Append the new patter created from the for-loop above to list `l`
   l.append(''.join(f))
   # Append a new, empty pattern to list `l` for use in the next iteration
   l.append((('.'*p+'#'+'.'*p+'\n')*p)+'#'*((p*2)+1)+'\n'+(('.'*p+'#'+'.'*p+'\n')*p))
 # When the above main loop is all finished, print out the second-to-last elment in 
 # list `l` as the very last element is the new, empty grid created just in case 
 # there is another iteration
 print(l[-2])

より広く、はるかに視覚的に魅力的な説明:

より広く、より視覚的に魅力的な説明のために、入力リストがである上記のコードの「メイン」ループをもう一度通過することを検討して[0,2]ください。この場合、「メイン」リストの要素lは次のようになります。

.#.
###
##.

そして

...#...
...#...
...#...
#######
...#...
...#...
...#...

リストにyはのみが含まれます0。gridの最後の要素にインデックスl[-1]を付けるPythonの方法を利用して、グリッドの左端の要素に次のようにラベルを付けることができます。

 0 ...#...\n 7        
 8 ...#...\n 15
16 ...#...\n 23
   #######\n <- Ignore this as it is nothing but `#`s and a new line
32 ...#...\n 39
40 ...#...\n 47
48 ...#...\n 55

どのようなパターンが見えますか?グリッドの一番左にあるすべてのインデックスは8の倍数であり、方程式を使用2^(n-1)-1してグリッド内のドットの各セグメントの((2^(n-1)-1)*2)+2長さを求めるため、グリッド全体の上端の長さを見つけることができます。(+2は、中央#のsと\n末尾を含む)。iリストを作成_し、範囲内のを呼び出すすべての整数をリストに追加することにより、任意のサイズのグリッドの左側で各要素のインデックス値を見つけるために呼び出す方程式を使用できます0=>length of grid l[-1]。そのアイテムがの倍数でありi、かつ_等しくないi*(2^(n-1)-1)ので、その中間セグメントを除外できるように#s上半分と下半分を分離します。ただし、左端の要素だけでなく、左からのすべてのドット要素が必要です。それには修正がありi+h、範囲0=>2^(n-1)からの値0=>length of grid l[-1]がリストに追加されるたびに、hが範囲内のすべての整数を含むリストをリストに追加するだけですので、毎回リストに追加される値の数と同じ数のドットの長さ。それがリストaです。

しかし、今、右半分のドットはどうですか?さて、インデックス作成の別の方法を見てみましょう。

   0 ...# 4  ...\n 7        
   8 ...# 12 ...\n 15
  16 ...# 20 ...\n 23
     #######\n <- Ignore this as it is nothing but `#`s and a new line
  32 ...# 36 ...\n 39
  40 ...# 44 ...\n 47
  48 ...# 52 ...\n 55

          ^
          | 

          These are the values we are looking at now

ご覧のとおり、現在中央にある値は、必要な値です。は、グリッドの右側にあるドットの各セグメントのインデックスの始まりであるためです。さて、ここでのパターンは何ですか?さて、もしそれが十分に明白でないなら、今、中間の値はすべての倍数ですi/2!その情報を使用して、別のリストを作成できるようになりました。このリストにbの倍数がi/2範囲から追加され、その範囲の0=>length of grid l[-1]各整数(再び呼び出します_)は(i/2)*(p*2)、の行を除外するために等しくなりません。そして今、私たちはそれらの特定のインデックスだけが必要なわけではありません。グリッドの右側にあるすべてのドット文字が必要です。まあ、リストでやったように、ここでリストに追加することもできます#トップを分離するのをと下半分、および_が既にリストaにないようにします。これは、8,16,32などを実際に必要としないためです。リスト内bab_+hhで、範囲内の各整数どこにあるかます0=>2^(n-1)

これで、リストabパックの両方が準備できました。これらをどのようにまとめますか?リストはここにあるWTG、およびCで来る。彼らは、グリッド内のドットの各特定の象限にインデックスを保持しますl[-1]。たとえばW、グリッドの象限1(インデックス0)に等しいすべてのインデックスのリストとしてリストを予約しましょう。このリストでは、リスト、2^(n-1)リストの最初のリストを追加しますaaグリッドの左半分にドットのすべてのインデックスが含まれており、そのので、それらすべてを分割しW、今含ま(2^(n-1))*(2^(n-1))要素を。私たちは、リストのために同じことをするだろうTが、違いと、Tリストから要素を含んでいるでしょうb、以来、T象限2(インデックス1)用に予約されています。リストはGリストと同じになりW、それはリストから残りの要素を含んでいるでしょう除き、a、、リストはCリストと同じであるTことが、今、リストから残りの要素が含まれている以外、グリッドにドットを含む象限の各象限に対応する4つのリストにすべて分割されています。これで、これらの4つのリスト(W、T、G、C)を使用して、listの最初の要素であるgridの各文字でgrid内のどの文字を置き換えるかをプログラムに伝えることができます。値はここにあるため、リストを利用するグリッドで最初の象限(インデックス0)のすべてのドットを置き換えますb。以上です!これで、すべてのl[-1]l[0]l0l[0]W

したがって、最終的に次のようになります。

.#.#...
####...
##.#...
#######
...#...
...#...
...#...

ふう!長いプロセスですね。しかし、それは完璧に機能し、再び、私は幸せになることができませんでした。:)

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