绘制索引分形


14

介绍

在这个挑战中,索引2×2的矩阵是这样的:

0 1
2 3

我们定义了一系列类似分形的图案F(L),其中Ln这些索引的长度列表,并且F(L)具有size 。2n-1 × 2n-1

  • 如果L == []F(L)则为1×1模式#
  • 如果L != [],则F(L)构造如下。取PL删除第一个元素后获得的图案。取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网格的四个副本.,将第一个替换为#,然后将它们与哈希值粘合在一起。结果是3×3网格

##.
###
.#.

下一个元素是2,因此我们获取.s 的3×3网格的四个副本,并用上述网格替换第三个。四个网格是

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

并将它们与#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)首先始终是a #
R. Kap

2
@ R.Kap好的,例子不是很清楚。定义是递归的,因此L = [2,0],您先砍下头看一下图案F([0]),然后砍下头[0]看一下图案F([])(即1x1网格)#。然后,使用切碎的索引0建立3x3模式,并使用切碎的索引2建立7x7模式。要回答您的问题:是的,您总是从1x1网格开始,因为这是递归的基本情况。
Zgarb '16

Answers:


6

CJam,59 47 43 41 40字节

感谢Sp3000节省1个字节。

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

在这里测试。

说明

有点过时了。稍后将修复。

4D列表的所有尺寸重新排序都让我感到头晕...

该代码使用示例部分中的迭代算法而不是其递归定义来非常直观地实现该规范。一个主要的高尔夫技巧是,我#在计算过程中使用空格而不是空格,而仅#在末尾使用空格来代替,从而简化了操作代码放在一个地方,并允许我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产品可迭代地进行此操作,以在每次迭代中扩展数组。该数组是使用01而不是.和构建的,#最后,它们被适当的字符替换。

迭代次数将与输入大小一样多。输入从右到左处理。迭代索引从开始1

使用质询中的示例,使用input [2,0],将数组初始化为

1 2
3 4

这对应于以一行和一列扩展的首字母1#),其目的将在以后阐明。这些列中的值并不重要,因为它们将被覆盖。他们可能同样是:

1 1
1 1

在每次迭代中,将现有数组乘以Kronecker乘以2×2零一数组,1该数组包含在输入的当前条目所指示的位置以及0其他条目处。在迭代i = 1 的示例中,由于最右边的输入项是0,所以零一数组是

1 0
0 0

这两个阵列的Kronecker乘积是

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

接下来,带有索引的行和列2^i将填充为1:

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

前三行和列构成第一次迭代的结果。和以前一样,这里有额外的行和列,它们对于在下一次迭代中扩展数组很有用。

在迭代i = 2时,由于当前输入值包含2上面的数组,因此将Kronecker乘以

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-th行和列

 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,123122字节

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

取得递归调用的结果,然后将每行替换为其自身加上一个散列以及一个相同长度的点字符串,如果需要,可以按相反的顺序进行替换,然后从该部分结果中创建一个点字符串,除了换行和中央列哈希,以及带有周围换行符的哈希字符串,然后以适当的顺序将这三个字符串连接在一起。


1

Ruby,143134字节

匿名函数。

第一行的重新排列节省了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

Ruby,150个字节

匿名函数。使用递归调用来构建字符串列表,每行一个字符串,然后在最后将它们全部连接在一起。

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

更广泛,更吸引人的解释:

对于更广泛,更吸引人的解释,请考虑第二遍上述代码中的“ main”循环,其中输入列表为[0,2]。在这种情况下,“主要”列表中的元素l将是:

.#.
###
##.

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

并且list y仅包含0。利用Python索引grid的最后一个元素的方式l[-1],我们可以像这样标记网格的最左边的元素:

 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,并且AND _还不等于i*(2^(n-1)-1),因此我们可以排除的中间部分#s将上半部分与下半部分分开。但是我们希望所有的点元素都位于左侧,而不仅仅是左侧的元素。好吧,有一个解决方法,那i+h就是每次将范围中0=>2^(n-1)的值0=>length of grid l[-1]添加到列表中时,只需将列表中的h 附加到列表中,其中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)排除#s分隔顶部和底部的行。下半部分,并且_不在列表a中,因为我们实际上不需要8,16,32等。在清单中b。现在,再次,我们不仅想要那些特定的索引。我们希望所有点字符都位于网格的右侧。好了,就像我们在list中所做的一样a,在这里我们还可以将range中每个整数在哪里的列表添加到列表b中。_+hh0=>2^(n-1)

现在,我们既有清单,a又有b装箱清单,可以开始使用了。我们现在如何将它们放在一起?这是清单WTG,和C进来,他们将举行索引网格点的每个象限具体l[-1]。例如,让我们保留list W作为所有等于网格象限1(索引0)的索引的列表。然后在此列表中,我们将添加2^(n-1)list中的第一个列表a,因为list a包含网格左半部分中所有点的索引,然后将它们全部拆分为W现在包含的(2^(n-1))*(2^(n-1))元素。我们将对list执行相同的操作T,但不同之处在于T将包含list中的元素b,因为T为象限2(索引1)保留。List G将与list相同W,不同之处在于它将包含list中其余的元素a,而list与list C相同T,只是它现在包含list 中其余的元素b。而且,就是这样!现在,对于网格中包含点的每个象限,我们都有索引值,所有索引值都分成与每个象限相对应的四个列表。现在,我们可以使用这4个列表(W,T,G,C)来告诉程序应使用grid l[-1]中的每个字符替换grid中的哪些字符l[0],这是list的第一个元素l。由于该值在0此处,它将使用网格l[0]利用列表替换第一象限(索引0)中的所有点W

因此,我们终于有了以下内容:

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

ew!漫长的过程,不是吗?但是,它运行得很好,再次,我不能更快乐。:)

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.