构造龙卷风


31

根据我的说法,龙卷风看起来像这样:

########
#######
 ######
  #####
   ####
   ###
    ##
     #

此龙卷风以width开头n,在下一行中,根据输入,从左侧或右侧删除一个字符。

输入值

输入将是任何两个唯一值的列表(一个由两个唯一字符组成的字符串也可以),以及一个可选的正整数(表示起始宽度)的列表。如果未采用可选的整数,则起始宽度比列表的长度大1。让起始宽度为n

如何制作龙卷风

在我的示例中,我选择了包含1s和0s的列表,尽管您可以选择任意两个不同的常量值,或者任意两个不同的常量字符的字符串。

第一行将由n非空白字符组成(您可以选择任何一致的字符;我选择#示例)。

然后,对于列表中的每个数字,如果数字为0,则删除左字符并创建一个新行;如果是1,请删除正确的字符并创建新行。

因此,以上龙卷风是的输出8, [1, 0, 0, 0, 1, 0, 0]

输出量

输出可以是字符串列表,字符列表或多行字符串。允许在每行末尾添加空白,并在末尾添加换行符。

测试用例

这些测试用例包括起始宽度和的使用列表1, 0

5, [1,0,0,1]

#####
####
 ###
  ##
  #

10, [1,0,0,1,0,1,0,0,1]

##########
#########
 ########
  #######
  ######
   #####
   ####
    ###
     ##
     #

7, [1,1,1,1,1,1]

#######
######
#####
####
###
##
#

100,
 [1,0,0,0,0,1,0,0,0,1,1,0,1,0,0,1,0,0,1,0,0,1,1,0,1,1,1,1,0,1,0,1,1,0,0,1,0,1,1,0,0,1,1,1,0,0,1,1,1,1,1,0,0,0,1,1,0,1,0,0,1,1,1,1,1,1,1,1,1,1,0,1,1,0,0,0,0,1,0,0,0,0,1,1,1,1,0,1,0,1,0,0,1,1,0,0,0,0,1]

长测试用例

规则

  • 适用标准漏洞
  • 以字节为单位的最短代码胜出!
  • 背景不必一定是空格(我忘了指定此内容)。
  • 您的语言只需要支持它可以处理的数字(宽度),但是如果您的解释器使用更大的数字大小进行重写,则从理论上讲它必须可以工作。

参考实施


3
从您的示例看来,起始位置总是比列表的长度大1。我们可以举一个例子吗?起始宽度可以小于列表的长度吗?
查理

@CarlosAlejo不...那么那没有意义,因为那样一来您将没有足够的元素最后删除...
HyperNeutrino

4
@HyperNeutrino用来观看他的第一次龙卷风的眼镜:i.imgur.com/TzMm94a.png
魔术

@MagicOctopusUrn ...大声笑
HyperNeutrino

Answers:



9

Python 2中66 59个字节

-7个字节感谢Arnold Palmer

x,z=input()
for i in range(x):print' '*sum(z[:i])+'#'*(x-i)

在线尝试!

0从右侧1删除,从左侧删除


1
ninja'd me:(((
Koishore Roy

同样的男人:/
阿诺德·帕尔默

如果替换len(z)+1x“可选的正整数” ,则节省7个字节,表示您不必使用它。问题的措辞听起来像是允许的,因为它说“如果不采用可选整数”与“如果没有给出可选整数”。
阿诺德·帕尔默

6

vim,85 82字节

o"cp:s/#/ <C-v><CR>0"cy$<ESC>"ayy7hR$/<ESC>"bdd:s/[][, ]\+/<C-v><CR>@/g<CR>ggAa#<C-v><ESC><ESC>^D@"^"cy$:2,$:norm D@"

<ESC>是0x1B,<CR>0x0D<C-v>是0x16。并且<ESC>OH是表示HOME键的多字节序列。

输入将a用作“左移”值和b“右移”值。

" @a will remove from the left; @b will remove from the right.
o"cp:s/#/ <C-v><CR>0"cy$<ESC>"ayy
7hR$/<ESC>"bdd

" split the input into digestible chunks
:s/[][, ]\+/<C-v><CR>@/g<CR>
gg

" Create the first line
Aa#<C-v><ESC><ESC>
^D
@"^"cy$

" Create tornado
:2,$:norm D@"

遗憾的是,没有TIO链接。通过将代码复制到tornado.vim中(用<ESC>实际字节替换,等等)并按如下方式运行,我无法在V. Test下运行它:

$ echo '8, [b, a, a, a, b, a, a]' > a.txt
$ { cat tornado.vim; echo ':wq'; } | vim a.txt
$ cat a.txt

由于尼尔的建议,-3个字节。


可以^代替使用␛OH吗?
尼尔

@Neil ^转到第一个非空白字符。␛OH转到第一个字符。

1
啊,对不起,我的意思0不是……
Neil

@Neil在那种情况下,是的,我应该这样做。谢谢。

5

05AB1E11 9字节

-2个字节,感谢Outgolfer的Erik

0×=²v¨yú=

从左侧1
删除:从右侧删除:0

在线尝试!

0×        # Make a string of n 0s
  =       # Print without popping
   ²v     # For each character in input (call it y):
     ¨    #   Remove the last character of the current string
      yú  #   Pad with y spaces
        = #   Print without popping

0×ηsηO具有如此大的潜力,但我无法弄清楚11个字节以下。
魔术章鱼缸

保留后的版本和替换ðy×ì的-2。
暴民埃里克(Erik the Outgolfer)'17年

4

视网膜30 28字节

.?
$`#$&$'¶
T`d`#`#.*
T`d` _

在线尝试!仅接受0和1的字符串,并根据该字符串计算宽度。说明:第一阶段获取输入字符串,并为每个边界点复制一次,然后#在该点插入a 。然后第二阶段将s 之后的所有数字更改#为more #,从而创建三角形。然后,第三种删除所有剩余的stanst,并将零更改为空格,从而导致torndao的“摆动”。


3

J,32个字节

' #'#~[:(],.1+i.@-@#)0,(+/\@:-.)

不打高尔夫球

' #'#~ [: ( ] ,. 1+i.@-@# )  0 , (+/\ @: -.)

在线尝试!


19个字节' #'#~0(,.#\.)@,+/\,其中输入与样本相反,这是因为挑战允许您选择两个不同的值。
英里

@miles对此以及您的其他评论。非常感谢他们,并请他们继续前进。
约拿(Jonah)


3

R85 82字节

Giuseppe节省了3个字节

function(n,l)for(k in 1:n)cat(rep(" ",sum(c(0,l)[1:k])),rep("%",n-k+1),"
",sep="")

在线尝试!

说明:

function(n,l){
  for(k in 1:n){                      # Proceed line by line
    cat(                              # Concatenate...
        rep(" ",sum(c(0,l)[1:k])),    # ...required number of leading spaces,...
        rep("%",n-k+1),               # ...required number of tornado characters...
        "\n",                         # ...and a new line.
        sep=""                        # Join without spaces
        )
  }
}

我知道制作矩阵不是最佳选择!
朱塞佩

1
82字节 -删除后{}使用了原义换行符,而不是'\n'
Giuseppe

3

Haskell,50个字节

h n=scanl(\s i->[(' ':),id]!!i$init s)$'#'<$[1..n]

在线尝试!

如果输入列表可以是函数名称列表,我们可以保存一个字节

Haskell,49个字节

f=id
g=(' ':)
h n=scanl(flip id.init)$'#'<$[1..n]

用法示例:h 5 [g,f,f,g]

在线尝试!

怎么运行的:

           '#'<$[1..n]   -- build the first line of the tornado, i.e. n times '#'
scanl(    )              -- repeatedly apply the given function to the starting
                         -- value and the next element of the input list and
                         -- return a list of the intermediate results
  \s i->                 -- the function takes a string s and a number i
            init s       -- and first drops the last element of s
      [    ]!!i          -- and then picks and apply a funtion from the list
        (' ':)           --  i = 0:  prepend a space
        id               --  i = 1:  do nothing

2

Python 2,58 57字节

编辑:由于xnor,保存了1个字节

l,a=input()
s="#"*l
for i in a+[0]:print s;s=" "*i+s[:-1]

在线尝试!

1从左侧删除,0从右侧删除。


1
我想你可以做 s=" "*i+s[:-1]左右互换。
xnor

2

[R 116个 109 102字节

-5字节归功于user2390246(另外2个是我自己保存的)

被user2390246超越

function(n,l){k=cumsum
m=matrix(' ',n,n)
for(i in 1:n)m[k(c(1,!l))[i]:k(c(n,-l))[i],i]='#'
write(m,'',n,,'')}

在线尝试!

返回带有n和向量的匿名函数l,,0用于从左侧1删除和从右侧删除,然后以正确的格式将结果打印到控制台。


1
您可以节省一些时间来计算行尾,因为您知道#s 的数量将为n-i + 1:在线尝试!如果您仅逐行打印而不是构造矩阵,虽然有一种更好的方法:codegolf.stackexchange.com/a/133720/66252
user2390246 '17

@ user2390246非常好!我也设法刮掉了另外两个字节:)
朱塞佩

2

Japt14 13字节

-1字节感谢@ETH

å+ uP £X+QpV´

输入是数组,然后是大小。数组值是""" ",分别代表从右侧或左侧移除。使用"代替#和作为字符串数组返回。

这里的想法是首先为每行创建左填充数组,从而为输入字符串创建一个数组。然后,每一行" s的量"每次减少1 的事实 s相加。

在线尝试!

这些都使用-R标志通过将输出与换行符连接来格式化输出。

说明

å+ uP £X+QpYnV

隐式:U=输入数组,V=输入数字。

å+ uP

å使用字符串串联(+)累计减少输入数组()。这导致减少的每个中间值的数组。然后,在数组前添加(u)空字符串(P)。

£X+

£每个值映射到X与(...)连接的自身()

QpV´

引号(Q)重复(pV--)次。V每次也会减少。


好一个 我认为您可以通过更改YnV
ETHproductions

@ETHproductions太好了,谢谢!我完全忘了在Japt中使用++--
贾斯汀·马里纳

2

阿诺德,3132字节

ArnoldC没有字符串连接,因此这会从8s 构建龙卷风,并使用1s对其进行间隔。ArnoldC还仅支持最多16位整数,因此输入超过7位将溢出。(因此只会制造迷你龙卷风)

1左边,其他任何数字都正确(尽管我不建议使用0,因为您不能以此开头。)

输入: 1221122

输出:

88888888
18888888
18888881
18888811
11888811
11188811
11188111
11181111

高尔夫代码:

IT'S SHOWTIME
HEY CHRISTMAS TREE d
YOU SET US UP 0
HEY CHRISTMAS TREE e
YOU SET US UP 0
HEY CHRISTMAS TREE m
YOU SET US UP 0
GET YOUR ASS TO MARS m
DO IT NOW
I WANT TO ASK YOU A BUNCH OF QUESTIONS AND I WANT TO HAVE THEM ANSWERED IMMEDIATELY
HEY CHRISTMAS TREE l
YOU SET US UP 0
GET YOUR ASS TO MARS l
DO IT NOW g m
DO IT NOW h l m
YOU HAVE BEEN TERMINATED
LISTEN TO ME VERY CAREFULLY g
I NEED YOUR CLOTHES YOUR BOOTS AND YOUR MOTORCYCLE m
GIVE THESE PEOPLE AIR
HEY CHRISTMAS TREE i
YOU SET US UP 2
HEY CHRISTMAS TREE n
YOU SET US UP m
STICK AROUND n
GET TO THE CHOPPER n
HERE IS MY INVITATION n
HE HAD TO SPLIT 10
ENOUGH TALK
BECAUSE I'M GOING TO SAY PLEASE n
GET TO THE CHOPPER i
HERE IS MY INVITATION i
GET UP 1
ENOUGH TALK
YOU HAVE NO RESPECT FOR LOGIC
CHILL
I'LL BE BACK i
HASTA LA VISTA, BABY
LISTEN TO ME VERY CAREFULLY h
I NEED YOUR CLOTHES YOUR BOOTS AND YOUR MOTORCYCLE l
I NEED YOUR CLOTHES YOUR BOOTS AND YOUR MOTORCYCLE m
GIVE THESE PEOPLE AIR
HEY CHRISTMAS TREE o
YOU SET US UP -2
GET TO THE CHOPPER o
HERE IS MY INVITATION o
GET UP l
ENOUGH TALK
HEY CHRISTMAS TREE k
YOU SET US UP 1
STICK AROUND o
GET TO THE CHOPPER k
HERE IS MY INVITATION k
YOU'RE FIRED 10
ENOUGH TALK
GET TO THE CHOPPER o
HERE IS MY INVITATION o
GET DOWN 1
ENOUGH TALK
CHILL
HEY CHRISTMAS TREE p
YOU SET US UP 0
HEY CHRISTMAS TREE f
YOU SET US UP 0
HEY CHRISTMAS TREE i
YOU SET US UP 0
HEY CHRISTMAS TREE q
YOU SET US UP l
HEY CHRISTMAS TREE d
YOU SET US UP 0
HEY CHRISTMAS TREE e
YOU SET US UP 1
HEY CHRISTMAS TREE a
YOU SET US UP 0
HEY CHRISTMAS TREE b
YOU SET US UP 0
HEY CHRISTMAS TREE c
YOU SET US UP l
STICK AROUND q
GET TO THE CHOPPER i
HERE IS MY INVITATION 0
ENOUGH TALK
GET TO THE CHOPPER a
HERE IS MY INVITATION d
ENOUGH TALK
GET TO THE CHOPPER b
HERE IS MY INVITATION e
ENOUGH TALK
GET TO THE CHOPPER c
HERE IS MY INVITATION l
ENOUGH TALK
STICK AROUND c
BECAUSE I'M GOING TO SAY PLEASE a
GET TO THE CHOPPER i
HERE IS MY INVITATION i
GET UP 1
YOU'RE FIRED 10
ENOUGH TALK
GET TO THE CHOPPER a
HERE IS MY INVITATION a
GET DOWN 1
ENOUGH TALK
BULLSHIT
GET TO THE CHOPPER f
HERE IS MY INVITATION b
LET OFF SOME STEAM BENNET c
ENOUGH TALK
BECAUSE I'M GOING TO SAY PLEASE f
GET TO THE CHOPPER i
HERE IS MY INVITATION i
GET UP 1
YOU'RE FIRED 10
ENOUGH TALK
BULLSHIT
GET TO THE CHOPPER i
HERE IS MY INVITATION i
GET UP 8
YOU'RE FIRED 10
ENOUGH TALK
YOU HAVE NO RESPECT FOR LOGIC
YOU HAVE NO RESPECT FOR LOGIC
GET TO THE CHOPPER c
HERE IS MY INVITATION c
GET DOWN 1
ENOUGH TALK
CHILL
GET TO THE CHOPPER i
HERE IS MY INVITATION i
HE HAD TO SPLIT 10
ENOUGH TALK
TALK TO THE HAND i
GET TO THE CHOPPER q
HERE IS MY INVITATION q
GET DOWN 1
ENOUGH TALK
GET TO THE CHOPPER p
HERE IS MY INVITATION m
HE HAD TO SPLIT k
I LET HIM GO 10
ENOUGH TALK
GET TO THE CHOPPER k
HERE IS MY INVITATION k
HE HAD TO SPLIT 10
ENOUGH TALK
GET TO THE CHOPPER f
HERE IS MY INVITATION p
YOU ARE NOT YOU YOU ARE ME 1
ENOUGH TALK
BECAUSE I'M GOING TO SAY PLEASE f
GET TO THE CHOPPER d
HERE IS MY INVITATION d
GET UP 1
ENOUGH TALK
BULLSHIT
GET TO THE CHOPPER e
HERE IS MY INVITATION e
GET UP 1
ENOUGH TALK
YOU HAVE NO RESPECT FOR LOGIC
CHILL
I'LL BE BACK i
HASTA LA VISTA, BABY

在线尝试!

非高尔夫代码(5178字节):

IT'S SHOWTIME
    HEY CHRISTMAS TREE left
        YOU SET US UP 0
    HEY CHRISTMAS TREE right
        YOU SET US UP 0
    HEY CHRISTMAS TREE input
        YOU SET US UP 0
        GET YOUR ASS TO MARS input
        DO IT NOW
        I WANT TO ASK YOU A BUNCH OF QUESTIONS AND I WANT TO HAVE THEM ANSWERED IMMEDIATELY
    HEY CHRISTMAS TREE width
    YOU SET US UP 0
    GET YOUR ASS TO MARS width
    DO IT NOW calcwidth input
    DO IT NOW buildline width input
YOU HAVE BEEN TERMINATED

LISTEN TO ME VERY CAREFULLY calcwidth
    I NEED YOUR CLOTHES YOUR BOOTS AND YOUR MOTORCYCLE input
    GIVE THESE PEOPLE AIR
    HEY CHRISTMAS TREE result
    YOU SET US UP 2
    HEY CHRISTMAS TREE calc
    YOU SET US UP input
    STICK AROUND calc
        GET TO THE CHOPPER calc
        HERE IS MY INVITATION calc
        HE HAD TO SPLIT 10
        ENOUGH TALK
        BECAUSE I'M GOING TO SAY PLEASE calc
            GET TO THE CHOPPER result
        HERE IS MY INVITATION result
        GET UP 1
        ENOUGH TALK
    YOU HAVE NO RESPECT FOR LOGIC
    CHILL
    I'LL BE BACK result
HASTA LA VISTA, BABY

LISTEN TO ME VERY CAREFULLY buildline
    I NEED YOUR CLOTHES YOUR BOOTS AND YOUR MOTORCYCLE width
    I NEED YOUR CLOTHES YOUR BOOTS AND YOUR MOTORCYCLE input
    GIVE THESE PEOPLE AIR

    HEY CHRISTMAS TREE ctr
        YOU SET US UP -2
        GET TO THE CHOPPER ctr
            HERE IS MY INVITATION ctr
            GET UP width
        ENOUGH TALK
    HEY CHRISTMAS TREE mask
        YOU SET US UP 1
        STICK AROUND ctr
            GET TO THE CHOPPER mask
                HERE IS MY INVITATION mask
                YOU'RE FIRED 10
            ENOUGH TALK
            GET TO THE CHOPPER ctr
                HERE IS MY INVITATION ctr
                GET DOWN 1
            ENOUGH TALK
        CHILL
    HEY CHRISTMAS TREE digit
        YOU SET US UP 0
    HEY CHRISTMAS TREE decider
        YOU SET US UP 0
    HEY CHRISTMAS TREE result
        YOU SET US UP 0
    HEY CHRISTMAS TREE lines
        YOU SET US UP width
    HEY CHRISTMAS TREE left
        YOU SET US UP 0
    HEY CHRISTMAS TREE right
        YOU SET US UP 1
    HEY CHRISTMAS TREE leftcounter
        YOU SET US UP 0
    HEY CHRISTMAS TREE rightcounter
        YOU SET US UP 0
    HEY CHRISTMAS TREE widthcounter
    YOU SET US UP width
    STICK AROUND lines
        GET TO THE CHOPPER result
            HERE IS MY INVITATION 0
        ENOUGH TALK
        GET TO THE CHOPPER leftcounter
            HERE IS MY INVITATION left
        ENOUGH TALK
        GET TO THE CHOPPER rightcounter
            HERE IS MY INVITATION right
        ENOUGH TALK
        GET TO THE CHOPPER widthcounter
            HERE IS MY INVITATION width
        ENOUGH TALK
        STICK AROUND widthcounter
            BECAUSE I'M GOING TO SAY PLEASE leftcounter
                GET TO THE CHOPPER result
                    HERE IS MY INVITATION result
                    GET UP 1
                    YOU'RE FIRED 10
                ENOUGH TALK
                GET TO THE CHOPPER leftcounter
                    HERE IS MY INVITATION leftcounter
                    GET DOWN 1
                ENOUGH TALK
            BULLSHIT
                GET TO THE CHOPPER decider
                    HERE IS MY INVITATION rightcounter
                    LET OFF SOME STEAM BENNET widthcounter
                ENOUGH TALK
                BECAUSE I'M GOING TO SAY PLEASE decider
                    GET TO THE CHOPPER result
                        HERE IS MY INVITATION result
                        GET UP 1
                        YOU'RE FIRED 10
                    ENOUGH TALK
                BULLSHIT
                    GET TO THE CHOPPER result
                        HERE IS MY INVITATION result
                        GET UP 8
                        YOU'RE FIRED 10
                    ENOUGH TALK
                YOU HAVE NO RESPECT FOR LOGIC
            YOU HAVE NO RESPECT FOR LOGIC

            GET TO THE CHOPPER widthcounter
                HERE IS MY INVITATION widthcounter
                GET DOWN 1
            ENOUGH TALK
        CHILL
        GET TO THE CHOPPER result
            HERE IS MY INVITATION result
            HE HAD TO SPLIT 10
        ENOUGH TALK
        TALK TO THE HAND result
        GET TO THE CHOPPER lines
            HERE IS MY INVITATION lines
            GET DOWN 1
        ENOUGH TALK
        GET TO THE CHOPPER digit
            HERE IS MY INVITATION input
            HE HAD TO SPLIT mask
            I LET HIM GO 10
        ENOUGH TALK
        GET TO THE CHOPPER mask
            HERE IS MY INVITATION mask
            HE HAD TO SPLIT 10
        ENOUGH TALK
        GET TO THE CHOPPER decider
            HERE IS MY INVITATION digit
            YOU ARE NOT YOU YOU ARE ME 1
        ENOUGH TALK
        BECAUSE I'M GOING TO SAY PLEASE decider
            GET TO THE CHOPPER left
                HERE IS MY INVITATION left
                GET UP 1
            ENOUGH TALK
        BULLSHIT
            GET TO THE CHOPPER right
                HERE IS MY INVITATION right
                GET UP 1
            ENOUGH TALK
        YOU HAVE NO RESPECT FOR LOGIC
    CHILL
    I'LL BE BACK result
HASTA LA VISTA, BABY

不幸的是,我认为这个答案无效,因为它限制了输入太多,并以不允许的方式(使用非空白背景)修改了输出。抱歉!
HyperNeutrino

@HyperNeutrino足够公平,这和您从ArnoldC获得的一样好。
TemporalWolf

那真不幸。很抱歉浪费您的时间和精力,但是我相信您将不得不删除它,因为挑战说明对此无效。您可以在Meta上询问您是否认为这是错误的选择,社区可以决定该怎么做。谢谢:)
HyperNeutrino

1
@HyperNeutrino我不相信ArnoldC会自动无效:它适用于ArnoldC支持的整个有效整数范围,并且不会通过使用较小的空间来“滥用”它:如果您以32位支持ArnoldC编写,我的回答是可以以这种精度工作而无需更改。规则没有规定填充字符的要求,也没有规定必须达到的最小高度龙卷风。但是,如果您愿意,我会在meta上提问。
TemporalWolf

1
你是对的。足够公平,继续下去;我误判了。好的答案:)
HyperNeutrino

1

哈斯克尔 67 64字节

输入被翻转:0表示右移,1左移:

f n=zipWith(\x y->(' '<$[1..y])++('#'<$[1..n-x]))[0..].scanl(+)0

在线尝试!

“无高尔夫球”

f n = zipWith (\x y-> replicate y ' ' ++ replicate (n-x) '#') [0..] . scanl (+) 0


1

C,68 63字节

s;f(w,i)int*i;{for(;w;s+=!*i++)printf("%*s%0*d\n",s,"",w--,0);}

这利用了printf()格式字符串中的动态字段宽度规范。该函数的调用方式如下:

#include <stdio.h>

s;f(w,i)int*i;{for(;w;s+=!*i++)printf("%*s%0*d\n",s,"",w--,0);}

int main() {
    f(8, (int[]){1, 0, 0, 0, 1, 0, 0});
}

通过删除int s=0;并放在s;之前节省5个字节f(w,i)像这样
MD XF

1

JavaScript(ES6),64字节

匿名函数采用循环语法(a)(b)的参数。在b数组中,空字符串表示从右侧删除,空格表示从左侧删除。

a=>b=>b.reduce((t,v)=>t+'\n'+(a.pop(),p+=v)+a,a=Array(a+1),p='')

如示例中那样使用1和0,得分为70

a=>b=>b.reduce((t,v)=>t+'\n'+(a.pop(),v?p:p+=' ')+a,a=Array(a+1),p='')

测试

F=
a=>b=>b.reduce((t,v)=>t+'\n'+(a.pop(),p+=v)+a,a=Array(a+1),p='')

function update() {
  var b=B.value.match(/\d/g)
  
  if (b) {
    b=b.map(v=>+v?'':' ')
    O.textContent = F(b.length+1)(b)
  }
  else
    O.textContent = 'invalid input'
}

update()
  
Input B (0 and 1) <input id=B value='1001' oninput='update()'>
<pre id=O></pre>


0

PowerShell,53字节

param($a,$b)"#"*$a;0..$a|%{" "*($i+=$b[$_])+"#"*--$a}

在线尝试!

将输入$a作为可选整数以及$b作为10s 的数组。(请注意,我的1和数组0是从挑战数组中翻转过来的。)构造的初始行#并将其留在管道上。然后从循环0$a。每次迭代,我们输出可能增加的空格数,然后输出预先减少的空格数#。是的,这将在最后吐出空白的尾随换行符,因为我们正在循环$a而不是列表中的项数。

所有单独的字符串都留在管道上,并且在程序完成时会隐含在它们之间的换行符输出。


0

C#,181字节

n=>a=>{var r=new string[n];r[0]=new string('#',n);for(int i=1,p;i<n;++i){r[i]=r[i-1];p=a[i-1]?r[i].LastIndexOf('#'):r[i].IndexOf('#');r[i]=r[i].Remove(p,1).Insert(p," ");}return r;}

在线尝试!

完整/格式化版本:

class P
{
    static void Main()
    {
        System.Func<int, System.Func<bool[], string[]>> f = n => a =>
        {
            var r=new string[n];
            r[0]=new string('#',n);

            for (int i = 1, p; i < n; ++i)
            {
                r[i] = r[i - 1];
                p = a[i - 1] ? r[i].LastIndexOf('#') : r[i].IndexOf('#');
                r[i] = r[i].Remove(p, 1).Insert(p, " ");
            }

            return r;
        };

        System.Console.WriteLine(string.Join("\n", f(5)(new[] { true, false, false, true })));

        System.Console.ReadLine();
    }
}

0

木炭,17字节

FN«P×#⁺¹ι¿I§⮌ηι↑↖

在线尝试!链接是最接近的详细代码版本。说明:

FN«

第一个输入给出循环迭代的次数。

P×#⁺¹ι

由于循环索引默认为零索引,因此我们在此处添加1以获得正确的#s数。

¿I§⮌ηι

从龙卷风的底部开始并进行处理会节省一个字节,但是然后我们需要反转第二个输入,以便可以对当前数字进行索引。

如果当前数字是a 1,请向上移动。这使得上一行#在末尾有多余的字符。

如果当前数字是a 0,请向上和向左移动。这使得上一行#在开始时有一个额外的内容。


0

C#,159字节

using System.Linq;n=>a=>new[]{new string('#',n)}.Concat(a.Select((_,i)=>{var s=a.Take(i+1).Count(j=>j==0);var h=n-i-1;return new string('#',h).PadLeft(s+h);}))

说明

 new[] { new string('#', n) }                //start with n number of hashes
                .Concat(                     //append...
                    a.Select((_, i) =>       //    map each element of array
                    {
                        var s = a.Take(i + 1).Count(j => j == 0);  // count the number of 0's up to, and including, this point
                        var h = n - i - 1;                         // number of hashes for this step
                        return new string('#', h).PadLeft(s + h);  // number of hashes padded left with number of 0s
                    }));

在线尝试!


0

PHP,136字节

$b=explode(',',end($argv));$a=$argc==3?$argv[1]:count($b)+1;$c=0;for(;$a;--$a){printf("% {$c}s%'#{$a}s\n",'','');$c+=!array_shift($b);}

保存在php文件中,然后使用进行测试php file.php 8 '1,0,0,0,1,0,0'。输出:

########
#######
 ######
  #####
   ####
   ###
    ##
     #

las,准备好输入是工作的一半。

str_repeat代替(printf...)goto的所有其他版本(158字节):

$b=explode(',',end($argv));$a=$argc==3?$argv[1]:count($b)+1;$c=0;z:
echo str_repeat(' ',$c).str_repeat('#',$a)."\n";while(--$a){$c+=!array_shift($b);goto z;}
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.