断弦金字塔


10

给定字符串n,创建一个字符串金字塔,相对于当前行,该金字塔分为几部分。

第一行包含未修改的字符串。

第二行包含被管道分成两半的字符串。

第三行将其分隔三分之二...

等等。每个子字符串的长度,其中l是字符串n的长度等于

地板(l / n)

剩下的字符将放在其自己的子字符串中。使用的最后一行是第一行,其中子字符串的长度为2。

测试用例:

输入:您好,世界。

输出:

Hello, world.

Hello,| world|.

Hell|o, w|orld|.

Hel|lo,| wo|rld|.

He|ll|o,| w|or|ld|.

输入:abcdefghij

输出:

abcdefghij

abcde|fghij

abc|def|ghi|j

ab|cd|ef|gh|ij

输入:01234567890abcdef

输出:

01234567890abcdef

01234567|890abcde|f

01234|56789|0abcd|ef

0123|4567|890a|bcde|f

012|345|678|90a|bcd|ef

01|23|45|67|89|0a|bc|de|f

额外规则:

  • 您可以编写完整的程序或函数,而使用较少的代码。

  • 输入内容的长度至少应为4个字符。

  • 如果您的语言支持,则必须使用换行符。如果不可能,将换行符替换为:

  • 输入将始终是可打印的ASCII。

  • 如果您的程序解决了P对NP,请减负100%。


排行榜:


0字节:return: false
Gabriel Benamy

3
不错的第一个挑战!一些澄清问题-输入是否仅可打印ASCII(我强烈建议“是”)?“在可能的情况下必须换行”是什么意思?
AdmBorkBork

3
开个玩笑。P vs NP是计算中尚未解决的问题。开玩笑的是,如果您能解决它,我将不再关心您的程序无法解决挑战这一事实。
朱利安·拉希涅

3
计算中真正未解决的问题是“制表符或空格” ...
FlipTack

3
不,真正的问题是Internet Explorer。
朱利安·拉希涅

Answers:


0

的JavaScript(ES6),103 101 91 84个字节

固定以尊重挑战要求

f=(s,n=0,p=s.length/++n|0)=>p>1?s.match(eval('/.{1,'+p+'}/g')).join`|`+'\n'+f(s,n):''

f将输入字符串作为第一个参数s并递归打印以控制拆分字符串的Lambda 。非常简单:只要子字符串的长度p大于1,就打印以'|'分隔的字符串 每个p字符,然后继续添加以下级别。然后这将再次调用该函数并使其p处于t / n下限位置,其中t原始字符串长度n为增量分频器。


我不认为n每次除以2是正确的。
尼尔

@尼尔,你是正确的,我是错误的。我已解决问题,并在此过程中节省了2个字节。
XavCo7

我想到了@ETHproductions,但是我不知道这是否算作STDOUT ...我想我需要紧接着做alert(f(s))吗?
XavCo7 '16

4

Perl,46 +1 = 47字节

-n标志跑

say s/.{$=}(?=.)/$&|/gr while($==y///c/++$,)-2

在线尝试!

代码细目

-n                                              #Reads input into the $_ variable
say s/.{$=}(?=.)/$&|/gr while($==y///c/++$,)-2
                                 y///c          #Transliteration.  Implicitly operates on $_, replacing every character with itself and counting replacements
                                                #y///c effectively returns the length of $_
                                      /++$,     #Increments $, (which starts off at 0) and divides the length of $_ by $,
                              $==               #Stores the result of this division into $=
                                                #$= forces its contents to be an integer, so it truncates any decimal
                             (             )-2  #Returns 0 if $= is equal to 2
                        while                   #Evaluates its RHS as the condition.  If truthy, evaluates its LHS.
    s/          /   /gr                         #Substitution.  Implicitly operates on $_.
                                                #Searches for its first argument and replaces it with its second argument, repeating until it's done, and returns the new string.  $_ is not modified.
      .{$=}                                     #Looks for a string of $= characters...
           (?=.)                                #...that is followed by at least one non-newline character, but does not include this character in the match...
                 $&|                            #...and replaces it with itself followed by a pipe character.
say                                             #Output the result of the substitution.

对于较长的输入,这似乎不起作用。
尼尔

2

Pyth,16个字节

Vh/lQ3j\|cQ/lQhN

V                # For N in range(1, \/ )
 h/lQ3           # 1+lenght(input)/3
      j\|        # join with '|'
         cQ      # chop input in
           /lQhN # lenght(input)/(N+1) pieces

试试这里


1
这可能适用于测试用例,但我认为它不适用于较长的输入。
尼尔

2

C,145个 131 128 125字节

l,n,i=1,j;f(char*s){l=strlen(s);puts(s);do{n=l/++i;for(j=0;j<l;)j&&(j%n||putchar('|')),putchar(s[j++]);puts("");}while(n>2);}

此函数以字符串作为参数,并将输出打印到STDOUT。

l,n,i=1,j;       // declare some variables
f(char*s){       // declare the function
l=strlen(s);     // get the length of the string
puts(s);         // output the initial version, with trailing newline
do{n=l/++i;      // n is the number of characters per "section",
                 //  and we'll do-while n>2 to stop at the right time
for(j=0;j<l;)    // loop through the characters of the string
j&&(             // if j != 0,
j%n||            // and j % n == 0,
putchar('|')),   // insert a | before this character
putchar(s[j++]); // print the character
puts("");        // print a newline after the loop
}while(n>2);}

一次如何工作i*i>l?看来它将开始重复各节。
尼尔

@尼尔,我不确定你的意思。你能举个例子吗?
门把手

@Neil啊,没关系,我明白你在说什么。这似乎是规范中的一个漏洞,该漏洞明确指出每个子字符串的长度为floor(l/n);。我不确定长时间输入的预期行为是什么,或者OP是否预期那样。
门把手

1

Pyth,17个字节

jmj\|cQ/lQdSh/lQ3

说明

     cQ/lQ         Divide into equal pieces (with the last shorter)
  j\|              Join with pipes
 m        d        Map to each row index...
           Sh/lQ3  ... up to the first row with substrings of length 2
j                  Join with newlines


0

Ruby 60 + 1 = 61字节

+1字节的-n标志。

z= ~/$/
(z/3+1).times{|n|puts $_.scan(/.{1,#{z/(n+1)}}/)*?|}

在Ideone上查看:http://ideone.com/RtoReG


0

Python 3,123个字节

f=lambda s:print(*['|'.join(s[i:i+n]for i in range(0,len(s),n))for n in[len(s)//i for i in range(1,len(s)//2+1)]],sep='\n')

在较长的字符串中,某些部分将重复,因为子字符串的长度公式为floor(l/n)。例如,如果字符串长度为13个字符,则拆分为5的字符串将与拆分为6的字符串相同floor(13/5)==floor(13/6)。我不确定操作人员是否期望这样做,或者这是疏忽大意。

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.