因此,这是我在此站点上遇到的第一个挑战。
面临的挑战是接受一个将为正的输入整数,并以升序(从到,包括n)打印的输出(其中是当前整数)。
例
给定输入5,程序将打印:
1
8
9
4
1
是1且是8且是9且是4且是1且
输入输出
输入将采用正整数形式。输出将是一个数字列表,以逗号或换行符分隔。
这是代码高尔夫球,因此最短的代码获胜。
因此,这是我在此站点上遇到的第一个挑战。
面临的挑战是接受一个将为正的输入整数,并以升序(从到,包括n)打印的输出(其中是当前整数)。
给定输入5,程序将打印:
1
8
9
4
1
是1且是8且是9且是4且是1且
输入将采用正整数形式。输出将是一个数字列表,以逗号或换行符分隔。
这是代码高尔夫球,因此最短的代码获胜。
Answers:
⍳*⊢-⍳
匿名前缀默认功能。TIO测试范围为[1..10]。
感谢@lirtosiast 3个字节。
⍳*⊢-⍳ ⍝ Tacit function
⍳ ⍝ Range. ⍳n generates the vector [1..n].
⊢- ⍝ Subtracted from the argument. The vector is now [n-1,n-2,...,0]
⍳* ⍝ Exponentiate using the range [1..n] as base. The result is the vector
⍝ [1^(n-1), 2^(n-2), 3^(n-3),...]
⍳*⊢-⍳
是5个字节,使用⎕IO←1
。
lambda n:[i**(n-i)for i in range(1,n+1)] #Outputs a list
n,i=input(),0
exec"print(n-i)**i;i+=1;"*n #Prints in reversed order
.+
*
_
$$.($.'*$($.>`$*)_¶
%~`^
.+¶
在线尝试!说明:
.+
*
将输入转换为一元。
_
匹配每个位置。然后设置几个替换变量。$`
成为比赛的左边;$>`
修改为左边并匹配;$.>`
修改它以获取长度,即当前索引。$'
同时是匹配的权利,$.'
长度也就是当前的指数。
$$.($.'*$($.>`$*)_¶
创建一个字符串$.(
加$.'
的重复$.>`*
加_
。例如,对于原始输入5中的索引2,其$.'
值为3且$.>`
为2,因此结果字符串为$.(2*2*2*_
。这是一个视网膜替代表达式,可以计算2³。每个字符串在其自己的行上输出。
%~`^
.+¶
对于前一阶段生成的每一行,在其前面添加一行.+
,将其变为替换阶段,然后对该阶段求值,从而计算表达式。
谢谢@Neil的2个字节!
INPUT a
FOR b=1TO a
?b^(a-b)
NEXT
REPL.IT的版本略有扩展,因为解释器不完全符合规范。
输出量
QBasic (qb.js)
Copyright (c) 2010 Steve Hanov
5
1
8
9
4
1
b^(a-b)
for b=1..a
)
n=>(g=i=>--n?++i**n+[,g(i)]:1)``
-3字节的@Shaggy积分,-1字节的@ l4m2!
f=(n,i=1)=>n--?[i++**n,...f(n,i)]:[]
n=>[...Array(n)].map(x=>++i**--n,i=0)
:Gy-^
以输入5
为例。
: % Implicit input. Range
% STACK: [1 2 3 4 5]
G % Push input again
% STACK: [1 2 3 4 5], 5
y % Duplicate from below
% STACK: [1 2 3 4 5], 5, [1 2 3 4 5]
- % Subtract, element-wise
% STACK: [1 2 3 4 5], [4 3 2 1 0]
^ % Power, element-wise. Implicit display
% STACK: [1 8 9 4 1]
for(int i=1;a+1>i;i++)System.out.println(Math.pow(i,a-i));
a
,我们不允许这样做。
n->{for(int i=0;i++<n;)System.out.println(Math.pow(i,n-i));}
60个字节(链接中的代码和测试用例)
LD<Rm
说明:
L # List in the range [1, (implicit) input integer]
# i.e. 5 → [1,2,3,4,5]
D< # Duplicate this list, and subtract 1 to make the range [0, input)
# i.e. [1,2,3,4,5] → [0,1,2,3,4]
R # Reverse it to make the range (input, 0]
# i.e. [0,1,2,3,4] → [4,3,2,1,0]
m # Take the power of the numbers in the lists (at the same indices)
# (and output implicitly)
# i.e. [1,2,3,4,5] and [4,3,2,1,0] → [1,8,9,4,1]