# 编写一个函数，告诉您删除了哪两行

19

2

Ilmari Karonen 2013年

1

le_vine

11

DroidDev 2013年

17

# 佩尔

``````sub foo {
@a = (2..5);
@a = grep \$_ != 2, (@a ? @a : (1..5));
@a = grep \$_ != 3, (@a ? @a : (1..5));
@a = grep \$_ != 4, (@a ? @a : (1..5));
@a = grep \$_ != 5, (@a ? @a : (1..5));
}
``````

9

# 红宝石

``````def which_lines_removed(arr = [*1..5])
arr -= [1]
arr -= [2]
arr -= [3]
arr -= [4]
(arr -= [5]).empty? ? 0 : arr
end
``````

``````def which_lines_removed(arr = [*1..5])
arr -= [1]
arr -= [2]
arr -= [3]
arr -= [4]
arr -= [5]
end
``````

4

# JavaScript，打了152个字符

``````function t() {
var fa = (f + '').match(/\d/g)
var ra = []
for (var i = 0; i < 5; i++) {
if (fa.indexOf(i + '') < 0) ra.push(i + 1)
}
return ra
}

function f() {
0; return t()
1; return t()
2; return t()
3; return t()
4; return t()
}
``````

``````function t(){for(a=[],i=0;++i<5;)if((f+'').indexOf(i)<0)a.push(i+1);return a}function f(){
return t(0)
return t(1)
return t(2)
return t(3)
return t(4)
}
``````

``````function f() {
0; var ra = []; for (var i = +![]; i < 5; i++) if ((f + '').match(/\d/g).indexOf(i + '') < +![]) ra.push(i); return ra
1; var ra = []; for (var i = +![]; i < 5; i++) if ((f + '').match(/\d/g).indexOf(i + '') < +![]) ra.push(i); return ra
2; var ra = []; for (var i = +![]; i < 5; i++) if ((f + '').match(/\d/g).indexOf(i + '') < +![]) ra.push(i); return ra
3; var ra = []; for (var i = +![]; i < 5; i++) if ((f + '').match(/\d/g).indexOf(i + '') < +![]) ra.push(i); return ra
4; var ra = []; for (var i = +![]; i < 5; i++) if ((f + '').match(/\d/g).indexOf(i + '') < +![]) ra.push(i); return ra
}
``````

3

## 红宝石

``````def f
a = [ 2, 3, 4, 5 ]
defined?(a) ? a = a.select { |num|    num != 2 } : a = [ 1, 3, 4, 5 ]
defined?(a) ? a = a.select { |num|    num != 3 } : a = [ 1, 2, 4, 5 ]
a = a.select { |num|    num != 4 }
(a = a.select { |num|    num != 5 }) == [] ? a = 0 : a
end
``````

3

# 蟒蛇

``````f=lambda:{1,2,3,4,5}-{
1,
2,
3,
4,
5,
} or 0
``````

2

## → 现场演示 ←

``````function(r){
r.shift();
r.splice(r.indexOf(2),1)
r.splice(r.indexOf(3),1);a=b=1;if(this.a&&this.b)return r
var a;r.splice(r.indexOf(4),1);b=1;if(this.b)return r
var b;r.pop();return r[0]?r:0
}
``````

315个字符

``````function(r){
var a;
var b;
var c;a=1;b=2;d=4;e=5;for(i in(z="abde".split("")))if(y=this[z[i]])r.push(y);return r.length?r:0
var d;a=1;b=2;c=3;e=5;for(i in(z="abce".split("")))if(y=this[z[i]])r.push(y);return r.length?r:0
var e;a=1;b=2;c=3;d=4;for(i in(z="abcd".split("")))if(y=this[z[i]])r.push(y);return r.length?r:0
}
``````

（也适用于已删除的3和4行）：

``````function(r){
var a;b=c=d=e=1;if(this.b)r.push(2);if(this.c)r.push(3);if(this.d)r.push(4);if(this.e)r.push(5);return r.length?r:0;
var b;a=c=d=e=1;if(this.a)r.push(1);if(this.c)r.push(3);if(this.d)r.push(4);if(this.e)r.push(5);return r.length?r:0;
var c;a=b=d=e=1;if(this.a)r.push(1);if(this.b)r.push(2);if(this.d)r.push(4);if(this.e)r.push(5);return r.length?r:0;
var d;a=b=c=e=1;if(this.a)r.push(1);if(this.b)r.push(2);if(this.c)r.push(3);if(this.e)r.push(5);return r.length?r:0;
var e;a=b=c=d=1;if(this.a)r.push(1);if(this.b)r.push(2);if(this.c)r.push(3);if(this.d)r.push(4);return r.length?r:0;
}
``````

2

JavaScript：

``````var f = function(){
1
2
a=[];for(i=0;i++<6;){if((f+'').indexOf(i)<0){a.push(i)}}return a.length?a:0;3
a=[];for(i=0;i++<6;){if((f+'').indexOf(i)<0){a.push(i)}}return a.length?a:0;4
a=[];for(i=0;i++<6;){if((f+'').indexOf(i)<0){a.push(i)}}return a.length?a:0;5
}
``````

2

# Java脚本

``````(function (i){

i += .1;     // line 1
i += .02;    // line 2
i += .003;   // line 3
i += .0004;  // line 4
i += .00005; // line 5

return (Math.round((.12345-i)*100000)/100000+'').match(/([1-5])/g) || 0 })(0)
``````

``````(Math.round((.12345 - (new (function(){

this.i = isFinite(this.i) ? this.i + .1 : .1 ;
this.i = isFinite(this.i) ? this.i + .02 : .02;
this.i = isFinite(this.i) ? this.i + .003 : .003;
this.i = isFinite(this.i) ? this.i + .0004 : .0004;
this.i = isFinite(this.i) ? this.i + .00005 : .00005;

})().i || 0) )*100000)/100000+'').match(/([1-5])/g) || 0
``````

logic8 2013年

Timtech，2013年

1
“ return”行位于函数内，因此该函数实际上有六行代码，这违反了挑战规则。

@ jawns317，我不确定如何定义“线”。有人可以提供一个清晰的定义吗？

@ logic8删除`function(){``}`（以及任何辅助函数）。计算行数。

1

# 普通口齿不清

``````(defun which-lines-are-removed (&aux (x (list 1 2 3 4 5)))
(setq x (remove-if #'(lambda (x) (eql x 1)) x))
(setq x (remove-if #'(lambda (x) (eql x 2)) x))
(setq x (remove-if #'(lambda (x) (eql x 3)) x))
(setq x (remove-if #'(lambda (x) (eql x 4)) x))
(setq x (remove-if #'(lambda (x) (eql x 5)) x))
)
``````

1

# 蟒蛇

``````def function(a = [1,2,3,4,5]):
delete(a, len(a)-5)#1
delete(a, len(a)-4)#2
delete(a, len(a)-3);print a if len(a)==2 else '',#3
delete(a, len(a)-2);print a if len(a)==2 else '',#4
delete(a, len(a)-1);print a if len(a)==2 else '',#5

def delete(a, i):
del a[i]
return a
``````

1. 如果按原样运行，它不会打印0（它假定最后两行已被注释，因此打印4、5
2. 它假定`print``return`是可互换的

SimonT

1

# 德贾武

``````local line n:
try:
dup
catch stack-empty:
dup set{ 1 2 3 4 5 }
delete-from swap n

func which-gone:
line 1
line 2
line 3
line 4
line 5
``````

0

# [R

``````trick <- function(sym, value) {
assign(sym, value, envir=parent.frame())
values <- unlist(as.list(parent.frame()))
if(length(values)==5) 0 else which(!1:5 %in% values)
}

reportRemovedLines <- function(){
trick("a", 1)
trick("b", 2)
trick("c", 3)
trick("d", 4)
trick("e", 5)
}
``````

``````funnyVersion <- function(trick = function(sym, value) {
assign(sym, value, envir=parent.frame())
values <- unlist(as.list(parent.frame()))
if(length(values)==5) 0 else which(!1:5 %in% values)
}){
trick("a", 1)
trick("b", 2)
trick("c", 3)
trick("d", 4)
trick("e", 5)
}
``````

0

## JavaScript（136/166个字符）

``````function(){b=[1,2,3,4,5],i=0
b.splice(0,1);i++
b.splice(1-i,1);i++
b.splice(2-i,1);i++
b.splice(3-i,1);i++
b.splice(4-i,1);i++
return b}
``````

``````function(b){
b=[2,3,4,5],i=1
b=b||[1,2,3,4,5],i=i||0,b.splice(1-i,1);i++
b=b||[1,2,3,4,5],i=i||0,b.splice(2-i,1);i++
b.splice(3-i,1);i++
b.splice(4-i,1);i++
return b}
``````

Bobby Marinoff 2014年

0

# [R

``````doit <- function() setdiff(1:5, c(
1,
2,
3,
4,
5
))
``````

``````doit<-function() setdiff(1:5, scan(text="
1
2
3
4
5
"))
``````

``````dysfunction <- function(E){
FUN <- function(){}
e <- substitute(E)
e[[1]] <- as.name("list")
nb <- quote(setdiff(as.list(1:5), x))
nb[[3]] <- e
body(FUN) <- nb
FUN
}
``````

``````df <- dysfunction({
1
2
3
4
5
})
``````

0

C ++

``````void function(int & i)
{
i=i|1;
i=i|2;
i=(i|4);
i=(i|8);
i=(i|16);
}

int[] func2(int i)
{
int arr[]={0,0};
int k=0,l=1;
for(int j=1;j<=16;j*=2,l++)
{
if((i&j)==0)
{
arr[k++]=l;
}
}
return arr;
}
``````

Cel Skeggs 2014年

zeeshan mughal 2014年

Cel Skeggs

C不能那样工作。它给出了编译器错误。可能您正在考虑使用C ++。
Cel Skeggs 2014年