给定一个整数数组和两个数字作为输入,请删除由数字指定的一定数量的第一个和最后一个元素。输入可以按照您想要的任何顺序。
您应该删除前x个元素,其中x是第一个数字输入,还应该删除最后y个元素,其中y是第二个数字输入。
保证结果数组的长度至少为2。
例子:
[1 2 3 4 5 6] 2 1 -> [3 4 5]
[6 2 4 3 5 1 3] 5 0 -> [1 3]
[1 2] 0 0 -> [1 2]
给定一个整数数组和两个数字作为输入,请删除由数字指定的一定数量的第一个和最后一个元素。输入可以按照您想要的任何顺序。
您应该删除前x个元素,其中x是第一个数字输入,还应该删除最后y个元素,其中y是第二个数字输入。
保证结果数组的长度至少为2。
[1 2 3 4 5 6] 2 1 -> [3 4 5]
[6 2 4 3 5 1 3] 5 0 -> [1 3]
[1 2] 0 0 -> [1 2]
Answers:
感谢Laikoni,节省了16个字节
多亏了Laikoni,节省了6个字节
多亏了Laikoni,节省了4个字节
当然可以改进,但是作为初学者,请尽我最大的努力。
r=(reverse.).drop
a#b=r b.r a
(5#0) [6,5,4,3,2,1,3]
xs
。
f x a b
为f a b x
,则只需删除x
:f a b=reverse.drop b.reverse.drop a
.。
a#b=let r=reverse in r.drop b.r.drop a
是38个字节。还是允许我们在此函数之外声明一个函数?
using System.Linq;(a,x,y)=>a.Skip(x).Take(a.Count-x-y)
使用List<int>
作为输入。
List
作为输入来保存字节,因此可以使用Count
代替Length
。
Where
时间仅比我也很满意的方法稍长:)
using System.Linq;
字节数:)
using
我在答案中添加的每一个内容Skip
以及Take
需要的方法和数量using
。
|n,i,j|&n[i..<[_]>::len(n)-j]
调用如下:
let a = &[1, 2, 3, 4, 5, 6];
let f = |n,i,j|&n[i..<[_]>::len(n)-j];
f(a, 2, 1)
我与借位检查器进行了很多有趣的战斗,弄清楚最短的方法是什么,以便推断出返回切片的生命周期。它在闭包周围的行为有些不稳定,因为它将推断生存期,但前提是您实际上没有将参数声明为引用类型。不幸的是,这与要求在签名中定义参数类型冲突,因为n.len方法调用需要知道其操作的类型。
我尝试解决此问题的其他方法:
fn f<T>(n:&[T],i:usize,j:usize)->&[T]{&n[i..n.len()-j]} // full function, elided lifetimes
let f:for<'a>fn(&'a[_],_,_)->&'a[_]=|n,i,j|&n[i..n.len()-j] // type annotation only for lifetimes. Currently in beta.
|n:&[_],i,j|n[i..n.len()-j].to_vec() // returns an owned value
|n,i,j|&(n as&[_])[i..(n as&[_]).len()-j] // casts to determine the type
|n,i,j|&(n:&[_])[i..n.len()-j] // type ascription (unstable feature)
|n,i,j|{let b:&[_]=n;&b[i..b.len()-j]} // re-assignment to declare the type
472个周期,5个节点,35行代码
m4,6
@0
MOV 0 ANY
S:MOV UP ACC
JEZ A
MOV ACC ANY
JMP S
A:MOV RIGHT ACC
L:JEZ B
MOV DOWN NIL
SUB 1
JMP L
B:MOV 0 RIGHT
MOV RIGHT NIL
@1
MOV RIGHT LEFT
MOV LEFT DOWN
MOV RIGHT DOWN
MOV DOWN LEFT
@2
MOV UP ACC
MOV UP LEFT
MOV ACC LEFT
@4
MOV 0 RIGHT
MOV UP NIL
S:MOV LEFT ACC
JEZ A
MOV ACC RIGHT
JMP S
A:MOV UP ACC
L:JEZ B
MOV RIGHT NIL
SUB 1
JMP L
B:MOV 0 UP
K:MOV RIGHT ACC
MOV ACC DOWN
JNZ K
@7
MOV UP ANY
顶部的m4,6不是代码的一部分,而是表示内存模块的位置。
通过将其粘贴到游戏中来自己玩此关卡:
function get_name()
return "ARRAY TRIMMER"
end
function get_description()
return { "RECIEVE AN ARRAY FROM IN.A", "RECIEVE TWO VALUES A THEN B FROM IN.T", "REMOVE THE FIRST A TERMS AND LAST B TERMS FROM IN.A", "ARRAYS ARE 0 TERMINATED" }
end
function get_streams()
input = {}
trim = {}
output = {}
arrayLengths = {}
a = math.random(1,5) - 3
b = math.random(1,7) - 4
arrayLengths[1] = 9+a
arrayLengths[2] = 9+b
arrayLengths[3] = 8-a
arrayLengths[4] = 9-b
s = 0
trimIndex = 1
for i = 1,4 do
for k = 1,arrayLengths[i] do
x = math.random(1,999)
input[k+s] = x
output[k+s] = x
end
input[s + arrayLengths[i] + 1]= 0
output[s + arrayLengths[i] + 1]= 0
a = math.random(0,3)
b = math.random(0,arrayLengths[i]-a)
trim[trimIndex] = a
trim[trimIndex+1] = b
trimIndex = trimIndex + 2
s = s + arrayLengths[i] + 1
end
s = 1
trimIndex = 1
for i = 1,4 do
for i = s,s+trim[trimIndex]-1 do
output[i]=-99
end
for i = s + arrayLengths[i] - trim[trimIndex+1], s + arrayLengths[i]-1 do
output[i]=-99
end
trimIndex = trimIndex +2
s = s + arrayLengths[i] + 1
end
trimmedOut = {}
for i = 1,39 do
if(output[i] ~= -99) then
table.insert(trimmedOut, output[i])
end
end
return {
{ STREAM_INPUT, "IN.A", 0, input },
{ STREAM_INPUT, "IN.T", 2, trim },
{ STREAM_OUTPUT, "OUT.A", 1, trimmedOut },
}
end
function get_layout()
return {
TILE_COMPUTE, TILE_COMPUTE, TILE_COMPUTE, TILE_COMPUTE,
TILE_MEMORY, TILE_COMPUTE, TILE_MEMORY, TILE_COMPUTE,
TILE_COMPUTE, TILE_COMPUTE, TILE_COMPUTE, TILE_COMPUTE,
}
end
所以我想这也算是一个不错的答案...
QJi-h)
输入如下:1)从头开始修剪的元素数量;2)从末端修剪的元素数量;3)数组。说明
Q % Implicit input (1). Increment by 1, since MATL indexing is 1-based.
Ji- % Complex 1i minus real input (2). In MATL, the end of the array is given by `1i`.
h % Concatenate indices to get range-based indexing 1+(1):end-(2).
) % Index into (implicitly taken) input array. Implicit display.
(l,i,j)->l.subList(i,l.size()-j)
如果我们真的限制数组,则为53个字节:
(a,i,j)->java.util.Arrays.copyOfRange(a,i,a.length-j)
tniQwi-&:)
它只有11个字节,有点长,但是我正在详细写出来,我自己也要学习。
---- Input ----
[1 2 3 4 5 6]
2
1
----- Code ----
% Implicit first input
t % Duplicate input.
% Stack: [1 2 3 4 5 6], [1 2 3 4 5 6]
n % Number of elements
% Stack: [1 2 3 4 5 6], 6
i % Second input
% Stack: [1 2 3 4 5 6], 6, 2
Q % Increment: [1 2 3 4 5 6], 6, 3
w % Swap last two elements
% Stack: [1 2 3 4 5 6], 3, 6
i % Third input
% Stack: [1 2 3 4 5 6], 3, 6, 1
- % Subtract
% Stack: [1 2 3 4 5 6], 3, 5
&: % Range with two input arguments, [3 4 5]
% Stack: [1 2 3 4 5 6], [3 4 5]
) % Use as index
% Stack: [3 4 5]
% Implicit display
J
,像这样使用时,不可能从中减去某些东西。我怀疑自己错了,我一辈子都想不起来...感谢您的回答,我非常是一名MATL新手...
)
以及更加令人(
发抖的 ……
(
混乱的不仅是我。:)我每次都背诵“ ddi”(=手册中的“ destination,data,indexs”),有时还是会出错。
感谢@Tas节省了一个字节!
#import<list>
int f(std::list<int>&l,int x,int y){for(l.resize(l.size()-y);x--;)l.pop_front();}
#import<list>
f(std::list<int>&l,int x,int y){for(l.resize(l.size()-y);x--;)l.pop_front();}
#include<list>
吗 您可以通过剃光一个字节int f
。编译器将不允许函数返回,但会对其进行警告
int f
它将适用于大多数编译器,我将对其进行编辑。在MinGW上,甚至完全省略了函数的类型。是的,#include<list>
这是一种包含标头的符合标准的方法,但#import<list>
至少应在GCC,MinGW和MSVC上工作,因此也应该很好。
⌽⎕↓⌽⎕↓⎕
这将数组作为第一个输入,然后是两个数字。
⎕ from the input array
⎕↓ drop the first input elements
⌽ reverse the array
⎕↓ drop first input elements
⌽ reverse again to go back to the original array
⎕↓⎕↓⍨-⎕
(()()){({}<{({}<{}>[()])}{}([]){{}({}<>)<>([])}{}<>>[()])}{}
Input is in this format:
x
a
r
r
a
y
y
Where x
is the number to take from the front, y
is the number to take from the back, and the array is just however many numbers you want, separated by newlines. Here are my first two (longer) attempts:
({}<>)<>{({}<{}>[()])}([])<>({}<><{{}({}<>)<>([])}{}><>){({}<{}>[()])}{}([]){{}({}<>)<>([])}<>{}
{({}<{}>[()])}{}([]){{}({}<>)<>([])}{}<>{({}<{}>[()])}{}([]){{}({}<>)<>([])}<>
And here is an explanation:
#Two times:
(()()){({}<
#Remove *n* numbers from the top of the stack
{({}<{}>[()])}{}
#Reverse the whole stack
([]){{}({}<>)<>([])}{}<>
>)[()]}{}
(⌽↓)/
Input format is y x A
Explanation
/
is Reduce, which inserts the function to the left between each pair of elements of the argument
(⌽↓)
is a function train equivalent to {⌽⍺↓⍵}
, which removes the first ⍺
elements of the array ⍵
and then reverses the array. (⍺
is the left argument and ⍵
is the right argument)
Thus, (⌽↓)/y x A
is equivalent to ⌽y↓⌽x↓A
, which is what is needed.
a->n->m->{int l=a.length-m-n,r[]=new int[l];System.arraycopy(a,n,r,0,l);return r;}
Alternative with same (82) byte-count using a loop:
(a,n,m)->{int l=a.length-m,r[]=new int[l-n],i=0;for(;n<l;r[i++]=a[n++]);return r;}
Explanation:
a->n->m->{ // Method with integer-array and two integer parameters and integer-array return-type
int l=a.length-m-n, // Length of the array minus the two integers
r[]=new int[l]; // Result integer-array
System.arraycopy(a,n,r,0,l); // Java built-in to copy part of an array to another array
return r; // Return result-String
} // End of method
arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
:The
java.lang.System.arraycopy()
method copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array. A subsequence of array components are copied from the source array referenced bysrc
to the destination array referenced bydest
. The number of components copied is equal to thelength
argument.The components at positions
srcPos
throughsrcPos + length - 1
in the source array are copied into positionsdestPos
throughdestPos + length - 1
, respectively, of the destination array.
(a,n,m)->
has the same byte-count as a->n->m->
. Although you're right I could have just used a regular call instead of currying. I'm kinda used of using currying when I have two (or more) parameters.. I've already made the mistake of using currying when I have four parameters a few times..
{a,s,e->a.drop(s).dropLast(e)}
Takes List<Int>
as input and drops from begin and then from end.
try it online
. Can you add a caller code? how compile lambda without type definitions in Kotlin? Thanks.
val f: (List<Int>, Int, Int) -> List<Int>
kb₍B&t;Bk₍
Takes input as [x, A, y] where A is the array to trim.
(-1 byte thanks to @Fatalize.)
kb₍B&t;Bk₍
. ,
does append (see the result of this partial program), it doesn't act like ∧
. Also do not try to copy things from old (2016-early 2017) Brachylog answers because it was the first version of the language, and programs are not retrocompatible (in particular, ,
in Brachylog v1 is now ∧
in Brachylog v2)
,
did append in the previous version, but it just didn't matter in this case because there was a t
after it anyway - lucky coincidence. And yeah, I realized the version differences after I posted this, I was still figuring things out and bumbling around at this stage. :)
>E<QE
Takes the arguments in the opposite order. <
and >
in Pyth trim based on argument order. For example, <Q5
will trim off all values in the input after the fifth one.
{_,@-<>}
Anonymous block that takes the inputs from the stack in the order x, y, array, and replaces them by the output array.
Consider inputs 2
, 1
, [10 20 30 40 50 60]
.
{ } e# Block
e# STACK: 2, 1, [10 20 30 40 50 60]
_ e# Duplicate
e# STACK: 2, 1, [10 20 30 40 50 60], [10 20 30 40 50 60]
, e# Length
e# STACK: 2, 1, [10 20 30 40 50 60], 6
@ e# Rotate
e# STACK: 2, [10 20 30 40 50 60], 6, 1
- e# Subtract
e# STACK: 2, [10 20 30 40 50 60], 5
< e# Slice before
e# STACK: 2, [10 20 30 40 50]
> e# Slice after
e# STACK: [30 40 50]
Solution:
{(0-z)_y _x}
Example:
q){(0-z)_y _x}[1 2 3 4 5 6;2;1]
3 4 5
q){(0-z)_y _x}[6 2 4 3 5 1 3;5;0]
1 3
q){(0-z)_y _x}[1 2;0;0]
1 2
Explanation:
{ } / lambda function
x / input array
y _ / drop y elements from .. (takes from start)
(0-z) / negative z ()
_ / drop -z elements from ... (takes from end)
(λ(a i j)(drop-right(drop a i)j))
It can be called like so:
((λ(a i j)(drop-right(drop a i)j)) '(1 2 3 4 5 6) 2 1)