给定一个整数n
作为输入,返回包含的列表n
,重复n
一次。例如,程序将把5
它变成[5,5,5,5,5]
。元素必须是整数,而不是字符串。不允许使用内置函数来完成任务。
这是代码高尔夫球,因此适用标准规则。
built-in
。
*
运算符可以吗?内置的不合格示例是什么?
给定一个整数n
作为输入,返回包含的列表n
,重复n
一次。例如,程序将把5
它变成[5,5,5,5,5]
。元素必须是整数,而不是字符串。不允许使用内置函数来完成任务。
这是代码高尔夫球,因此适用标准规则。
built-in
。
*
运算符可以吗?内置的不合格示例是什么?
Answers:
x
请注意,这不是n
n
内置的“重复次数”,它的功能比这更通用。例如4,5,6x1,2,3
等于[4, 5, 5, 6, 6, 6]
。由于只有一个参数,果冻正好用它作为左,对于提供的链接右边的参数都,但这个功能不是固有的到x
。
如果这不算数,那么会有各种有趣的2字节替代方案:
x` ṁ` Ra Rị R» a€ oR oḶ oṬ oẊ Ḷị Ḷ» Ṭị Ṭ» Ẋị Ẋ» ị€ ṛ€ ȧ€ »€
等等
x
“一切顺利”,您可能会禁止使用此答案,但是最肯定的不是-链接解析和数组强制逻辑会隐式地将“ 0字节”转换成repeat([n], n)
,这正是其他答案做。
ṁ
,ẋ
因此,答案可能是三个答案中的任何一个。“重复n
n
时间” 没有3个内置函数(一个希望如此),因此它们不能全部成为它的“内置”函数。
f={a=[];t=_this;while{count a<t}do{a=a+[t]};a}
致电:
hint format["%1", 5 call f]
输出:
i--
,并且+=
在其中?
五个同样简短的解决方案。最后两个由Zacharý提供。
⍴⍨
⍴
周期性[R ESHAPE
⍨
自
/⍨
/
复制
⍨
自
\⍨
\
扩大
⍨
自
⌿⍨
⌿
沿第一个(也是唯一一个)轴复制
⍨
自
⍀⍨
⍀
沿第一个(也是唯一一个)轴扩展
⍨
自
⌿⍨
和⍀⍨
工作。
⍨
通过键入``selfie。
~
是not
将1:n转换0
为大小为n 的s 的数组的 运算符 。您可以!
代替它使用。
rep(n<-scan(),n)
too close to a builtin?
.D)
.
f f r 2
2
r
r d
f s t f
d
dip f s t
f
t
dot f
dot
s
s dip
f
dab
f
is an alias for dab
(tail).
s
is subtraction, as explained on the wiki: (x, y) → (0, y−x) when x ≤ y.
t
maps (a, b, c…) to (b+c+…, a+b+c+…).
f s t
maps (a, b, c…) to a. This is our “head” function.
d
dips only the head of its argument: (a, b, c…) → (|a−1|, b, c…)
r
is the main repetition logic. We map (a, b) to (*r(|a−1|, b), b).
For example, r(4, 7) will evaluate as
r(4, 7)
= r(3, 7), 7
= r(2, 7), 7, 7
= r(1, 7), 7, 7, 7
= r(0, 7), 7, 7, 7, 7
→ This would call r(1, 7), but (1, 7) ≥ (0, 7), so surrender!
= 0, 7, 7, 7, 7, 7.
Finally, we define 2
, which maps n → (n, n), and define main
as f f r 2
, computing r(n, n) and chopping off the first two elements.
\newcommand{\f}[1]{#1\count0=2\loop,#1\advance\count0 by1\ifnum\count0<#1\repeat}
\documentclass[12pt,a4paper]{article}
\begin{document}
\newcommand{\f}[1]{#1\count0=2\loop,#1\advance\count0 by1\ifnum\count0<#1\repeat}
\f{5}
\f{10}
\end{document}
´R
´ -- Apply next function twice to same argument
R -- given an integer n and some element, replicate the element n-times
ṠIR
replicate>>=id
Thanks to @nimi, I don't need any import anymore. Yay!
It's a function that takes an integer argument; for example, the following returns [5,5,5,5,5]
:
(replicate>>=id) 5
id=<<replicate
? It's also 14 bytes but doesn't need the import.
n->java.util.Arrays.stream(new int[n]).map(i->n)
-2 bytes thanks to @Jakob
Inspired by the comments in @OlivierGrégoire's post, and optimized a little further. Takes an integer input, creates an IntStream of n
elements, then maps each element to n
and returns it.
java.util.Arrays.stream(new int[n])
.
-4 bytes thanks to @DomHastings
sub{(@_)x"@_"}
Is x
a builtin that does the entire task? Sort of? Not really? Rules unclear?
Edit: Yeah, probably it's fine.
$_[0]
to @_
! Also the second can be"@_"
I think...
$_=$_ x$_
with perl -pe
?
x
does string repetition, not list repetition, unless the left operand is in parentheses (or is a qw
operator) and the x
is evaluated in list context. And of course $_
is a scalar, not a list.
$~
Same as the APL answer: reflexively shape the input. In other words:
$~ y
y $ y
NB. y copies of y
>-[-[-<]>>+<]>->#[->+>+<<]>>[-<;<<.>>>]
Prints N
N
times. Works by generating 32, taking input, then duplicating the input twice, then output the first for each 1 in the second.
int*f(k){int*r=malloc(k*4),a=k;for(;a-->0;)r[a]=k;k=r;}
Returns a list of k
integers.
eax
for locals. Go figure.
*f(k){int r[k],
instead of int*f(k){int*r=malloc(k*4),
{[[_]*_1]}
Explanation:
{[[_]*_1]}
{ } /* Anonymous function */
_ /* The input (_1) */
[ ] /* As a list */
*_1 /* Repeated _1 times */
[ ] /* Pushed to the stream */
[_]*_
= [_1]*_2
. Because the first underscore is the first, it has automatically the number 1.
[->+>+<<]>[->.<]
[->+>+<<] Duplicate 'n' into the next 2 cells to the right
> Move to the first duplicate
[->.<] Print 'n', 'n' times
As I'm sure you're aware, brainfuck takes input and output values as ASCII characters. So a !
is represented as the value 33.
yes $1|sed $1q
As a zsh function, 20 19 bytes:
f(){yes $1|sed $1q}
yes $1|sed $1q
?
declare -i
integer variables. But it also has to be an array. I'm not sure bash even supports an integer array (like eval declare -ia "$1"
to use the first function arg as the name of an array return value.) I upvoted this because it follows the spirit of the question; I doubt the question meant to exclude languages that don't really have integer lists / arrays.
tY"
Explanation:
t % duplicate elements
Y" % replicate elements of array
% (implicit) convert to string and display
n->{int a[]=new int[n],i=n;for(;i-->0;)a[i]=n;return a;}
-2 bytes thanks to @KevinCruijssen
n->{int a[]=new int[n],i=n;for(;i-->0;)a[i]=n;return a;}
IntStream.generate(() -> n).limit(n)
but decided it wasn't worth typing up and upvoted this instead :)
Array.init
. Just assign it to a variable with a type alias, and boom: let f: (Int, Int) -> [Int] = Array.init; print(f(5, 5))