折叠一些毛巾!


19

我在某处听说过技术尚无法做到的一件事就是折叠毛巾1。因此,现在要证明该声明为假!

给定一个作为输入的字符串,该字符串由矩形(毛巾)组成,如下所示,将每条毛巾对折两次。例如:

+------+    +------+        +--+
|      |    |      |        |  |
|      |    |      |        |  |
|      | -> +------+ ->     +--+
|      |    
|      |    
|      |    
+------+    

请注意,折叠一条毛巾后,首先将其折叠,然后再从左至右折叠。您的程序也必须模仿这种行为。另请注意,在测试用例中,毛巾停留在同一位置,但折叠了。

规则:

  • 输入/输出的标准方法。
  • 有标准漏洞。
  • 输入和输出应为字符串。
  • 只要毛巾相对彼此在正确的位置,就可以随便拖曳任何东西。
  • 您可以假设毛巾两边的长度始终可以被2整除。
  • 作为输入传递的毛巾将始终为矩形。
  • 毛巾将始终分开-但是,它们可能会以可变的量分开。

  • ,所以最短的代码获胜!

测试用例:

Input:
+------+
|      |
|      |
|      |
|      |
|      |
|      |
+------+
Output:
    +--+
    |  |
    |  |
    +--+




Input:
+--+ +--+ +--+
|  | |  | |  |
|  | |  | |  |
+--+ +--+ +--+

Output:
  ++   ++   ++
  ++   ++   ++


Input:
+----+
|    |
|    |
|    |
|    | ++
+----+ ++

Output:

   +-+
   | |
   +-+

        +

Input:
+--+
+--+     ++
         ||
         ||
         ++
Output:
  ++
          +
          +

1:这已被Geobits和Laikoni否决。但是,我确实在某处听到过。


为什么要下票?如果有可以解决的问题,请告诉我。
“ SparklePony同志” 17年


@Laikoni看来技术可以做任何事情:-)
Xcoder先生17年

@LuisMendo编辑后,毛巾之间总会有空隙。
“ SparklePony同志” 17年

给定毛巾会始终水平排列吗?我的意思是在别的下面没有毛巾了吗?
Dead Possum

Answers:


5

视网膜,245字节

m1+`^((.)*(?!\+ )[+|]([- ])*[+|].*¶(?<-2>.)*)([+|][- ]*[+|])
$1$.4$* 
m+`^((.)*) ( *) (.*¶(?<-2>.)*)(?(2)(?!))\|\3\|
$1|$3|$4 $3 
m+`^((.)*)\|( *)\|(?=.*¶(?<-2>.)*(?(2)(?!)) )
$1+$.3$*-+
([+|])\1
 $1
(?!\+ )([+|])([- ])\2(\2*)\3\1
$.3$*   $1$3$1

在线尝试!

注意:某些行以空格结尾。说明:

m1+`                    Repeatedly search from the beginning of input
    ^((.)*              Optional indentation
      (?!\+ )           Towel does not start with a plus and a space
      [+|]([- ])*[+|]   A row of towel
      .*¶               Rest of the line
      (?<-2>.)*         Some indentation on the next line
     )
     ([+|][- ]*[+|])    Another row of towel
$1$.4$*                 Replace the second row with blanks

删除每条毛巾的其他所有行(这是可行的,因为所有毛巾的高度都相等),

m+`             Repeatedly search
   ^((.)*)      Optional indentation
    ( *)        Find some space
    (.*¶        Rest of the line
     (?<-2>.)*) Matching indentation on the next line
    (?(2)(?!))  Ensure that the indentation exactly matches
    \|\3\|      Piece of towel
$1|$3|$4 $3     Swap the piece into the space above

向上移动所有分开的毛巾,

m+`                 Repeatedly search
   ^((.)*)          Optional indentation
    \|( *)\|        Piece of towel
    (?=             Followed by
       .*¶          Rest of the line
       (?<-2>.)*    Matching indentation on the next line
       (?(2)(?!))   Ensure that the indentation exactly matches
        )           Nothing on the next line
$1+$.3$*-+          Change the piece into a towel bottom

并固定毛巾的底部,有效地将它们折叠起来。

([+|])\1    Match a row of towel of width 2, which is just ++ or ||
 $1         Change the first column to a space, which folds it

将宽度2的毛巾向右折叠。

(?!\+ )     Towel does not start with a plus and a space
([+|])      Start with a + or a |
([- ])\2    Then at least two -s or spaces
(\2*)\3     Then an even number of -s or spaces
\1          Then another + or |
$.3$*       Replace the left half with spaces
$1$3$1      Replace the first character of the right half

将剩余的毛巾向右折叠。


我有兴趣的更详细说明了如何在正则表达式的作品
Kritixi LITHOS

@KritixiLithos之类的东西,还是有一些特定的东西?
尼尔,

是的,谢谢。我是否假设这<-2>是一个.NET平衡组是否正确?
Kritixi Lithos

@KritixiLithos它使用它们:(?<-2>.)*每次弹出捕获,因此不能重复执行重复的操作(.)*,同时(?(2)(?!))检查是否还有捕获,因此重复相同的次数。
尼尔

3

图像包的八度277272字节

function x=f(x)
[i,j,v]=find(bwlabel(x-32));a=@(w)accumarray(v,w,[],@max);r=-a(-i);R=a(i);s=-a(-j);S=a(j);x(:)=32;for k =1:nnz(r)
u=(r(k)+R(k)-1)/2;v=(s(k)+S(k)+1)/2;p=v:S(k);x(r(k),p)=45;x(u,p)=45;q=r(k):u;x(q,v)=124;x(q,S(k))=124;x(u,[v S(k)])=43;x(r(k),[v S(k)])=43;end

输入和输出是2D char数组。

在线尝试!或验证所有测试用例:1234。(请注意,endfunction在测试案例中,只需要将功能与后续代码分开即可。如果功能保存在自己的文件中,则没有必要。)

可读的版本和说明

function x = f(x)
[i,j,v] = find(bwlabel(x-32)); % make background equal to 0 by subtracting 32.
% Then label each connected component (i.e. towel) with a unique integer
% Then get row indices (i) and column indices (j) of nonzero values (v)
a = @(w)accumarray(v,w,[],@max); % helper function. Gives the maximum of w for
% each group given by an integer label in v
r = -a(-i); % upper coordinate of each towel (minimum row index)
R = a(i); % lower coordinate of each towel (maximum row index)
s = -a(-j); % left coordinate of each towel (minimum column index)
S = a(j); % right coordinate of each towel (maximum column index)
x(:) = 32; % remove all towels: fill x with spaces
for k = 1:nnz(r) % for each original towel: create the new, folded towel 
    u = (r(k)+R(k)-1)/2; % new lower coordinate
    v = s(k)+S(k)+1)/2; % new left coordinate
    p = v:S(k); % column indices of horizontal edges
    x(r(k),p) = 45; % fill upper horizontal edge with '-'
    x(u,p) = 45; % fill lower horizontal edge with '-'
    q = r(k):u; % row indices of vertical edges
    x(q,v) = 124; % fill left vertical edge with '|'
    x(q,S(k)) = 124; % fill right vertical edge with '|'
    x(u,[v S(k)]) = 43; % fill lower corners with '+'
    x(r(k),[v S(k)]) = 43; % fill upper corners with '+'
end
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.