我的设备出现故障...滥用其未定义的字符串行为!


12

救命!我的设备出现故障,每当我尝试重复一个String时,都会得到混乱的结果。与其重复N次相同的字符串,不如使用每个字符填充一个NxN的正方形,然后将正方形堆叠起来。

例如,给定String "Test"和number 2,而不是"TestTest",我得到:

TT
TT
ee
ee
ss
ss
tt
tt

一段时间后,我开始喜欢它。今天的任务是重现这种奇怪的行为。给定一个仅由可打印的ASCII组成的非空字符串,再加上一个整数,输出故障设备返回的字符串。

  • 所有标准规则均适用。

  • 输入和输出可以通过任何合理的方式进行处理。

  • 这是,因此每种语言中以字节为单位的最短代码获胜。


测试用例

输入值 
输出量

----------

“测试”,2

TT
TT
ee
ee
ss
ss
tt
tt

----------

“ UuU”,3

单位
单位
单位
u
u
u
单位
单位
单位

----------

“ A”,5

美国AAAA
美国AAAA
美国AAAA
美国AAAA
美国AAAA

----------

您可以在此处找到更大的测试用例。祝你好运,打高尔夫球!



1
“行列表”而不是由换行符分隔的字符串是否有效?
geokavel

21
嗯,我看不到标题中的“滥用其未定义的行为”在任务中实际上如何显示。没有未定义的行为,任务是重现非常明确定义的行为。
圣保罗Ebermann

3
it fills an NxN square-陈述不正确。
Magic Octopus Urn'Sep

Answers:


9

果冻,4 字节

受到Xcoder先生的果冻虐待的启发

x⁹×Y

一个完整的程序,将字符串和数字作为命令行参数并打印结果(由于是双向链接,它会返回字符串和换行符的列表,这可能实际上是不可接受的)。

在线尝试!

怎么样?

天真(不辱骂)的五种方式是:

x⁹x€Y - Main link: list of characters, s; number, n  e.g. "xyz", 2
 ⁹    - chain's right argument, n                         2
x     - times (repeat each element)                       ['x','x','y','y','z','z']
  x€  - times for €ach                                   [['x','x'],['x','x'],['y','y'],['y','y'],['z','z'],['z','z']]
    Y - join with newlines                                ['x','x','\n','x','x','\n','y','y','\n','y','y','\n','z','z','\n','z','z']
      - as a full program: implicit print
      -   note: this could be submitted as a dyadic link (AKA unnamed function)

Xcoder先生使用的滥用行为(Python operator.mul可能作用于strand和a int来重复str-这里是单个字符-以及使用它的原子将×其左参数矢量化),在这里也可以替换x€×-产生完整程序:

x⁹×Y - Main link: list of characters, s; number, n  e.g. "xyz", 2
 ⁹   - chain's right argument, n                         2
x    - times (repeat each element)                       ['x','x','y','y','z','z']
  ×  - multiply (vectorises)                             ["xx","xx","yy","yy","zz","zz"]
     -     (note, these "..." are actually strings, something not usually seen in Jelly) 
    Y - join with newlines                                ["xx",'\n',"xx",'\n',"yy",'\n',"yy",'\n',"zz",'\n',"zz"]
      - implicit print

嘿关于虐待的挑战滥用
在Outgolfer埃里克





3

PowerShell,31个字节

param($a,$b)$a|%{,("$_"*$b)*$b}

在线尝试!

说明:

param($a,$b)                    # Takes input $a (char-array) and $b (integer)
            $a|%{             } # Loop through every character in $a
                   "$_"*$b      # Construct a string of $b length of that character
                 ,(       )*$b  # Repeat that $b times
                                # Implicit Write-Output inserts a newline between elements



2

MATL,5个字节

t&Y"!

在线尝试!

说明

t     % Implicitly input a number, n. Duplicate
&Y"   % Implicitly input a string. Three-input repelem function. Repeats
      % each entry in the first input (s) the specified numbers of times 
      % vertically (n) and horizontally (n). Gives a char matrix
!     % Transpose. Implicit display

2

C ++,125个 123字节

-2字节感谢aschepler

#include<string>
using s=std::string;s u(s a,int b){s r;for(auto&c:a)for(int i=0;i<b*b;){if(!(i++%b))r+=10;r+=c;}return r;}

确保+=被调用的运算符的重载采用char此指令中的数据类型if(!(i++%b))r+=10


2
using s=std::string;typedef std::string s;两个字节短。
aschepler

2

Japt,7个字节

输出字符串数组。

VÆmpVÃy

试试看-R仅出于可视化目的标记)


说明

字符串U和整数的隐式输入V

VÆ    Ã

0to 生成一个整数数组,V-1并将每个整数传递给一个函数。

mpV

将(m)映射U并重复(r)每个字符V时间。

y

转置并隐式输出结果数组。


1

R,59个字节

function(S,n)write(rep(strsplit(S,"")[[1]],e=n*n),"",n,,"")

写入标准输出。

将字符串拆分为字符,每次重复n^2,然后以宽度打印n且不使用分隔符。

在线尝试!


1

J,15 14字节

[:,/]$"1 0~[,[

肯定不是最佳的。返回2D字符数组。需要n为其左参数和字符串作为的权利。

在移动设备上,因此缺少常规的便利设施。

说明

(旧答案)

[:,/(2#[)$"1 0]

$"1 0 重塑每个角色以

(2#[)一个n* n矩阵。

,/ 将矩阵连接在一起以产生输出。



@英里辉煌!我想说的,应该亲自回答。
科尔

@miles发布您的信息:)
Ven



1

木炭,9字节

FS«GTIηι↓

在线尝试!

说明

FS         For each character (i) in the next input as a string
   «
    G    ι  Polygon using i as fill
      T      Right, down, then left
       Iη   The second input (h) casted (to a number)
           ↓ Move down

1

Brainfuck,103字节

,[>,]-[>+<-----]>---[-<<->>]<<[->+<]>[[->+>+<<]>>[-<<+>>]<[<<<[<]>.[>]>>-]++++++++++.[-]<<<[<]>[-]>[>]>

在线尝试(请确保打开动态内存,否则它将无法运行)

注意:输入略有不同。该代码接受一个字符串,其中最后一个字符是重复次数的数字。因此输入可能看起来像Test5

此代码需要无限制的磁带,并且依赖字节包装行为。

取消高尔夫:

,[>,]< Take Input
>-[>+<-----]>--- ASCII 0, to use in next step
[-<<->>]<< Convert ASCII number to raw number by subtracting ASCII 0
[->+<]> Move the number over one to separate it from the phrase
[
  [->+>+<<]>>[-<<+>>]< Copy the number
  [
    <<<[<]> Back to Letter
    . Print Letter
    [>]>>- Back to Counter
  ]
  ++++++++++.[-]< Print the newline
  <<[<]>[-]> Clear Letter
  [>]> Back to Counter
]

1

SOGLOnline commit 2940dbe,4 字节

∙ι*I

这是针对特定的提交而进行的,即我在更改之前所做的一次,当用于字符串数组时,将每个字母重复x次以重复每个项目x次。是没有该版本的在线解释器,可以看到它不起作用。

要尝试提交,请下载index.html文件,打开文件,在程序粘贴中∙ι*I,在输入中输入类似

Test
2

说明:

∙ι*I
∙     get an array with 2nd input amount of items of the 1st input
 ι    pop one implicit input to cycle back to the number
  *   multiply horizontally each separate character
   I  rotate clockwise

如果不起作用,为什么要链接?
isaacg

@isaacg好问题。首先,我打算写些什么,但忘记了
dzaima

1

爪哇8,152个 128 118 100字节

s->n->{for(char c:s)for(int j=0;j++<n;System.out.println("".valueOf(new char[n]).replace('\0',c)));}

在线尝试!


2
100个字节:s->n->{for(char c:s)for(int j=0;j++<n;System.out.println("".valueOf(new char[n]).replace('\0',c)));}
Nevay

1

APL(Dyalog),8字节

将重复作为左参数,将文本作为右参数。

{⍺⌿⍺/⍪⍵}

在线尝试!

{} 一个未命名的lambda,其中代表左参数和右参数

⍪⍵ 使文本成为一栏表格

⍺/水平 复制时间

⍺⌿垂直 复制时间


1

Japt,7个字节

mpV² òV

返回字符串数组。

在线尝试!带有-R标志以使用换行符连接数组。

说明

mpV² òV     Implicit input of U=string, V=number
m           Map each char in the input string to...
 pV²        Itself repeated V² times
     òV     Cut the resulting string into partitions of length V




0

Kotlin 1.1-99字节

fun s(s:String,c:Int)=s.flatMap{p->List(c,{p})}.joinToString("\n"){p->List(c,{p}).joinToString("")}

以字符串形式返回整个输出。

无法使用TryItOnline,因为不支持1.1

测试

fun s(s:String,c:Int)=s.flatMap{p->List(c,{p})}.joinToString("\n"){p->List(c,{p}).joinToString("")}

fun main(args: Array<String>) {
    println(s("Hello World", 5))
}

如果可以接受字符串列表作为输出,则为84:

fun s(s:String,c:Int)=s.flatMap{p->List(c,{p})}.map{p->List(c,{p}).joinToString("")}

0

PHP,97字节

for($i=0;$i<strlen($s=$argv[1]);$i++)for($j=0;$j<$r=$argv[2];$j++)echo str_repeat($s[$i],$r)."
";

0

Mathematica,49个字节

(z=#2;Grid[Column/@Table[#,z,z]&/@Characters@#])&

输入

[“测试”,4]


0

Pyth,12个字节

心胸狭窄,但我还没到那儿。

VQp*+*Nszbsz

说明:

VQ          For every letter in the first input...
  p         Print without newline...
   *+*Nszsz (The index * int(second input) + newline) * int(the second input)

测试套件


0

Clojure82 75字节

#(dotimes[x(count %1)](dotimes[y %2](prn(apply str(repeat %2(get %1 x))))))

在线尝试!

未压缩:

#(dotimes [x (count %1)]
  (dotimes [y %2]
    (prn
      (apply str
        (repeat %2 (get %1 x))))))

编辑:通过用stdlib重复函数替换for循环,减少了一些字符。


0

C#(.NET Core),68 + 18字节

a=>n=>new int[a.Length*n].Select((x,i)=>Enumerable.Repeat(a[i/n],n))

还包括在字节数中:

using System.Linq;

在线尝试!

输出是字符集合的集合(每行一个集合)。

说明:

a => n =>                                // Take a string and a number
    new int[a.Length * n]                // Create new collection, 'n' times larger than 'a'
    .Select((x, i) =>                    // Replace every member with
        Enumerable.Repeat(a[i / n], n)   //     a collection of repeated character from 'a', based on index
    )
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.